Files
openclaw/src/runtime.test.ts
dwc1997 0635a8c1a8 test(runtime): add unit tests for terminal runtime helpers (#96736)
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
2026-06-28 18:35:56 -07:00

77 lines
2.1 KiB
TypeScript

// Tests for terminal runtime helpers.
import { describe, expect, it, vi } from "vitest";
// Mock dependencies
vi.mock("../packages/terminal-core/src/progress-line.js", () => ({
clearActiveProgressLine: vi.fn(),
}));
vi.mock("../packages/terminal-core/src/restore.js", () => ({
restoreTerminalState: vi.fn(),
}));
import { createNonExitingRuntime, writeRuntimeJson } from "./runtime.js";
describe("createNonExitingRuntime", () => {
it("returns runtime with exit function", () => {
const runtime = createNonExitingRuntime();
expect(typeof runtime.exit).toBe("function");
});
it("exit function throws error", () => {
const runtime = createNonExitingRuntime();
expect(() => runtime.exit(1)).toThrow("exit 1");
});
it("exit function includes code in error message", () => {
const runtime = createNonExitingRuntime();
expect(() => runtime.exit(42)).toThrow("exit 42");
});
});
describe("writeRuntimeJson", () => {
it("writes JSON using writeJson when available", () => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
writeStdout: vi.fn(),
writeJson: vi.fn(),
};
writeRuntimeJson(runtime, { key: "value" });
expect(runtime.writeJson).toHaveBeenCalledWith({ key: "value" }, 2);
});
it("writes JSON using log when writeJson not available", () => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
writeRuntimeJson(runtime, { key: "value" });
expect(runtime.log).toHaveBeenCalled();
});
it("uses custom space parameter", () => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
writeStdout: vi.fn(),
writeJson: vi.fn(),
};
writeRuntimeJson(runtime, { key: "value" }, 4);
expect(runtime.writeJson).toHaveBeenCalledWith({ key: "value" }, 4);
});
it("handles zero space parameter", () => {
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
writeRuntimeJson(runtime, { key: "value" }, 0);
expect(runtime.log).toHaveBeenCalledWith('{"key":"value"}');
});
});