Skip to content

Open source CapCut alternative.

TypeScript

test(analytics): the phone's rollup fixture comes out of the pipeline

David Lec2f06e30b53d
diff --git a/.github/workflows/analytics-contract.yml b/.github/workflows/analytics-contract.yml
new file mode 100644
index 00000000..358c99b3
--- /dev/null
+++ b/.github/workflows/analytics-contract.yml
@@ -0,0 +1,70 @@
+name: Analytics Contract
+
+# The phone decodes the analytics rollup the site's nightly pipeline writes.
+# The site test regenerates the rollup from the pipeline and fails when the
+# checked-in fixture is behind it; the DonkeyKit tests decode that fixture.
+# Together they fail any change that moves the rollup's shape without the
+# phone's model moving with it.
+#
+# The fixture's bytes follow whatever the pipeline imports (the referral
+# source list, the tier limits), so the site job runs on every site change.
+
+on:
+ workflow_dispatch:
+ pull_request:
+ paths:
+ - site/**
+ - apps/ios/DonkeyKit/**
+ - .github/workflows/analytics-contract.yml
+ push:
+ branches: [main]
+ paths:
+ - site/**
+ - apps/ios/DonkeyKit/**
+ - .github/workflows/analytics-contract.yml
+
+permissions:
+ contents: read
+
+concurrency:
+ group: analytics-contract-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ site:
+ name: Site analytics tests
+ runs-on: ubuntu-latest
+ timeout-minutes: 20
+ defaults:
+ run:
+ working-directory: site
+ steps:
+ - uses: actions/checkout@v7
+
+ - uses: actions/setup-node@v7
+ with:
+ node-version: 24
+ cache: npm
+ cache-dependency-path: site/package-lock.json
+
+ - name: Install dependencies
+ run: npm ci
+
+ # The pipeline module constructs its database client on import; the
+ # tests never open a connection, so a placeholder address is enough.
+ - name: Test
+ env:
+ DATABASE_URL: postgresql://placeholder:placeholder@localhost:5432/placeholder
+ DIRECT_URL: postgresql://placeholder:placeholder@localhost:5432/placeholder
+ run: ./node_modules/.bin/bun test src/lib/analytics
+
+ donkeykit:
+ name: DonkeyKit tests
+ runs-on: macos-26
+ timeout-minutes: 30
+ steps:
+ - uses: actions/checkout@v7
+
+ - name: Test
+ working-directory: apps/ios/DonkeyKit
+ run: swift test
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
new file mode 100644
index 00000000..c05d0eff
--- /dev/null
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
@@ -0,0 +1,207 @@
+{
+ "billing": {
+ "canceling": [
+ {
+ "comment": "Export broke.",
+ "customerId": "cus_p",
+ "email": "[email protected]",
+ "endsAt": "2026-09-08T00:00:00.000Z",
+ "feedback": "switched_service",
+ "requestedAt": "2026-08-31T10:00:00.000Z",
+ "subscriptionId": "sub_1"
+ }
+ ],
+ "churned": 1,
+ "dashboardUrl": "https://dashboard.stripe.com/test",
+ "days": [
+ "2026-08-30",
+ "2026-08-31",
+ "2026-09-01",
+ "2026-09-02"
+ ],
+ "events": [
+ {
+ "amountMicros": null,
+ "customerId": "cus_p",
+ "day": "2026-08-31",
+ "detail": "switched_service · Export broke.",
+ "email": "[email protected]",
+ "kind": "canceled",
+ "objectId": "sub_1"
+ },
+ {
+ "amountMicros": "20000000",
+ "customerId": "cus_d",
+ "day": "2026-08-31",
+ "detail": "Your card has insufficient funds.",
+ "email": "[email protected]",
+ "kind": "declined",
+ "objectId": "pi_3"
+ }
+ ],
+ "funded": 2,
+ "fundedMicros": "25000000",
+ "revenue": [
+ {
+ "cancels": 0,
+ "declinedMicros": "0",
+ "otherMicros": "0",
+ "proMicros": "20000000",
+ "refundedMicros": "0",
+ "topupMicros": "0"
+ },
+ {
+ "cancels": 1,
+ "declinedMicros": "20000000",
+ "otherMicros": "0",
+ "proMicros": "0",
+ "refundedMicros": "0",
+ "topupMicros": "0"
+ },
+ {
+ "cancels": 0,
+ "declinedMicros": "0",
+ "otherMicros": "0",
+ "proMicros": "0",
+ "refundedMicros": "0",
+ "topupMicros": "5000000"
+ },
+ {
+ "cancels": 0,
+ "declinedMicros": "0",
+ "otherMicros": "0",
+ "proMicros": "0",
+ "refundedMicros": "1000000",
+ "topupMicros": "0"
+ }
+ ],
+ "subscribers": 2,
+ "window": {
+ "abandonedCheckouts": 1,
+ "cancels": 1,
+ "declinedCustomers": 1,
+ "declinedMicros": "20000000",
+ "netMicros": "24000000",
+ "paidMicros": "25000000",
+ "refundedMicros": "1000000"
+ }
+ },
+ "days": [
+ "2026-08-30",
+ "2026-08-31",
+ "2026-09-01"
+ ],
+ "generatedAt": "2026-09-02T04:00:00.000Z",
+ "missing": [
+ {
+ "day": "2026-08-31",
+ "sources": [
+ "db",
+ "posthog"
+ ]
+ }
+ ],
+ "referrals": {
+ "days": [
+ {
+ "counts": [
+ 0,
+ 0,
+ 0,
+ 1,
+ 0,
+ 0,
+ 0,
+ 1
+ ],
+ "day": "2026-08-31",
+ "others": [
+ "a podcast"
+ ],
+ "respondents": 1
+ },
+ {
+ "counts": [
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0
+ ],
+ "day": "2026-09-01",
+ "others": [],
+ "respondents": 0
+ }
+ ],
+ "sources": [
+ "discord",
+ "tiktok",
+ "instagram",
+ "youtube",
+ "reddit",
+ "search",
+ "friend",
+ "other"
+ ]
+ },
+ "sources": [
+ "inference",
+ "renders",
+ "copies",
+ "libraryAssets",
+ "templates",
+ "creditLedger",
+ "posthog"
+ ],
+ "users": [
+ {
+ "activity": [
+ 0,
+ 0,
+ 64
+ ],
+ "balanceMicros": "0",
+ "email": "[email protected]",
+ "fundedMicros": "5000000",
+ "id": "u_new",
+ "name": "New Person",
+ "registeredAt": "2026-08-31T12:00:00.000Z",
+ "storageBytes": "0",
+ "storageQuotaBytes": 262144000
+ },
+ {
+ "activity": [
+ 65,
+ 0,
+ 64
+ ],
+ "balanceMicros": "3000000",
+ "email": "[email protected]",
+ "fundedMicros": "20000000",
+ "id": "u_pro",
+ "name": "Pro Person",
+ "registeredAt": "2026-08-01T10:00:00.000Z",
+ "storageBytes": "1048576",
+ "storageQuotaBytes": 107374182400
+ },
+ {
+ "activity": [
+ 2,
+ 0,
+ 2
+ ],
+ "balanceMicros": "500000",
+ "email": "[email protected]",
+ "id": "u_su",
+ "name": "Super Person",
+ "registeredAt": "2026-07-01T00:00:00.000Z",
+ "storageBytes": "0",
+ "storageQuotaBytes": null,
+ "superUser": true
+ }
+ ],
+ "version": 1
+}
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index a4f72ece..968744fb 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -197,6 +197,19 @@ DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift test
The camera shows its fallback state in the simulator; capture, torch, zoom,
and HDR need a device.
+### The analytics contract
+
+The Analytics screen decodes the rollup the site's nightly pipeline writes,
+and the two sides are held together by one fixture. The site builds it by
+running the real consolidation over a small fixed record, so the JSON is what
+the server serves today; the checked-in copy lives with the DonkeyKit tests,
+and the site's tests fail while that copy is behind the pipeline. The DonkeyKit
+tests decode the copy through the phone's model. Changing what the rollup
+carries means regenerating the fixture from `site/` with
+`npm run analytics:rollup-fixture` and running the DonkeyKit tests; the
+Analytics Contract workflow runs both on every change to either side, and the
+TestFlight script runs the DonkeyKit tests before it archives.
+
iOS commits never carry the ` [rebuild]` label — that label releases the Mac
app.
diff --git a/scripts/ship-ios-testflight.sh b/scripts/ship-ios-testflight.sh
index 30745505..a49b5f24 100755
--- a/scripts/ship-ios-testflight.sh
+++ b/scripts/ship-ios-testflight.sh
@@ -67,6 +67,12 @@ else
BUILD_ROOT="$SOURCE"
fi
+# The model tests hold the phone to the site's contracts (the analytics
+# rollup fixture among them), so every build that ships reads what the
+# server serves.
+echo "==> Testing DonkeyKit"
+swift test --package-path "$BUILD_ROOT/apps/ios/DonkeyKit" --scratch-path "$ROOT/dist/ios-kit-build" --quiet
+
cat > "$EXPORT_PLIST" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
diff --git a/site/package.json b/site/package.json
index ccf21441..baa4ac6a 100644
--- a/site/package.json
+++ b/site/package.json
@@ -25,6 +25,7 @@
"eval:cut-headless": "bun run scripts/eval-cut-headless.ts",
"email:dev": "email dev --dir src/emails --port 3001",
"test": "bun test src packages/effects-kit/src packages/abexp/src",
+ "analytics:rollup-fixture": "bun run scripts/analytics-rollup-fixture.ts",
"test:genvideo": "bun run scripts/genvideo-selftest.ts",
"eval:genvideo": "bun run scripts/genvideo-eval.ts",
"eval:cut-perf": "bun run scripts/eval-cut-perf.ts",
diff --git a/site/scripts/analytics-rollup-fixture.ts b/site/scripts/analytics-rollup-fixture.ts
new file mode 100644
index 00000000..bcab3687
--- /dev/null
+++ b/site/scripts/analytics-rollup-fixture.ts
@@ -0,0 +1,14 @@
+// Rewrites the analytics rollup fixture the DonkeyKit tests decode. Run it
+// after changing what the rollup carries, then run the DonkeyKit tests.
+//
+// npm run analytics:rollup-fixture
+import { mkdirSync, writeFileSync } from "node:fs";
+import { dirname, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { ROLLUP_FIXTURE_PATH, rollupFixtureJson } from "@/lib/analytics/rollup-fixture";
+
+const path = resolve(fileURLToPath(new URL("../..", import.meta.url)), ROLLUP_FIXTURE_PATH);
+mkdirSync(dirname(path), { recursive: true });
+writeFileSync(path, await rollupFixtureJson());
+console.log(`wrote ${path}`);
diff --git a/site/src/lib/analytics/rollup-fixture.test.ts b/site/src/lib/analytics/rollup-fixture.test.ts
new file mode 100644
index 00000000..ab1f7e27
--- /dev/null
+++ b/site/src/lib/analytics/rollup-fixture.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, test } from "bun:test";
+import { readFileSync } from "node:fs";
+import { resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { ROLLUP_FIXTURE_PATH, buildRollupFixture, rollupFixtureJson } from "./rollup-fixture";
+import { analyticsRollupSchema } from "./schema";
+
+describe("the phone's rollup fixture", () => {
+ test("is a rollup the dashboards read", async () => {
+ const parsed = analyticsRollupSchema.parse(await buildRollupFixture());
+ expect(parsed.days).toHaveLength(3);
+ expect(parsed.billing?.days).toHaveLength(4);
+ expect(parsed.missing).toEqual([{ day: "2026-08-31", sources: ["db", "posthog"] }]);
+ expect(parsed.users.map((u) => u.id)).toEqual(["u_new", "u_pro", "u_su"]);
+ });
+
+ test("matches the copy checked in with the DonkeyKit tests", async () => {
+ const here = fileURLToPath(new URL(".", import.meta.url));
+ const checkedIn = readFileSync(resolve(here, "../../../..", ROLLUP_FIXTURE_PATH), "utf8");
+ const current = await rollupFixtureJson();
+ if (checkedIn !== current) {
+ throw new Error(
+ "The rollup's shape changed. Run `npm run analytics:rollup-fixture` in site/, then `swift test` in apps/ios/DonkeyKit so the phone's model still reads it.",
+ );
+ }
+ expect(checkedIn).toBe(current);
+ });
+});
diff --git a/site/src/lib/analytics/rollup-fixture.ts b/site/src/lib/analytics/rollup-fixture.ts
new file mode 100644
index 00000000..d1d6551d
--- /dev/null
+++ b/site/src/lib/analytics/rollup-fixture.ts
@@ -0,0 +1,100 @@
+// The rollup the phone's model is tested against. It is the real consolidation
+// (buildRollup) run over a small fixed record, so the JSON says exactly what
+// the nightly job writes today.
+//
+// The checked-in copy lives with the DonkeyKit tests; `npm run
+// analytics:rollup-fixture` rewrites it. rollup-fixture.test.ts fails while
+// the copy is behind this code, and the DonkeyKit tests decode the copy, so a
+// change to the rollup's shape reaches the phone in the same change.
+import { buildRollup } from "./pipeline";
+import type { AnalyticsRollup, AnalyticsSnapshotFile } from "./schema";
+import type { AnalyticsDayFiles } from "./pipeline";
+
+export const ROLLUP_FIXTURE_PATH =
+ "apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json";
+
+// Three closed days of activity, and a billing window that runs one day
+// further, to today: the two windows never line up by index.
+const DAYS = ["2026-08-30", "2026-08-31", "2026-09-01"];
+const BILLING_DAYS = [...DAYS, "2026-09-02"];
+
+const snapshot: AnalyticsSnapshotFile = {
+ balances: [
+ { balanceMicros: "3000000", userId: "u_pro" },
+ { balanceMicros: "500000", userId: "u_su" },
+ ],
+ generatedAt: "2026-09-02T04:00:00.000Z",
+ storage: [{ bytes: "1048576", userId: "u_pro" }],
+ stripe: {
+ abandonedCheckouts: [
+ { customerId: "cus_a", day: "2026-08-31", email: "[email protected]", id: "cs_1", kind: "pro" },
+ ],
+ charges: [
+ { amountMicros: "20000000", customerId: "cus_p", day: "2026-08-30", email: "[email protected]", failure: null, id: "ch_1", kind: "pro", paymentIntentId: "pi_1", status: "paid", userId: "u_pro" },
+ { amountMicros: "5000000", customerId: "cus_n", day: "2026-09-01", email: "[email protected]", failure: null, id: "ch_2", kind: "topup", paymentIntentId: "pi_2", status: "paid", userId: "u_new" },
+ { amountMicros: "20000000", customerId: "cus_d", day: "2026-08-31", email: "[email protected]", failure: "Your card has insufficient funds.", id: "ch_3", kind: "pro", paymentIntentId: "pi_3", status: "declined", userId: null },
+ ],
+ dashboardUrl: "https://dashboard.stripe.com/test",
+ refunds: [{ amountMicros: "1000000", chargeId: "ch_2", day: "2026-09-02", id: "re_1" }],
+ subscriptions: [
+ { cancelAt: "2026-09-08T00:00:00.000Z", cancelScheduled: true, canceledAt: "2026-08-31T10:00:00.000Z", comment: "Export broke.", createdAt: "2026-08-09T00:00:00.000Z", customerId: "cus_p", email: "[email protected]", feedback: "switched_service", id: "sub_1", status: "active", userId: "u_pro" },
+ { cancelAt: null, cancelScheduled: false, canceledAt: null, comment: null, createdAt: "2026-07-01T00:00:00.000Z", customerId: "cus_s", email: "[email protected]", feedback: null, id: "sub_2", status: "active", userId: "u_su" },
+ { cancelAt: null, cancelScheduled: false, canceledAt: "2026-07-15T00:00:00.000Z", comment: null, createdAt: "2026-06-01T00:00:00.000Z", customerId: "cus_r", email: "[email protected]", feedback: null, id: "sub_3", status: "canceled", userId: "u_gone" },
+ ],
+ },
+ users: [
+ { createdAt: "2026-08-01T10:00:00.000Z", email: "[email protected]", id: "u_pro", name: "Pro Person" },
+ {
+ createdAt: "2026-08-31T12:00:00.000Z",
+ email: "[email protected]",
+ id: "u_new",
+ name: "New Person",
+ referralAnsweredAt: "2026-08-31T12:05:00.000Z",
+ referralOther: "a podcast",
+ referralSources: ["youtube", "other"],
+ },
+ {
+ createdAt: "2026-07-01T00:00:00.000Z",
+ email: "[email protected]",
+ id: "u_su",
+ name: "Super Person",
+ superUser: true,
+ },
+ ],
+ version: 1,
+};
+
+const dayFiles = new Map<string, AnalyticsDayFiles>([
+ [
+ "2026-08-30",
+ {
+ db: { active: { inference: ["u_pro"], renders: ["u_su"] }, day: "2026-08-30", generatedAt: "2026-08-31T04:00:00.000Z", version: 1 },
+ posthog: { activeDistinctIds: ["u_pro"], day: "2026-08-30", generatedAt: "2026-08-31T04:00:00.000Z", version: 1 },
+ },
+ ],
+ // A night the extraction missed: both files absent.
+ ["2026-08-31", { db: null, posthog: null }],
+ [
+ "2026-09-01",
+ {
+ db: { active: { renders: ["u_su"] }, day: "2026-09-01", generatedAt: "2026-09-02T04:00:00.000Z", version: 1 },
+ // An anonymous pre-sign-in id rides along and drops out.
+ posthog: { activeDistinctIds: ["u_pro", "u_new", "anon-7f3a"], day: "2026-09-01", generatedAt: "2026-09-02T04:00:00.000Z", version: 1 },
+ },
+ ],
+]);
+
+export function buildRollupFixture(): Promise<AnalyticsRollup> {
+ return buildRollup({
+ billingDays: BILLING_DAYS,
+ days: DAYS,
+ generatedAt: "2026-09-02T04:00:00.000Z",
+ posthogConfigured: true,
+ readDayFiles: async (day) => dayFiles.get(day) ?? { db: null, posthog: null },
+ snapshot,
+ });
+}
+
+export async function rollupFixtureJson(): Promise<string> {
+ return `${JSON.stringify(await buildRollupFixture(), null, 2)}\n`;
+}