refactor(matrix,tests): extract helpers and inject send-queue timing

This commit is contained in:
Peter Steinberger
2026-02-24 23:37:45 +00:00
parent a2529c25ff
commit 58309fd8d9
5 changed files with 108 additions and 46 deletions

View File

@@ -14,6 +14,22 @@ import type { SandboxContext } from "./types.js";
const mockedExecDockerRaw = vi.mocked(execDockerRaw);
function getDockerScript(args: string[]): string {
return String(args[5] ?? "");
}
function getDockerPathArg(args: string[]): string {
return String(args.at(-1) ?? "");
}
function getScriptsFromCalls(): string[] {
return mockedExecDockerRaw.mock.calls.map(([args]) => getDockerScript(args));
}
function findCallByScriptFragment(fragment: string) {
return mockedExecDockerRaw.mock.calls.find(([args]) => getDockerScript(args).includes(fragment));
}
function createSandbox(overrides?: Partial<SandboxContext>): SandboxContext {
return createSandboxTestContext({
overrides: {
@@ -31,7 +47,7 @@ describe("sandbox fs bridge shell compatibility", () => {
beforeEach(() => {
mockedExecDockerRaw.mockClear();
mockedExecDockerRaw.mockImplementation(async (args) => {
const script = args[5] ?? "";
const script = getDockerScript(args);
if (script.includes('readlink -f -- "$cursor"')) {
return {
stdout: Buffer.from(`${String(args.at(-2) ?? "")}\n`),
@@ -73,7 +89,7 @@ describe("sandbox fs bridge shell compatibility", () => {
expect(mockedExecDockerRaw).toHaveBeenCalled();
const scripts = mockedExecDockerRaw.mock.calls.map(([args]) => args[5] ?? "");
const scripts = getScriptsFromCalls();
const executables = mockedExecDockerRaw.mock.calls.map(([args]) => args[3] ?? "");
expect(executables.every((shell) => shell === "sh")).toBe(true);
@@ -86,7 +102,7 @@ describe("sandbox fs bridge shell compatibility", () => {
await bridge.readFile({ filePath: "a.txt" });
const scripts = mockedExecDockerRaw.mock.calls.map(([args]) => args[5] ?? "");
const scripts = getScriptsFromCalls();
const canonicalScript = scripts.find((script) => script.includes("allow_final"));
expect(canonicalScript).toBeDefined();
// "; " joining can create "do; cmd", which is invalid in POSIX sh.
@@ -101,11 +117,9 @@ describe("sandbox fs bridge shell compatibility", () => {
await bridge.readFile({ filePath: inboundPath });
const readCall = mockedExecDockerRaw.mock.calls.find(([args]) =>
String(args[5] ?? "").includes('cat -- "$1"'),
);
const readCall = findCallByScriptFragment('cat -- "$1"');
expect(readCall).toBeDefined();
const readPath = String(readCall?.[0].at(-1) ?? "");
const readPath = readCall ? getDockerPathArg(readCall[0]) : "";
expect(readPath).toContain("file_1095---");
});
@@ -124,7 +138,7 @@ describe("sandbox fs bridge shell compatibility", () => {
expect(args).toEqual(
expect.arrayContaining(["moltbot-sbx-test", "sh", "-c", 'set -eu; cat -- "$1"']),
);
expect(args.at(-1)).toBe("/workspace-two/README.md");
expect(getDockerPathArg(args)).toBe("/workspace-two/README.md");
});
it("blocks writes into read-only bind mounts", async () => {
@@ -166,7 +180,7 @@ describe("sandbox fs bridge shell compatibility", () => {
it("rejects container-canonicalized paths outside allowed mounts", async () => {
mockedExecDockerRaw.mockImplementation(async (args) => {
const script = args[5] ?? "";
const script = getDockerScript(args);
if (script.includes('readlink -f -- "$cursor"')) {
return {
stdout: Buffer.from("/etc/passwd\n"),
@@ -190,7 +204,7 @@ describe("sandbox fs bridge shell compatibility", () => {
const bridge = createSandboxFsBridge({ sandbox: createSandbox() });
await expect(bridge.readFile({ filePath: "a.txt" })).rejects.toThrow(/escapes allowed mounts/i);
const scripts = mockedExecDockerRaw.mock.calls.map(([args]) => args[5] ?? "");
const scripts = getScriptsFromCalls();
expect(scripts.some((script) => script.includes('cat -- "$1"'))).toBe(false);
});
});

View File

@@ -35,7 +35,10 @@ type DispatchInboundParams = {
text?: string;
isReasoning?: boolean;
}) => boolean | Promise<boolean>;
sendFinalReply: (payload: { text?: string }) => boolean | Promise<boolean>;
sendFinalReply: (payload: {
text?: string;
isReasoning?: boolean;
}) => boolean | Promise<boolean>;
};
replyOptions?: {
onReasoningStream?: () => Promise<void> | void;
@@ -449,7 +452,7 @@ describe("processDiscordMessage draft streaming", () => {
await params?.dispatcher.sendFinalReply({
text: "Reasoning:\nthis should stay internal",
isReasoning: true,
} as never);
});
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
});