|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import test from 'node:test' |
| 3 | + |
| 4 | +import { runWithTempDataDir } from '@/lib/server/test-utils/run-with-temp-data-dir' |
| 5 | + |
| 6 | +test('GET /api/chats/[id]/context-pack returns structured and markdown handoff context', () => { |
| 7 | + const output = runWithTempDataDir<{ |
| 8 | + status: number |
| 9 | + markdownStatus: number |
| 10 | + missingStatus: number |
| 11 | + schemaVersion: number |
| 12 | + linkedTaskCount: number |
| 13 | + recentMessageCount: number |
| 14 | + markdownContentType: string |
| 15 | + markdownIncludesTitle: boolean |
| 16 | + markdownIncludesTask: boolean |
| 17 | + }>(` |
| 18 | + const storageMod = await import('./src/lib/server/storage') |
| 19 | + const repoMod = await import('@/lib/server/messages/message-repository') |
| 20 | + const routeMod = await import('./src/app/api/chats/[id]/context-pack/route') |
| 21 | + const storage = storageMod.default || storageMod |
| 22 | + const repo = repoMod.default || repoMod |
| 23 | + const route = routeMod.default || routeMod |
| 24 | +
|
| 25 | + const now = Date.now() |
| 26 | + storage.saveSessions({ |
| 27 | + sess_pack_1: { |
| 28 | + id: 'sess_pack_1', |
| 29 | + name: 'Context pack test', |
| 30 | + cwd: process.env.WORKSPACE_DIR, |
| 31 | + user: 'tester', |
| 32 | + provider: 'openai', |
| 33 | + model: 'gpt-4o-mini', |
| 34 | + claudeSessionId: null, |
| 35 | + codexThreadId: 'codex-pack-thread', |
| 36 | + messages: [], |
| 37 | + createdAt: now, |
| 38 | + lastActiveAt: now, |
| 39 | + runContext: { |
| 40 | + objective: 'Hand off a release session.', |
| 41 | + constraints: ['Keep the pack short.'], |
| 42 | + keyFacts: ['The current tag is local only.'], |
| 43 | + discoveries: [], |
| 44 | + failedApproaches: [], |
| 45 | + currentPlan: ['Run checks'], |
| 46 | + completedSteps: [], |
| 47 | + blockers: [], |
| 48 | + parentContext: null, |
| 49 | + updatedAt: now, |
| 50 | + version: 1, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | + storage.saveTasks({ |
| 55 | + task_pack_1: { |
| 56 | + id: 'task_pack_1', |
| 57 | + title: 'Verify context pack', |
| 58 | + description: 'Exercise API route.', |
| 59 | + status: 'running', |
| 60 | + sessionId: 'sess_pack_1', |
| 61 | + agentId: null, |
| 62 | + createdAt: now, |
| 63 | + updatedAt: now + 3, |
| 64 | + }, |
| 65 | + }) |
| 66 | +
|
| 67 | + repo.appendMessage('sess_pack_1', { role: 'user', text: 'Prepare handoff.', time: now, attachedFiles: ['/tmp/handoff.md'] }) |
| 68 | + repo.appendMessage('sess_pack_1', { role: 'assistant', text: 'Ready.', time: now + 1 }) |
| 69 | +
|
| 70 | + const response = await route.GET( |
| 71 | + new Request('http://local/api/chats/sess_pack_1/context-pack?messages=1'), |
| 72 | + { params: Promise.resolve({ id: 'sess_pack_1' }) }, |
| 73 | + ) |
| 74 | + const payload = await response.json() |
| 75 | +
|
| 76 | + const markdownResponse = await route.GET( |
| 77 | + new Request('http://local/api/chats/sess_pack_1/context-pack?format=markdown'), |
| 78 | + { params: Promise.resolve({ id: 'sess_pack_1' }) }, |
| 79 | + ) |
| 80 | + const markdown = await markdownResponse.text() |
| 81 | +
|
| 82 | + const missingResponse = await route.GET( |
| 83 | + new Request('http://local/api/chats/missing/context-pack'), |
| 84 | + { params: Promise.resolve({ id: 'missing' }) }, |
| 85 | + ) |
| 86 | +
|
| 87 | + console.log(JSON.stringify({ |
| 88 | + status: response.status, |
| 89 | + markdownStatus: markdownResponse.status, |
| 90 | + missingStatus: missingResponse.status, |
| 91 | + schemaVersion: payload.schemaVersion, |
| 92 | + linkedTaskCount: payload.linkedTasks.length, |
| 93 | + recentMessageCount: payload.recentMessages.length, |
| 94 | + markdownContentType: markdownResponse.headers.get('content-type') || '', |
| 95 | + markdownIncludesTitle: markdown.includes('# Session Context Pack: Context pack test'), |
| 96 | + markdownIncludesTask: markdown.includes('Verify context pack'), |
| 97 | + })) |
| 98 | + `, { prefix: 'swarmclaw-context-pack-route-' }) |
| 99 | + |
| 100 | + assert.equal(output.status, 200) |
| 101 | + assert.equal(output.markdownStatus, 200) |
| 102 | + assert.equal(output.missingStatus, 404) |
| 103 | + assert.equal(output.schemaVersion, 1) |
| 104 | + assert.equal(output.linkedTaskCount, 1) |
| 105 | + assert.equal(output.recentMessageCount, 1) |
| 106 | + assert.match(output.markdownContentType, /text\/markdown/) |
| 107 | + assert.equal(output.markdownIncludesTitle, true) |
| 108 | + assert.equal(output.markdownIncludesTask, true) |
| 109 | +}) |
0 commit comments