Harden API test Docker setup and stabilize coverage fixtures
This commit is contained in:
1 parent
2a5ef9d9e8
commit
a3a7d4914b
7 files changed
+130
-11
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { OPENAPI_FILE } from '../paths';
|
||||
|
||||
/**
|
||||
@@ -26,6 +26,13 @@ export function loadOperations(): ApiOperation[] {
|
||||
return cached;
|
||||
}
|
||||
|
||||
if (!existsSync(OPENAPI_FILE)) {
|
||||
throw new Error(
|
||||
`Coverage oracle ${OPENAPI_FILE} is missing. It is written by tests/api/global-setup.ts; ` +
|
||||
'run the suite through `pnpm test:api` or `pnpm test:api:run` rather than importing specs directly.',
|
||||
);
|
||||
}
|
||||
|
||||
const document = JSON.parse(readFileSync(OPENAPI_FILE, 'utf8'));
|
||||
const operations: ApiOperation[] = [];
|
||||
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
/* eslint-disable no-console */
|
||||
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { copyFileSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { type FullConfig, request } from '@playwright/test';
|
||||
import { ApiClient } from './client';
|
||||
import { disableCoverageRecording } from './coverage/recorder';
|
||||
import { assertDisposableTarget, COVERAGE_DIR, SEED_FILE } from './paths';
|
||||
import {
|
||||
assertDisposableTarget,
|
||||
COVERAGE_DIR,
|
||||
LOCAL_OPENAPI_FILE,
|
||||
OPENAPI_FILE,
|
||||
SEED_FILE,
|
||||
} from './paths';
|
||||
import { seedEnvironment } from './seed/setup';
|
||||
|
||||
const HEARTBEAT_TIMEOUT_MS = 120_000;
|
||||
@@ -34,6 +40,48 @@ async function waitForHeartbeat(baseURL: string, timeoutMs: number) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The coverage oracle must describe the server actually under test, so fetch its
|
||||
* generated OpenAPI document rather than assuming a local `pnpm build` ran.
|
||||
* public/openapi.json is a gitignored build artifact and is usually absent in a
|
||||
* checkout that only runs the Compose stack.
|
||||
*/
|
||||
async function fetchOpenApiDocument(baseURL: string) {
|
||||
const url = `${baseURL}/openapi.json`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const document = await response.json();
|
||||
|
||||
if (!document?.paths) {
|
||||
throw new Error('response has no "paths" object');
|
||||
}
|
||||
|
||||
writeFileSync(OPENAPI_FILE, JSON.stringify(document));
|
||||
return;
|
||||
} catch (error) {
|
||||
if (existsSync(LOCAL_OPENAPI_FILE)) {
|
||||
console.warn(
|
||||
`Could not fetch ${url} (${(error as Error).message}); ` +
|
||||
`using the locally generated ${LOCAL_OPENAPI_FILE} as the coverage oracle instead.`,
|
||||
);
|
||||
copyFileSync(LOCAL_OPENAPI_FILE, OPENAPI_FILE);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Could not fetch the OpenAPI document from ${url} (${(error as Error).message}) and no ` +
|
||||
`local ${LOCAL_OPENAPI_FILE} exists. The server under test must serve its generated spec ` +
|
||||
'(it is built by `pnpm build:openapi`), or generate it locally with `pnpm openapi:generate`.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default async function globalSetup(config: FullConfig) {
|
||||
// Seeding traffic must not count towards endpoint coverage.
|
||||
disableCoverageRecording();
|
||||
@@ -58,6 +106,7 @@ export default async function globalSetup(config: FullConfig) {
|
||||
|
||||
console.log(`Waiting for ${baseURL}/api/heartbeat ...`);
|
||||
await waitForHeartbeat(baseURL, HEARTBEAT_TIMEOUT_MS);
|
||||
await fetchOpenApiDocument(baseURL);
|
||||
|
||||
if (skipSeed) {
|
||||
console.log(`Reusing seed state from ${SEED_FILE} (API_SKIP_SEED is set).`);
|
||||
|
||||
+7
-1
@@ -19,7 +19,13 @@ export const RUNTIME_DIR = path.join(
|
||||
);
|
||||
export const COVERAGE_DIR = path.join(RUNTIME_DIR, 'coverage');
|
||||
export const SEED_FILE = path.join(RUNTIME_DIR, 'seed.json');
|
||||
export const OPENAPI_FILE = path.join(ROOT_DIR, 'public/openapi.json');
|
||||
/**
|
||||
* Coverage oracle: the OpenAPI document of the server under test, downloaded by
|
||||
* global setup from `${BASE_URL}/openapi.json` (see tests/api/global-setup.ts).
|
||||
*/
|
||||
export const OPENAPI_FILE = path.join(RUNTIME_DIR, 'openapi.json');
|
||||
/** Locally generated spec (`pnpm openapi:generate`), used only as a fallback. */
|
||||
export const LOCAL_OPENAPI_FILE = path.join(ROOT_DIR, 'public/openapi.json');
|
||||
|
||||
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);
|
||||
|
||||
|
||||
@@ -182,6 +182,13 @@ function customEvent(
|
||||
export function buildDataset(now = Date.now()): Dataset {
|
||||
const nowSec = Math.floor(now / 1000);
|
||||
const range = { startAt: (nowSec - DAYS * DAY) * 1000, endAt: now };
|
||||
// Backdated traffic is anchored to noon UTC of each calendar day rather than
|
||||
// to `now`, so the per-persona hour offsets below never straddle a UTC day
|
||||
// boundary. Specs bucket by day in UTC and assert e.g. "3 purchases on each of
|
||||
// 3 distinct days"; anchoring to `now` made that fail for runs started within
|
||||
// a few hours after 00:00 UTC.
|
||||
const todayStart = nowSec - (nowSec % DAY);
|
||||
const dayAnchor = (daysAgo: number) => todayStart - daysAgo * DAY + 12 * HOUR;
|
||||
const visits: Visit[] = [];
|
||||
const batch: SendPayload[] = [];
|
||||
let pageviews = 0;
|
||||
@@ -190,7 +197,7 @@ export function buildDataset(now = Date.now()): Dataset {
|
||||
// Backdated traffic: days 1..6 ago (today only receives the live events below).
|
||||
for (let day = DAYS - 1; day >= 1; day--) {
|
||||
PERSONAS.forEach((persona, index) => {
|
||||
const start = nowSec - day * DAY - (index + 1) * HOUR;
|
||||
const start = dayAnchor(day) - (index + 1) * HOUR;
|
||||
const pageCount = 2 + ((day + index) % 3);
|
||||
const pages = PAGES.slice(0, pageCount);
|
||||
|
||||
@@ -296,7 +303,7 @@ export function buildDataset(now = Date.now()): Dataset {
|
||||
// Secondary website (owned by the regular user): two personas, five days.
|
||||
for (let day = 5; day >= 1; day--) {
|
||||
PERSONAS.slice(0, 2).forEach((persona, index) => {
|
||||
const timestamp = nowSec - day * DAY - (index + 1) * HOUR;
|
||||
const timestamp = dayAnchor(day) - (index + 1) * HOUR;
|
||||
|
||||
visits.push({
|
||||
events: [
|
||||
@@ -314,13 +321,13 @@ export function buildDataset(now = Date.now()): Dataset {
|
||||
0,
|
||||
'purchase',
|
||||
{ revenue: 19.99, currency: CURRENCY, quantity: 1 },
|
||||
nowSec - 2 * DAY,
|
||||
dayAnchor(2),
|
||||
),
|
||||
);
|
||||
|
||||
// Link clicks and pixel hits (sessions are keyed by the link/pixel id).
|
||||
PERSONAS.slice(0, 5).forEach((persona, index) => {
|
||||
const timestamp = nowSec - (index + 1) * DAY - 30 * 60;
|
||||
const timestamp = dayAnchor(index + 1) - 30 * 60;
|
||||
|
||||
batch.push({
|
||||
type: 'event',
|
||||
|
||||
Reference in new issue
Block a user