railyard/apps/insights/tests/api/revenue.spec.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

246 lines
8.8 KiB
TypeScript

import { expect, test } from './fixtures';
import { dateRange, TIMEZONE } from './helpers/dates';
/**
* Seeded revenue on `seed.website` (see seed/dataset.ts): the 3 identified personas
* each send a `purchase` on 3 days with revenue 49.5 / 59.5 / 69.5 in USD.
* 9 purchases, sum 535.5, average 59.5. Nothing else in the suite ingests
* `revenue` for this website, so these totals are asserted exactly.
*/
const PURCHASE_REVENUES = [49.5, 59.5, 69.5];
const PURCHASE_COUNT = 9;
const PURCHASE_TOTAL = PURCHASE_REVENUES.reduce((sum, value) => sum + value, 0) * 3;
test.describe('Revenue', () => {
let base = '';
test.beforeAll(({ seed }) => {
base = `/api/websites/${seed.website.id}`;
});
test('GET /api/websites/{websiteId}/revenue/chart returns revenue per event over time', async ({
admin,
share,
viewer,
seed,
}) => {
const params = dateRange(seed, {
currency: seed.data.currency,
unit: 'day',
timezone: TIMEZONE,
});
const response = await admin.get(`${base}/revenue/chart`, { params });
const shared = await (await share()).get(`${base}/revenue/chart`, { params });
const denied = await viewer.get(`${base}/revenue/chart`, { params });
const missingCurrency = await admin.get(`${base}/revenue/chart`, {
params: dateRange(seed),
});
const missingRange = await admin.get(`${base}/revenue/chart`, {
params: { currency: seed.data.currency },
});
expect(response.status).toBe(200);
expect(Object.keys(response.body)).toEqual(['chart']);
// Purchases land on 3 distinct days, 3 per day.
expect(response.body.chart).toHaveLength(3);
for (const point of response.body.chart) {
expect(point).toEqual({
x: 'purchase',
t: expect.any(String),
y: expect.anything(),
count: 3,
});
expect(Number(point.y)).toBeCloseTo(PURCHASE_TOTAL / 3, 5);
}
expect(shared.status).toBe(200);
expect(denied.status).toBe(401);
expect(missingCurrency.status).toBe(400);
expect(missingRange.status).toBe(400);
});
test('GET /api/websites/{websiteId}/revenue/metrics breaks revenue down by a dimension', async ({
admin,
share,
viewer,
seed,
}) => {
const params = dateRange(seed, { currency: seed.data.currency });
const byType: Record<string, any> = {};
for (const type of ['country', 'region', 'referrer', 'channel']) {
byType[type] = await admin.get(`${base}/revenue/metrics`, { params: { ...params, type } });
}
const shared = await (await share()).get(`${base}/revenue/metrics`, {
params: { ...params, type: 'country' },
});
const denied = await viewer.get(`${base}/revenue/metrics`, {
params: { ...params, type: 'country' },
});
const badType = await admin.get(`${base}/revenue/metrics`, {
params: { ...params, type: 'browser' },
});
const missingType = await admin.get(`${base}/revenue/metrics`, { params });
const missingCurrency = await admin.get(`${base}/revenue/metrics`, {
params: dateRange(seed, { type: 'country' }),
});
for (const [type, response] of Object.entries(byType)) {
expect(response.status, type).toBe(200);
expect(Array.isArray(response.body), type).toBe(true);
expect(response.body.length, type).toBeGreaterThan(0);
for (const row of response.body) {
// Region rows also carry the parent `country`.
expect(Object.keys(row).sort(), type).toEqual(
type === 'region' ? ['country', 'name', 'value'] : ['name', 'value'],
);
}
// Every breakdown adds up to the full seeded revenue.
const total = response.body.reduce((sum: number, row: any) => sum + Number(row.value), 0);
expect(total, type).toBeCloseTo(PURCHASE_TOTAL, 5);
}
// The test image has no GeoIP database, so country/region are unresolved
// (null on Postgres, '' on ClickHouse's non-nullable String columns).
const unresolved = seed.db === 'clickhouse' ? '' : null;
expect(byType.country.body).toEqual([{ name: unresolved, value: expect.anything() }]);
expect(byType.region.body).toEqual([
{ country: unresolved, name: unresolved, value: expect.anything() },
]);
// alice (google.com, utm+gclid => paidAds) buys 3 x 49.5, bob (t.co => organicSocial)
// 3 x 59.5 and carol (no referrer, no query string => direct) 3 x 69.5.
const asMap = (rows: any[]) =>
Object.fromEntries(rows.map(row => [String(row.name), Number(row.value)]));
expect(asMap(byType.referrer.body)).toEqual({
'google.com': 148.5,
't.co': 178.5,
// No referrer is null on Postgres and '' on ClickHouse (see `unresolved` above).
[String(unresolved)]: 208.5,
});
expect(asMap(byType.channel.body)).toEqual({
paidAds: 148.5,
organicSocial: 178.5,
direct: 208.5,
});
expect(shared.status).toBe(200);
expect(denied.status).toBe(401);
expect(badType.status).toBe(400);
expect(missingType.status).toBe(400);
expect(missingCurrency.status).toBe(400);
});
test('GET /api/websites/{websiteId}/revenue/sessions lists sessions with revenue', async ({
admin,
share,
viewer,
seed,
}) => {
const params = dateRange(seed, { currency: seed.data.currency });
const response = await admin.get(`${base}/revenue/sessions`, {
params: { ...params, pageSize: 100 },
});
const paged = await admin.get(`${base}/revenue/sessions`, {
params: { ...params, page: 2, pageSize: 2 },
});
const searched = await admin.get(`${base}/revenue/sessions`, {
params: { ...params, search: 'zzz-no-such-session' },
});
const shared = await (await share()).get(`${base}/revenue/sessions`, { params });
const denied = await viewer.get(`${base}/revenue/sessions`, { params });
const missingCurrency = await admin.get(`${base}/revenue/sessions`, {
params: dateRange(seed),
});
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
data: expect.any(Array),
count: 3,
page: 1,
pageSize: 100,
});
expect(response.body.data).toHaveLength(3);
for (const row of response.body.data) {
expect(row).toMatchObject({
id: expect.any(String),
websiteId: seed.website.id,
hostname: seed.data.hostname,
browser: expect.any(String),
os: expect.any(String),
device: expect.any(String),
firstAt: expect.any(String),
lastAt: expect.any(String),
createdAt: expect.any(String),
});
}
expect(paged.status).toBe(200);
expect(paged.body).toMatchObject({ count: 3, page: 2, pageSize: 2 });
expect(paged.body.data).toHaveLength(1);
expect(searched.status).toBe(200);
expect(searched.body.data).toHaveLength(0);
expect(shared.status).toBe(200);
expect(denied.status).toBe(401);
expect(missingCurrency.status).toBe(400);
});
test('GET /api/websites/{websiteId}/revenue/stats returns revenue totals', async ({
admin,
share,
viewer,
seed,
}) => {
const params = dateRange(seed, { currency: seed.data.currency });
const response = await admin.get(`${base}/revenue/stats`, { params });
const otherCurrency = await admin.get(`${base}/revenue/stats`, {
params: { ...params, currency: 'EUR' },
});
const shared = await (await share()).get(`${base}/revenue/stats`, { params });
const denied = await viewer.get(`${base}/revenue/stats`, { params });
const missingCurrency = await admin.get(`${base}/revenue/stats`, { params: dateRange(seed) });
const missingRange = await admin.get(`${base}/revenue/stats`, {
params: { currency: seed.data.currency },
});
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
count: PURCHASE_COUNT,
unique_count: 3,
total_sessions: expect.any(Number),
});
// The comparison window precedes the seeded range and holds no revenue.
expect(Object.keys(response.body.comparison).sort()).toEqual([
'arpu',
'average',
'count',
'sum',
'total_sessions',
'unique_count',
]);
expect(response.body.comparison.count).toBe(0);
expect(Number(response.body.comparison.sum ?? 0)).toBe(0);
expect(Number(response.body.sum)).toBeCloseTo(PURCHASE_TOTAL, 5);
expect(Number(response.body.average)).toBeCloseTo(PURCHASE_TOTAL / PURCHASE_COUNT, 5);
expect(response.body.total_sessions).toBeGreaterThanOrEqual(3);
expect(Number(response.body.arpu)).toBeCloseTo(
PURCHASE_TOTAL / response.body.total_sessions,
5,
);
// Nothing was ingested in EUR.
expect(otherCurrency.status).toBe(200);
expect(otherCurrency.body.count).toBe(0);
expect(Number(otherCurrency.body.sum ?? 0)).toBe(0);
expect(shared.status).toBe(200);
expect(denied.status).toBe(401);
expect(missingCurrency.status).toBe(400);
expect(missingRange.status).toBe(400);
});
});