-
Notifications
You must be signed in to change notification settings - Fork 31
test(lib): add comprehensive unit tests for resolve-subagent-path (#328) #628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,25 @@ | |
| import { mkdtemp, mkdir, rm, writeFile } from "fs/promises"; | ||
| import { tmpdir } from "os"; | ||
| import { dirname, join, relative } from "path"; | ||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { resolveSubagentPath } from "@/lib/resolve-subagent-path"; | ||
|
|
||
| let mockAccessError: NodeJS.ErrnoException | null = null; | ||
|
|
||
| vi.mock("fs/promises", async () => { | ||
| const actual = await vi.importActual<typeof import("fs/promises")>("fs/promises"); | ||
| return { | ||
| ...actual, | ||
| access: vi.fn(async (path, mode) => { | ||
| if (mockAccessError) { | ||
| throw mockAccessError; | ||
| } | ||
| return actual.access(path, mode); | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| describe("resolveSubagentPath", () => { | ||
| let fixtureRoot: string; | ||
| let projectsPath: string; | ||
|
|
@@ -15,12 +30,14 @@ describe("resolveSubagentPath", () => { | |
| const agentId = "abc123"; | ||
|
|
||
| beforeEach(async () => { | ||
| mockAccessError = null; | ||
| fixtureRoot = await mkdtemp(join(tmpdir(), "failproofai-subagents-")); | ||
| projectsPath = join(fixtureRoot, "projects"); | ||
| await mkdir(join(projectsPath, projectName, sessionId, "subagents"), { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| mockAccessError = null; | ||
| await rm(fixtureRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
|
|
@@ -74,4 +91,21 @@ describe("resolveSubagentPath", () => { | |
|
|
||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, traversalAgentId)).resolves.toBeNull(); | ||
| }); | ||
|
|
||
| it("prevents path traversal when projectName attempts to escape projectsPath", async () => { | ||
| const traversalProject = "../../outside"; | ||
| const escapedCandidate = join(projectsPath, traversalProject, `agent-${agentId}.jsonl`); | ||
| await touch(escapedCandidate); | ||
|
|
||
| await expect(resolveSubagentPath(projectsPath, traversalProject, sessionId, agentId)).resolves.toBeNull(); | ||
| }); | ||
|
Comment on lines
+95
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Keep traversal fixtures inside the per-test temporary directory.
🤖 Prompt for AI Agents |
||
|
|
||
| it("stops searching and returns null when an unexpected error occurs (e.g., EACCES)", async () => { | ||
| const err = new Error("Permission denied") as NodeJS.ErrnoException; | ||
| err.code = "EACCES"; | ||
| mockAccessError = err; | ||
|
|
||
| const result = await resolveSubagentPath(projectsPath, projectName, sessionId, agentId); | ||
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: FailproofAI/failproofai
Length of output: 409
🏁 Script executed:
Repository: FailproofAI/failproofai
Length of output: 6744
🏁 Script executed:
Repository: FailproofAI/failproofai
Length of output: 48531
Assert that unexpected errors stop candidate search.
The EACCES expectation only checks
result === null; it would still pass if the loop checked the remaining candidates. Clear the mock call history inbeforeEach, then assert thataccesswas called exactly once with the project-level candidate in the error case.Also applies to: 103-110
🤖 Prompt for AI Agents