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

371 lines
11 KiB
TypeScript

/* eslint-disable no-console */
/**
* Orchestrates the API integration suite:
*
* 1. docker compose up (builds the umami image, starts Postgres [+ ClickHouse])
* 2. waits for /api/heartbeat
* 3. runs Playwright against the container (tests/api, playwright.api.config.ts)
* 4. dumps container logs on failure
* 5. tears the stack down (unless --keep)
*
* Usage: tsx scripts/test-api.ts [options] [-- playwright args]
*/
import { spawnSync } from 'node:child_process';
import { mkdirSync, writeFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { connect } from 'node:net';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { assertDisposableTarget } from '../tests/api/paths';
const ROOT_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const COMPOSE_FILE = 'docker-compose.test.yml';
const PROJECT_NAME = process.env.COMPOSE_PROJECT_NAME || 'umami-api-test';
const PORT = process.env.UMAMI_TEST_PORT || '3100';
const BASE_URL = process.env.PLAYWRIGHT_BASE_URL || `http://localhost:${PORT}`;
// PLAYWRIGHT_BASE_URL selects an already-running server: no Compose stack is
// built, started, or torn down for it.
const EXTERNAL_TARGET = !!process.env.PLAYWRIGHT_BASE_URL;
const HEARTBEAT_TIMEOUT_MS = 180_000;
// Mirrors RUNTIME_DIR in tests/api/paths.ts (one state dir per target host).
const LOG_FILE = path.join(
ROOT_DIR,
'tests/api/.runtime',
new URL(BASE_URL).host.replace(/[^a-z0-9.-]+/gi, '-'),
'docker.log',
);
const HELP = `
Usage: pnpm test:api [options] [-- playwright args]
Options:
--clickhouse Run against Postgres + ClickHouse (analytics stored in ClickHouse)
--keep Leave the containers running after the tests finish
--no-build Skip the image build (use the existing UMAMI_TEST_IMAGE)
--up-only Start the stack, wait for readiness, and exit (see --keep)
--down Tear down the stack and exit
--grep <expr> Only run matching tests (implies --coverage-report)
--coverage-report Print endpoint coverage without failing the run
-h, --help Show this help
Environment:
UMAMI_TEST_PORT Host port for the app (default 3100)
UMAMI_TEST_IMAGE Image tag to build/run (default umami-test:local)
COMPOSE_PROJECT_NAME Compose project name (default umami-api-test)
PLAYWRIGHT_BASE_URL Test an already-running server instead of starting the
Compose stack (pass --clickhouse if it stores analytics there)
API_ALLOW_DESTRUCTIVE=1 Required for non-loopback PLAYWRIGHT_BASE_URL targets: the suite
recreates websites, ingests data and toggles global 2FA
API_SKIP_SEED=1 Reuse the previous seed when re-running against a kept stack
(each run seeds additively otherwise)
`;
interface Options {
clickhouse: boolean;
keep: boolean;
build: boolean;
upOnly: boolean;
down: boolean;
grep?: string;
coverageReport: boolean;
passthrough: string[];
}
function parseArgs(argv: string[]): Options {
const options: Options = {
clickhouse: false,
keep: false,
build: true,
upOnly: false,
down: false,
coverageReport: false,
passthrough: [],
};
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === '--') {
options.passthrough.push(...argv.slice(i + 1));
break;
}
if (arg.startsWith('--grep=')) {
options.grep = arg.slice('--grep='.length);
continue;
}
switch (arg) {
case '--clickhouse':
options.clickhouse = true;
break;
case '--keep':
options.keep = true;
break;
case '--no-build':
options.build = false;
break;
case '--up-only':
options.upOnly = true;
break;
case '--down':
options.down = true;
break;
case '--coverage-report':
options.coverageReport = true;
break;
case '--grep':
options.grep = argv[++i];
break;
case '-h':
case '--help':
console.log(HELP);
process.exit(0);
break;
default:
console.error(`Unknown option: ${arg}`);
console.log(HELP);
process.exit(2);
}
}
return options;
}
function run(command: string, args: string[], env: Record<string, string> = {}) {
const result = spawnSync(command, args, {
cwd: ROOT_DIR,
stdio: 'inherit',
env: { ...process.env, ...env },
});
if (result.error) {
throw result.error;
}
return result.status ?? 1;
}
function capture(command: string, args: string[]) {
const result = spawnSync(command, args, {
cwd: ROOT_DIR,
encoding: 'utf8',
env: process.env,
maxBuffer: 64 * 1024 * 1024,
});
return `${result.stdout ?? ''}${result.stderr ?? ''}`;
}
function composeArgs(profiles: string[], args: string[]) {
return [
'compose',
'-f',
COMPOSE_FILE,
'-p',
PROJECT_NAME,
...profiles.flatMap(profile => ['--profile', profile]),
...args,
];
}
// Docker reports a taken host port as an opaque "ports are not available" error
// after the image has already been built, so check up front. Probe by connecting
// rather than binding: on Windows a second listen() on a busy port can succeed
// unless the owner set SO_EXCLUSIVEADDRUSE, so a bind test gives false negatives.
function isPortFree(port: number) {
return new Promise<boolean>(resolve => {
const socket = connect({ host: '127.0.0.1', port });
const done = (free: boolean) => {
socket.destroy();
resolve(free);
};
socket.setTimeout(1000);
socket.once('connect', () => done(false));
socket.once('timeout', () => done(true));
socket.once('error', () => done(true));
});
}
async function waitForHeartbeat(url: string, timeoutMs: number) {
const deadline = Date.now() + timeoutMs;
let lastError = '';
while (Date.now() < deadline) {
try {
const response = await fetch(`${url}/api/heartbeat`);
if (response.ok) {
return;
}
lastError = `HTTP ${response.status}`;
} catch (error) {
lastError = (error as Error).message;
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
throw new Error(`Timed out waiting for ${url}/api/heartbeat (${lastError})`);
}
async function main() {
const options = parseArgs(process.argv.slice(2));
const profile = options.clickhouse ? 'clickhouse' : 'postgres';
const appService = options.clickhouse ? 'umami-clickhouse' : 'umami';
const logServices = options.clickhouse ? [appService, 'db', 'clickhouse'] : [appService, 'db'];
const teardown = () => {
console.log('\nStopping test stack...');
run('docker', composeArgs(['postgres', 'clickhouse'], ['down', '-v', '--remove-orphans']));
};
if (EXTERNAL_TARGET && (options.upOnly || options.down)) {
console.error(
'PLAYWRIGHT_BASE_URL selects an already-running server; --up-only and --down manage ' +
'the local Compose stack and cannot be combined with it.',
);
return 2;
}
if (options.down) {
teardown();
return 0;
}
const dumpLogs = () => {
const logs = capture('docker', composeArgs([profile], ['logs', '--no-color', ...logServices]));
mkdirSync(path.dirname(LOG_FILE), { recursive: true });
writeFileSync(LOG_FILE, logs);
console.log(`\n--- docker compose logs (${logServices.join(', ')}) ---\n`);
console.log(logs);
console.log(`--- end of logs (saved to ${path.relative(ROOT_DIR, LOG_FILE)}) ---\n`);
};
let exitCode = 1;
let started = false;
const onSignal = () => {
console.log('\nInterrupted.');
if (started && !options.keep) {
teardown();
}
process.exit(130);
};
process.on('SIGINT', onSignal);
process.on('SIGTERM', onSignal);
try {
if (EXTERNAL_TARGET) {
try {
assertDisposableTarget(BASE_URL);
} catch (error) {
console.error((error as Error).message);
return 2;
}
console.log(`Using the already-running server at ${BASE_URL} (PLAYWRIGHT_BASE_URL is set).`);
} else {
// A kept stack from a previous run legitimately owns the port; only
// complain when something else is listening on it.
const ownStackRunning = capture(
'docker',
composeArgs([profile], ['ps', '-q', '--status', 'running', appService]),
).trim();
if (!ownStackRunning && !(await isPortFree(Number(PORT)))) {
console.error(
`Port ${PORT} is already in use on this machine, so the umami container cannot be published there.
` +
`Stop whatever is listening on ${PORT} (e.g. another dev server), or pick a different port:
` +
` UMAMI_TEST_PORT=3200 pnpm test:api`,
);
return 2;
}
console.log(`Starting test stack (profile: ${profile}, port: ${PORT})...`);
const upArgs = ['up', '-d', '--wait', '--wait-timeout', '300'];
if (options.build) {
upArgs.push('--build');
}
started = true;
if (run('docker', composeArgs([profile], upArgs)) !== 0) {
console.error('docker compose up failed.');
dumpLogs();
return 1;
}
}
await waitForHeartbeat(BASE_URL, HEARTBEAT_TIMEOUT_MS);
console.log(`umami is ready at ${BASE_URL}`);
if (options.upOnly) {
options.keep = true;
console.log(
`\nRun tests with: PLAYWRIGHT_BASE_URL=${BASE_URL} UMAMI_TEST_DB=${profile} pnpm test:api:run` +
'\nRe-running against the same stack? Add API_SKIP_SEED=1 (seeding is additive).' +
'\nStop the stack with: pnpm test:api:down',
);
return 0;
}
// Invoke the Playwright CLI through the current node binary instead of
// `pnpm exec`: on Windows that resolves to pnpm.cmd, which Node refuses to
// spawn without a shell (EINVAL, CVE-2024-27980 hardening).
const playwrightCli = path.join(
path.dirname(createRequire(import.meta.url).resolve('@playwright/test/package.json')),
'cli.js',
);
const playwrightArgs = [playwrightCli, 'test', '-c', 'playwright.api.config.ts'];
if (options.grep) {
playwrightArgs.push('--grep', options.grep);
}
playwrightArgs.push(...options.passthrough);
exitCode = run(process.execPath, playwrightArgs, {
PLAYWRIGHT_BASE_URL: BASE_URL,
PLAYWRIGHT_SKIP_WEB_SERVER: '1',
UMAMI_TEST_DB: profile,
API_COVERAGE:
process.env.API_COVERAGE ?? (options.grep || options.coverageReport ? 'report' : 'enforce'),
});
if (exitCode !== 0 && started) {
dumpLogs();
}
return exitCode;
} finally {
process.off('SIGINT', onSignal);
process.off('SIGTERM', onSignal);
if (started && !options.keep) {
teardown();
} else if (started) {
console.log(`\nStack left running (${BASE_URL}). Stop it with: pnpm test:api:down`);
}
}
}
main()
.then(code => {
process.exit(code);
})
.catch(error => {
console.error(error);
process.exit(1);
});