From 1654b121bc0a3bc7d455c5ff6454dd2d7e838ea2 Mon Sep 17 00:00:00 2001 From: catlog22 Date: Mon, 29 Dec 2025 12:22:09 +0800 Subject: [PATCH] test(routes): add integration tests for CodexLens and discovery routes Solution-ID: SOL-1735386000005 Issue-ID: ISS-1766921318981-19 Task-ID: T4 --- .../integration/codexlens-routes.test.ts | 239 ++++++++++++ .../integration/discovery-routes.test.ts | 362 ++++++++++++++++++ 2 files changed, 601 insertions(+) create mode 100644 ccw/tests/integration/codexlens-routes.test.ts create mode 100644 ccw/tests/integration/discovery-routes.test.ts diff --git a/ccw/tests/integration/codexlens-routes.test.ts b/ccw/tests/integration/codexlens-routes.test.ts new file mode 100644 index 00000000..234f426e --- /dev/null +++ b/ccw/tests/integration/codexlens-routes.test.ts @@ -0,0 +1,239 @@ +/** + * Integration tests for CodexLens routes. + * + * Notes: + * - Targets runtime implementation shipped in `ccw/dist`. + * - Exercises real HTTP request/response flow via a minimal test server. + */ + +import { after, before, describe, it, mock } from 'node:test'; +import assert from 'node:assert/strict'; +import http from 'node:http'; + +const codexLensRoutesUrl = new URL('../../dist/core/routes/codexlens-routes.js', import.meta.url); +codexLensRoutesUrl.searchParams.set('t', String(Date.now())); + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +let mod: any; + +type JsonResponse = { status: number; json: any; text: string }; + +async function requestJson( + baseUrl: string, + method: string, + path: string, + body?: unknown, +): Promise { + const url = new URL(path, baseUrl); + const payload = body === undefined ? null : Buffer.from(JSON.stringify(body), 'utf8'); + + return new Promise((resolve, reject) => { + const req = http.request( + url, + { + method, + headers: { + Accept: 'application/json', + ...(payload + ? { 'Content-Type': 'application/json', 'Content-Length': String(payload.length) } + : {}), + }, + }, + (res) => { + let responseBody = ''; + res.on('data', (chunk) => { + responseBody += chunk.toString(); + }); + res.on('end', () => { + let json: any = null; + try { + json = responseBody ? JSON.parse(responseBody) : null; + } catch { + json = null; + } + resolve({ status: res.statusCode || 0, json, text: responseBody }); + }); + }, + ); + req.on('error', reject); + if (payload) req.write(payload); + req.end(); + }); +} + +function handlePostRequest( + req: http.IncomingMessage, + res: http.ServerResponse, + handler: (body: unknown) => Promise, +): void { + let body = ''; + req.on('data', (chunk) => { + body += chunk.toString(); + }); + req.on('end', async () => { + try { + const parsed = body ? JSON.parse(body) : {}; + const result = await handler(parsed); + + if (result?.error) { + res.writeHead(result.status || 500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: result.error })); + } else { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(result)); + } + } catch (err: any) { + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: err?.message || String(err) })); + } + }); +} + +async function createServer(initialPath: string, broadcasts: any[]): Promise<{ server: http.Server; baseUrl: string }> { + const server = http.createServer(async (req, res) => { + const url = new URL(req.url || '/', 'http://localhost'); + const pathname = url.pathname; + + const ctx = { + pathname, + url, + req, + res, + initialPath, + handlePostRequest, + broadcastToClients(data: unknown) { + broadcasts.push(data); + }, + }; + + try { + const handled = await mod.handleCodexLensRoutes(ctx); + if (!handled) { + res.writeHead(404, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Not Found' })); + } + } catch (err: any) { + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: err?.message || String(err) })); + } + }); + + await new Promise((resolve) => server.listen(0, () => resolve())); + const addr = server.address(); + const port = typeof addr === 'object' && addr ? addr.port : 0; + return { server, baseUrl: `http://127.0.0.1:${port}` }; +} + +describe('codexlens routes integration', async () => { + before(async () => { + mock.method(console, 'log', () => {}); + mock.method(console, 'error', () => {}); + mod = await import(codexLensRoutesUrl.href); + }); + + after(() => { + mock.restoreAll(); + }); + + it('GET /api/codexlens/status returns venv status', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'GET', '/api/codexlens/status'); + assert.equal(res.status, 200); + assert.equal(typeof res.json, 'object'); + assert.equal(typeof res.json.ready, 'boolean'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + it('GET /api/codexlens/config returns configuration payload', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'GET', '/api/codexlens/config'); + assert.equal(res.status, 200); + assert.equal(typeof res.json?.index_dir, 'string'); + assert.equal(typeof res.json?.index_count, 'number'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + it('POST /api/codexlens/config validates required index_dir', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'POST', '/api/codexlens/config', {}); + assert.equal(res.status, 400); + assert.ok(String(res.json.error).includes('index_dir is required')); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + it('GET /api/codexlens/indexing-status returns inProgress flag', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'GET', '/api/codexlens/indexing-status'); + assert.equal(res.status, 200); + assert.equal(res.json.success, true); + assert.equal(typeof res.json.inProgress, 'boolean'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + it('GET /api/codexlens/semantic/status returns semantic availability', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'GET', '/api/codexlens/semantic/status'); + assert.equal(res.status, 200); + assert.equal(typeof res.json?.available, 'boolean'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + it('GET /api/codexlens/indexes returns indexes list payload', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'GET', '/api/codexlens/indexes'); + assert.equal(res.status, 200); + assert.equal(res.json.success, true); + assert.equal(Array.isArray(res.json.indexes), true); + + const totalSize = + typeof res.json.totalSize === 'number' ? res.json.totalSize : res.json.summary?.totalSize; + const totalSizeFormatted = + typeof res.json.totalSizeFormatted === 'string' + ? res.json.totalSizeFormatted + : res.json.summary?.totalSizeFormatted; + + assert.equal(typeof totalSize, 'number'); + assert.equal(typeof totalSizeFormatted, 'string'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); + + it('GET /api/codexlens/dashboard-init returns aggregated payload', async () => { + const broadcasts: any[] = []; + const { server, baseUrl } = await createServer(process.cwd(), broadcasts); + try { + const res = await requestJson(baseUrl, 'GET', '/api/codexlens/dashboard-init'); + assert.equal(res.status, 200); + assert.equal(typeof res.json?.installed, 'boolean'); + assert.ok(res.json?.config); + assert.equal(typeof res.json.config.index_dir, 'string'); + assert.equal(typeof res.json.config.index_count, 'number'); + assert.ok(res.json?.semantic); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + }); +}); diff --git a/ccw/tests/integration/discovery-routes.test.ts b/ccw/tests/integration/discovery-routes.test.ts new file mode 100644 index 00000000..6a1806b1 --- /dev/null +++ b/ccw/tests/integration/discovery-routes.test.ts @@ -0,0 +1,362 @@ +/** + * Integration tests for discovery routes. + * + * Notes: + * - Targets runtime implementation shipped in `ccw/dist`. + * - Uses a temporary project directory to isolate `.workflow/issues/discoveries`. + */ + +import { after, before, describe, it, mock } from 'node:test'; +import assert from 'node:assert/strict'; +import http from 'node:http'; +import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const discoveryRoutesUrl = new URL('../../dist/core/routes/discovery-routes.js', import.meta.url); +discoveryRoutesUrl.searchParams.set('t', String(Date.now())); + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +let mod: any; + +type JsonResponse = { status: number; json: any; text: string }; + +async function requestJson( + baseUrl: string, + method: string, + path: string, + body?: unknown, +): Promise { + const url = new URL(path, baseUrl); + const payload = body === undefined ? null : Buffer.from(JSON.stringify(body), 'utf8'); + + return new Promise((resolve, reject) => { + const req = http.request( + url, + { + method, + headers: { + Accept: 'application/json', + ...(payload + ? { 'Content-Type': 'application/json', 'Content-Length': String(payload.length) } + : {}), + }, + }, + (res) => { + let responseBody = ''; + res.on('data', (chunk) => { + responseBody += chunk.toString(); + }); + res.on('end', () => { + let json: any = null; + try { + json = responseBody ? JSON.parse(responseBody) : null; + } catch { + json = null; + } + resolve({ status: res.statusCode || 0, json, text: responseBody }); + }); + }, + ); + req.on('error', reject); + if (payload) req.write(payload); + req.end(); + }); +} + +function handlePostRequest( + req: http.IncomingMessage, + res: http.ServerResponse, + handler: (body: unknown) => Promise, +): void { + let body = ''; + req.on('data', (chunk) => { + body += chunk.toString(); + }); + req.on('end', async () => { + try { + const parsed = body ? JSON.parse(body) : {}; + const result = await handler(parsed); + + if (result?.error) { + res.writeHead(result.status || 500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: result.error })); + } else { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(result)); + } + } catch (err: any) { + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: err?.message || String(err) })); + } + }); +} + +async function createServer(initialPath: string): Promise<{ server: http.Server; baseUrl: string }> { + const server = http.createServer(async (req, res) => { + const url = new URL(req.url || '/', 'http://localhost'); + const pathname = url.pathname; + + const ctx = { + pathname, + url, + req, + res, + initialPath, + handlePostRequest, + broadcastToClients() {}, + }; + + try { + const handled = await mod.handleDiscoveryRoutes(ctx); + if (!handled) { + res.writeHead(404, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Not Found' })); + } + } catch (err: any) { + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: err?.message || String(err) })); + } + }); + + await new Promise((resolve) => server.listen(0, () => resolve())); + const addr = server.address(); + const port = typeof addr === 'object' && addr ? addr.port : 0; + return { server, baseUrl: `http://127.0.0.1:${port}` }; +} + +function createDiscoveryFixture(projectRoot: string): { discoveryId: string; findingId: string; discoveryDir: string } { + const discoveryId = `DSC-TEST-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`; + const findingId = 'F-001'; + + const discoveryDir = join(projectRoot, '.workflow', 'issues', 'discoveries', discoveryId); + const perspectivesDir = join(discoveryDir, 'perspectives'); + mkdirSync(perspectivesDir, { recursive: true }); + + const createdAt = new Date().toISOString(); + writeFileSync( + join(discoveryDir, 'discovery-state.json'), + JSON.stringify( + { + target_pattern: 'src/**/*.ts', + phase: 'complete', + created_at: createdAt, + updated_at: createdAt, + metadata: { perspectives: ['bug'], created_at: createdAt }, + perspectives: [{ id: 'bug', status: 'completed' }], + results: { issues_generated: 0 }, + total_findings: 1, + issues_generated: 0, + priority_distribution: { '3': 1 }, + }, + null, + 2, + ), + 'utf8', + ); + + writeFileSync( + join(perspectivesDir, 'bug.json'), + JSON.stringify( + { + summary: { total: 1 }, + findings: [ + { + id: findingId, + title: 'Example finding', + description: 'Example description', + priority: '3', + perspective: 'bug', + file: 'src/example.ts', + line: 42, + suggested_issue: { title: 'Example issue', priority: 3, labels: ['bug'] }, + }, + ], + }, + null, + 2, + ), + 'utf8', + ); + + return { discoveryId, findingId, discoveryDir }; +} + +describe('discovery routes integration', async () => { + before(async () => { + mock.method(console, 'log', () => {}); + mock.method(console, 'error', () => {}); + mod = await import(discoveryRoutesUrl.href); + }); + + after(() => { + mock.restoreAll(); + }); + + it('GET /api/discoveries lists discovery sessions', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson(baseUrl, 'GET', '/api/discoveries'); + assert.equal(res.status, 200); + assert.equal(Array.isArray(res.json.discoveries), true); + assert.equal(res.json.total, 1); + assert.equal(res.json.discoveries[0].discovery_id, discoveryId); + assert.equal(typeof res.json.discoveries[0].phase, 'string'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); + + it('GET /api/discoveries/:id returns discovery detail and 404 for missing', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson(baseUrl, 'GET', `/api/discoveries/${encodeURIComponent(discoveryId)}`); + assert.equal(res.status, 200); + assert.equal(res.json.target_pattern, 'src/**/*.ts'); + assert.ok(res.json.progress); + assert.equal(Array.isArray(res.json.perspectives), true); + assert.equal(Array.isArray(res.json.discovery_issues), true); + + const missing = await requestJson(baseUrl, 'GET', '/api/discoveries/DSC-NOT-FOUND'); + assert.equal(missing.status, 404); + assert.ok(String(missing.json.error).includes('not found')); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); + + it('GET /api/discoveries/:id/findings returns flattened findings', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId, findingId } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson(baseUrl, 'GET', `/api/discoveries/${encodeURIComponent(discoveryId)}/findings`); + assert.equal(res.status, 200); + assert.equal(Array.isArray(res.json.findings), true); + assert.equal(res.json.total, 1); + assert.equal(res.json.findings[0].id, findingId); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); + + it('GET /api/discoveries/:id/progress returns progress payload', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson(baseUrl, 'GET', `/api/discoveries/${encodeURIComponent(discoveryId)}/progress`); + assert.equal(res.status, 200); + assert.equal(res.json.discovery_id, discoveryId); + assert.ok(res.json.progress); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); + + it('PATCH /api/discoveries/:id/findings/:fid updates finding status', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId, findingId } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson( + baseUrl, + 'PATCH', + `/api/discoveries/${encodeURIComponent(discoveryId)}/findings/${encodeURIComponent(findingId)}`, + { status: 'dismissed', dismissed: true }, + ); + assert.equal(res.status, 200); + assert.equal(res.json.success, true); + assert.equal(res.json.finding_id, findingId); + + const bugPath = join(projectRoot, '.workflow', 'issues', 'discoveries', discoveryId, 'perspectives', 'bug.json'); + const content = JSON.parse(readFileSync(bugPath, 'utf8')); + assert.equal(content.findings[0].status, 'dismissed'); + assert.equal(content.findings[0].dismissed, true); + assert.equal(typeof content.findings[0].updated_at, 'string'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); + + it('POST /api/discoveries/:id/export appends issues.jsonl and marks findings exported', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId, findingId } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson( + baseUrl, + 'POST', + `/api/discoveries/${encodeURIComponent(discoveryId)}/export`, + { export_all: true }, + ); + assert.equal(res.status, 200); + assert.equal(res.json.success, true); + assert.equal(res.json.exported_count, 1); + + const issuesPath = join(projectRoot, '.workflow', 'issues', 'issues.jsonl'); + assert.equal(existsSync(issuesPath), true); + const issuesLines = readFileSync(issuesPath, 'utf8').split('\n').filter((l) => l.trim()); + assert.equal(issuesLines.length, 1); + const issue = JSON.parse(issuesLines[0]); + assert.equal(issue.source, 'discovery'); + assert.equal(issue.source_discovery_id, discoveryId); + assert.equal(issue.source_finding_id, findingId); + + const bugPath = join(projectRoot, '.workflow', 'issues', 'discoveries', discoveryId, 'perspectives', 'bug.json'); + const content = JSON.parse(readFileSync(bugPath, 'utf8')); + assert.equal(content.findings[0].exported, true); + assert.equal(typeof content.findings[0].exported_at, 'string'); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); + + it('DELETE /api/discoveries/:id deletes discovery session', async () => { + const projectRoot = mkdtempSync(join(tmpdir(), 'ccw-discovery-routes-project-')); + try { + const { discoveryId, discoveryDir } = createDiscoveryFixture(projectRoot); + const { server, baseUrl } = await createServer(projectRoot); + try { + const res = await requestJson(baseUrl, 'DELETE', `/api/discoveries/${encodeURIComponent(discoveryId)}`); + assert.equal(res.status, 200); + assert.equal(res.json.success, true); + assert.equal(res.json.deleted, discoveryId); + assert.equal(existsSync(discoveryDir), false); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } + } finally { + rmSync(projectRoot, { recursive: true, force: true }); + } + }); +}); +