|
| 1 | +import { execFileSync } from 'child_process'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import request from 'supertest'; |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 7 | +import { createApp } from '../../packages/codedelta-server/src'; |
| 8 | + |
| 9 | +function run(cmd: string, cwd: string): void { |
| 10 | + execFileSync('sh', ['-c', cmd], { cwd, stdio: 'pipe' }); |
| 11 | +} |
| 12 | + |
| 13 | +async function waitForWikiReady( |
| 14 | + app: ReturnType<typeof createApp>['app'], |
| 15 | + repoId: string, |
| 16 | + commit: string, |
| 17 | + timeoutMs = 120_000, |
| 18 | +): Promise<Record<string, unknown>> { |
| 19 | + const deadline = Date.now() + timeoutMs; |
| 20 | + for (;;) { |
| 21 | + const res = await request(app).get(`/api/repos/${repoId}/wiki/status?commit=${commit}`); |
| 22 | + expect(res.status).toBe(200); |
| 23 | + if (res.body.state === 'ready') return res.body; |
| 24 | + if (res.body.state === 'error') { |
| 25 | + throw new Error(`wiki generation failed: ${res.body.error}`); |
| 26 | + } |
| 27 | + if (Date.now() > deadline) throw new Error('timed out waiting for wiki generation'); |
| 28 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe('codedelta-server wiki (none provider, deterministic path)', () => { |
| 33 | + let tmpDir: string; |
| 34 | + let cacheRoot: string; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codedelta-wiki-')); |
| 38 | + cacheRoot = path.join(tmpDir, '.codedelta'); |
| 39 | + run('git init -b main', tmpDir); |
| 40 | + run('git config user.email "test@example.com"', tmpDir); |
| 41 | + run('git config user.name "Test User"', tmpDir); |
| 42 | + fs.writeFileSync(path.join(tmpDir, 'README.md'), '# wiki demo\n\n\n\nDemo repository for wiki tests.\n'); |
| 43 | + fs.mkdirSync(path.join(tmpDir, 'docs'), { recursive: true }); |
| 44 | + // 1x1 PNG |
| 45 | + fs.writeFileSync( |
| 46 | + path.join(tmpDir, 'docs', 'badge.png'), |
| 47 | + Buffer.from( |
| 48 | + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', |
| 49 | + 'base64', |
| 50 | + ), |
| 51 | + ); |
| 52 | + fs.mkdirSync(path.join(tmpDir, 'src'), { recursive: true }); |
| 53 | + fs.writeFileSync( |
| 54 | + path.join(tmpDir, 'src', 'auth.ts'), |
| 55 | + [ |
| 56 | + 'export function login(user: string): boolean {', |
| 57 | + ' return validate(user);', |
| 58 | + '}', |
| 59 | + '', |
| 60 | + 'export function validate(user: string): boolean {', |
| 61 | + ' return user.length > 0;', |
| 62 | + '}', |
| 63 | + '', |
| 64 | + ].join('\n'), |
| 65 | + ); |
| 66 | + fs.writeFileSync( |
| 67 | + path.join(tmpDir, 'src', 'server.ts'), |
| 68 | + [ |
| 69 | + "import { login } from './auth';", |
| 70 | + '', |
| 71 | + 'export function handleRequest(user: string): string {', |
| 72 | + " return login(user) ? 'ok' : 'denied';", |
| 73 | + '}', |
| 74 | + '', |
| 75 | + ].join('\n'), |
| 76 | + ); |
| 77 | + run('git add . && git commit -m "initial commit"', tmpDir); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach(() => { |
| 81 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('generates a wiki, serves toc/pages, and answers ask deterministically', async () => { |
| 85 | + const { app } = createApp({ cacheRoot }); |
| 86 | + const importRes = await request(app).post('/api/repos/import').send({ source: 'local', input: tmpDir }); |
| 87 | + expect(importRes.status).toBe(201); |
| 88 | + const repoId = importRes.body.id as string; |
| 89 | + const commit = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: tmpDir, encoding: 'utf8' }).trim(); |
| 90 | + |
| 91 | + // Status before generation. |
| 92 | + const absent = await request(app).get(`/api/repos/${repoId}/wiki/status?commit=${commit}`); |
| 93 | + expect(absent.status).toBe(200); |
| 94 | + expect(absent.body.state).toBe('absent'); |
| 95 | + |
| 96 | + // TOC before generation → 404 guidance. |
| 97 | + const earlyToc = await request(app).get(`/api/repos/${repoId}/wiki/toc?commit=${commit}`); |
| 98 | + expect(earlyToc.status).toBe(404); |
| 99 | + |
| 100 | + // Kick off generation (background job). |
| 101 | + const gen = await request(app).post(`/api/repos/${repoId}/wiki/generate?commit=${commit}`); |
| 102 | + expect([200, 202]).toContain(gen.status); |
| 103 | + |
| 104 | + const ready = await waitForWikiReady(app, repoId, commit); |
| 105 | + expect(ready.llmUsed).toBe(false); |
| 106 | + |
| 107 | + // Re-generate on a ready wiki is a no-op. |
| 108 | + const regen = await request(app).post(`/api/repos/${repoId}/wiki/generate?commit=${commit}`); |
| 109 | + expect(regen.status).toBe(200); |
| 110 | + expect(regen.body.status).toBe('ready'); |
| 111 | + |
| 112 | + // TOC: overview + architecture first, then module sections. |
| 113 | + const toc = await request(app).get(`/api/repos/${repoId}/wiki/toc?commit=${commit}`); |
| 114 | + expect(toc.status).toBe(200); |
| 115 | + const sections = toc.body.sections as Array<{ id: string; kind: string }>; |
| 116 | + expect(sections[0].id).toBe('overview'); |
| 117 | + expect(sections[1].id).toBe('architecture'); |
| 118 | + expect(sections.length).toBeGreaterThanOrEqual(2); |
| 119 | + |
| 120 | + // Overview page: markdown with README excerpt, citations array present. |
| 121 | + const overview = await request(app).get( |
| 122 | + `/api/repos/${repoId}/wiki/page?commit=${commit}§ion=overview`, |
| 123 | + ); |
| 124 | + expect(overview.status).toBe(200); |
| 125 | + expect(overview.body.markdown).toContain('# Overview'); |
| 126 | + expect(overview.body.markdown).toContain('/wiki/asset?'); |
| 127 | + expect(overview.body.markdown).toContain(encodeURIComponent('docs/badge.png')); |
| 128 | + expect(overview.body.markdown).toContain('Demo repository for wiki tests.'); |
| 129 | + expect(Array.isArray(overview.body.citations)).toBe(true); |
| 130 | + |
| 131 | + const asset = await request(app).get( |
| 132 | + `/api/repos/${repoId}/wiki/asset?commit=${commit}&path=${encodeURIComponent('docs/badge.png')}`, |
| 133 | + ); |
| 134 | + expect(asset.status).toBe(200); |
| 135 | + expect(asset.headers['content-type']).toMatch(/image\/png/); |
| 136 | + expect(asset.body.length).toBeGreaterThan(0); |
| 137 | + |
| 138 | + // Every TOC section has a retrievable page. |
| 139 | + for (const section of sections) { |
| 140 | + const page = await request(app).get( |
| 141 | + `/api/repos/${repoId}/wiki/page?commit=${commit}§ion=${section.id}`, |
| 142 | + ); |
| 143 | + expect(page.status).toBe(200); |
| 144 | + expect(typeof page.body.markdown).toBe('string'); |
| 145 | + expect(page.body.markdown.length).toBeGreaterThan(0); |
| 146 | + } |
| 147 | + |
| 148 | + // Unknown section → 404. |
| 149 | + const missing = await request(app).get( |
| 150 | + `/api/repos/${repoId}/wiki/page?commit=${commit}§ion=nope`, |
| 151 | + ); |
| 152 | + expect(missing.status).toBe(404); |
| 153 | + |
| 154 | + // Ask without provider: deterministic answer grounded in matched symbols. |
| 155 | + const ask = await request(app) |
| 156 | + .post(`/api/repos/${repoId}/wiki/ask`) |
| 157 | + .send({ commit, question: 'how does login validate the user?' }); |
| 158 | + expect(ask.status).toBe(200); |
| 159 | + expect(ask.body.provider.used).toBe(false); |
| 160 | + expect(ask.body.answer).toContain('login'); |
| 161 | + expect(Array.isArray(ask.body.citations)).toBe(true); |
| 162 | + expect(Array.isArray(ask.body.evidence)).toBe(true); |
| 163 | + expect(ask.body.evidence.length).toBeGreaterThan(0); |
| 164 | + |
| 165 | + // Ask validation errors. |
| 166 | + const noQuestion = await request(app).post(`/api/repos/${repoId}/wiki/ask`).send({ commit }); |
| 167 | + expect(noQuestion.status).toBe(400); |
| 168 | + const noCommit = await request(app) |
| 169 | + .post(`/api/repos/${repoId}/wiki/ask`) |
| 170 | + .send({ question: 'anything' }); |
| 171 | + expect(noCommit.status).toBe(400); |
| 172 | + }, 180_000); |
| 173 | + |
| 174 | + it('rejects generate without commit and unknown repo', async () => { |
| 175 | + const { app } = createApp({ cacheRoot }); |
| 176 | + const importRes = await request(app).post('/api/repos/import').send({ source: 'local', input: tmpDir }); |
| 177 | + const repoId = importRes.body.id as string; |
| 178 | + |
| 179 | + const noCommit = await request(app).post(`/api/repos/${repoId}/wiki/generate`); |
| 180 | + expect(noCommit.status).toBe(400); |
| 181 | + |
| 182 | + const badRepo = await request(app).post(`/api/repos/does-not-exist/wiki/generate?commit=abc`); |
| 183 | + expect(badRepo.status).toBe(404); |
| 184 | + }); |
| 185 | +}); |
0 commit comments