Files
claw3d/tests/unit/agentStateExecutor.test.ts
T
Luke The Dev c3556d2daa fix(security): close remaining path validation gaps (#77)
Harden the SSH agent-state and skill-removal paths to match the local security model, and avoid rejecting valid local workspace skill removals.

Made-with: Cursor

Co-authored-by: iamlukethedev <lucas.guilherme@smartwayslfl.com>
2026-03-27 22:21:41 -05:00

63 lines
2.0 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { runSshJson } from "@/lib/ssh/gateway-host";
import {
restoreAgentStateOverSsh,
trashAgentStateOverSsh,
} from "@/lib/ssh/agent-state";
vi.mock("@/lib/ssh/gateway-host", () => ({
runSshJson: vi.fn(),
}));
describe("agent state ssh executor", () => {
const mockedRunSshJson = vi.mocked(runSshJson);
beforeEach(() => {
mockedRunSshJson.mockReset();
});
it("trashes agent state via ssh", () => {
mockedRunSshJson.mockReturnValueOnce({ trashDir: "/tmp/trash", moved: [] });
const result = trashAgentStateOverSsh({ sshTarget: "me@host", agentId: "my-agent" });
expect(result).toEqual({ trashDir: "/tmp/trash", moved: [] });
expect(runSshJson).toHaveBeenCalledTimes(1);
expect(runSshJson).toHaveBeenCalledWith(
expect.objectContaining({
sshTarget: "me@host",
argv: ["bash", "-s", "--", "my-agent"],
label: "trash agent state (my-agent)",
input: expect.stringContaining('python3 - "$1"'),
})
);
const call = mockedRunSshJson.mock.calls[0]?.[0];
expect(call?.input).toContain("workspace-{agent_id}");
});
it("restores agent state via ssh", () => {
mockedRunSshJson.mockReturnValueOnce({ restored: [] });
const result = restoreAgentStateOverSsh({
sshTarget: "me@host",
agentId: "my-agent",
trashDir: "/tmp/trash",
});
expect(result).toEqual({ restored: [] });
expect(runSshJson).toHaveBeenCalledTimes(1);
expect(runSshJson).toHaveBeenCalledWith(
expect.objectContaining({
sshTarget: "me@host",
argv: ["bash", "-s", "--", "my-agent", "/tmp/trash"],
label: "restore agent state (my-agent)",
input: expect.stringContaining('python3 - "$1" "$2"'),
})
);
const call = mockedRunSshJson.mock.calls[0]?.[0];
expect(call?.input).toContain('trash_root = base / "trash" / "studio-delete-agent"');
expect(call?.input).toContain('trashDir is not under {trash_root}');
});
});