Files
gstack/browse/test/test-server.ts
Rocky 834c6db075
Some checks failed
Workflow Lint / actionlint (push) Has been cancelled
Build CI Image / build (push) Has been cancelled
Skill Docs Freshness / check-freshness (push) Has been cancelled
Periodic Evals / build-image (push) Has been cancelled
Periodic Evals / evals (map[file:test/codex-e2e.test.ts name:e2e-codex]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/gemini-e2e.test.ts name:e2e-gemini]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-e2e-design.test.ts name:e2e-design]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-e2e-plan.test.ts name:e2e-plan]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-e2e-qa-bugs.test.ts name:e2e-qa-bugs]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-e2e-qa-workflow.test.ts name:e2e-qa-workflow]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-e2e-review.test.ts name:e2e-review]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-e2e-workflow.test.ts name:e2e-workflow]) (push) Has been cancelled
Periodic Evals / evals (map[file:test/skill-routing-e2e.test.ts name:e2e-routing]) (push) Has been cancelled
Initial import from garrytan/gstack@026751e (main snapshot via local relay)
Source: https://github.com/garrytan/gstack/commit/026751e
2026-05-19 21:18:17 +02:00

58 lines
1.7 KiB
TypeScript

/**
* Tiny Bun.serve for test fixtures
* Serves HTML files from test/fixtures/ on a random available port
*/
import * as path from 'path';
import * as fs from 'fs';
const FIXTURES_DIR = path.resolve(import.meta.dir, 'fixtures');
export function startTestServer(port: number = 0): { server: ReturnType<typeof Bun.serve>; url: string } {
const server = Bun.serve({
port,
hostname: '127.0.0.1',
fetch(req) {
const url = new URL(req.url);
// Echo endpoint — returns request headers as JSON
if (url.pathname === '/echo') {
const headers: Record<string, string> = {};
req.headers.forEach((value, key) => { headers[key] = value; });
return new Response(JSON.stringify(headers, null, 2), {
headers: { 'Content-Type': 'application/json' },
});
}
let filePath = url.pathname === '/' ? '/basic.html' : url.pathname;
// Remove leading slash
filePath = filePath.replace(/^\//, '');
const fullPath = path.join(FIXTURES_DIR, filePath);
if (!fs.existsSync(fullPath)) {
return new Response('Not Found', { status: 404 });
}
const content = fs.readFileSync(fullPath, 'utf-8');
const ext = path.extname(fullPath);
const contentType = ext === '.html' ? 'text/html' : 'text/plain';
return new Response(content, {
headers: { 'Content-Type': contentType },
});
},
});
const url = `http://127.0.0.1:${server.port}`;
return { server, url };
}
// If run directly, start and print URL
if (import.meta.main) {
const { server, url } = startTestServer(9450);
console.log(`Test server running at ${url}`);
console.log(`Fixtures: ${FIXTURES_DIR}`);
console.log('Press Ctrl+C to stop');
}