railyard/apps/console/test/sites-insights.test.ts
caelum0x 1bc04fa81b feat(insights): the Insights fork behind the add-on, site entries, tenant summaries, caps that say what happens (PY1.3)
Railyard side of PY1.3 (RYP-D2, RYP-E1, SC-E3). Acceptance:
1. apps/insights: umami-software/umami v3.4.0 (MIT, commit ec0ff503),
   vendored --depth 1, LICENSE kept, NOTICE added. Fork additions only:
   src/railyard/{ingest,summary,tenancy,api-only}.ts, /v1/ingest/batch (the
   edge collector contract v1, bearer INSIGHTS_INGEST_TOKEN, no IP ever,
   retried batches de-duplicated by migration 90_railyard_ingest_receipts),
   /api/v1/tenants/{org}/summary (bearer INSIGHTS_API_TOKEN), src/proxy.ts
   (INSIGHTS_API_ONLY=1 serves only the Railyard API and the heartbeat).
   Tenant isolation is tested at the API level (an app id owned by another org
   is refused) and at the SQL level (upstream migrations 01-26 plus 90 on a
   throwaway cluster: org B never reads org A's rows, even naming its app).
   Upstream vitest suite: not run on the Mac (pnpm 12.3.4 pinned upstream,
   disk floor); it runs in the image build on box 2 or CI.
2. No upstream product name in anything a customer loads: the page tag, the
   console pages and the fork's additions (tests); the upstream dashboard is
   never served in API-only mode.
3. The console writes the collector's site entry on "Add app", on
   auto-created apps from deploys, and rewrites every entry on add-on, plan or
   pause changes (lib/sites.ts, SITES KV binding). The bridge gains
   `sites` (the project's app, site key, page tag and collector URLs, entry
   written) and the analytics proof is the first event from Insights. The
   console's Insights page shows visitors, events, first event and the
   visit -> signup -> first action funnel.
5. insights-edge/src/caps.ts: at 100% of the cap events are still counted and
   not stored until the next UTC month; the console shows the same message
   from a byte-identical copy (lib/caps.ts, tested).
4 (384 MB at 50 events a second for 10 min) is measured on staging before deploy.

Env: INSIGHTS_API_URL, INSIGHTS_API_TOKEN, SITES KV binding (console);
INSIGHTS_INGEST_TOKEN, INSIGHTS_API_TOKEN, INSIGHTS_API_ONLY (Insights).

Tests: console 54/54 (tsc clean), insights fork 7/7 (incl. SQL isolation),
insights-edge caps + collector + sim 14/14, tests/contract 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 21:23:38 +03:00

90 lines
4.9 KiB
TypeScript

// Site entries for the edge collector (RYP-E1) and the Insights tenant client (PY1.3).
import { test } from "node:test";
import assert from "node:assert/strict";
import { addApp } from "../lib/apps.ts";
import { firstEvent, insightsClient } from "../lib/insights.ts";
import { applyLadder } from "../lib/ladder.ts";
import { MemoryRepo } from "../lib/repo.ts";
import { MemorySites, registerSite, syncSites } from "../lib/sites.ts";
import { emptyEntitlement } from "../lib/types.ts";
const now = new Date("2026-09-29T12:00:00Z");
const ORG = "0b8f6a3e-1111-4a3e-8b8f-0b8f6a3e1111";
test("an app's site entry carries the org, its cap and the pause; a pause rewrites every entry", async () => {
const repo = new MemoryRepo(() => now);
const org = await repo.createOrgForUser({ userId: "u", email: "a@x.com", name: "a" });
await repo.saveEntitlement(applyLadder(emptyEntitlement(org.id, now), { kind: "card_check_paid", paymentId: "p", customerId: null, at: now }).ent);
const sites = new MemorySites();
const app = await addApp(repo, org.id, "shop", now);
await registerSite(sites, repo, app, now);
assert.deepEqual(sites.entries.get(`site:${app.site_key}`), { org_id: org.id, app_id: app.id, events_limit: 100_000, paused: false });
const ent = await repo.getEntitlement(org.id);
await repo.saveEntitlement({ ...ent!, paused: true, status: "paused" });
assert.equal(await syncSites(sites, repo, org.id, now), 1);
assert.equal(sites.entries.get(`site:${app.site_key}`)?.paused, true);
});
test("the Insights client sends the bearer token only as a header and validates the answer", async () => {
const calls: { url: string; auth: string | null }[] = [];
const body = {
org_id: ORG,
since: "2026-09-22T12:00:00.000Z",
apps: [
{ app_id: "5d1c0000-aaaa-4aaa-8aaa-5d1c0000aaaa", first_event_at: "2026-09-29T11:00:00.000Z", events: 4, visitors: 2 },
{ app_id: "5d1c0000-bbbb-4bbb-8bbb-5d1c0000bbbb", first_event_at: null, events: 0, visitors: 0 },
],
funnel: { steps: [{ name: "visit", sessions: 2 }, { name: "signup", sessions: 1 }, { name: "first_action", sessions: 1 }] },
};
const client = insightsClient({
apiUrl: "https://insights.internal/",
token: "tok_0123456789abcdef",
fetchImpl: (async (url: string, init: RequestInit) => {
calls.push({ url, auth: new Headers(init.headers).get("authorization") });
return new Response(JSON.stringify(body), { status: 200 });
}) as typeof fetch,
});
const s = await client.summary(ORG, { days: 7 });
assert.equal(calls[0].url, `https://insights.internal/api/v1/tenants/${ORG}/summary?days=7`);
assert.equal(calls[0].auth, "Bearer tok_0123456789abcdef");
assert.ok(!calls[0].url.includes("tok_"));
assert.deepEqual(firstEvent(s), { app_id: body.apps[0].app_id, at: "2026-09-29T11:00:00.000Z" });
assert.equal(firstEvent(s, body.apps[1].app_id), null);
await assert.rejects(client.summary("nope"), /invalid/);
const bad = insightsClient({ apiUrl: "https://i", token: "t", fetchImpl: (async () => new Response("{}", { status: 200 })) as typeof fetch });
await assert.rejects(bad.summary(ORG), /unexpected shape/);
});
test("the Insights page model: rows per app and the funnel with step-to-step rates", async () => {
const { insightsView } = await import("../lib/insights-view.ts");
const v = insightsView(
{
org_id: ORG,
since: "x",
apps: [{ app_id: "5d1c0000-aaaa-4aaa-8aaa-5d1c0000aaaa", first_event_at: "2026-09-29T11:00:00.000Z", events: 4, visitors: 2 }],
funnel: { steps: [{ name: "visit", sessions: 4 }, { name: "signup", sessions: 2 }, { name: "first_action", sessions: 1 }] },
},
new Map([["5d1c0000-aaaa-4aaa-8aaa-5d1c0000aaaa", "shop"]]),
);
assert.deepEqual(v.apps, [{ name: "shop", firstEvent: "2026-09-29 11:00 UTC", visitors: 2, events: 4 }]);
assert.deepEqual(v.steps.map((s) => [s.label, s.sessions, s.ofPrevious]), [["Visited", 4, null], ["Signed up", 2, "50%"], ["Did a first action", 1, "50%"]]);
assert.equal(v.empty, false);
});
test("the console's caps module is a byte-for-byte copy of the edge collector's (one message everywhere)", async () => {
const { readFileSync } = await import("node:fs");
const { join, dirname } = await import("node:path");
const { fileURLToPath } = await import("node:url");
const here = dirname(fileURLToPath(import.meta.url));
assert.equal(readFileSync(join(here, "../lib/caps.ts"), "utf8"), readFileSync(join(here, "../../insights-edge/src/caps.ts"), "utf8"));
const { insightsView } = await import("../lib/insights-view.ts");
const v = insightsView(
{ org_id: ORG, since: "x", apps: [{ app_id: "a", first_event_at: null, events: 5, visitors: 1, month_events: 100 }], funnel: { steps: [{ name: "visit", sessions: 0 }, { name: "signup", sessions: 0 }, { name: "first_action", sessions: 0 }] } },
new Map(),
100,
new Date("2026-09-29T12:00:00Z"),
);
assert.match(v.capNote ?? "", /still counted, and are not stored until 2026-10-01/);
});