Skip to content

Commit 7b7f795

Browse files
committed
test(task): strengthen title request-option coverage and keep unknown disk fields
1 parent 344ef96 commit 7b7f795

4 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎src/api/providers/__tests__/openrouter.spec.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,54 @@ describe("OpenRouterHandler", () => {
996996
})
997997
})
998998

999+
it("sends no request options when the caller passes none", async () => {
1000+
const handler = new OpenRouterHandler(
1001+
makeApiHandlerOptions({
1002+
openRouterApiKey: "test-key",
1003+
openRouterModelId: "openai/gpt-4o",
1004+
}),
1005+
)
1006+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
1007+
installMockClient(mockCreate)
1008+
1009+
await handler.completePrompt("test prompt")
1010+
1011+
expect(mockCreate).toHaveBeenLastCalledWith(expect.objectContaining({ model: "openai/gpt-4o" }), undefined)
1012+
})
1013+
1014+
it("forwards only the abort signal when no deadline is given", async () => {
1015+
const handler = new OpenRouterHandler(
1016+
makeApiHandlerOptions({
1017+
openRouterApiKey: "test-key",
1018+
openRouterModelId: "openai/gpt-4o",
1019+
}),
1020+
)
1021+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
1022+
installMockClient(mockCreate)
1023+
const { signal } = new AbortController()
1024+
1025+
await handler.completePrompt("test prompt", { abortSignal: signal })
1026+
1027+
expect(mockCreate).toHaveBeenLastCalledWith(expect.objectContaining({ model: "openai/gpt-4o" }), { signal })
1028+
})
1029+
1030+
it("forwards only the deadline when no signal is given", async () => {
1031+
const handler = new OpenRouterHandler(
1032+
makeApiHandlerOptions({
1033+
openRouterApiKey: "test-key",
1034+
openRouterModelId: "openai/gpt-4o",
1035+
}),
1036+
)
1037+
const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
1038+
installMockClient(mockCreate)
1039+
1040+
await handler.completePrompt("test prompt", { timeoutMs: 5_000 })
1041+
1042+
expect(mockCreate).toHaveBeenLastCalledWith(expect.objectContaining({ model: "openai/gpt-4o" }), {
1043+
timeout: 5_000,
1044+
})
1045+
})
1046+
9991047
it("does not start a request when the caller already aborted", async () => {
10001048
const handler = new OpenRouterHandler(mockOptions)
10011049
const mockCreate = vitest.fn()

‎src/api/providers/__tests__/poe.spec.ts‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,31 @@ describe("PoeHandler", () => {
402402
})
403403

404404
describe("completePrompt", () => {
405-
it("forwards the abort signal and deadline to the generation", async () => {
405+
it("links the caller's stop into the merged generation signal", async () => {
406406
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
407407
mockGenerateText.mockResolvedValue({ text: "generated response" })
408-
const { signal } = new AbortController()
408+
const abortController = new AbortController()
409409

410-
await handler.completePrompt("complete this", { abortSignal: signal, timeoutMs: 5_000 })
410+
await handler.completePrompt("complete this", { abortSignal: abortController.signal, timeoutMs: 5_000 })
411411

412-
const options = mockGenerateText.mock.calls[0][0]
413-
// The deadline is merged into a per-request signal that still follows the caller's stop.
414-
expect(options.abortSignal).not.toBe(signal)
415-
expect(options.abortSignal?.aborted).toBe(false)
412+
const mergedSignal = mockGenerateText.mock.calls[0][0].abortSignal
413+
// A per-request signal carries both the caller's stop and the deadline.
414+
expect(mergedSignal).not.toBe(abortController.signal)
415+
expect(mergedSignal?.aborted).toBe(false)
416+
417+
abortController.abort()
418+
expect(mergedSignal?.aborted).toBe(true)
419+
})
420+
421+
it("aborts the merged generation signal when the deadline passes", async () => {
422+
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
423+
mockGenerateText.mockResolvedValue({ text: "generated response" })
424+
425+
await handler.completePrompt("complete this", { timeoutMs: 200 })
426+
427+
const mergedSignal = mockGenerateText.mock.calls[0][0].abortSignal
428+
expect(mergedSignal?.aborted).toBe(false)
429+
await vi.waitFor(() => expect(mergedSignal?.aborted).toBe(true))
416430
})
417431

418432
it("does not start a generation when the caller already aborted", async () => {

‎src/core/task-persistence/TaskHistoryStore.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,9 @@ export class TaskHistoryStore {
10571057
// Stryker disable next-line StringLiteral: internal discrimination literal between missing and titled reasons
10581058
throw new TitleWriteSkipped("titled")
10591059
}
1060-
written = mergeHistoryDelta(parsed.data, incoming as HistoryItem, { title })
1060+
// Merge into the raw record, not the parsed copy: Zod strips unknown keys, and a
1061+
// field written by another extension version must not be deleted by a title write.
1062+
written = mergeHistoryDelta(existing, incoming as HistoryItem, { title })
10611063
return written
10621064
},
10631065
})

‎src/core/task-persistence/__tests__/TaskHistoryStore.crossInstance.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,22 @@ describe("TaskHistoryStore cross-instance safety", () => {
208208
storeWithOnWrite.dispose()
209209
})
210210

211+
it("preserves fields written by another extension version when applying a title", async () => {
212+
await storeA.initialize()
213+
await storeA.upsert(makeHistoryItem({ id: "title-extra-fields", task: "Prompt" }))
214+
const filePath = path.join(tmpDir, "tasks", "title-extra-fields", GlobalFileNames.historyItem)
215+
const onDisk = JSON.parse(await fs.readFile(filePath, "utf8"))
216+
// A field this version does not know about, written by another build.
217+
onDisk.futureField = "kept"
218+
await fs.writeFile(filePath, JSON.stringify(onDisk), "utf8")
219+
220+
await expect(storeA.setTitleIfUnset("title-extra-fields", "Auto title")).resolves.toBe("applied")
221+
222+
const final = JSON.parse(await fs.readFile(filePath, "utf8"))
223+
expect(final.title).toBe("Auto title")
224+
expect(final.futureField).toBe("kept")
225+
})
226+
211227
it("re-throws unexpected errors from safeWriteJson", async () => {
212228
await storeA.initialize()
213229
await storeA.upsert(makeHistoryItem({ id: "title-io-err", task: "Prompt" }))

0 commit comments

Comments
 (0)