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

92 lines
3.2 KiB
TypeScript

import { test as base, request } from '@playwright/test';
import { ApiClient } from './client';
import { login } from './helpers/auth';
import { SHARE_CONTEXT_HEADER, SHARE_TOKEN_HEADER } from './helpers/constants';
import { readSeedState, type SeedState } from './seed/state';
export interface WorkerTokens {
admin: string;
user: string;
viewer: string;
apiKey: { id: string; key: string };
}
interface WorkerFixtures {
/** Seeded entities and dataset metadata written by global setup. */
seed: SeedState;
/** Auth tokens created once per worker process. */
tokens: WorkerTokens;
}
interface TestFixtures {
/** Unauthenticated client. */
api: ApiClient;
/** Logged in as the built-in admin (owns the seeded website, link, pixel, team). */
admin: ApiClient;
/** Logged in as the seeded regular user (owns website2, member of the seeded team). */
user: ApiClient;
/** Logged in as the seeded view-only user, who owns nothing. */
viewer: ApiClient;
/** Authenticated with an admin API key (`umami_…`) instead of a session token. */
apiKey: ApiClient;
/** Resolves a share slug to a client authenticated with a share token. */
share: (slug?: string) => Promise<ApiClient>;
}
export const test = base.extend<TestFixtures, WorkerFixtures>({
// biome-ignore lint/correctness/noEmptyPattern: Playwright requires the destructuring form
seed: [async ({}, use) => use(readSeedState()), { scope: 'worker' }],
tokens: [
async ({ seed }, use, workerInfo) => {
const context = await request.newContext({ baseURL: workerInfo.project.use.baseURL });
const raw = new ApiClient(context);
try {
const admin = await login(raw, seed.admin);
const user = await login(raw, seed.user);
const viewer = await login(raw, seed.viewer);
const created = await raw
.bearer(admin)
.post('/api/me/api-keys', { name: `api-tests-worker-${workerInfo.workerIndex}` });
if (created.status !== 200 || !created.body?.key) {
throw new Error(`Failed to create API key (${created.status}): ${created.text}`);
}
const apiKey = { id: created.body.id as string, key: created.body.key as string };
await use({ admin, user, viewer, apiKey });
await raw.bearer(admin).del(`/api/me/api-keys/${apiKey.id}`);
} finally {
await context.dispose();
}
},
{ scope: 'worker' },
],
api: async ({ request }, use) => use(new ApiClient(request)),
admin: async ({ api, tokens }, use) => use(api.bearer(tokens.admin)),
user: async ({ api, tokens }, use) => use(api.bearer(tokens.user)),
viewer: async ({ api, tokens }, use) => use(api.bearer(tokens.viewer)),
apiKey: async ({ api, tokens }, use) => use(api.bearer(tokens.apiKey.key)),
share: async ({ api, seed }, use) =>
use(async (slug = seed.share.slug) => {
const response = await api.get(`/api/share/${slug}`);
if (response.status !== 200 || !response.body?.token) {
throw new Error(`Failed to resolve share ${slug} (${response.status}): ${response.text}`);
}
return api.with({
[SHARE_TOKEN_HEADER]: response.body.token,
[SHARE_CONTEXT_HEADER]: '1',
});
}),
});
export { expect } from '@playwright/test';