import { after, before, test } from 'node:test'; import assert from 'node:assert/strict'; import { createServer as createHttpServer } from 'node:http'; import { createServer } from 'vite'; import { fileURLToPath } from 'node:url'; let upstream, frontend, base; const received = []; const stl = Buffer.from([0, 255, 1, 128, 30, 0]); before(async () => { upstream = createHttpServer(async (req, res) => { const chunks = []; for await (const chunk of req) chunks.push(chunk); received.push({ method: req.method, url: req.url, body: Buffer.concat(chunks).toString() }); if (req.url.startsWith('/files/')) { res.writeHead(200, { 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename="model.step"' }); return res.end(stl); } res.writeHead(req.url === '/api/missing' ? 404 : req.method === 'POST' ? 202 : 200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ ok: req.url !== '/api/missing', task_id: 'test-task' })); }); await new Promise(resolve => upstream.listen(0, '127.0.0.1', resolve)); process.env.BACKEND_URL = `http://127.0.0.1:${upstream.address().port}`; frontend = await createServer({ root: fileURLToPath(new URL('../', import.meta.url)), configFile: fileURLToPath(new URL('../vite.config.js', import.meta.url)), logLevel: 'silent', server: { host: '127.0.0.1', port: 0, strictPort: false } }); await frontend.listen(); base = `http://127.0.0.1:${frontend.httpServer.address().port}`; }); after(async () => { await frontend?.close(); upstream?.closeAllConnections(); if (upstream) await new Promise(resolve => upstream.close(resolve)); delete process.env.BACKEND_URL; }); test('frontend independently serves the page and never exposes backend configuration', async () => { const r = await fetch(base); assert.equal(r.status, 200); assert.match(await r.text(), /CAD World/); for (const path of ['/backend/.env', '/.env', '/@fs/Users/jerry/linkhand/CADmind/backend/.env']) { const response = await fetch(base + path); assert.ok(response.status >= 400, path); } }); test('proxy preserves task JSON, image payload, verbs, query parameters and status codes', async () => { const payload = { action: 'generate', text: '测试安装片', images: ['data:image/png;base64,dGVzdA=='] }; const r = await fetch(base + '/api/tasks', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); assert.equal(r.status, 202); assert.equal((await r.json()).task_id, 'test-task'); assert.deepEqual(JSON.parse(received.at(-1).body), payload); for (const [method, url] of [['GET', '/api/tasks/test-task?cursor=2'], ['DELETE', '/api/tasks/test-task'], ['DELETE', '/api/parts/example'], ['POST', '/api/parts/example/restore']]) { await fetch(base + url, { method }); assert.equal(received.at(-1).method, method); assert.equal(received.at(-1).url, url); } const missing = await fetch(base + '/api/missing'); assert.equal(missing.status, 404); assert.equal((await missing.json()).ok, false); }); test('proxy preserves binary CAD files and download headers', async () => { const r = await fetch(base + '/files/example/model.step?download=1'); assert.deepEqual(Buffer.from(await r.arrayBuffer()), stl); assert.equal(r.headers.get('content-disposition'), 'attachment; filename="model.step"'); assert.equal(received.at(-1).url, '/files/example/model.step?download=1'); });