-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard-start.test.ts
More file actions
56 lines (47 loc) · 2.17 KB
/
Copy pathdashboard-start.test.ts
File metadata and controls
56 lines (47 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
serve: vi.fn(),
on: vi.fn(),
close: vi.fn(),
error: vi.fn(),
}));
vi.mock("@hono/node-server", () => ({ serve: mocks.serve }));
vi.mock("../src/core/logger.js", () => ({
RuntimeLogger: { error: mocks.error, info: vi.fn(), warn: vi.fn(), debug: vi.fn() },
}));
import { CommandCenterDashboard } from "../src/core/dashboard.js";
describe("dashboard server startup", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.close.mockImplementation((callback?: () => void) => callback?.());
mocks.serve.mockReturnValue({ on: mocks.on, close: mocks.close });
});
it("binds the configured host and reports server errors", () => {
process.env.PROMPT_REFINER_DASHBOARD_HOST = "127.0.0.1";
CommandCenterDashboard.start(3999, ".");
expect(mocks.serve).toHaveBeenCalledWith(expect.objectContaining({ port: 3999, hostname: "127.0.0.1" }));
const handler = mocks.on.mock.calls[0][1];
handler(Object.assign(new Error("busy"), { code: "EADDRINUSE" }));
handler(Object.assign(new Error("failure"), { code: "EIO" }));
expect(mocks.error).toHaveBeenCalledTimes(2);
delete process.env.PROMPT_REFINER_DASHBOARD_HOST;
});
it("rethrows synchronous startup failures after logging", () => {
mocks.serve.mockImplementation(() => {
throw new Error("startup failed");
});
expect(() => CommandCenterDashboard.start(3999, ".")).toThrow("startup failed");
expect(mocks.error).toHaveBeenCalledWith("Dashboard failed to start on port 3999", expect.any(Error));
});
it("closes the active dashboard server and tolerates repeated stops", async () => {
CommandCenterDashboard.start(3999, ".");
await CommandCenterDashboard.stop();
await CommandCenterDashboard.stop();
expect(mocks.close).toHaveBeenCalledOnce();
});
it("rejects when closing the active dashboard server fails", async () => {
mocks.close.mockImplementationOnce((callback?: (error?: Error) => void) => callback?.(new Error("close failed")));
CommandCenterDashboard.start(3999, ".");
await expect(CommandCenterDashboard.stop()).rejects.toThrow("close failed");
});
});