test(FN-4643): complete Step 3 — cover native streaming behavior
Fusion-Task-Id: FN-4643 Fusion-Task-Lineage: edcfb4a1-f714-47e8-a919-50b35b40e6af
This commit is contained in:
@@ -50,9 +50,10 @@ describe("BubblewrapBackend", () => {
|
||||
};
|
||||
|
||||
const nativeStub: SandboxBackend = {
|
||||
capabilities: () => ({ id: "native", supportsNetworkPolicy: false, supportsFilesystemPolicy: false, platform: "any" }),
|
||||
capabilities: () => ({ id: "native", supportsNetworkPolicy: false, supportsFilesystemPolicy: false, supportsStreaming: true, platform: "any" }),
|
||||
prepare: vi.fn(async () => undefined),
|
||||
run: vi.fn(async () => runResult),
|
||||
runStreaming: vi.fn(async () => ({ outcome: "success", stdout: "", stderr: "", bufferOverflow: false })),
|
||||
dispose: vi.fn(async () => undefined),
|
||||
};
|
||||
|
||||
|
||||
@@ -35,9 +35,10 @@ vi.mock("../../sandbox/native.js", () => ({
|
||||
NativeSandboxBackend: class {
|
||||
prepare = nativePrepareMock;
|
||||
run = nativeRunMock;
|
||||
runStreaming = vi.fn();
|
||||
dispose = nativeDisposeMock;
|
||||
capabilities() {
|
||||
return { id: "native", supportsNetworkPolicy: false, supportsFilesystemPolicy: false, platform: "any" };
|
||||
return { id: "native", supportsNetworkPolicy: false, supportsFilesystemPolicy: false, supportsStreaming: true, platform: "any" };
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
163
packages/engine/src/sandbox/__tests__/native-streaming.test.ts
Normal file
163
packages/engine/src/sandbox/__tests__/native-streaming.test.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { NativeSandboxBackend } from "../native.js";
|
||||
|
||||
const spawnMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("node:child_process", async () => {
|
||||
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
|
||||
return {
|
||||
...actual,
|
||||
spawn: spawnMock,
|
||||
};
|
||||
});
|
||||
|
||||
class FakeChild extends EventEmitter {
|
||||
pid?: number;
|
||||
stdout = new PassThrough();
|
||||
stderr = new PassThrough();
|
||||
kill = vi.fn();
|
||||
|
||||
constructor(pid = 1234) {
|
||||
super();
|
||||
this.pid = pid;
|
||||
}
|
||||
}
|
||||
|
||||
describe("NativeSandboxBackend.runStreaming", () => {
|
||||
const processKillSpy = vi.spyOn(process, "kill");
|
||||
let platformSpy: ReturnType<typeof vi.spyOn<typeof process, "platform">>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.clearAllMocks();
|
||||
platformSpy = vi.spyOn(process, "platform", "get");
|
||||
platformSpy.mockReturnValue("linux");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
platformSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("returns success result", async () => {
|
||||
const child = new FakeChild();
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
const promise = backend.runStreaming("echo ok", { cwd: "/tmp", timeout: 1000, maxBuffer: 1024 });
|
||||
child.stdout.write("ok");
|
||||
child.stderr.write("warn");
|
||||
child.emit("close", 0, null);
|
||||
|
||||
await expect(promise).resolves.toEqual({ outcome: "success", stdout: "ok", stderr: "warn", bufferOverflow: false });
|
||||
});
|
||||
|
||||
it("returns non-zero-exit result", async () => {
|
||||
const child = new FakeChild();
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
const promise = backend.runStreaming("exit 2", { cwd: "/tmp", timeout: 1000, maxBuffer: 1024 });
|
||||
child.emit("close", 2, null);
|
||||
|
||||
await expect(promise).resolves.toMatchObject({ outcome: "non-zero-exit", exitCode: 2, signal: null });
|
||||
});
|
||||
|
||||
it("returns timeout result and escalates SIGTERM then SIGKILL", async () => {
|
||||
const child = new FakeChild(4321);
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
const promise = backend.runStreaming("sleep", { cwd: "/tmp", timeout: 100, maxBuffer: 1024 });
|
||||
await vi.advanceTimersByTimeAsync(100);
|
||||
expect(processKillSpy).toHaveBeenCalledWith(-4321, "SIGTERM");
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
expect(processKillSpy).toHaveBeenCalledWith(-4321, "SIGKILL");
|
||||
child.emit("close", null, "SIGKILL");
|
||||
|
||||
await expect(promise).resolves.toMatchObject({ outcome: "timeout", timeoutMs: 100 });
|
||||
});
|
||||
|
||||
it("returns pre-start abort and does not spawn", async () => {
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
await expect(backend.runStreaming("echo", { cwd: "/tmp", timeout: 100, maxBuffer: 1024, signal: controller.signal }))
|
||||
.resolves.toEqual({ outcome: "aborted", phase: "pre-start", stdout: "", stderr: "" });
|
||||
expect(spawnMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns mid-flight abort and escalates", async () => {
|
||||
const child = new FakeChild(2233);
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
const controller = new AbortController();
|
||||
|
||||
const promise = backend.runStreaming("echo", { cwd: "/tmp", timeout: 1000, maxBuffer: 1024, signal: controller.signal });
|
||||
controller.abort();
|
||||
expect(processKillSpy).toHaveBeenCalledWith(-2233, "SIGTERM");
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
expect(processKillSpy).toHaveBeenCalledWith(-2233, "SIGKILL");
|
||||
child.emit("close", null, "SIGTERM");
|
||||
|
||||
await expect(promise).resolves.toMatchObject({ outcome: "aborted", phase: "mid-flight" });
|
||||
});
|
||||
|
||||
it("truncates stdout overflow", async () => {
|
||||
const child = new FakeChild();
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
const promise = backend.runStreaming("echo", { cwd: "/tmp", timeout: 1000, maxBuffer: 5 });
|
||||
child.stdout.write("abcdef");
|
||||
child.stdout.write("ignored");
|
||||
child.emit("close", 0, null);
|
||||
|
||||
await expect(promise).resolves.toEqual({ outcome: "success", stdout: "abcde", stderr: "", bufferOverflow: true });
|
||||
});
|
||||
|
||||
it("truncates stderr overflow", async () => {
|
||||
const child = new FakeChild();
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
const promise = backend.runStreaming("echo", { cwd: "/tmp", timeout: 1000, maxBuffer: 5 });
|
||||
child.stderr.write("abcdef");
|
||||
child.stderr.write("ignored");
|
||||
child.emit("close", 0, null);
|
||||
|
||||
await expect(promise).resolves.toEqual({ outcome: "success", stdout: "", stderr: "abcde", bufferOverflow: true });
|
||||
});
|
||||
|
||||
it("uses win32 branch without process group", async () => {
|
||||
platformSpy.mockReturnValue("win32");
|
||||
const child = new FakeChild(8877);
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
|
||||
const promise = backend.runStreaming("sleep", { cwd: "/tmp", timeout: 100, maxBuffer: 1024 });
|
||||
await vi.advanceTimersByTimeAsync(100);
|
||||
expect(child.kill).toHaveBeenCalledWith("SIGTERM");
|
||||
expect(processKillSpy).not.toHaveBeenCalledWith(-8877, "SIGTERM");
|
||||
child.emit("close", null, "SIGTERM");
|
||||
|
||||
await expect(promise).resolves.toMatchObject({ outcome: "timeout", timeoutMs: 100 });
|
||||
expect(spawnMock).toHaveBeenCalledWith("sleep", expect.objectContaining({ detached: false }));
|
||||
});
|
||||
|
||||
it("returns spawn-error", async () => {
|
||||
const child = new FakeChild();
|
||||
spawnMock.mockReturnValue(child as any);
|
||||
const backend = new NativeSandboxBackend();
|
||||
const err = new Error("ENOENT");
|
||||
|
||||
const promise = backend.runStreaming("missing", { cwd: "/tmp", timeout: 1000, maxBuffer: 1024 });
|
||||
child.emit("error", err);
|
||||
|
||||
await expect(promise).resolves.toEqual({ outcome: "spawn-error", error: err, stdout: "", stderr: "" });
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,14 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { SandboxBackend, SandboxCapabilities, SandboxPolicy, SandboxRunOptions, SandboxRunResult } from "../types.js";
|
||||
import type {
|
||||
SandboxBackend,
|
||||
SandboxCapabilities,
|
||||
SandboxPolicy,
|
||||
SandboxRunOptions,
|
||||
SandboxRunResult,
|
||||
SandboxRunStreamingOptions,
|
||||
SandboxStreamingResult,
|
||||
} from "../types.js";
|
||||
|
||||
describe("SandboxBackend types", () => {
|
||||
it("accepts a minimal mock backend", async () => {
|
||||
@@ -10,6 +18,7 @@ describe("SandboxBackend types", () => {
|
||||
id: "native",
|
||||
supportsNetworkPolicy: false,
|
||||
supportsFilesystemPolicy: false,
|
||||
supportsStreaming: true,
|
||||
platform: "any",
|
||||
};
|
||||
},
|
||||
@@ -24,6 +33,14 @@ describe("SandboxBackend types", () => {
|
||||
bufferExceeded: false,
|
||||
};
|
||||
},
|
||||
async runStreaming(_command: string, _options: SandboxRunStreamingOptions): Promise<SandboxStreamingResult> {
|
||||
return {
|
||||
outcome: "success",
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
bufferOverflow: false,
|
||||
};
|
||||
},
|
||||
async dispose(): Promise<void> {},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user