|
| 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