repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/rtk/__serial__/exec.test.ts
Rawimport { describe, expect, it } from "vitest";
import { execRtkProcess } from "../src/exec.ts";
const run = (script: string, options = {}) =>
execRtkProcess(process.execPath, ["-e", script], {
timeout: 2_000,
maxBuffer: 16 * 1024,
...options,
});
describe("bounded RTK subprocesses", () => {
it("preserves stdout, stderr and RTK no-match exit status", async () => {
expect(
await run(
"process.stdout.write('rewrite');process.stderr.write('diagnostic');process.exitCode=1",
),
).toEqual({
stdout: "rewrite",
stderr: "diagnostic",
code: 1,
killed: false,
truncated: false,
});
});
it.each(["stdout", "stderr"])(
"rejects excessive %s rather than accepting a partial command",
async (stream) => {
const result = await run(
`process.${stream}.write('x'.repeat(1024 * 1024));setInterval(()=>{},1000)`,
);
expect(result).toMatchObject({ killed: true, truncated: true });
expect(result.code).not.toBe(0);
expect(Buffer.byteLength(result.stdout)).toBeLessThanOrEqual(16 * 1024);
expect(Buffer.byteLength(result.stderr)).toBeLessThanOrEqual(16 * 1024);
},
);
it("terminates a hung probe at its timeout", async () => {
const result = await run(
"process.on('SIGTERM',()=>{});setInterval(()=>{},1000)",
{ timeout: 200 },
);
expect(result).toMatchObject({ killed: true, truncated: false });
expect(result.code).not.toBe(0);
});
it("honors cancellation before spawn and while running", async () => {
const abort = new AbortController();
const pending = run("setInterval(()=>{},1000)", { signal: abort.signal });
abort.abort();
expect((await pending).code).not.toBe(0);
expect(
(
await run("process.stdout.write('must not run')", {
signal: AbortSignal.abort(),
})
).stdout,
).toBe("");
});
it("fails closed when the executable is missing", async () => {
const result = await execRtkProcess(
"pi-ext-nonexistent-rtk-executable",
[],
{ timeout: 200, maxBuffer: 16 * 1024 },
);
expect(result.code).not.toBe(0);
expect(result.stdout).toBe("");
});
});