Files
openclaw/test/scripts/npm-verify-exec.test.ts
2026-05-28 14:06:46 +02:00

69 lines
1.7 KiB
TypeScript

import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { runNpmVerifyCommand } from "../../scripts/lib/npm-verify-exec.ts";
const tempDirs: string[] = [];
function makeTempRoot(): string {
const root = mkdtempSync(path.join(tmpdir(), "openclaw-npm-verify-exec-"));
tempDirs.push(root);
return root;
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { force: true, recursive: true });
}
});
describe("npm verifier command execution", () => {
it("trims successful command output", () => {
const root = makeTempRoot();
expect(
runNpmVerifyCommand(
{
command: process.execPath,
args: ["-e", "process.stdout.write(' ok\\n')"],
},
root,
{ timeoutMs: 5_000 },
),
).toBe("ok");
});
it("bounds hung commands even when they ignore SIGTERM", () => {
const root = makeTempRoot();
const startedAt = Date.now();
expect(() =>
runNpmVerifyCommand(
{
command: process.execPath,
args: ["-e", "process.on('SIGTERM', () => {}); setInterval(() => {}, 1000);"],
},
root,
{ timeoutMs: 100 },
),
).toThrow(/ETIMEDOUT|timed out/u);
expect(Date.now() - startedAt).toBeLessThan(2_500);
});
it("bounds buffered command output", () => {
const root = makeTempRoot();
expect(() =>
runNpmVerifyCommand(
{
command: process.execPath,
args: ["-e", "process.stdout.write('x'.repeat(2048));"],
},
root,
{ maxBufferBytes: 1024, timeoutMs: 5_000 },
),
).toThrow(/ENOBUFS|maxBuffer/u);
});
});