Skip to content

Commit 7aa3cbc

Browse files
committed
test(runtime): add unit tests for terminal runtime helpers
Add unit tests for createNonExitingRuntime and writeRuntimeJson functions in src/runtime.ts to verify runtime behavior. Tests cover: - createNonExitingRuntime returns runtime with exit function - exit function throws error with code - writeRuntimeJson writes JSON using writeJson when available - writeRuntimeJson writes JSON using log when writeJson not available - uses custom space parameter - handles zero space parameter
1 parent 0757cad commit 7aa3cbc

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

src/runtime.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Tests for terminal runtime helpers.
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
// Mock dependencies
5+
vi.mock("../packages/terminal-core/src/progress-line.js", () => ({
6+
clearActiveProgressLine: vi.fn(),
7+
}));
8+
9+
vi.mock("../packages/terminal-core/src/restore.js", () => ({
10+
restoreTerminalState: vi.fn(),
11+
}));
12+
13+
import { createNonExitingRuntime, writeRuntimeJson } from "./runtime.js";
14+
15+
describe("createNonExitingRuntime", () => {
16+
it("returns runtime with exit function", () => {
17+
const runtime = createNonExitingRuntime();
18+
expect(typeof runtime.exit).toBe("function");
19+
});
20+
21+
it("exit function throws error", () => {
22+
const runtime = createNonExitingRuntime();
23+
expect(() => runtime.exit(1)).toThrow("exit 1");
24+
});
25+
26+
it("exit function includes code in error message", () => {
27+
const runtime = createNonExitingRuntime();
28+
expect(() => runtime.exit(42)).toThrow("exit 42");
29+
});
30+
});
31+
32+
describe("writeRuntimeJson", () => {
33+
it("writes JSON using writeJson when available", () => {
34+
const runtime = {
35+
log: vi.fn(),
36+
error: vi.fn(),
37+
exit: vi.fn(),
38+
writeStdout: vi.fn(),
39+
writeJson: vi.fn(),
40+
};
41+
writeRuntimeJson(runtime, { key: "value" });
42+
expect(runtime.writeJson).toHaveBeenCalledWith({ key: "value" }, 2);
43+
});
44+
45+
it("writes JSON using log when writeJson not available", () => {
46+
const runtime = {
47+
log: vi.fn(),
48+
error: vi.fn(),
49+
exit: vi.fn(),
50+
};
51+
writeRuntimeJson(runtime, { key: "value" });
52+
expect(runtime.log).toHaveBeenCalled();
53+
});
54+
55+
it("uses custom space parameter", () => {
56+
const runtime = {
57+
log: vi.fn(),
58+
error: vi.fn(),
59+
exit: vi.fn(),
60+
writeStdout: vi.fn(),
61+
writeJson: vi.fn(),
62+
};
63+
writeRuntimeJson(runtime, { key: "value" }, 4);
64+
expect(runtime.writeJson).toHaveBeenCalledWith({ key: "value" }, 4);
65+
});
66+
67+
it("handles zero space parameter", () => {
68+
const runtime = {
69+
log: vi.fn(),
70+
error: vi.fn(),
71+
exit: vi.fn(),
72+
};
73+
writeRuntimeJson(runtime, { key: "value" }, 0);
74+
expect(runtime.log).toHaveBeenCalledWith('{"key":"value"}');
75+
});
76+
});

0 commit comments

Comments
 (0)