package dokimi import ( "strings" "testing" "time" "gno.land/p/nt/avl/v0" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" ) var ( alice = testutils.TestAddress("alice") bob = testutils.TestAddress("bob") ) // The plan used across the tests: a 15% target and 3,800 exposures per arm. const ( tTitle = "Short signup page vs current page" tHypothesis = "A shorter signup page gets more visitors to finish signing up." tMetric = "signup_completed" tControl = "Current page" tVariant = "Short page" tDirection = "increase" tTarget = "15" tMinSample = int64(3800) ) // reset clears the registry so every test starts from an empty realm. func reset() { experiments = avl.NewTree() lastID = 0 } // Note: testing.SetRealm applies to the frame that calls it, so the tests call // it directly instead of through a helper. func TestRegister(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) uassert.Equal(t, uint64(1), id) uassert.Equal(t, 1, Count()) uassert.Equal(t, "open", Status(id)) uassert.Equal(t, alice.String(), OwnerOf(id).String()) uassert.Equal(t, 64, len(DigestOf(id))) second := Register(cross(cur), "Second test", tHypothesis, tMetric, tControl, tVariant, "decrease", "2.5%", 100) uassert.Equal(t, uint64(2), second) uassert.Equal(t, 2, Count()) uassert.NotEqual(t, DigestOf(id), DigestOf(second)) } func TestRegisterRejectsBadPlans(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) uassert.AbortsContains(t, cur, "title is required", func() { Register(cross(cur), "", tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) }) uassert.AbortsContains(t, cur, "direction must be", func() { Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, "up", tTarget, tMinSample) }) uassert.AbortsContains(t, cur, "target:", func() { Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "fifteen", tMinSample) }) uassert.AbortsContains(t, cur, "target must be between", func() { Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "0", tMinSample) }) uassert.AbortsContains(t, cur, "minimum sample", func() { Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 0) }) uassert.AbortsContains(t, cur, "different names", func() { Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tControl, tDirection, tTarget, tMinSample) }) uassert.Equal(t, 0, Count(), "a refused plan leaves nothing behind") } func TestMatches(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) uassert.True(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample)) uassert.True(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "15.00%", tMinSample), "same target written another way") uassert.False(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "10", tMinSample), "a lowered target") uassert.False(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1500), "a lowered sample") uassert.False(t, Matches(id, tTitle, tHypothesis+" ", tMetric, tControl, tVariant, tDirection, tTarget, tMinSample), "an edited hypothesis") uassert.False(t, Matches(id, tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, "not a number", tMinSample)) } func TestOnlyOwnerCanWrite(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) AddReport(cross(cur), id, 100, 10, 100, 12, "dashboard") testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() { AddReport(cross(cur), id, 200, 20, 200, 60, "") }) uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() { AddNote(cross(cur), id, "nothing to see") }) uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() { Close(cross(cur), id) }) uassert.Equal(t, "open", Status(id)) uassert.Equal(t, 1, len(mustGet(id).reports)) uassert.Equal(t, 0, len(mustGet(id).notes)) } func TestReportRules(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) uassert.AbortsContains(t, cur, "conversions cannot exceed exposures", func() { AddReport(cross(cur), id, 100, 101, 100, 12, "") }) uassert.AbortsContains(t, cur, "counts cannot be negative", func() { AddReport(cross(cur), id, 100, 10, -1, 0, "") }) uassert.AbortsContains(t, cur, "source must be a single line", func() { AddReport(cross(cur), id, 100, 10, 100, 12, "line one\nline two") }) AddReport(cross(cur), id, 100, 10, 100, 12, "first look") // Totals are running totals: none of the four may go down. uassert.AbortsContains(t, cur, "totals cannot go down", func() { AddReport(cross(cur), id, 99, 10, 100, 12, "") }) uassert.AbortsContains(t, cur, "totals cannot go down", func() { AddReport(cross(cur), id, 200, 9, 200, 30, "") }) uassert.AbortsContains(t, cur, "totals cannot go down", func() { AddReport(cross(cur), id, 200, 20, 200, 11, "") }) // The same totals again, or higher ones, are accepted. AddReport(cross(cur), id, 100, 10, 100, 12, "no new traffic") AddReport(cross(cur), id, 200, 20, 210, 30, "") uassert.Equal(t, 3, len(mustGet(id).reports)) lift, ok := LiftOf(id) uassert.True(t, ok) // (30*200 - 20*210) * 10000 / (20*210) = 4285.7 uassert.Equal(t, int64(4285), lift) } func TestLiftOfWithoutReports(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) _, ok := LiftOf(id) uassert.False(t, ok) } func TestCloseNeedsAReport(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) uassert.AbortsWithMessage(t, cur, "add at least one report before closing", func() { Close(cross(cur), id) }) uassert.Equal(t, "open", Status(id)) } // A test stopped before the promised sample is inconclusive even when the // numbers look like a clear win. func TestCloseStoppedEarly(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) AddReport(cross(cur), id, 1530, 268, 1547, 400, "dashboard") outcome := Close(cross(cur), id) uassert.Equal(t, "inconclusive", outcome) uassert.Equal(t, "inconclusive", Status(id)) } func TestCloseMissed(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) AddReport(cross(cur), id, 4000, 700, 4000, 784, "dashboard") // +12.00% uassert.Equal(t, "missed", Close(cross(cur), id)) uassert.Equal(t, "missed", Status(id)) } func TestCloseMet(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) AddReport(cross(cur), id, 4000, 700, 4000, 805, "dashboard") // +15.00% uassert.Equal(t, "met", Close(cross(cur), id)) uassert.Equal(t, "met", Status(id)) } // The verdict uses the last report, not the best one. func TestCloseUsesLastReport(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1000) AddReport(cross(cur), id, 1000, 100, 1000, 130, "day 1") // +30%, would be met AddReport(cross(cur), id, 4000, 700, 4000, 784, "day 4") // +12%, missed uassert.Equal(t, "missed", Close(cross(cur), id)) } func TestClosedIsFinal(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) AddReport(cross(cur), id, 4000, 700, 4000, 784, "dashboard") Close(cross(cur), id) uassert.AbortsWithMessage(t, cur, "experiment is closed", func() { AddReport(cross(cur), id, 5000, 900, 5000, 1200, "one more look") }) uassert.AbortsWithMessage(t, cur, "experiment is already closed", func() { Close(cross(cur), id) }) uassert.Equal(t, "missed", Status(id)) uassert.Equal(t, 1, len(mustGet(id).reports)) } func TestNotes(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) uassert.AbortsWithMessage(t, cur, "note is empty", func() { AddNote(cross(cur), id, "") }) AddNote(cross(cur), id, "Traffic is simulated in a development environment.") AddReport(cross(cur), id, 1530, 268, 1547, 304, "dashboard") Close(cross(cur), id) // Notes stay open after closing, so an owner can explain what happened next. AddNote(cross(cur), id, "Ended early at 92% chance to be best and rolled out anyway.") uassert.Equal(t, 2, len(mustGet(id).notes)) } func TestUnknownExperiment(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) uassert.AbortsWithMessage(t, cur, "experiment 7 not found", func() { AddReport(cross(cur), 7, 1, 0, 1, 0, "") }) uassert.AbortsWithMessage(t, cur, "experiment 0 not found", func() { Close(cross(cur), 0) }) } // Another realm can own an experiment. A person cannot then write to it, // even the person who sent the transaction that made the realm register it. func TestRealmAsOwner(cur realm, t *testing.T) { reset() const shop = "gno.land/r/demo/shop" testing.SetRealm(testing.NewCodeRealm(shop)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) uassert.NotEqual(t, alice.String(), OwnerOf(id).String()) testing.SetRealm(testing.NewUserRealm(alice)) uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() { AddReport(cross(cur), id, 4000, 700, 4000, 805, "") }) testing.SetRealm(testing.NewCodeRealm(shop)) AddReport(cross(cur), id, 4000, 700, 4000, 805, "counted by the shop realm") uassert.Equal(t, "met", Close(cross(cur), id)) } func TestHeightsAreRecorded(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) locked := mustGet(id).lockedHeight testing.SkipHeights(50) AddReport(cross(cur), id, 100, 10, 100, 12, "") uassert.Equal(t, locked+50, mustGet(id).reports[0].Height) testing.SkipHeights(25) Close(cross(cur), id) uassert.Equal(t, locked+75, mustGet(id).closedHeight) } // A correction may lower a total, needs a reason, and leaves the mistaken // report on the record. func TestCorrection(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1000) uassert.AbortsWithMessage(t, cur, "there is no report to correct yet", func() { AddCorrection(cross(cur), id, 100, 10, 100, 12, "nothing to fix") }) AddReport(cross(cur), id, 4000, 700, 4000, 905, "typed 905 instead of 805") uassert.AbortsContains(t, cur, "totals cannot go down", func() { AddReport(cross(cur), id, 4000, 700, 4000, 805, "") }) uassert.AbortsWithMessage(t, cur, "a correction needs a reason", func() { AddCorrection(cross(cur), id, 4000, 700, 4000, 805, "") }) uassert.AbortsContains(t, cur, "conversions cannot exceed exposures", func() { AddCorrection(cross(cur), id, 4000, 4001, 4000, 805, "fix") }) AddCorrection(cross(cur), id, 4000, 700, 4000, 805, "Variant conversions were mistyped as 905.") exp := mustGet(id) uassert.Equal(t, 2, len(exp.reports), "the mistaken report is still there") uassert.False(t, exp.reports[0].Correction) uassert.True(t, exp.reports[1].Correction) uassert.Equal(t, int64(905), exp.reports[0].Variant.Conversions) // Later reports build on the corrected totals. uassert.AbortsContains(t, cur, "totals cannot go down", func() { AddReport(cross(cur), id, 4000, 700, 4000, 804, "") }) AddReport(cross(cur), id, 4100, 715, 4100, 830, "day 5") page := Render("1") uassert.True(t, strings.Contains(page, "**1 of 3 rows are corrections.**")) uassert.True(t, strings.Contains(page, "**Correction.** Variant conversions were mistyped as 905")) uassert.True(t, strings.Contains(page, "")) } func TestCorrectionRules(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, 1000) AddReport(cross(cur), id, 4000, 700, 4000, 905, "") // Only the owner can correct. testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsWithMessage(t, cur, "only the owner can do this", func() { AddCorrection(cross(cur), id, 4000, 700, 4000, 100, "sabotage") }) // The verdict uses the correction when it is the last report. testing.SetRealm(testing.NewUserRealm(alice)) AddCorrection(cross(cur), id, 4000, 700, 4000, 784, "Read the wrong column.") uassert.Equal(t, "missed", Close(cross(cur), id)) // A closed experiment cannot be corrected. uassert.AbortsWithMessage(t, cur, "experiment is closed", func() { AddCorrection(cross(cur), id, 4000, 700, 4000, 905, "second thoughts") }) uassert.False(t, strings.Contains(Render("1"), "")) } // No form field uses a number input: scrolling over one changes its value. func TestFormsUseTextInputs(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) AddReport(cross(cur), id, 100, 10, 100, 12, "") uassert.False(t, strings.Contains(Render(""), "type=\"number\"")) uassert.False(t, strings.Contains(Render("1"), "type=\"number\"")) } func TestSpan(t *testing.T) { uassert.Equal(t, "less than a minute", span(0)) uassert.Equal(t, "less than a minute", span(59*time.Second)) uassert.Equal(t, "1 minutes", span(60*time.Second)) uassert.Equal(t, "119 minutes", span(119*time.Minute)) uassert.Equal(t, "2 hours", span(120*time.Minute)) uassert.Equal(t, "47 hours", span(47*time.Hour+59*time.Minute)) uassert.Equal(t, "2 days", span(48*time.Hour)) uassert.Equal(t, "19 days", span(19*24*time.Hour+3*time.Hour)) } // The page says how long the plan had been locked when results first arrived. func TestRenderShowsLockToReportGap(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) testing.SkipHeights(1200) AddReport(cross(cur), id, 100, 10, 100, 12, "") page := Render("1") uassert.True(t, strings.Contains(page, "The first report came 1,200 blocks after the plan was locked")) } func TestReportLimit(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) for i := 0; i < MaxReports; i++ { AddReport(cross(cur), id, int64(i+1), 0, int64(i+1), 0, "") } uassert.AbortsContains(t, cur, "report limit reached", func() { AddReport(cross(cur), id, 1000, 0, 1000, 0, "") }) } func TestRenderHome(cur realm, t *testing.T) { reset() home := Render("") uassert.True(t, strings.Contains(home, "# Dokimi")) uassert.True(t, strings.Contains(home, "None yet.")) uassert.True(t, strings.Contains(home, "")) testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) home = Render("") uassert.True(t, strings.Contains(home, "["+tTitle+"]("+webPath+":1)")) uassert.True(t, strings.Contains(home, "+15.00%")) uassert.True(t, strings.Contains(home, "3,800")) uassert.True(t, strings.Contains(home, "| Locked |")) uassert.False(t, strings.Contains(home, "None yet.")) AddReport(cross(cur), id, 100, 10, 100, 12, "") uassert.True(t, strings.Contains(Render(""), "| Running |")) } func TestRenderExperiment(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) id := Register(cross(cur), tTitle, tHypothesis, tMetric, tControl, tVariant, tDirection, tTarget, tMinSample) page := Render("1") uassert.True(t, strings.Contains(page, "# "+tTitle)) uassert.True(t, strings.Contains(page, "> A shorter signup page gets more visitors to finish signing up")) uassert.True(t, strings.Contains(page, "[All experiments]("+webPath+")")) uassert.True(t, strings.Contains(page, DigestOf(id))) uassert.True(t, strings.Contains(page, alice.String())) uassert.True(t, strings.Contains(page, "## No results yet")) uassert.True(t, strings.Contains(page, "")) AddReport(cross(cur), id, 1530, 268, 1547, 304, "analytics dashboard") page = Render("1") uassert.True(t, strings.Contains(page, "## Still running")) uassert.True(t, strings.Contains(page, "268 of 1,530 (17.51%)")) uassert.True(t, strings.Contains(page, "+12.18%")) uassert.True(t, strings.Contains(page, "analytics dashboard")) AddNote(cross(cur), id, "Ended early.") Close(cross(cur), id) page = Render("1") uassert.True(t, strings.Contains(page, "## Verdict: Inconclusive")) uassert.True(t, strings.Contains(page, "Stopped before the planned sample")) uassert.True(t, strings.Contains(page, "Ended early")) uassert.True(t, strings.Contains(page, "reached 1,530")) uassert.False(t, strings.Contains(page, ""), "no report form once closed") uassert.False(t, strings.Contains(page, ""), "no close form once closed") uassert.True(t, strings.Contains(page, "")) } func TestRenderNotFound(t *testing.T) { reset() uassert.True(t, strings.Contains(Render("42"), "does not exist")) uassert.True(t, strings.Contains(Render("nope"), "# Not found")) uassert.True(t, strings.Contains(Render("-1"), "# Not found")) } // Text typed by an owner must not be able to break the page layout. func TestRenderEscapesUserText(cur realm, t *testing.T) { reset() testing.SetRealm(testing.NewUserRealm(alice)) Register(cross(cur), "Bad | title x", "A # heading attempt", tMetric, "A|B", tVariant, tDirection, tTarget, tMinSample) home := Render("") uassert.False(t, strings.Contains(home, "Bad | title"), "pipe in a table cell is escaped") uassert.False(t, strings.Contains(home, "x"), "raw HTML is escaped") page := Render("1") uassert.False(t, strings.Contains(page, "x")) uassert.False(t, strings.Contains(page, "| A|B |")) }