Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/git-safe/__tests__/spawn.test.ts

Raw
import { describe, expect, it } from "vitest";
import { SpawnTimeoutError, spawnExpect, spawnWithTimeout } from "../spawn";

describe("spawnWithTimeout", () => {
	it("marks timed-out processes as killed", async () => {
		const result = await spawnWithTimeout(
			process.execPath,
			["-e", "setTimeout(() => {}, 10_000)"],
			{ timeout: 50 },
		);

		expect(result.killed).toBe(true);
		expect(result.code).not.toBe(0);
	});

	it("does not mark successful processes as killed", async () => {
		const result = await spawnWithTimeout(
			process.execPath,
			["-e", "process.stdout.write(process.env.GIT_TERMINAL_PROMPT ?? '')"],
			{ timeout: 5_000 },
		);

		expect(result).toMatchObject({ stdout: "0", code: 0, killed: false });
	});

	it("throws SpawnTimeoutError through spawnExpect", async () => {
		await expect(
			spawnExpect(process.execPath, ["-e", "setTimeout(() => {}, 10_000)"], {
				timeout: 50,
			}),
		).rejects.toBeInstanceOf(SpawnTimeoutError);
	});
});