mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-05 22:23:37 +00:00
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
77 lines
2.1 KiB
TypeScript
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"}');
|
|
});
|
|
});
|