|
3 | 3 | import * as fs from "fs/promises" |
4 | 4 | import * as path from "path" |
5 | 5 | import * as os from "os" |
| 6 | +import * as recovery from "../../../utils/jsonFileRecovery" |
6 | 7 |
|
7 | 8 | import type { HistoryItem } from "@roo-code/types" |
8 | 9 |
|
@@ -167,12 +168,28 @@ describe("TaskHistoryStore reconcileDelegationState", () => { |
167 | 168 | }) |
168 | 169 |
|
169 | 170 | afterEach(async () => { |
| 171 | + vi.restoreAllMocks() |
170 | 172 | safeWriteJsonMock.mockImplementation(writeJson) |
171 | 173 | for (const disposable of disposables) disposable.dispose() |
172 | 174 | disposables.clear() |
173 | 175 | await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {}) |
174 | 176 | }) |
175 | 177 |
|
| 178 | + it.each(["EACCES", "EIO", "ELOCKED", "missing"])("handles failed startup recovery: %s", async (code) => { |
| 179 | + const item = makeItem({ id: "recovery-failure", status: "active" }) |
| 180 | + await seedItems([item]) |
| 181 | + await store.initialize() |
| 182 | + store.dispose() |
| 183 | + const target = path.join(tmpDir, "tasks", item.id, GlobalFileNames.historyItem) |
| 184 | + await fs.rename(target, path.join(path.dirname(target), `.${path.basename(target)}.bak_1.tmp`)) |
| 185 | + const recover = vi.spyOn(recovery, "recoverMissingJsonFile") |
| 186 | + if (code === "missing") recover.mockResolvedValue(false) |
| 187 | + else recover.mockRejectedValue(Object.assign(new Error("recovery failed"), { code })) |
| 188 | + await store.reconcile() |
| 189 | + expect(recover).toHaveBeenCalledWith(target, { waitForStaleLock: false }) |
| 190 | + expect(store.get(item.id)).toEqual(code === "missing" ? undefined : item) |
| 191 | + }) |
| 192 | + |
176 | 193 | it("repairs orphaned delegation: delegated parent whose child does not exist → active", async () => { |
177 | 194 | const parent = makeItem({ id: "parent-1", status: "delegated", awaitingChildId: "missing-child" }) |
178 | 195 | await seedItems([parent]) |
@@ -444,26 +461,36 @@ describe("TaskHistoryStore reconcileDelegationState", () => { |
444 | 461 | await expect(fs.access(intentPath)).rejects.toThrow() |
445 | 462 | }) |
446 | 463 |
|
447 | | - it("replays a both-at-target intent without writing task files", async () => { |
448 | | - const child = makeItem({ id: "child-at-target", status: "interrupted", parentTaskId: "parent-at-target" }) |
449 | | - const parent = makeItem({ id: "parent-at-target", status: "active" }) |
450 | | - await seedItems([parent, child]) |
451 | | - const intentPath = path.join(tmpDir, "tasks", GlobalFileNames.delegationRepairIntent) |
452 | | - await fs.writeFile( |
453 | | - intentPath, |
454 | | - JSON.stringify(makeRepairIntent({ ...parent, status: "delegated", awaitingChildId: child.id }, child)), |
455 | | - ) |
456 | | - |
457 | | - const writeCalls: string[] = [] |
458 | | - safeWriteJsonMock.mockImplementation(async (filePath, data) => { |
459 | | - writeCalls.push(filePath) |
460 | | - await writeJson(filePath, data) |
461 | | - }) |
462 | | - await store.initialize() |
| 464 | + it.each([false, true])( |
| 465 | + "replays a both-at-target intent without writing task files (backup: %s)", |
| 466 | + async (backup) => { |
| 467 | + const child = makeItem({ id: "child-at-target", status: "interrupted", parentTaskId: "parent-at-target" }) |
| 468 | + const parent = makeItem({ id: "parent-at-target", status: "active" }) |
| 469 | + await seedItems([parent, child]) |
| 470 | + const intentPath = path.join(tmpDir, "tasks", GlobalFileNames.delegationRepairIntent) |
| 471 | + await fs.writeFile( |
| 472 | + intentPath, |
| 473 | + JSON.stringify(makeRepairIntent({ ...parent, status: "delegated", awaitingChildId: child.id }, child)), |
| 474 | + ) |
463 | 475 |
|
464 | | - expect(writeCalls.filter((filePath) => filePath.endsWith(GlobalFileNames.historyItem))).toEqual([]) |
465 | | - await expect(fs.access(intentPath)).rejects.toThrow() |
466 | | - }) |
| 476 | + if (backup) { |
| 477 | + await fs.rename( |
| 478 | + intentPath, |
| 479 | + path.join(path.dirname(intentPath), `.${path.basename(intentPath)}.bak_1.tmp`), |
| 480 | + ) |
| 481 | + } |
| 482 | + |
| 483 | + const writeCalls: string[] = [] |
| 484 | + safeWriteJsonMock.mockImplementation(async (filePath, data) => { |
| 485 | + writeCalls.push(filePath) |
| 486 | + await writeJson(filePath, data) |
| 487 | + }) |
| 488 | + await store.initialize() |
| 489 | + |
| 490 | + expect(writeCalls.filter((filePath) => filePath.endsWith(GlobalFileNames.historyItem))).toEqual([]) |
| 491 | + await expect(fs.access(intentPath)).rejects.toThrow() |
| 492 | + }, |
| 493 | + ) |
467 | 494 |
|
468 | 495 | it("removes the repair-intent file after successful replay", async () => { |
469 | 496 | const child = makeItem({ id: "child-deferred-index", status: "active", parentTaskId: "parent-deferred-index" }) |
|
0 commit comments