-
Notifications
You must be signed in to change notification settings - Fork 11
test: cover resolveSubagentPath candidates #331
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
Open
looooown2006
wants to merge
2
commits into
exospherehost:main
Choose a base branch
from
looooown2006:test/resolve-subagent-path-328
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // @vitest-environment node | ||
| 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 { resolveSubagentPath } from "@/lib/resolve-subagent-path"; | ||
|
|
||
| describe("resolveSubagentPath", () => { | ||
| let fixtureRoot: string; | ||
| let projectsPath: string; | ||
|
|
||
| const projectName = "project"; | ||
| const sessionId = "session"; | ||
| const agentId = "abc123"; | ||
|
|
||
| beforeEach(async () => { | ||
| fixtureRoot = await mkdtemp(join(tmpdir(), "failproofai-subagents-")); | ||
| projectsPath = join(fixtureRoot, "projects"); | ||
| await mkdir(join(projectsPath, projectName, sessionId, "subagents"), { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await rm(fixtureRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| async function touch(path: string): Promise<void> { | ||
| await mkdir(dirname(path), { recursive: true }); | ||
| await writeFile(path, ""); | ||
| } | ||
|
|
||
| it("returns candidate 1 when the project-level agent log exists", async () => { | ||
| const candidate = join(projectsPath, projectName, `agent-${agentId}.jsonl`); | ||
| await touch(candidate); | ||
|
|
||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, agentId)).resolves.toBe(candidate); | ||
| }); | ||
|
|
||
| it("returns candidate 2 when candidate 1 is missing", async () => { | ||
| const candidate = join(projectsPath, projectName, sessionId, `agent-${agentId}.jsonl`); | ||
| await touch(candidate); | ||
|
|
||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, agentId)).resolves.toBe(candidate); | ||
| }); | ||
|
|
||
| it("returns candidate 3 when candidates 1 and 2 are missing", async () => { | ||
| const candidate = join(projectsPath, projectName, sessionId, "subagents", `agent-${agentId}.jsonl`); | ||
| await touch(candidate); | ||
|
|
||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, agentId)).resolves.toBe(candidate); | ||
| }); | ||
|
|
||
| it("returns candidate 1 when all candidate paths exist", async () => { | ||
| const candidate1 = join(projectsPath, projectName, `agent-${agentId}.jsonl`); | ||
| const candidate2 = join(projectsPath, projectName, sessionId, `agent-${agentId}.jsonl`); | ||
| const candidate3 = join(projectsPath, projectName, sessionId, "subagents", `agent-${agentId}.jsonl`); | ||
| await touch(candidate3); | ||
| await touch(candidate2); | ||
| await touch(candidate1); | ||
|
|
||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, agentId)).resolves.toBe(candidate1); | ||
| }); | ||
|
|
||
| it("returns null when none of the candidate paths exist", async () => { | ||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, agentId)).resolves.toBeNull(); | ||
| }); | ||
|
|
||
| it("does not resolve an existing candidate outside the projects path", async () => { | ||
| const traversalAgentId = "../../../../escape"; | ||
| const escapedCandidate = join(projectsPath, projectName, `agent-${traversalAgentId}.jsonl`); | ||
| expect(relative(projectsPath, escapedCandidate).startsWith("..")).toBe(true); | ||
| expect(relative(fixtureRoot, escapedCandidate).startsWith("..")).toBe(false); | ||
| await touch(escapedCandidate); | ||
|
|
||
| await expect(resolveSubagentPath(projectsPath, projectName, sessionId, traversalAgentId)).resolves.toBeNull(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Test leak: escaped file not cleaned up.
The path traversal test creates a file outside
fixtureRoot(at/tmp/escape.jsonlor similar), but theafterEachhook only removesfixtureRoot. This leaves the escaped file behind, polluting the test environment.The calculation:
join(projectsPath, projectName, "agent-../../../../escape.jsonl")projectsPath = "/tmp/failproofai-subagents-XXXXXX/projects"/tmp/escape.jsonl(outside the fixture)🧹 Proposed fix: deepen fixture structure to contain the escaped path
beforeEach(async () => { fixtureRoot = await mkdtemp(join(tmpdir(), "failproofai-subagents-")); - projectsPath = join(fixtureRoot, "projects"); + // Use a deeper structure so that even with traversal, we stay within fixtureRoot + projectsPath = join(fixtureRoot, "a", "b", "c", "d", "projects"); await mkdir(join(projectsPath, projectName, sessionId, "subagents"), { recursive: true }); });With this change:
escapedCandidatenormalizes to/tmp/failproofai-subagents-XXXXXX/a/b/c/escape.jsonlprojectsPath(satisfies line 60's assertion)fixtureRoot(properly cleaned up)🤖 Prompt for AI Agents