|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import * as os from "os"; |
| 5 | + |
| 6 | +describe("Crash Recovery", () => { |
| 7 | + describe("atomic file writes", () => { |
| 8 | + let tempDir: string; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "crash-test-")); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should leave no .tmp files after successful write via atomicWriteSync pattern", () => { |
| 19 | + const targetPath = path.join(tempDir, "test-file.json"); |
| 20 | + const tempPath = `${targetPath}.tmp`; |
| 21 | + const data = JSON.stringify({ test: "data" }); |
| 22 | + |
| 23 | + fs.writeFileSync(tempPath, data); |
| 24 | + fs.renameSync(tempPath, targetPath); |
| 25 | + |
| 26 | + expect(fs.existsSync(targetPath)).toBe(true); |
| 27 | + expect(fs.existsSync(tempPath)).toBe(false); |
| 28 | + expect(JSON.parse(fs.readFileSync(targetPath, "utf-8"))).toEqual({ test: "data" }); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should handle rename of non-existent temp file gracefully", () => { |
| 32 | + const targetPath = path.join(tempDir, "test-file.json"); |
| 33 | + const tempPath = `${targetPath}.tmp`; |
| 34 | + |
| 35 | + expect(() => fs.renameSync(tempPath, targetPath)).toThrow(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("indexing lock file", () => { |
| 40 | + let tempDir: string; |
| 41 | + let indexPath: string; |
| 42 | + let lockPath: string; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "lock-test-")); |
| 46 | + indexPath = path.join(tempDir, ".opencode", "index"); |
| 47 | + lockPath = path.join(indexPath, "indexing.lock"); |
| 48 | + fs.mkdirSync(indexPath, { recursive: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should detect when lock file exists", () => { |
| 56 | + expect(fs.existsSync(lockPath)).toBe(false); |
| 57 | + |
| 58 | + fs.writeFileSync(lockPath, JSON.stringify({ startedAt: new Date().toISOString(), pid: process.pid })); |
| 59 | + |
| 60 | + expect(fs.existsSync(lockPath)).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should parse lock file contents correctly", () => { |
| 64 | + const lockData = { startedAt: "2025-01-19T12:00:00.000Z", pid: 12345 }; |
| 65 | + fs.writeFileSync(lockPath, JSON.stringify(lockData)); |
| 66 | + |
| 67 | + const parsed = JSON.parse(fs.readFileSync(lockPath, "utf-8")); |
| 68 | + expect(parsed.startedAt).toBe("2025-01-19T12:00:00.000Z"); |
| 69 | + expect(parsed.pid).toBe(12345); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should remove lock file on successful completion", () => { |
| 73 | + fs.writeFileSync(lockPath, JSON.stringify({ startedAt: new Date().toISOString(), pid: process.pid })); |
| 74 | + expect(fs.existsSync(lockPath)).toBe(true); |
| 75 | + |
| 76 | + fs.unlinkSync(lockPath); |
| 77 | + |
| 78 | + expect(fs.existsSync(lockPath)).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should handle missing lock file in cleanup gracefully", () => { |
| 82 | + expect(fs.existsSync(lockPath)).toBe(false); |
| 83 | + |
| 84 | + if (fs.existsSync(lockPath)) { |
| 85 | + fs.unlinkSync(lockPath); |
| 86 | + } |
| 87 | + |
| 88 | + expect(fs.existsSync(lockPath)).toBe(false); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("recovery behavior", () => { |
| 93 | + let tempDir: string; |
| 94 | + let indexPath: string; |
| 95 | + let lockPath: string; |
| 96 | + let fileHashCachePath: string; |
| 97 | + |
| 98 | + beforeEach(() => { |
| 99 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "recovery-test-")); |
| 100 | + indexPath = path.join(tempDir, ".opencode", "index"); |
| 101 | + lockPath = path.join(indexPath, "indexing.lock"); |
| 102 | + fileHashCachePath = path.join(indexPath, "file-hashes.json"); |
| 103 | + fs.mkdirSync(indexPath, { recursive: true }); |
| 104 | + }); |
| 105 | + |
| 106 | + afterEach(() => { |
| 107 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should clear file hash cache when lock file is present (simulating crash recovery)", () => { |
| 111 | + fs.writeFileSync(fileHashCachePath, JSON.stringify({ "file1.ts": "hash1", "file2.ts": "hash2" })); |
| 112 | + fs.writeFileSync(lockPath, JSON.stringify({ startedAt: new Date().toISOString(), pid: process.pid })); |
| 113 | + |
| 114 | + expect(fs.existsSync(fileHashCachePath)).toBe(true); |
| 115 | + expect(fs.existsSync(lockPath)).toBe(true); |
| 116 | + |
| 117 | + if (fs.existsSync(lockPath)) { |
| 118 | + fs.unlinkSync(fileHashCachePath); |
| 119 | + fs.unlinkSync(lockPath); |
| 120 | + } |
| 121 | + |
| 122 | + expect(fs.existsSync(fileHashCachePath)).toBe(false); |
| 123 | + expect(fs.existsSync(lockPath)).toBe(false); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should not clear hash cache when no lock file exists", () => { |
| 127 | + fs.writeFileSync(fileHashCachePath, JSON.stringify({ "file1.ts": "hash1" })); |
| 128 | + |
| 129 | + expect(fs.existsSync(lockPath)).toBe(false); |
| 130 | + expect(fs.existsSync(fileHashCachePath)).toBe(true); |
| 131 | + |
| 132 | + const content = JSON.parse(fs.readFileSync(fileHashCachePath, "utf-8")); |
| 133 | + expect(content).toEqual({ "file1.ts": "hash1" }); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments