|
| 1 | +import { afterEach, describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as os from "os"; |
| 5 | +import * as path from "path"; |
| 6 | +import { writeFileAtomic } from "../common/private-storage"; |
| 7 | +import { classifyMcpError } from "../mcp/mcp-manager"; |
| 8 | +import { sweepOldBackgroundLogs } from "../tools/bash-handler"; |
| 9 | + |
| 10 | +const tempDirs: string[] = []; |
| 11 | + |
| 12 | +function tempDir(prefix: string): string { |
| 13 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 14 | + tempDirs.push(dir); |
| 15 | + return dir; |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + for (const dir of tempDirs.splice(0)) { |
| 20 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 21 | + } |
| 22 | +}); |
| 23 | + |
| 24 | +describe("writeFileAtomic", () => { |
| 25 | + it("replaces the target and leaves no .tmp behind", () => { |
| 26 | + const dir = tempDir("dc-atomic-"); |
| 27 | + const target = path.join(dir, "index.json"); |
| 28 | + fs.writeFileSync(target, "old", "utf8"); |
| 29 | + writeFileAtomic(target, "new"); |
| 30 | + assert.equal(fs.readFileSync(target, "utf8"), "new"); |
| 31 | + assert.ok(!fs.existsSync(`${target}.tmp`), "tmp file must be cleaned up"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("writes 0600 on POSIX", () => { |
| 35 | + if (process.platform === "win32") { |
| 36 | + return; |
| 37 | + } |
| 38 | + const dir = tempDir("dc-atomic-mode-"); |
| 39 | + const target = path.join(dir, "s.json"); |
| 40 | + writeFileAtomic(target, "{}"); |
| 41 | + const mode = fs.statSync(target).mode & 0o777; |
| 42 | + assert.equal(mode, 0o600); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("classifyMcpError", () => { |
| 47 | + it("classifies timeouts", () => { |
| 48 | + assert.equal(classifyMcpError("Timed out after 60000ms"), "timeout"); |
| 49 | + assert.equal(classifyMcpError("deadline exceeded"), "timeout"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("classifies connection failures", () => { |
| 53 | + assert.equal(classifyMcpError("Failed to start MCP server: ENOENT"), "connection"); |
| 54 | + assert.equal(classifyMcpError("connection refused"), "connection"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("classifies auth failures", () => { |
| 58 | + assert.equal(classifyMcpError("HTTP 401 unauthorized"), "auth"); |
| 59 | + assert.equal(classifyMcpError("invalid api key"), "auth"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("classifies protocol and busy", () => { |
| 63 | + assert.equal(classifyMcpError("invalid json payload"), "protocol"); |
| 64 | + assert.equal(classifyMcpError("server busy, retry later"), "busy"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("returns unknown otherwise", () => { |
| 68 | + assert.equal(classifyMcpError("something unexpected"), "unknown"); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("sweepOldBackgroundLogs", () => { |
| 73 | + it("deletes only expired .log files", () => { |
| 74 | + const dir = tempDir("dc-sweep-"); |
| 75 | + |
| 76 | + const oldLog = path.join(dir, "old.log"); |
| 77 | + const freshLog = path.join(dir, "fresh.log"); |
| 78 | + const otherFile = path.join(dir, "keep.txt"); |
| 79 | + fs.writeFileSync(oldLog, "old"); |
| 80 | + fs.writeFileSync(freshLog, "fresh"); |
| 81 | + fs.writeFileSync(otherFile, "keep"); |
| 82 | + |
| 83 | + // Simulate old file: 8 days ago; fresh: now. |
| 84 | + const now = Date.now(); |
| 85 | + const eightDaysAgo = now - 8 * 24 * 60 * 60 * 1000; |
| 86 | + fs.utimesSync(oldLog, new Date(eightDaysAgo), new Date(eightDaysAgo)); |
| 87 | + |
| 88 | + sweepOldBackgroundLogs(now, dir); |
| 89 | + |
| 90 | + assert.ok(!fs.existsSync(oldLog), "expired .log must be deleted"); |
| 91 | + assert.ok(fs.existsSync(freshLog), "fresh .log must be kept"); |
| 92 | + assert.ok(fs.existsSync(otherFile), "non-log file must be kept"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("is a no-op when the directory does not exist", () => { |
| 96 | + const missing = path.join(os.tmpdir(), "dc-no-such-sweep-dir"); |
| 97 | + assert.doesNotThrow(() => sweepOldBackgroundLogs(Date.now(), missing)); |
| 98 | + }); |
| 99 | +}); |
0 commit comments