Adds prettier 3.9.8 (exact) with a shared config (single quotes, 120 columns, trailing commas), .prettierignore, .editorconfig and the `format` / `format:check` scripts, then formats all TypeScript, JavaScript, CSS, JSON and YAML sources. Purely mechanical: no behaviour change. Typecheck passes, the vitest suite and the 74 IDE node tests produce the same results as before (socket-binding tests still cannot run inside this sandbox). Previously the longest source line was ~3000 characters; reviewing the following semantic changes now produces readable diffs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
183 lines
7.1 KiB
TypeScript
183 lines
7.1 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
import { createServer } from 'node:http';
|
|
import { spawn, type ChildProcess } from 'node:child_process';
|
|
import { generateKeyPairSync, randomUUID } from 'node:crypto';
|
|
import { writeFile } from 'node:fs/promises';
|
|
import { exportJWK, SignJWT } from 'jose';
|
|
import pg from 'pg';
|
|
|
|
const enabled = process.env.WF_API_LOAD === '1';
|
|
if (enabled) {
|
|
try {
|
|
process.loadEnvFile('.env.local');
|
|
} catch {}
|
|
}
|
|
describe.skipIf(!enabled)('local control API load fixture (no model, IDE or deployment load)', () => {
|
|
const tenants = Array.from({ length: 10 }, () => `load-${randomUUID()}`);
|
|
let admin: pg.Client, child: ChildProcess, issuer: ReturnType<typeof createServer>;
|
|
let origin = '',
|
|
issuerOrigin = '',
|
|
logs = '';
|
|
const tokens: string[] = [];
|
|
beforeAll(async () => {
|
|
admin = new pg.Client({ connectionString: process.env.DATABASE_ADMIN_URL });
|
|
await admin.connect();
|
|
for (const id of tenants) {
|
|
await admin.query('INSERT INTO tenants(id,name,region) VALUES($1,$1,$2)', [id, 'local-load-fixture']);
|
|
await admin.query("INSERT INTO memberships(tenant_id,user_id,role) VALUES($1,'load-owner','owner')", [id]);
|
|
}
|
|
const keys = generateKeyPairSync('rsa', { modulusLength: 2048 });
|
|
const jwk = { ...(await exportJWK(keys.publicKey)), kid: 'local-load-fixture', alg: 'RS256', use: 'sig' };
|
|
issuer = createServer((_req, res) => {
|
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
res.end(JSON.stringify({ keys: [jwk] }));
|
|
});
|
|
await new Promise<void>(resolve => issuer.listen(0, '127.0.0.1', resolve));
|
|
issuerOrigin = `http://127.0.0.1:${(issuer.address() as { port: number }).port}`;
|
|
const portProbe = createServer();
|
|
await new Promise<void>(resolve => portProbe.listen(0, '127.0.0.1', resolve));
|
|
const port = (portProbe.address() as { port: number }).port;
|
|
await new Promise<void>(resolve => portProbe.close(() => resolve()));
|
|
origin = `http://127.0.0.1:${port}`;
|
|
child = spawn(process.execPath, ['--import', 'tsx', 'apps/api/src/main.ts'], {
|
|
cwd: process.cwd(),
|
|
env: {
|
|
PATH: process.env.PATH,
|
|
NODE_ENV: 'test',
|
|
TSX_TSCONFIG_PATH: `${process.cwd()}/apps/api/tsconfig.json`,
|
|
DATABASE_URL: process.env.DATABASE_URL,
|
|
HOST: '127.0.0.1',
|
|
PORT: String(port),
|
|
OIDC_ISSUER: issuerOrigin,
|
|
OIDC_AUDIENCE: 'wishforge-api',
|
|
WF_DISPATCH_ENABLED: 'false',
|
|
WF_DISPATCH_TENANTS: '',
|
|
WF_REGION: 'local-load-fixture',
|
|
},
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
});
|
|
for (const stream of [child.stdout, child.stderr])
|
|
stream?.on('data', data => {
|
|
logs = (logs + String(data)).slice(-5000);
|
|
});
|
|
for (let attempt = 0; attempt < 100; attempt++) {
|
|
if (child.exitCode !== null) throw new Error(`Fixture API exited: ${logs}`);
|
|
if (
|
|
await fetch(`${origin}/readyz`).then(
|
|
r => r.ok,
|
|
() => false,
|
|
)
|
|
)
|
|
break;
|
|
if (attempt === 99) throw new Error(`Fixture API unavailable: ${logs}`);
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
}
|
|
for (const tenantId of tenants)
|
|
tokens.push(
|
|
await new SignJWT({ tenant_id: tenantId, azp: 'local-load-client' })
|
|
.setProtectedHeader({ alg: 'RS256', kid: jwk.kid })
|
|
.setSubject('load-owner')
|
|
.setIssuer(issuerOrigin)
|
|
.setAudience('wishforge-api')
|
|
.setIssuedAt()
|
|
.setExpirationTime('10m')
|
|
.sign(keys.privateKey),
|
|
);
|
|
}, 30000);
|
|
afterAll(async () => {
|
|
if (child && child.exitCode === null) {
|
|
child.kill('SIGTERM');
|
|
await Promise.race([
|
|
new Promise(resolve => child.once('exit', resolve)),
|
|
new Promise(resolve => setTimeout(resolve, 3000)),
|
|
]);
|
|
if (child.exitCode === null) child.kill('SIGKILL');
|
|
}
|
|
issuer?.closeAllConnections();
|
|
if (issuer) await new Promise<void>(resolve => issuer.close(() => resolve()));
|
|
if (admin) {
|
|
for (const table of [
|
|
'memberships',
|
|
'entities',
|
|
'run_events',
|
|
'idempotency',
|
|
'outbox',
|
|
'usage_ledger',
|
|
'audit_events',
|
|
'tenants',
|
|
])
|
|
await admin.query(`DELETE FROM ${table} WHERE ${table === 'tenants' ? 'id' : 'tenant_id'} = ANY($1::text[])`, [
|
|
tenants,
|
|
]);
|
|
await admin.end();
|
|
}
|
|
}, 15000);
|
|
it('handles 20 concurrent submission streams across 10 tenants with durable idempotency', async () => {
|
|
const projects: string[] = [];
|
|
for (const [index] of tenants.entries()) {
|
|
const response = await fetch(`${origin}/v1/projects`, {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${tokens[index]}`, 'content-type': 'application/json' },
|
|
body: JSON.stringify({
|
|
name: 'No-model API load fixture',
|
|
repositoryUrl: 'https://example.invalid/no-network-fixture.git',
|
|
}),
|
|
});
|
|
expect(response.ok).toBe(true);
|
|
projects.push((await response.json()).data.id);
|
|
}
|
|
const timings: number[] = [],
|
|
ids = new Set<string>();
|
|
const begin = performance.now();
|
|
await Promise.all(
|
|
Array.from({ length: 20 }, async (_, stream) => {
|
|
const tenant = Math.floor(stream / 2);
|
|
let first = '';
|
|
for (let repeat = 0; repeat < 10; repeat++) {
|
|
const start = performance.now();
|
|
const response = await fetch(`${origin}/v1/projects/${projects[tenant]}/wishes`, {
|
|
method: 'POST',
|
|
headers: {
|
|
authorization: `Bearer ${tokens[tenant]}`,
|
|
'content-type': 'application/json',
|
|
'idempotency-key': `no-model-load-stream-${stream}`,
|
|
},
|
|
body: JSON.stringify({
|
|
goal: 'Control API fixture only; no provider configured',
|
|
acceptanceCriteria: ['One durable run per idempotency key'],
|
|
budget: { maxTokens: 1000 },
|
|
}),
|
|
});
|
|
const result = await response.json();
|
|
timings.push(performance.now() - start);
|
|
expect(response.status).toBe(202);
|
|
if (first) expect(result.data.run_id).toBe(first);
|
|
first = result.data.run_id;
|
|
ids.add(first);
|
|
}
|
|
}),
|
|
);
|
|
expect(ids.size).toBe(20);
|
|
const sorted = [...timings].sort((a, b) => a - b),
|
|
p95 = sorted[Math.ceil(sorted.length * 0.95) - 1]!;
|
|
const report = {
|
|
scope:
|
|
'Local HTTP control-plane fixture with real PostgreSQL and ephemeral signed OIDC tokens. No model/IDE/deployment workload; not production SLO evidence.',
|
|
at: new Date().toISOString(),
|
|
tenants: 10,
|
|
concurrentStreams: 20,
|
|
requests: timings.length,
|
|
uniqueRuns: ids.size,
|
|
httpStatus: 202,
|
|
p50Ms: sorted[Math.ceil(sorted.length * 0.5) - 1],
|
|
p95Ms: p95,
|
|
maxMs: sorted.at(-1),
|
|
totalMs: performance.now() - begin,
|
|
modelCalls: 0,
|
|
};
|
|
if (process.env.WF_API_LOAD_REPORT)
|
|
await writeFile(process.env.WF_API_LOAD_REPORT, JSON.stringify(report, null, 2), { mode: 0o600 });
|
|
expect(p95).toBeLessThan(2000);
|
|
}, 60000);
|
|
});
|