Files
hyperframes/packages/cli/src/utils/commandResult.test.ts
T
Miguel Ángel ea7d43c616 fix(cli): stopping remove-background on a Mac no longer aborts the process (#6848)
* fix(cli): stopping remove-background on a Mac no longer aborts the process

* fix(cli): a quit after background removal released its session exits at once

* Revert "fix(cli): a quit after background removal released its session exits at once"

This reverts commit 3b9d6ffc24.
2026-10-31 17:08:22 +00:00

74 lines
2.2 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import {
CliResultSignal,
CliRuntimeError,
CliUsageError,
consumeCommandResult,
drainToExit,
endProcess,
failCommand,
failUsage,
finishCommand,
requestCliExit,
setCommandResult,
} from "./commandResult.js";
afterEach(() => {
consumeCommandResult();
});
describe("command result contract", () => {
it("keeps a failure from being overwritten by a later success", () => {
setCommandResult({ exitCode: 1, kind: "runtime_error", presented: true });
setCommandResult({ exitCode: 0, kind: "success" });
expect(consumeCommandResult().exitCode).toBe(1);
});
it("represents fatal and early-success control flow without process.exit", () => {
expect(() => failCommand(2)).toThrow(CliRuntimeError);
expect(() => failUsage(2)).toThrow(CliUsageError);
expect(() => finishCommand()).toThrow(CliResultSignal);
});
it("records a requested exit when no root handler is registered", () => {
requestCliExit(1);
expect(consumeCommandResult()).toMatchObject({ exitCode: 1, kind: "runtime_error" });
});
});
describe("ending the process", () => {
const host = () => {
const pending: (() => void)[] = [];
const calls = { exit: vi.fn(), setExitCode: vi.fn() };
const later = ((run: () => void) => {
pending.push(run);
return { unref: () => undefined };
}) as unknown as typeof setTimeout;
return { pending, calls, ...calls, later };
};
it("exits at once when nothing native needs tearing down", async () => {
vi.resetModules();
const fresh = await import("./commandResult.js");
const ending = host();
fresh.endProcess(143, ending);
expect(ending.exit).toHaveBeenCalledWith(143);
expect(ending.pending).toHaveLength(0);
});
it("after CoreML ran, leaves the exit to Node's own teardown and forces it only after a grace", () => {
const ending = host();
drainToExit();
endProcess(143, ending);
expect(ending.exit).not.toHaveBeenCalled();
expect(ending.setExitCode).toHaveBeenCalledWith(143);
expect(ending.pending).toHaveLength(1);
ending.pending[0]!();
expect(ending.exit).toHaveBeenCalledWith(143);
});
});