24e5dda022
Keep Q&A in a separate persistent conversation with scoped evidence reads, no production tools and immutable delivery files. Add the completed-board UI, recovery handling and regression coverage.
116 lines
4.2 KiB
TypeScript
116 lines
4.2 KiB
TypeScript
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join, resolve } from 'node:path';
|
|
import { spawn } from 'node:child_process';
|
|
import { createServer, type Server } from 'node:http';
|
|
import { expect, it } from 'vitest';
|
|
|
|
const listen = async (server: Server): Promise<number> => {
|
|
await new Promise<void>((done) => server.listen(0, '127.0.0.1', done));
|
|
return (server.address() as { port: number }).port;
|
|
};
|
|
const close = async (server: Server): Promise<void> => {
|
|
server.closeAllConnections();
|
|
await new Promise<void>((done) => server.close(() => done()));
|
|
};
|
|
it('keeps account and AI APIs on separate upstreams with the appropriate headers', async () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'vibemake-platform-proxy-'));
|
|
const scripts = join(dir, 'scripts'),
|
|
dist = join(dir, 'designer/dist');
|
|
mkdirSync(scripts, { recursive: true });
|
|
mkdirSync(dist, { recursive: true });
|
|
copyFileSync(resolve(__dirname, '../../../scripts/serve.mjs'), join(scripts, 'serve.mjs'));
|
|
copyFileSync(resolve(__dirname, '../../../scripts/pcb-preview.mjs'), join(scripts, 'pcb-preview.mjs'));
|
|
writeFileSync(join(dist, 'index.html'), '<h1>Platform fixture</h1>');
|
|
writeFileSync(join(dist, 'pcb-service-config.json'), JSON.stringify({ base: '/pcb/' }));
|
|
const seen: {
|
|
service: string;
|
|
path?: string;
|
|
origin?: string;
|
|
referer?: string;
|
|
authorization?: string;
|
|
body: string;
|
|
}[] = [];
|
|
const upstream = (service: string) =>
|
|
createServer((req, res) => {
|
|
let body = '';
|
|
req.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
req.on('end', () => {
|
|
seen.push({
|
|
service,
|
|
path: req.url,
|
|
origin: req.headers.origin,
|
|
referer: req.headers.referer,
|
|
authorization: req.headers.authorization,
|
|
body,
|
|
});
|
|
res.setHeader('content-type', 'application/json');
|
|
res.end(JSON.stringify({ service }));
|
|
});
|
|
});
|
|
const platform = upstream('platform'),
|
|
pcb = upstream('pcb');
|
|
const platformPort = await listen(platform),
|
|
pcbPort = await listen(pcb);
|
|
const reservation = createServer();
|
|
const port = await listen(reservation);
|
|
await close(reservation);
|
|
const child = spawn(process.execPath, [join(scripts, 'serve.mjs')], {
|
|
env: {
|
|
...process.env,
|
|
HOST: '127.0.0.1',
|
|
PORT: String(port),
|
|
API_TARGET: `http://127.0.0.1:${platformPort}`,
|
|
PCB_API_TARGET: `http://127.0.0.1:${pcbPort}`,
|
|
PCB_API_ORIGIN: 'http://127.0.0.1:5173',
|
|
PUBLIC_ORIGIN: '',
|
|
TLS_CERT: '',
|
|
TLS_KEY: '',
|
|
},
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
});
|
|
try {
|
|
await new Promise<void>((done, reject) => {
|
|
child.stdout.once('data', () => done());
|
|
child.once('exit', (code) => reject(Error(`Server exited ${code}`)));
|
|
child.stderr.once('data', (bytes) => reject(Error(String(bytes))));
|
|
});
|
|
const base = `http://127.0.0.1:${port}`;
|
|
const response = await fetch(base + '/api/pcb/workspace/projects?name=board', {
|
|
method: 'POST',
|
|
body: '{"name":"board"}',
|
|
headers: { Origin: base, Referer: base + '/pcb/', Authorization: 'Bearer test-token' },
|
|
});
|
|
expect(await response.json()).toEqual({ service: 'platform' });
|
|
expect(response.headers.get('cache-control')).toBe('no-store');
|
|
expect(seen[0]).toEqual({
|
|
service: 'platform',
|
|
path: '/api/pcb/workspace/projects?name=board',
|
|
origin: undefined,
|
|
referer: undefined,
|
|
authorization: 'Bearer test-token',
|
|
body: '{"name":"board"}',
|
|
});
|
|
expect(await (await fetch(base + '/pcb/pcb-api/v1/tasks')).json()).toEqual({ service: 'pcb' });
|
|
expect(seen[1]).toMatchObject({
|
|
service: 'pcb',
|
|
path: '/api/v1/tasks',
|
|
origin: 'http://127.0.0.1:5173',
|
|
});
|
|
const rejected = await fetch(base + '/api/auth/logout', {
|
|
method: 'POST',
|
|
headers: { Origin: 'http://foreign.invalid' },
|
|
});
|
|
expect(rejected.status).toBe(403);
|
|
expect(seen).toHaveLength(2);
|
|
} finally {
|
|
const exited = new Promise<void>((done) => child.once('exit', () => done()));
|
|
child.kill();
|
|
await exited;
|
|
await Promise.all([close(platform), close(pcb)]);
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
}, 20000);
|