|
1 | 1 | import { describe, expect, it } from "bun:test" |
2 | 2 | import { homedir } from "node:os" |
3 | 3 | import { join } from "node:path" |
4 | | -// @ts-expect-error - paths.mjs is a JavaScript module without type declarations |
5 | | -import { AGENTS_TARGET_DIR, getAgentsSourceDir, getPackageRoot } from "../src/paths.mjs" |
| 4 | +import { |
| 5 | + AGENTS_TARGET_DIR, |
| 6 | + getAgentsSourceDir, |
| 7 | + getErrorMessage, |
| 8 | + getPackageRoot, |
| 9 | +} from "../src/paths.mjs" |
6 | 10 |
|
7 | 11 | describe("paths.mjs exports", () => { |
8 | 12 | describe("getPackageRoot", () => { |
@@ -94,4 +98,72 @@ describe("paths.mjs exports", () => { |
94 | 98 | expect(AGENTS_TARGET_DIR.startsWith("/") || /^[A-Z]:/i.test(AGENTS_TARGET_DIR)).toBe(true) |
95 | 99 | }) |
96 | 100 | }) |
| 101 | + |
| 102 | + describe("getErrorMessage", () => { |
| 103 | + const testFile = "test-agent.md" |
| 104 | + const testTargetPath = "/home/user/.config/opencode/agents/test-agent.md" |
| 105 | + |
| 106 | + it("should return permission message for EACCES error", () => { |
| 107 | + const error = Object.assign(new Error("EACCES"), { code: "EACCES" }) |
| 108 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 109 | + expect(result).toBe( |
| 110 | + "Permission denied. Check write permissions for /home/user/.config/opencode/agents", |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + it("should return disk full message for ENOSPC error", () => { |
| 115 | + const error = Object.assign(new Error("ENOSPC"), { code: "ENOSPC" }) |
| 116 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 117 | + expect(result).toBe("Disk full. Free up space and try again") |
| 118 | + }) |
| 119 | + |
| 120 | + it("should return source file not found message for ENOENT error", () => { |
| 121 | + const error = Object.assign(new Error("ENOENT"), { code: "ENOENT" }) |
| 122 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 123 | + expect(result).toBe(`Source file not found: ${testFile}`) |
| 124 | + }) |
| 125 | + |
| 126 | + it("should return read-only filesystem message for EROFS error", () => { |
| 127 | + const error = Object.assign(new Error("EROFS"), { code: "EROFS" }) |
| 128 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 129 | + expect(result).toBe("Read-only file system. Cannot write to target directory") |
| 130 | + }) |
| 131 | + |
| 132 | + it("should return too many open files message for EMFILE error", () => { |
| 133 | + const error = Object.assign(new Error("EMFILE"), { code: "EMFILE" }) |
| 134 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 135 | + expect(result).toBe("Too many open files. Close some applications and try again") |
| 136 | + }) |
| 137 | + |
| 138 | + it("should return too many open files message for ENFILE error", () => { |
| 139 | + const error = Object.assign(new Error("ENFILE"), { code: "ENFILE" }) |
| 140 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 141 | + expect(result).toBe("Too many open files. Close some applications and try again") |
| 142 | + }) |
| 143 | + |
| 144 | + it("should return error message for unknown error codes", () => { |
| 145 | + const error = Object.assign(new Error("Something went wrong"), { code: "UNKNOWN" }) |
| 146 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 147 | + expect(result).toBe("Something went wrong") |
| 148 | + }) |
| 149 | + |
| 150 | + it("should return 'Unknown error' when error has no message or known code", () => { |
| 151 | + const error = Object.assign(new Error(""), { code: "UNKNOWN" }) |
| 152 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 153 | + expect(result).toBe("Unknown error") |
| 154 | + }) |
| 155 | + |
| 156 | + it("should handle error without code property", () => { |
| 157 | + const error = new Error("Generic error") |
| 158 | + const result = getErrorMessage(error, testFile, testTargetPath) |
| 159 | + expect(result).toBe("Generic error") |
| 160 | + }) |
| 161 | + |
| 162 | + it("should use dirname of targetPath for EACCES message", () => { |
| 163 | + const error = Object.assign(new Error("EACCES"), { code: "EACCES" }) |
| 164 | + const deepPath = "/very/deep/nested/path/file.md" |
| 165 | + const result = getErrorMessage(error, "file.md", deepPath) |
| 166 | + expect(result).toBe("Permission denied. Check write permissions for /very/deep/nested/path") |
| 167 | + }) |
| 168 | + }) |
97 | 169 | }) |
0 commit comments