package dokimi import ( "testing" "gno.land/p/nt/uassert/v0" ) // samplePlan is a valid plan with a 15% target and 3,800 exposures promised // for each arm. func samplePlan() Plan { return Plan{ Title: "Short signup page vs current page", Hypothesis: "A shorter signup page gets more visitors to finish signing up than the current page.", Metric: "signup_completed", Control: "Current page", Variant: "Short page", Direction: Increase, TargetBps: 1500, MinSample: 3800, } } func TestPlanValidate(t *testing.T) { uassert.NoError(t, samplePlan().Validate()) cases := []struct { name string change func(p *Plan) want string }{ {"empty title", func(p *Plan) { p.Title = "" }, "title is required"}, {"empty hypothesis", func(p *Plan) { p.Hypothesis = "" }, "hypothesis is required"}, {"empty metric", func(p *Plan) { p.Metric = "" }, "metric is required"}, {"same arm names", func(p *Plan) { p.Variant = p.Control }, "different names"}, {"bad direction", func(p *Plan) { p.Direction = "sideways" }, "direction must be"}, {"zero target", func(p *Plan) { p.TargetBps = 0 }, "target must be between"}, {"huge target", func(p *Plan) { p.TargetBps = MaxTargetBps + 1 }, "target must be between"}, {"zero sample", func(p *Plan) { p.MinSample = 0 }, "minimum sample must be between"}, {"huge sample", func(p *Plan) { p.MinSample = MaxCount + 1 }, "minimum sample must be between"}, {"newline in title", func(p *Plan) { p.Title = "a\nb" }, "single line"}, } for _, c := range cases { p := samplePlan() c.change(&p) uassert.ErrorContains(t, p.Validate(), c.want, c.name) } } func TestPlanValidateLength(t *testing.T) { p := samplePlan() long := "" for i := 0; i <= MaxNameLen; i++ { long += "x" } p.Title = long uassert.ErrorContains(t, p.Validate(), "title is longer than") } func TestDigest(t *testing.T) { a := samplePlan() b := samplePlan() uassert.Equal(t, 64, len(a.Digest())) uassert.Equal(t, a.Digest(), b.Digest(), "same plan, same digest") // Changing any single field changes the digest. b.TargetBps = 1000 uassert.NotEqual(t, a.Digest(), b.Digest(), "target is part of the digest") c := samplePlan() c.MinSample = 1500 uassert.NotEqual(t, a.Digest(), c.Digest(), "sample is part of the digest") d := samplePlan() d.Direction = Decrease uassert.NotEqual(t, a.Digest(), d.Digest(), "direction is part of the digest") } // Moving text from one field to its neighbour must not keep the digest. func TestDigestFieldBoundaries(t *testing.T) { a := samplePlan() a.Control = "ab" a.Variant = "c" b := samplePlan() b.Control = "a" b.Variant = "bc" uassert.NotEqual(t, a.Digest(), b.Digest()) } func TestTally(t *testing.T) { uassert.NoError(t, Tally{Exposures: 10, Conversions: 10}.Validate()) uassert.NoError(t, Tally{}.Validate()) uassert.ErrorContains(t, Tally{Exposures: 5, Conversions: 6}.Validate(), "cannot exceed") uassert.ErrorContains(t, Tally{Exposures: -1}.Validate(), "negative") uassert.ErrorContains(t, Tally{Exposures: MaxCount + 1}.Validate(), "maximum") uassert.Equal(t, int64(0), Tally{}.RateBps()) uassert.Equal(t, int64(1751), Tally{Exposures: 1530, Conversions: 268}.RateBps()) uassert.Equal(t, int64(10000), Tally{Exposures: 7, Conversions: 7}.RateBps()) earlier := Tally{Exposures: 100, Conversions: 20} uassert.True(t, Tally{Exposures: 100, Conversions: 20}.Covers(earlier)) uassert.True(t, Tally{Exposures: 150, Conversions: 21}.Covers(earlier)) uassert.False(t, Tally{Exposures: 99, Conversions: 20}.Covers(earlier)) uassert.False(t, Tally{Exposures: 150, Conversions: 19}.Covers(earlier)) } func TestLiftBps(t *testing.T) { // (304*1530 - 268*1547) * 10000 / (268*1547) = 1218.6, rounded toward zero. lift, ok := LiftBps(Tally{Exposures: 1530, Conversions: 268}, Tally{Exposures: 1547, Conversions: 304}) uassert.True(t, ok) uassert.Equal(t, int64(1218), lift) // Equal rates give no lift. lift, ok = LiftBps(Tally{Exposures: 1000, Conversions: 100}, Tally{Exposures: 500, Conversions: 50}) uassert.True(t, ok) uassert.Equal(t, int64(0), lift) // A worse variant gives a negative lift: 8% vs 10% is -20%. lift, ok = LiftBps(Tally{Exposures: 1000, Conversions: 100}, Tally{Exposures: 1000, Conversions: 80}) uassert.True(t, ok) uassert.Equal(t, int64(-2000), lift) // Undefined cases. _, ok = LiftBps(Tally{Exposures: 1000, Conversions: 0}, Tally{Exposures: 1000, Conversions: 80}) uassert.False(t, ok, "control without conversions") _, ok = LiftBps(Tally{}, Tally{Exposures: 1000, Conversions: 80}) uassert.False(t, ok, "control without exposures") _, ok = LiftBps(Tally{Exposures: 1000, Conversions: 80}, Tally{}) uassert.False(t, ok, "variant without exposures") _, ok = LiftBps(Tally{Exposures: 5, Conversions: 6}, Tally{Exposures: 10, Conversions: 1}) uassert.False(t, ok, "invalid tally") } // The largest allowed counts must not overflow int64. func TestLiftBpsAtLimits(t *testing.T) { lift, ok := LiftBps( Tally{Exposures: MaxCount, Conversions: 1}, Tally{Exposures: MaxCount, Conversions: MaxCount}, ) uassert.True(t, ok) uassert.Equal(t, (MaxCount-1)*BpsScale, lift) } func TestJudgeStoppedEarly(t *testing.T) { // Looks like a win, but both arms are short of the 3,800 promised. v := Judge(samplePlan(), Tally{Exposures: 1530, Conversions: 268}, Tally{Exposures: 1547, Conversions: 400}) uassert.Equal(t, string(Inconclusive), string(v.Outcome)) uassert.True(t, v.LiftDefined) uassert.True(t, v.LiftBps > 1500, "the raw lift is above target and still does not count") uassert.Equal(t, int64(1751), v.ControlRateBps) uassert.Equal(t, "Stopped before the planned sample: Current page reached 1,530 and Short page reached 1,547 of the 3,800 exposures promised for each.", v.Reason) } func TestJudgeOneArmShort(t *testing.T) { v := Judge(samplePlan(), Tally{Exposures: 3800, Conversions: 665}, Tally{Exposures: 3799, Conversions: 800}) uassert.Equal(t, string(Inconclusive), string(v.Outcome)) } func TestJudgeMissed(t *testing.T) { // 17.5% vs 19.6% at full sample: +12.0%, below the 15% target. v := Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 700}, Tally{Exposures: 4000, Conversions: 784}) uassert.Equal(t, string(Missed), string(v.Outcome)) uassert.Equal(t, int64(1200), v.LiftBps) } // The verdict sentence claims only what the registry can prove: the target // was locked in the plan. func TestJudgeReasonWording(t *testing.T) { v := Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 700}, Tally{Exposures: 4000, Conversions: 805}) uassert.Equal(t, "The change of +15.00% reached the target of +15.00% locked in the plan.", v.Reason) v = Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 700}, Tally{Exposures: 4000, Conversions: 784}) uassert.Equal(t, "The change of +12.00% did not reach the target of +15.00% locked in the plan.", v.Reason) } func TestJudgeMet(t *testing.T) { // 17.5% vs 20.125% at full sample: exactly +15.0%. v := Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 700}, Tally{Exposures: 4000, Conversions: 805}) uassert.Equal(t, string(Met), string(v.Outcome)) uassert.Equal(t, int64(1500), v.LiftBps) // One conversion fewer falls just short. v = Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 700}, Tally{Exposures: 4000, Conversions: 804}) uassert.Equal(t, string(Missed), string(v.Outcome)) } func TestJudgeDecrease(t *testing.T) { p := samplePlan() p.Direction = Decrease p.TargetBps = 1000 // the metric should fall by at least 10% // 10% down to 8% is -20%: met. v := Judge(p, Tally{Exposures: 4000, Conversions: 400}, Tally{Exposures: 4000, Conversions: 320}) uassert.Equal(t, string(Met), string(v.Outcome)) uassert.Equal(t, int64(-2000), v.LiftBps) // 10% up to 12% moved the wrong way: missed. v = Judge(p, Tally{Exposures: 4000, Conversions: 400}, Tally{Exposures: 4000, Conversions: 480}) uassert.Equal(t, string(Missed), string(v.Outcome)) } func TestJudgeNoControlConversions(t *testing.T) { v := Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 0}, Tally{Exposures: 4000, Conversions: 10}) uassert.Equal(t, string(Inconclusive), string(v.Outcome)) uassert.False(t, v.LiftDefined) } func TestJudgeInvalidInput(t *testing.T) { bad := samplePlan() bad.TargetBps = 0 v := Judge(bad, Tally{Exposures: 4000, Conversions: 700}, Tally{Exposures: 4000, Conversions: 900}) uassert.Equal(t, string(Inconclusive), string(v.Outcome)) v = Judge(samplePlan(), Tally{Exposures: 4000, Conversions: 4001}, Tally{Exposures: 4000, Conversions: 900}) uassert.Equal(t, string(Inconclusive), string(v.Outcome)) } func TestParsePercent(t *testing.T) { good := []struct { in string want int64 }{ {"15", 1500}, {"15%", 1500}, {" 15 % ", 1500}, {"12.5", 1250}, {"12.50", 1250}, {"0.25%", 25}, {".5", 50}, {"7.", 700}, {"0", 0}, {"100", 10000}, } for _, c := range good { got, err := ParsePercent(c.in) uassert.NoError(t, err, c.in) uassert.Equal(t, c.want, got, c.in) } bad := []string{"", "%", ".", "-5", "+5", "1.234", "abc", "1e3", "12,5", "1234567"} for _, in := range bad { _, err := ParsePercent(in) uassert.Error(t, err, in) } } func TestRateText(t *testing.T) { uassert.Equal(t, "17.51%", Tally{Exposures: 1530, Conversions: 268}.RateText()) uassert.Equal(t, "100.00%", Tally{Exposures: 7, Conversions: 7}.RateText()) uassert.Equal(t, "1.00%", Tally{Exposures: 100, Conversions: 1}.RateText()) uassert.Equal(t, "0.9900%", Tally{Exposures: 10000, Conversions: 99}.RateText()) uassert.Equal(t, "0.0164%", Tally{Exposures: 200745, Conversions: 33}.RateText()) uassert.Equal(t, "0.0571%", Tally{Exposures: 201229, Conversions: 115}.RateText()) uassert.Equal(t, "0.0000%", Tally{Exposures: MaxCount, Conversions: 0}.RateText()) uassert.Equal(t, "0.00%", Tally{}.RateText()) uassert.Equal(t, "0.00%", Tally{Exposures: 5, Conversions: 6}.RateText(), "invalid tally") } // A rare-event trial: the lift comes from the raw counts, so it stays exact // even though both rates are far below one basis point of difference. func TestJudgeRareEvents(t *testing.T) { p := samplePlan() p.Direction = Decrease p.TargetBps = 5000 p.MinSample = 100000 v := Judge(p, Tally{Exposures: 201229, Conversions: 115}, Tally{Exposures: 200745, Conversions: 33}) uassert.Equal(t, string(Met), string(v.Outcome)) // (33*201229 - 115*200745) * 10000 / (115*200745) = -7123.5 uassert.Equal(t, int64(-7123), v.LiftBps) } func TestCommas(t *testing.T) { uassert.Equal(t, "0", Commas(0)) uassert.Equal(t, "999", Commas(999)) uassert.Equal(t, "1,000", Commas(1000)) uassert.Equal(t, "1,530", Commas(1530)) uassert.Equal(t, "10,000,000", Commas(MaxCount)) uassert.Equal(t, "-1,234", Commas(-1234)) uassert.Equal(t, "-999", Commas(-999)) } func TestFormat(t *testing.T) { uassert.Equal(t, "+12.20%", FormatBps(1220)) uassert.Equal(t, "+0.05%", FormatBps(5)) uassert.Equal(t, "+0.00%", FormatBps(0)) uassert.Equal(t, "-3.50%", FormatBps(-350)) uassert.Equal(t, "+100.00%", FormatBps(10000)) uassert.Equal(t, "17.50%", FormatRate(1750)) uassert.Equal(t, "0.00%", FormatRate(0)) }