Skip to content

Commit c5a2ac7

Browse files
committed
test: add unit tests for paths.mjs functions and constants
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent 9ac58fe commit c5a2ac7

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

tests/paths.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { describe, expect, it } from "bun:test"
2+
import { homedir } from "node:os"
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"
6+
7+
describe("paths.mjs exports", () => {
8+
describe("getPackageRoot", () => {
9+
it("should return the directory containing the module", () => {
10+
// When called with import.meta.url from this test file,
11+
// it should return the tests/ directory
12+
const result = getPackageRoot(import.meta.url)
13+
expect(result).toContain("tests")
14+
expect(result.endsWith("tests")).toBe(true)
15+
})
16+
17+
it("should resolve to an absolute path", () => {
18+
const result = getPackageRoot(import.meta.url)
19+
// Absolute paths start with / on Unix or drive letter on Windows
20+
expect(result.startsWith("/") || /^[A-Z]:/i.test(result)).toBe(true)
21+
})
22+
23+
it("should handle file:// URLs correctly", () => {
24+
// Simulate a file URL similar to what import.meta.url provides
25+
const testUrl = `file://${join(process.cwd(), "src", "test-module.mjs")}`
26+
const result = getPackageRoot(testUrl)
27+
expect(result).toContain("src")
28+
expect(result.endsWith("src")).toBe(true)
29+
})
30+
31+
it("should resolve consistently for the same input", () => {
32+
const result1 = getPackageRoot(import.meta.url)
33+
const result2 = getPackageRoot(import.meta.url)
34+
expect(result1).toBe(result2)
35+
})
36+
})
37+
38+
describe("getAgentsSourceDir", () => {
39+
it("should join the package root with 'agents' directory", () => {
40+
const packageRoot = "/some/package/root"
41+
const result = getAgentsSourceDir(packageRoot)
42+
expect(result).toBe(join(packageRoot, "agents"))
43+
})
44+
45+
it("should return correct path for actual package root", () => {
46+
const actualRoot = join(import.meta.dirname, "..")
47+
const result = getAgentsSourceDir(actualRoot)
48+
expect(result).toBe(join(actualRoot, "agents"))
49+
expect(result.endsWith("agents")).toBe(true)
50+
})
51+
52+
it("should handle paths with trailing slash", () => {
53+
const packageRoot = "/some/package/root/"
54+
const result = getAgentsSourceDir(packageRoot)
55+
// join() normalizes paths, so trailing slash is handled
56+
expect(result).toContain("agents")
57+
})
58+
59+
it("should handle relative paths", () => {
60+
const packageRoot = "."
61+
const result = getAgentsSourceDir(packageRoot)
62+
expect(result).toBe("agents")
63+
})
64+
65+
it("should handle paths with special characters", () => {
66+
const packageRoot = "/path/with spaces/package"
67+
const result = getAgentsSourceDir(packageRoot)
68+
expect(result).toBe(join(packageRoot, "agents"))
69+
})
70+
})
71+
72+
describe("AGENTS_TARGET_DIR", () => {
73+
it("should resolve to ~/.config/opencode/agents/", () => {
74+
const expected = join(homedir(), ".config", "opencode", "agents")
75+
expect(AGENTS_TARGET_DIR).toBe(expected)
76+
})
77+
78+
it("should start with the user home directory", () => {
79+
expect(AGENTS_TARGET_DIR.startsWith(homedir())).toBe(true)
80+
})
81+
82+
it("should contain the expected directory structure", () => {
83+
expect(AGENTS_TARGET_DIR).toContain(".config")
84+
expect(AGENTS_TARGET_DIR).toContain("opencode")
85+
expect(AGENTS_TARGET_DIR).toContain("agents")
86+
})
87+
88+
it("should end with 'agents' directory", () => {
89+
expect(AGENTS_TARGET_DIR.endsWith("agents")).toBe(true)
90+
})
91+
92+
it("should be an absolute path", () => {
93+
// Absolute paths start with / on Unix or drive letter on Windows
94+
expect(AGENTS_TARGET_DIR.startsWith("/") || /^[A-Z]:/i.test(AGENTS_TARGET_DIR)).toBe(true)
95+
})
96+
})
97+
})

0 commit comments

Comments
 (0)