-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdebug.test.ts
More file actions
297 lines (270 loc) · 9.01 KB
/
debug.test.ts
File metadata and controls
297 lines (270 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { describe, it, expect, beforeEach, afterEach } from "vitest"
import fs from "node:fs"
import path from "node:path"
import os from "node:os"
import { Hono } from "hono"
import { createBookStorage } from "@adt/storage"
import { openBookDb } from "@adt/storage"
import { errorHandler } from "../middleware/error-handler.js"
import { createDebugRoutes } from "./debug.js"
import type { PipelineService } from "../services/pipeline-service.js"
function makeMockPipelineService(
overrides?: Partial<PipelineService>
): PipelineService {
return {
getStatus: () => null,
addListener: () => () => {},
startPipeline: async () => {},
...overrides,
}
}
describe("Debug routes", () => {
let tmpDir: string
let app: Hono
const label = "test-book"
function seedLlmLogs(dbPath: string) {
const db = openBookDb(dbPath)
try {
// Insert LLM log entries with known data
const entries = [
{
step: "text-classification",
item_id: `${label}_p1`,
data: {
promptName: "classify-text",
modelId: "gpt-4o",
cacheHit: false,
durationMs: 1200,
usage: { inputTokens: 500, outputTokens: 200 },
validationErrors: [],
},
},
{
step: "text-classification",
item_id: `${label}_p2`,
data: {
promptName: "classify-text",
modelId: "gpt-4o",
cacheHit: true,
durationMs: 50,
usage: { inputTokens: 500, outputTokens: 200 },
},
},
{
step: "page-sectioning",
item_id: `${label}_p1`,
data: {
promptName: "section-page",
modelId: "gpt-4o",
cacheHit: false,
durationMs: 2000,
usage: { inputTokens: 1000, outputTokens: 500 },
validationErrors: ["Invalid section type"],
},
},
{
step: "metadata",
item_id: "book",
data: {
promptName: "extract-metadata",
modelId: "gpt-4o",
cacheHit: false,
durationMs: 3000,
usage: { inputTokens: 2000, outputTokens: 300 },
},
},
]
for (const entry of entries) {
db.run(
"INSERT INTO llm_log (timestamp, step, item_id, data) VALUES (?, ?, ?, ?)",
[new Date().toISOString(), entry.step, entry.item_id, JSON.stringify(entry.data)]
)
}
} finally {
db.close()
}
}
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "debug-routes-"))
// Create a book with extracted pages
const storage = createBookStorage(label, tmpDir)
try {
storage.putExtractedPage({
pageId: `${label}_p1`,
pageNumber: 1,
text: "Page one text",
pageImage: {
imageId: `${label}_p1_page`,
pngBuffer: Buffer.from("fake-png"),
hash: "abc123",
width: 800,
height: 600,
},
images: [],
})
storage.putExtractedPage({
pageId: `${label}_p2`,
pageNumber: 2,
text: "Page two text",
pageImage: {
imageId: `${label}_p2_page`,
pngBuffer: Buffer.from("fake-png-2"),
hash: "def456",
width: 800,
height: 600,
},
images: [],
})
// Add node_data with multiple versions
storage.putNodeData("text-classification", `${label}_p1`, { version: "v1" })
storage.putNodeData("text-classification", `${label}_p1`, { version: "v2" })
storage.putNodeData("text-classification", `${label}_p1`, { version: "v3" })
} finally {
storage.close()
}
// Seed LLM logs
const dbPath = path.join(tmpDir, label, `${label}.db`)
seedLlmLogs(dbPath)
const pipelineService = makeMockPipelineService()
const routes = createDebugRoutes(pipelineService, tmpDir, tmpDir)
app = new Hono()
app.onError(errorHandler)
app.route("/api", routes)
})
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true })
})
describe("GET /api/books/:label/debug/llm-logs", () => {
it("returns paginated logs with total count", async () => {
const res = await app.request(`/api/books/${label}/debug/llm-logs`)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.total).toBe(4)
expect(body.logs).toHaveLength(4)
// Newest first
expect(body.logs[0].step).toBe("metadata")
})
it("filters by step", async () => {
const res = await app.request(
`/api/books/${label}/debug/llm-logs?step=text-classification`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.total).toBe(2)
expect(body.logs).toHaveLength(2)
for (const log of body.logs) {
expect(log.step).toBe("text-classification")
}
})
it("filters by itemId", async () => {
const res = await app.request(
`/api/books/${label}/debug/llm-logs?itemId=${label}_p1`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.total).toBe(2)
})
it("respects limit and offset", async () => {
const res = await app.request(
`/api/books/${label}/debug/llm-logs?limit=2&offset=1`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.total).toBe(4)
expect(body.logs).toHaveLength(2)
})
it("clamps limit to 200", async () => {
const res = await app.request(
`/api/books/${label}/debug/llm-logs?limit=999`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.logs.length).toBeLessThanOrEqual(200)
})
it("returns 404 for nonexistent book", async () => {
const res = await app.request("/api/books/no-such-book/debug/llm-logs")
expect(res.status).toBe(404)
})
})
describe("GET /api/books/:label/debug/stats", () => {
it("returns aggregated stats by step", async () => {
const res = await app.request(`/api/books/${label}/debug/stats`)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.steps).toBeDefined()
expect(Array.isArray(body.steps)).toBe(true)
expect(body.totals).toBeDefined()
// Check total counts
expect(body.totals.calls).toBe(4)
expect(body.totals.cacheHits).toBe(1)
// Check step-level data
const textStep = body.steps.find(
(s: { step: string }) => s.step === "text-classification"
)
expect(textStep).toBeDefined()
expect(textStep.calls).toBe(2)
expect(textStep.cacheHits).toBe(1)
})
it("includes pipeline run timing when available", async () => {
const pipelineService = makeMockPipelineService({
getStatus: () => ({
label,
status: "completed",
startedAt: 1000,
completedAt: 5000,
}),
})
const routes = createDebugRoutes(pipelineService, tmpDir, tmpDir)
const appWithTiming = new Hono()
appWithTiming.onError(errorHandler)
appWithTiming.route("/api", routes)
const res = await appWithTiming.request(`/api/books/${label}/debug/stats`)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.pipelineRun).toBeDefined()
expect(body.pipelineRun.status).toBe("completed")
expect(body.pipelineRun.wallClockMs).toBe(4000)
})
it("returns 404 for nonexistent book", async () => {
const res = await app.request("/api/books/no-such-book/debug/stats")
expect(res.status).toBe(404)
})
})
describe("GET /api/books/:label/debug/versions/:node/:itemId", () => {
it("returns version list without data by default", async () => {
const res = await app.request(
`/api/books/${label}/debug/versions/text-classification/${label}_p1`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.versions).toHaveLength(3)
// Newest first
expect(body.versions[0].version).toBe(3)
expect(body.versions[0].data).toBeUndefined()
})
it("includes data when includeData=true", async () => {
const res = await app.request(
`/api/books/${label}/debug/versions/text-classification/${label}_p1?includeData=true`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.versions).toHaveLength(3)
expect(body.versions[0].data).toBeDefined()
expect(body.versions[0].data.version).toBe("v3")
})
it("returns empty list for unknown node/item", async () => {
const res = await app.request(
`/api/books/${label}/debug/versions/unknown-node/unknown-item`
)
expect(res.status).toBe(200)
const body = await res.json()
expect(body.versions).toHaveLength(0)
})
it("returns 404 for nonexistent book", async () => {
const res = await app.request(
"/api/books/no-such-book/debug/versions/text-classification/p1"
)
expect(res.status).toBe(404)
})
})
})