dokimi_test.gno
19.02 Kb · 476 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
294// A correction may lower a total, needs a reason, and leaves the mistaken
295// report on the record.
296func TestCorrection(cur realm, t *testing.T) {
297 reset()
298 testing.SetRealm(testing.NewUserRealm(alice))
299 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1000)
300
301 uassert.AbortsWithMessage(t, cur, "there is no report to correct yet", func() {
302 AddCorrection(cross(cur), id, 100, 10, 100, 12, "nothing to fix")
303 })
304
305 AddReport(cross(cur), id, 4000, 700, 4000, 905, "typed 905 instead of 805")
306 uassert.AbortsContains(t, cur, "totals cannot go down", func() {
307 AddReport(cross(cur), id, 4000, 700, 4000, 805, "")
308 })
309 uassert.AbortsWithMessage(t, cur, "a correction needs a reason", func() {
310 AddCorrection(cross(cur), id, 4000, 700, 4000, 805, "")
311 })
312 uassert.AbortsContains(t, cur, "conversions cannot exceed exposures", func() {
313 AddCorrection(cross(cur), id, 4000, 4001, 4000, 805, "fix")
314 })
315
316 AddCorrection(cross(cur), id, 4000, 700, 4000, 805, "Variant conversions were mistyped as 905.")
317 exp := mustGet(id)
318 uassert.Equal(t, 2, len(exp.reports), "the mistaken report is still there")
319 uassert.False(t, exp.reports[0].Correction)
320 uassert.True(t, exp.reports[1].Correction)
321 uassert.Equal(t, int64(905), exp.reports[0].Variant.Conversions)
322
323 // Later reports build on the corrected totals.
324 uassert.AbortsContains(t, cur, "totals cannot go down", func() {
325 AddReport(cross(cur), id, 4000, 700, 4000, 804, "")
326 })
327 AddReport(cross(cur), id, 4100, 715, 4100, 830, "day 5")
328
329 page := Render("1")
330 uassert.True(t, strings.Contains(page, "**1 of 3 rows are corrections.**"))
331 uassert.True(t, strings.Contains(page, "**Correction.** Variant conversions were mistyped as 905"))
332 uassert.True(t, strings.Contains(page, "<gno-form exec=\"AddCorrection\">"))
333}
334
335func TestCorrectionRules(cur realm, t *testing.T) {
336 reset()
337 testing.SetRealm(testing.NewUserRealm(alice))
338 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1000)
339 AddReport(cross(cur), id, 4000, 700, 4000, 905, "")
340
341 // Only the owner can correct.
342 testing.SetRealm(testing.NewUserRealm(bob))
343 uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() {
344 AddCorrection(cross(cur), id, 4000, 700, 4000, 100, "sabotage")
345 })
346
347 // The verdict uses the correction when it is the last report.
348 testing.SetRealm(testing.NewUserRealm(alice))
349 AddCorrection(cross(cur), id, 4000, 700, 4000, 784, "Read the wrong column.")
350 uassert.Equal(t, "missed", Close(cross(cur), id))
351
352 // A closed experiment cannot be corrected.
353 uassert.AbortsWithMessage(t, cur, "experiment is closed", func() {
354 AddCorrection(cross(cur), id, 4000, 700, 4000, 905, "second thoughts")
355 })
356 uassert.False(t, strings.Contains(Render("1"), "<gno-form exec=\"AddCorrection\">"))
357}
358
359// No form field uses a number input: scrolling over one changes its value.
360func TestFormsUseTextInputs(cur realm, t *testing.T) {
361 reset()
362 testing.SetRealm(testing.NewUserRealm(alice))
363 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
364 AddReport(cross(cur), id, 100, 10, 100, 12, "")
365 uassert.False(t, strings.Contains(Render(""), "type=\"number\""))
366 uassert.False(t, strings.Contains(Render("1"), "type=\"number\""))
367}
368
369func TestSpan(t *testing.T) {
370 uassert.Equal(t, "less than a minute", span(0))
371 uassert.Equal(t, "less than a minute", span(59*time.Second))
372 uassert.Equal(t, "1 minutes", span(60*time.Second))
373 uassert.Equal(t, "119 minutes", span(119*time.Minute))
374 uassert.Equal(t, "2 hours", span(120*time.Minute))
375 uassert.Equal(t, "47 hours", span(47*time.Hour+59*time.Minute))
376 uassert.Equal(t, "2 days", span(48*time.Hour))
377 uassert.Equal(t, "19 days", span(19*24*time.Hour+3*time.Hour))
378}
379
380// The page says how long the plan had been locked when results first arrived.
381func TestRenderShowsLockToReportGap(cur realm, t *testing.T) {
382 reset()
383 testing.SetRealm(testing.NewUserRealm(alice))
384 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
385 testing.SkipHeights(1200)
386 AddReport(cross(cur), id, 100, 10, 100, 12, "")
387 page := Render("1")
388 uassert.True(t, strings.Contains(page, "The first report came 1,200 blocks after the plan was locked"))
389}
390
391func TestReportLimit(cur realm, t *testing.T) {
392 reset()
393 testing.SetRealm(testing.NewUserRealm(alice))
394 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
395 for i := 0; i < MaxReports; i++ {
396 AddReport(cross(cur), id, int64(i+1), 0, int64(i+1), 0, "")
397 }
398 uassert.AbortsContains(t, cur, "report limit reached", func() {
399 AddReport(cross(cur), id, 1000, 0, 1000, 0, "")
400 })
401}
402
403func TestRenderHome(cur realm, t *testing.T) {
404 reset()
405 home := Render("")
406 uassert.True(t, strings.Contains(home, "# Dokimi"))
407 uassert.True(t, strings.Contains(home, "None yet."))
408 uassert.True(t, strings.Contains(home, "<gno-form exec=\"Register\">"))
409
410 testing.SetRealm(testing.NewUserRealm(alice))
411 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
412 home = Render("")
413 uassert.True(t, strings.Contains(home, "["+tTitle+"]("+webPath+":1)"))
414 uassert.True(t, strings.Contains(home, "+15.00%"))
415 uassert.True(t, strings.Contains(home, "3,800"))
416 uassert.True(t, strings.Contains(home, "| Locked |"))
417 uassert.False(t, strings.Contains(home, "None yet."))
418
419 AddReport(cross(cur), id, 100, 10, 100, 12, "")
420 uassert.True(t, strings.Contains(Render(""), "| Running |"))
421}
422
423func TestRenderExperiment(cur realm, t *testing.T) {
424 reset()
425 testing.SetRealm(testing.NewUserRealm(alice))
426 id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)
427
428 page := Render("1")
429 uassert.True(t, strings.Contains(page, "# "+tTitle))
430 uassert.True(t, strings.Contains(page, "> A shorter signup page gets more visitors to finish signing up"))
431 uassert.True(t, strings.Contains(page, "[All experiments]("+webPath+")"))
432 uassert.True(t, strings.Contains(page, DigestOf(id)))
433 uassert.True(t, strings.Contains(page, alice.String()))
434 uassert.True(t, strings.Contains(page, "## No results yet"))
435 uassert.True(t, strings.Contains(page, "<gno-form exec=\"AddReport\">"))
436
437 AddReport(cross(cur), id, 1530, 268, 1547, 304, "analytics dashboard")
438 page = Render("1")
439 uassert.True(t, strings.Contains(page, "## Still running"))
440 uassert.True(t, strings.Contains(page, "268 of 1,530 (17.51%)"))
441 uassert.True(t, strings.Contains(page, "+12.18%"))
442 uassert.True(t, strings.Contains(page, "analytics dashboard"))
443
444 AddNote(cross(cur), id, "Ended early.")
445 Close(cross(cur), id)
446 page = Render("1")
447 uassert.True(t, strings.Contains(page, "## Verdict: Inconclusive"))
448 uassert.True(t, strings.Contains(page, "Stopped before the planned sample"))
449 uassert.True(t, strings.Contains(page, "Ended early"))
450 uassert.True(t, strings.Contains(page, "reached 1,530"))
451 uassert.False(t, strings.Contains(page, "<gno-form exec=\"AddReport\">"), "no report form once closed")
452 uassert.False(t, strings.Contains(page, "<gno-form exec=\"Close\">"), "no close form once closed")
453 uassert.True(t, strings.Contains(page, "<gno-form exec=\"AddNote\">"))
454}
455
456func TestRenderNotFound(t *testing.T) {
457 reset()
458 uassert.True(t, strings.Contains(Render("42"), "does not exist"))
459 uassert.True(t, strings.Contains(Render("nope"), "# Not found"))
460 uassert.True(t, strings.Contains(Render("-1"), "# Not found"))
461}
462
463// Text typed by an owner must not be able to break the page layout.
464func TestRenderEscapesUserText(cur realm, t *testing.T) {
465 reset()
466 testing.SetRealm(testing.NewUserRealm(alice))
467 Register(cross(cur), "Bad | title <b>x</b>", "A # heading attempt", tMetric, "A|B", tVariant, tDirection, tTarget, tMinSample)
468
469 home := Render("")
470 uassert.False(t, strings.Contains(home, "Bad | title"), "pipe in a table cell is escaped")
471 uassert.False(t, strings.Contains(home, "<b>x</b>"), "raw HTML is escaped")
472
473 page := Render("1")
474 uassert.False(t, strings.Contains(page, "<b>x</b>"))
475 uassert.False(t, strings.Contains(page, "| A|B |"))
476}