Skip to content

Commit 236f2ec

Browse files
committed
test: add comprehensive unit tests for getErrorMessage function
Signed-off-by: leocavalcante <[email protected]>
1 parent e4b7934 commit 236f2ec

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/paths.d.mts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Type declarations for paths.mjs
3+
*/
4+
5+
/**
6+
* Get the package root directory from a module's import.meta.url
7+
*/
8+
export function getPackageRoot(importMetaUrl: string): string
9+
10+
/**
11+
* Get the source directory containing agent markdown files.
12+
*/
13+
export function getAgentsSourceDir(packageRoot: string): string
14+
15+
/**
16+
* The target directory where agents are installed.
17+
*/
18+
export declare const AGENTS_TARGET_DIR: string
19+
20+
/**
21+
* Returns a user-friendly error message based on the error code.
22+
*/
23+
export function getErrorMessage(
24+
error: Error & { code?: string },
25+
file: string,
26+
targetPath: string,
27+
): string

tests/paths.test.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { describe, expect, it } from "bun:test"
22
import { homedir } from "node:os"
33
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"
610

711
describe("paths.mjs exports", () => {
812
describe("getPackageRoot", () => {
@@ -94,4 +98,72 @@ describe("paths.mjs exports", () => {
9498
expect(AGENTS_TARGET_DIR.startsWith("/") || /^[A-Z]:/i.test(AGENTS_TARGET_DIR)).toBe(true)
9599
})
96100
})
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+
})
97169
})

0 commit comments

Comments
 (0)