logicsrc/packages/validators/src/openabtest.test.ts
Anthony Ettinger 2b9e9a6b2c OpenABTest 0.2-draft: goal conversions
0.1 conversions are purchases with a price, so button, onboarding and
landing-page experiments had nothing conformant to report. 0.2 adds a goal
conversion: metrics.conversion "unique-participants-with-goal" with
metrics.goal, metrics.profit "none", and conversion events carrying exactly
one of price or goal (optional observed value). Backward compatible: every
0.1 document stays valid; goal forms require "0.2-draft". The reference
implementation is w3bs.org/ab-testing.

@logicsrc/schemas and @logicsrc/validators 0.5.0, @logicsrc/sdk 0.4.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 11:46:36 +00:00

143 lines
7.1 KiB
TypeScript

import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { validate } from "./index.js";
function fixture(name: string) {
return JSON.parse(readFileSync(new URL(`../../schemas/fixtures/openabtest/${name}.json`, import.meta.url), "utf8"));
}
describe("OpenABTest draft contracts", () => {
it("accepts the every-referred-purchase experiment", () => {
expect(validate("openabtest-manifest", fixture("chovy-manifest")).ok).toBe(true);
});
it.each(["eligibility", "assignment", "exposure", "conversion", "adjustment", "reconciliation-pending", "reconciliation"])("accepts %s independently", (name) => {
expect(validate("openabtest-event", fixture(name)).ok).toBe(true);
});
it("rejects duplicate variant identities even when weights differ", () => {
const manifest = fixture("chovy-manifest");
manifest.variants[1].id = manifest.variants[0].id;
manifest.variants[1].weight = 2;
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
});
it.each([0, -1, 0.5, Number.MAX_SAFE_INTEGER + 1])("rejects invalid weight %s", (weight) => {
const manifest = fixture("chovy-manifest");
manifest.variants[0].weight = weight;
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
});
it("rejects unsafe sums and backwards windows", () => {
const manifest = fixture("chovy-manifest");
manifest.variants[0].weight = Number.MAX_SAFE_INTEGER;
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
manifest.variants[0].weight = 1;
manifest.window.endsAt = manifest.window.startsAt;
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
});
it("rejects client pricing authority and malformed discounts", () => {
const manifest = fixture("chovy-manifest");
manifest.assignment.authority = "client";
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
manifest.assignment.authority = "server";
manifest.variants[0].parameters.discountBps = "500";
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
});
it.each(["assignment", "exposure", "conversion"])("requires assignment attribution on %s", (name) => {
const event = fixture(name);
delete event.payload.assignmentId;
expect(validate("openabtest-event", event).ok).toBe(false);
});
it("rejects unrecognized event payloads and identifiable participant values", () => {
const event = fixture("exposure");
event.payload.email = "person@example.com";
expect(validate("openabtest-event", event).ok).toBe(false);
delete event.payload.email;
event.participantId = "person@example.com";
expect(validate("openabtest-event", event).ok).toBe(false);
});
it("checks the accepted discount and half-up usage arithmetic", () => {
const event = fixture("conversion");
event.payload.price.chargedUnitPriceMinor = 38000;
expect(validate("openabtest-event", event).ok).toBe(false);
Object.assign(event.payload.price, { listUnitPriceMinor: 101, chargedUnitPriceMinor: 91, quantityMilliUnits: 500, totalMinor: 46 });
expect(validate("openabtest-event", event).ok).toBe(true);
event.payload.price.totalMinor = 45;
expect(validate("openabtest-event", event).ok).toBe(false);
});
it("rejects a conversion dated before the accepted offer", () => {
const event = fixture("conversion");
event.at = "2026-09-14T00:00:00Z";
expect(validate("openabtest-event", event).ok).toBe(false);
});
it("requires a revenue reduction and evidence for refunds", () => {
const event = fixture("adjustment");
event.payload.amountMinor = 3600;
expect(validate("openabtest-event", event).ok).toBe(false);
event.payload.amountMinor = -3600;
event.payload.proofRefs = [];
expect(validate("openabtest-event", event).ok).toBe(false);
});
it("withholds payouts when actual costs or fees are missing", () => {
const event = fixture("reconciliation");
event.payload.actualCostMinor = null;
expect(validate("openabtest-event", event).ok).toBe(false);
event.payload.state = "pending";
expect(validate("openabtest-event", event).ok).toBe(false);
Object.assign(event.payload, { affiliateMinor: null, retainedProfitMinor: null, payoutStatus: "withheld" });
expect(validate("openabtest-event", event).ok).toBe(true);
});
it("rejects inconsistent accounting and unsupported profit claims", () => {
const event = fixture("reconciliation");
event.payload.affiliateMinor = 21000;
expect(validate("openabtest-event", event).ok).toBe(false);
event.payload.retainedProfitMinor = 4000;
expect(validate("openabtest-event", event).ok).toBe(false);
Object.assign(event.payload, { affiliateMinor: 0, actualCostMinor: 36000, retainedProfitMinor: -1000, payoutStatus: "withheld" });
expect(validate("openabtest-event", event).ok).toBe(true);
});
it("accepts 0.2 goal experiments and goal conversions", () => {
expect(validate("openabtest-manifest", fixture("goal-manifest")).ok).toBe(true);
expect(validate("openabtest-event", fixture("goal-conversion")).ok).toBe(true);
const valued = fixture("goal-conversion");
valued.payload.value = { currency: "USD", amountMinor: 4900 };
expect(validate("openabtest-event", valued).ok).toBe(true);
valued.payload.value.amountMinor = -1;
expect(validate("openabtest-event", valued).ok).toBe(false);
});
it("keeps 0.1 documents valid and gates goal forms behind 0.2-draft", () => {
const old = fixture("chovy-manifest");
old.openabtest = "0.2-draft";
expect(validate("openabtest-manifest", old).ok).toBe(true);
const manifest = fixture("goal-manifest");
manifest.openabtest = "0.1-draft";
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
const event = fixture("goal-conversion");
event.openabtest = "0.1-draft";
expect(validate("openabtest-event", event).ok).toBe(false);
});
it("requires a named goal and a conversion-rate primary for goal experiments", () => {
const manifest = fixture("goal-manifest");
delete manifest.metrics.goal;
expect(validate("openabtest-manifest", manifest).ok).toBe(false);
const profit = fixture("goal-manifest");
profit.metrics.primary = "retained-profit-per-eligible-visitor";
expect(validate("openabtest-manifest", profit).ok).toBe(false);
const stray = fixture("chovy-manifest");
stray.metrics.goal = "signup-completed";
expect(validate("openabtest-manifest", stray).ok).toBe(false);
});
it("takes exactly one of price or goal on a conversion", () => {
const both = fixture("conversion");
both.openabtest = "0.2-draft";
both.payload.goal = "signup-completed";
expect(validate("openabtest-event", both).ok).toBe(false);
const neither = fixture("goal-conversion");
delete neither.payload.goal;
expect(validate("openabtest-event", neither).ok).toBe(false);
});
it("requires reconciliation evidence without bearer URLs", () => {
const event = fixture("reconciliation");
event.payload.proofRefs = [];
expect(validate("openabtest-event", event).ok).toBe(false);
event.payload.proofRefs = ["https://example.com/receipt?token=secret"];
expect(validate("openabtest-event", event).ok).toBe(false);
});
});