Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

dokimi_test.gno

15.79 Kb · 401 lines
  1package dokimi
  2
  3import (
  4	"strings"
  5	"testing"
  6	"time"
  7
  8	"gno.land/p/nt/avl/v0"
  9	"gno.land/p/nt/testutils/v0"
 10	"gno.land/p/nt/uassert/v0"
 11)
 12
 13var (
 14	alice = testutils.TestAddress("alice")
 15	bob   = testutils.TestAddress("bob")
 16)
 17
 18// The plan used across the tests: a 15% target and 3,800 exposures per arm.
 19const (
 20	tTitle      = "Short signup page vs current page"
 21	tHypothesis = "A shorter signup page gets more visitors to finish signing up."
 22	tMetric     = "signup_completed"
 23	tControl    = "Current page"
 24	tVariant    = "Short page"
 25	tDirection  = "increase"
 26	tTarget     = "15"
 27	tMinSample  = int64(3800)
 28)
 29
 30// reset clears the registry so every test starts from an empty realm.
 31func reset() {
 32	experiments = avl.NewTree()
 33	lastID = 0
 34}
 35
 36// Note: testing.SetRealm applies to the frame that calls it, so the tests call
 37// it directly instead of through a helper.
 38
 39func TestRegister(cur realm, t *testing.T) {
 40	reset()
 41	testing.SetRealm(testing.NewUserRealm(alice))
 42
 43	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
 44	uassert.Equal(t, uint64(1), id)
 45	uassert.Equal(t, 1, Count())
 46	uassert.Equal(t, "open", Status(id))
 47	uassert.Equal(t, alice.String(), OwnerOf(id).String())
 48	uassert.Equal(t, 64, len(DigestOf(id)))
 49
 50	second := Register(cross(cur), "Second test", tHypothesis, tMetric, tControl, tVariant, "decrease", "2.5%", 100)
 51	uassert.Equal(t, uint64(2), second)
 52	uassert.Equal(t, 2, Count())
 53	uassert.NotEqual(t, DigestOf(id), DigestOf(second))
 54}
 55
 56func TestRegisterRejectsBadPlans(cur realm, t *testing.T) {
 57	reset()
 58	testing.SetRealm(testing.NewUserRealm(alice))
 59
 60	uassert.AbortsContains(t, cur, "title is required", func() {
 61		Register(cross(cur), "", tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
 62	})
 63	uassert.AbortsContains(t, cur, "direction must be", func() {
 64		Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, "up", tTarget, tMinSample)
 65	})
 66	uassert.AbortsContains(t, cur, "target:", func() {
 67		Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "fifteen", tMinSample)
 68	})
 69	uassert.AbortsContains(t, cur, "target must be between", func() {
 70		Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "0", tMinSample)
 71	})
 72	uassert.AbortsContains(t, cur, "minimum sample", func() {
 73		Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 0)
 74	})
 75	uassert.AbortsContains(t, cur, "different names", func() {
 76		Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tControl, tDirection, tTarget, tMinSample)
 77	})
 78	uassert.Equal(t, 0, Count(), "a refused plan leaves nothing behind")
 79}
 80
 81func TestMatches(cur realm, t *testing.T) {
 82	reset()
 83	testing.SetRealm(testing.NewUserRealm(alice))
 84	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
 85
 86	uassert.True(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample))
 87	uassert.True(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "15.00%", tMinSample), "same target written another way")
 88	uassert.False(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "10", tMinSample), "a lowered target")
 89	uassert.False(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1500), "a lowered sample")
 90	uassert.False(t, Matches(id, tTitle, tHypothesis+" ", tMetric, tControl, tVariant, tDirection, tTarget, tMinSample), "an edited hypothesis")
 91	uassert.False(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "not a number", tMinSample))
 92}
 93
 94func TestOnlyOwnerCanWrite(cur realm, t *testing.T) {
 95	reset()
 96	testing.SetRealm(testing.NewUserRealm(alice))
 97	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
 98	AddReport(cross(cur), id, 100, 10, 100, 12, "dashboard")
 99
100	testing.SetRealm(testing.NewUserRealm(bob))
101	uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() {
102		AddReport(cross(cur), id, 200, 20, 200, 60, "")
103	})
104	uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() {
105		AddNote(cross(cur), id, "nothing to see")
106	})
107	uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() {
108		Close(cross(cur), id)
109	})
110	uassert.Equal(t, "open", Status(id))
111	uassert.Equal(t, 1, len(mustGet(id).reports))
112	uassert.Equal(t, 0, len(mustGet(id).notes))
113}
114
115func TestReportRules(cur realm, t *testing.T) {
116	reset()
117	testing.SetRealm(testing.NewUserRealm(alice))
118	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
119
120	uassert.AbortsContains(t, cur, "conversions cannot exceed exposures", func() {
121		AddReport(cross(cur), id, 100, 101, 100, 12, "")
122	})
123	uassert.AbortsContains(t, cur, "counts cannot be negative", func() {
124		AddReport(cross(cur), id, 100, 10, -1, 0, "")
125	})
126	uassert.AbortsContains(t, cur, "source must be a single line", func() {
127		AddReport(cross(cur), id, 100, 10, 100, 12, "line one\nline two")
128	})
129
130	AddReport(cross(cur), id, 100, 10, 100, 12, "first look")
131
132	// Totals are running totals: none of the four may go down.
133	uassert.AbortsContains(t, cur, "totals cannot go down", func() {
134		AddReport(cross(cur), id, 99, 10, 100, 12, "")
135	})
136	uassert.AbortsContains(t, cur, "totals cannot go down", func() {
137		AddReport(cross(cur), id, 200, 9, 200, 30, "")
138	})
139	uassert.AbortsContains(t, cur, "totals cannot go down", func() {
140		AddReport(cross(cur), id, 200, 20, 200, 11, "")
141	})
142
143	// The same totals again, or higher ones, are accepted.
144	AddReport(cross(cur), id, 100, 10, 100, 12, "no new traffic")
145	AddReport(cross(cur), id, 200, 20, 210, 30, "")
146	uassert.Equal(t, 3, len(mustGet(id).reports))
147
148	lift, ok := LiftOf(id)
149	uassert.True(t, ok)
150	// (30*200 - 20*210) * 10000 / (20*210) = 4285.7
151	uassert.Equal(t, int64(4285), lift)
152}
153
154func TestLiftOfWithoutReports(cur realm, t *testing.T) {
155	reset()
156	testing.SetRealm(testing.NewUserRealm(alice))
157	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
158	_, ok := LiftOf(id)
159	uassert.False(t, ok)
160}
161
162func TestCloseNeedsAReport(cur realm, t *testing.T) {
163	reset()
164	testing.SetRealm(testing.NewUserRealm(alice))
165	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
166	uassert.AbortsWithMessage(t, cur, "add at least one report before closing", func() {
167		Close(cross(cur), id)
168	})
169	uassert.Equal(t, "open", Status(id))
170}
171
172// A test stopped before the promised sample is inconclusive even when the
173// numbers look like a clear win.
174func TestCloseStoppedEarly(cur realm, t *testing.T) {
175	reset()
176	testing.SetRealm(testing.NewUserRealm(alice))
177	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
178	AddReport(cross(cur), id, 1530, 268, 1547, 400, "dashboard")
179
180	outcome := Close(cross(cur), id)
181	uassert.Equal(t, "inconclusive", outcome)
182	uassert.Equal(t, "inconclusive", Status(id))
183}
184
185func TestCloseMissed(cur realm, t *testing.T) {
186	reset()
187	testing.SetRealm(testing.NewUserRealm(alice))
188	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
189	AddReport(cross(cur), id, 4000, 700, 4000, 784, "dashboard") // +12.00%
190	uassert.Equal(t, "missed", Close(cross(cur), id))
191	uassert.Equal(t, "missed", Status(id))
192}
193
194func TestCloseMet(cur realm, t *testing.T) {
195	reset()
196	testing.SetRealm(testing.NewUserRealm(alice))
197	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
198	AddReport(cross(cur), id, 4000, 700, 4000, 805, "dashboard") // +15.00%
199	uassert.Equal(t, "met", Close(cross(cur), id))
200	uassert.Equal(t, "met", Status(id))
201}
202
203// The verdict uses the last report, not the best one.
204func TestCloseUsesLastReport(cur realm, t *testing.T) {
205	reset()
206	testing.SetRealm(testing.NewUserRealm(alice))
207	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1000)
208	AddReport(cross(cur), id, 1000, 100, 1000, 130, "day 1") // +30%, would be met
209	AddReport(cross(cur), id, 4000, 700, 4000, 784, "day 4") // +12%, missed
210	uassert.Equal(t, "missed", Close(cross(cur), id))
211}
212
213func TestClosedIsFinal(cur realm, t *testing.T) {
214	reset()
215	testing.SetRealm(testing.NewUserRealm(alice))
216	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
217	AddReport(cross(cur), id, 4000, 700, 4000, 784, "dashboard")
218	Close(cross(cur), id)
219
220	uassert.AbortsWithMessage(t, cur, "experiment is closed", func() {
221		AddReport(cross(cur), id, 5000, 900, 5000, 1200, "one more look")
222	})
223	uassert.AbortsWithMessage(t, cur, "experiment is already closed", func() {
224		Close(cross(cur), id)
225	})
226	uassert.Equal(t, "missed", Status(id))
227	uassert.Equal(t, 1, len(mustGet(id).reports))
228}
229
230func TestNotes(cur realm, t *testing.T) {
231	reset()
232	testing.SetRealm(testing.NewUserRealm(alice))
233	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
234
235	uassert.AbortsWithMessage(t, cur, "note is empty", func() {
236		AddNote(cross(cur), id, "")
237	})
238	AddNote(cross(cur), id, "Traffic is simulated in a development environment.")
239
240	AddReport(cross(cur), id, 1530, 268, 1547, 304, "dashboard")
241	Close(cross(cur), id)
242
243	// Notes stay open after closing, so an owner can explain what happened next.
244	AddNote(cross(cur), id, "Ended early at 92% chance to be best and rolled out anyway.")
245	uassert.Equal(t, 2, len(mustGet(id).notes))
246}
247
248func TestUnknownExperiment(cur realm, t *testing.T) {
249	reset()
250	testing.SetRealm(testing.NewUserRealm(alice))
251	uassert.AbortsWithMessage(t, cur, "experiment 7 not found", func() {
252		AddReport(cross(cur), 7, 1, 0, 1, 0, "")
253	})
254	uassert.AbortsWithMessage(t, cur, "experiment 0 not found", func() {
255		Close(cross(cur), 0)
256	})
257}
258
259// Another realm can own an experiment. A person cannot then write to it,
260// even the person who sent the transaction that made the realm register it.
261func TestRealmAsOwner(cur realm, t *testing.T) {
262	reset()
263	const shop = "gno.land/r/demo/shop"
264
265	testing.SetRealm(testing.NewCodeRealm(shop))
266	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
267	uassert.NotEqual(t, alice.String(), OwnerOf(id).String())
268
269	testing.SetRealm(testing.NewUserRealm(alice))
270	uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() {
271		AddReport(cross(cur), id, 4000, 700, 4000, 805, "")
272	})
273
274	testing.SetRealm(testing.NewCodeRealm(shop))
275	AddReport(cross(cur), id, 4000, 700, 4000, 805, "counted by the shop realm")
276	uassert.Equal(t, "met", Close(cross(cur), id))
277}
278
279func TestHeightsAreRecorded(cur realm, t *testing.T) {
280	reset()
281	testing.SetRealm(testing.NewUserRealm(alice))
282	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
283	locked := mustGet(id).lockedHeight
284
285	testing.SkipHeights(50)
286	AddReport(cross(cur), id, 100, 10, 100, 12, "")
287	uassert.Equal(t, locked+50, mustGet(id).reports[0].Height)
288
289	testing.SkipHeights(25)
290	Close(cross(cur), id)
291	uassert.Equal(t, locked+75, mustGet(id).closedHeight)
292}
293
294func TestSpan(t *testing.T) {
295	uassert.Equal(t, "less than a minute", span(0))
296	uassert.Equal(t, "less than a minute", span(59*time.Second))
297	uassert.Equal(t, "1 minutes", span(60*time.Second))
298	uassert.Equal(t, "119 minutes", span(119*time.Minute))
299	uassert.Equal(t, "2 hours", span(120*time.Minute))
300	uassert.Equal(t, "47 hours", span(47*time.Hour+59*time.Minute))
301	uassert.Equal(t, "2 days", span(48*time.Hour))
302	uassert.Equal(t, "19 days", span(19*24*time.Hour+3*time.Hour))
303}
304
305// The page says how long the plan had been locked when results first arrived.
306func TestRenderShowsLockToReportGap(cur realm, t *testing.T) {
307	reset()
308	testing.SetRealm(testing.NewUserRealm(alice))
309	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
310	testing.SkipHeights(1200)
311	AddReport(cross(cur), id, 100, 10, 100, 12, "")
312	page := Render("1")
313	uassert.True(t, strings.Contains(page, "The first report came 1,200 blocks after the plan was locked"))
314}
315
316func TestReportLimit(cur realm, t *testing.T) {
317	reset()
318	testing.SetRealm(testing.NewUserRealm(alice))
319	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
320	for i := 0; i < MaxReports; i++ {
321		AddReport(cross(cur), id, int64(i+1), 0, int64(i+1), 0, "")
322	}
323	uassert.AbortsContains(t, cur, "report limit reached", func() {
324		AddReport(cross(cur), id, 1000, 0, 1000, 0, "")
325	})
326}
327
328func TestRenderHome(cur realm, t *testing.T) {
329	reset()
330	home := Render("")
331	uassert.True(t, strings.Contains(home, "# Dokimi"))
332	uassert.True(t, strings.Contains(home, "None yet."))
333	uassert.True(t, strings.Contains(home, "<gno-form exec=\"Register\">"))
334
335	testing.SetRealm(testing.NewUserRealm(alice))
336	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
337	home = Render("")
338	uassert.True(t, strings.Contains(home, "["+tTitle+"]("+webPath+":1)"))
339	uassert.True(t, strings.Contains(home, "+15.00%"))
340	uassert.True(t, strings.Contains(home, "3,800"))
341	uassert.True(t, strings.Contains(home, "| Locked |"))
342	uassert.False(t, strings.Contains(home, "None yet."))
343
344	AddReport(cross(cur), id, 100, 10, 100, 12, "")
345	uassert.True(t, strings.Contains(Render(""), "| Running |"))
346}
347
348func TestRenderExperiment(cur realm, t *testing.T) {
349	reset()
350	testing.SetRealm(testing.NewUserRealm(alice))
351	id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
352
353	page := Render("1")
354	uassert.True(t, strings.Contains(page, "# "+tTitle))
355	uassert.True(t, strings.Contains(page, "> A shorter signup page gets more visitors to finish signing up"))
356	uassert.True(t, strings.Contains(page, "[All experiments]("+webPath+")"))
357	uassert.True(t, strings.Contains(page, DigestOf(id)))
358	uassert.True(t, strings.Contains(page, alice.String()))
359	uassert.True(t, strings.Contains(page, "## No results yet"))
360	uassert.True(t, strings.Contains(page, "<gno-form exec=\"AddReport\">"))
361
362	AddReport(cross(cur), id, 1530, 268, 1547, 304, "analytics dashboard")
363	page = Render("1")
364	uassert.True(t, strings.Contains(page, "## Still running"))
365	uassert.True(t, strings.Contains(page, "268 of 1,530 (17.51%)"))
366	uassert.True(t, strings.Contains(page, "+12.18%"))
367	uassert.True(t, strings.Contains(page, "analytics dashboard"))
368
369	AddNote(cross(cur), id, "Ended early.")
370	Close(cross(cur), id)
371	page = Render("1")
372	uassert.True(t, strings.Contains(page, "## Verdict: Inconclusive"))
373	uassert.True(t, strings.Contains(page, "Stopped before the planned sample"))
374	uassert.True(t, strings.Contains(page, "Ended early"))
375	uassert.True(t, strings.Contains(page, "reached 1,530"))
376	uassert.False(t, strings.Contains(page, "<gno-form exec=\"AddReport\">"), "no report form once closed")
377	uassert.False(t, strings.Contains(page, "<gno-form exec=\"Close\">"), "no close form once closed")
378	uassert.True(t, strings.Contains(page, "<gno-form exec=\"AddNote\">"))
379}
380
381func TestRenderNotFound(t *testing.T) {
382	reset()
383	uassert.True(t, strings.Contains(Render("42"), "does not exist"))
384	uassert.True(t, strings.Contains(Render("nope"), "# Not found"))
385	uassert.True(t, strings.Contains(Render("-1"), "# Not found"))
386}
387
388// Text typed by an owner must not be able to break the page layout.
389func TestRenderEscapesUserText(cur realm, t *testing.T) {
390	reset()
391	testing.SetRealm(testing.NewUserRealm(alice))
392	Register(cross(cur), "Bad | title <b>x</b>", "A # heading attempt", tMetric, "A|B", tVariant, tDirection, tTarget, tMinSample)
393
394	home := Render("")
395	uassert.False(t, strings.Contains(home, "Bad | title"), "pipe in a table cell is escaped")
396	uassert.False(t, strings.Contains(home, "<b>x</b>"), "raw HTML is escaped")
397
398	page := Render("1")
399	uassert.False(t, strings.Contains(page, "<b>x</b>"))
400	uassert.False(t, strings.Contains(page, "| A|B |"))
401}