-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard-coverage.test.ts
More file actions
214 lines (194 loc) · 8.97 KB
/
Copy pathdashboard-coverage.test.ts
File metadata and controls
214 lines (194 loc) · 8.97 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { AgenticBlackboard } from "../src/core/blackboard.js";
import { ConfigManager } from "../src/core/config.js";
import { CommandCenterDashboard, isSameOriginRequest } from "../src/core/dashboard.js";
import { RuntimeLogger } from "../src/core/logger.js";
import { ArchitecturalScout, NodeDetector, PythonDetector } from "../src/detectors/project-scout.js";
import { EventStore } from "../src/history/event-store.js";
import { TimelineProvider } from "../src/history/timeline.js";
describe("dashboard deterministic fallbacks", () => {
let directory: string;
let store: EventStore;
beforeEach(() => {
directory = fs.mkdtempSync(path.join(os.tmpdir(), "dashboard-coverage-"));
process.env.PROMPT_REFINER_GLOBAL_DIR = path.join(directory, "global");
(EventStore as unknown as { instance: EventStore | null }).instance = null;
store = EventStore.getInstance();
vi.spyOn(console, "error").mockImplementation(() => undefined);
});
afterEach(() => {
vi.restoreAllMocks();
(EventStore as unknown as { instance: EventStore | null }).instance?.close();
(EventStore as unknown as { instance: EventStore | null }).instance = null;
delete process.env.PROMPT_REFINER_GLOBAL_DIR;
fs.rmSync(directory, { recursive: true, force: true });
});
it("rejects malformed origin URLs", () => {
expect(isSameOriginRequest("not a url", "also invalid")).toBe(false);
});
it("sanitizes endpoints without ports and logs plain route errors", () => {
expect((CommandCenterDashboard as any).buildHealth).toBeTypeOf("function");
vi.spyOn(ConfigManager, "getSemanticConfig").mockReturnValue({
localEnabled: false,
mcpSamplingEnabled: false,
baseUrl: "https://localhost/v1",
models: [],
timeoutMs: 1,
temperature: 0,
allowNonLoopback: false,
});
expect((CommandCenterDashboard as any).buildHealth(directory).semantic.local.endpoint).toBe("https://localhost");
expect(() => (CommandCenterDashboard as any).logRouteError("plain", "plain failure")).not.toThrow();
});
it("builds fallback state and filters missing projects", async () => {
const missing = path.join(directory, "missing");
vi.spyOn(AgenticBlackboard, "getGlobalData").mockReturnValue({
projects: [missing],
} as any);
vi.spyOn(AgenticBlackboard, "getLogs").mockReturnValue([]);
vi.spyOn(AgenticBlackboard, "getActiveIntents").mockReturnValue([]);
vi.spyOn(AgenticBlackboard, "getLastRefinement").mockReturnValue(null);
vi.spyOn(ArchitecturalScout, "detectPatterns").mockResolvedValue([]);
vi.spyOn(NodeDetector, "detect").mockResolvedValue({});
vi.spyOn(PythonDetector, "detect").mockResolvedValue({});
vi.spyOn(store, "getRepositoryStats").mockImplementationOnce(() => {
throw new Error("stats unavailable");
});
CommandCenterDashboard.createApp(directory);
const state = await (CommandCenterDashboard as any).buildState(directory);
expect(state).toMatchObject({
selectedPath: directory,
globalLogs: [],
stack: "Unknown",
framework: "Unknown",
pattern: "Standard",
stats: { commits: 0, prompts: 0, lessons: 0 },
});
expect(state.projects).toEqual([path.resolve(directory)]);
});
it("handles global dashboard data without a projects collection", async () => {
vi.spyOn(AgenticBlackboard, "getGlobalData").mockReturnValue({} as any);
CommandCenterDashboard.createApp(directory);
expect((CommandCenterDashboard as any).getVisibleProjects()).toEqual([path.resolve(directory)]);
});
it("sanitizes malformed, duplicated, and disabled health telemetry", () => {
vi.spyOn(ConfigManager, "getSemanticConfig").mockReturnValue({
localEnabled: false,
mcpSamplingEnabled: false,
baseUrl: "invalid",
models: [],
timeoutMs: 1,
temperature: 0,
allowNonLoopback: false,
});
const db = (store as any).db;
const repoId = store.ensureRepository(directory).id;
store.recordEvent({
id: "malformed",
event_type: "semantic_request_completed",
repo_id: repoId,
summary: "malformed",
timestamp: "2026-01-02T00:00:00.000Z",
details_json: "{",
});
store.recordEvent({
id: "typed",
event_type: "semantic_request_completed",
repo_id: repoId,
summary: "typed",
timestamp: "2026-01-01T00:00:00.000Z",
details_json: JSON.stringify({
taskName: 42,
provider: "local",
model: "m",
latencyMs: -4.6,
fallbackFrom: ["a", "", 3, "b"],
}),
});
expect(db.prepare("SELECT COUNT(*) AS count FROM events").get().count).toBe(2);
const health = (CommandCenterDashboard as any).buildHealth(directory);
expect(health.semantic.status).toBe("disabled");
expect(health.semantic.local.endpoint).toBe("invalid");
expect(health.semantic.totals).toMatchObject({
completed: 2,
averageLatencyMs: 0,
fallbackCompletions: 1,
});
expect(health.semantic.providers).toEqual(expect.arrayContaining([
expect.objectContaining({ provider: "unknown", averageLatencyMs: null }),
expect.objectContaining({ provider: "local", averageLatencyMs: 0, models: ["m"] }),
]));
});
it("returns route-owned failures without leaking thrown details", async () => {
const app = CommandCenterDashboard.createApp(directory);
const routeCases: Array<[string, () => void]> = [
["/api/state", () => vi.spyOn(CommandCenterDashboard as any, "buildState").mockRejectedValueOnce(new Error("state secret"))],
["/api/timeline", () => vi.spyOn(TimelineProvider.prototype, "getUnifiedTimeline").mockImplementationOnce(() => { throw new Error("timeline secret"); })],
["/api/commits", () => vi.spyOn(EventStore, "getInstance").mockImplementationOnce(() => { throw new Error("commit secret"); })],
["/api/lessons", () => vi.spyOn(EventStore, "getInstance").mockImplementationOnce(() => { throw new Error("lesson secret"); })],
["/api/templates", () => vi.spyOn(EventStore, "getInstance").mockImplementationOnce(() => { throw new Error("template secret"); })],
["/api/tournaments", () => vi.spyOn(EventStore, "getInstance").mockImplementationOnce(() => { throw new Error("tournament secret"); })],
["/api/health", () => vi.spyOn(CommandCenterDashboard as any, "buildHealth").mockImplementationOnce(() => { throw new Error("health secret"); })],
["/", () => vi.spyOn(CommandCenterDashboard as any, "buildState").mockRejectedValueOnce("root failure")],
];
for (const [route, arrange] of routeCases) {
arrange();
const response = await app.request(route);
expect(response.status, route).toBe(500);
}
expect(RuntimeLogger.error).toBeDefined();
});
it("returns sanitized tournament mutation failures", async () => {
const app = CommandCenterDashboard.createApp(directory);
vi.spyOn(EventStore, "getInstance").mockImplementationOnce(() => { throw new Error("tournament write secret"); });
const response = await app.request("/api/tournaments/run", {
method: "POST",
headers: { "content-type": "application/json", origin: "http://localhost" },
body: JSON.stringify({
baseline: "baseline",
variantA: "variant a",
variantB: "variant b",
}),
});
expect(response.status).toBe(500);
expect(await response.json()).toEqual({ error: "Failed to run tournament" });
});
it("renders an Error without a stack in the root failure page", async () => {
const app = CommandCenterDashboard.createApp(directory);
const error = new Error("root message");
error.stack = "";
vi.spyOn(CommandCenterDashboard as any, "buildState").mockRejectedValueOnce(error);
const response = await app.request("/");
const html = await response.text();
expect(html).toContain("See sanitized runtime logs");
expect(html).not.toContain("root message");
});
it("handles review persistence failures and successful template approval", async () => {
const app = CommandCenterDashboard.createApp(directory);
const request = (kind: string, id: string, decision: "approve" | "reject") => app.request(
`/api/review/${kind}/${id}`,
{
method: "POST",
headers: { "content-type": "application/json", origin: "http://localhost" },
body: JSON.stringify({ decision }),
},
);
vi.spyOn(EventStore, "getInstance").mockImplementationOnce(() => { throw "review failure"; });
expect((await request("lesson", "id", "approve")).status).toBe(500);
const repoId = store.ensureRepository(directory).id;
store.recordTemplate({
id: "template",
repo_id: repoId,
category: "quality",
title: "Template",
template_text: "Verify.",
usage_notes: "",
source_type: "test",
success_score: 1,
});
expect((await request("template", "template", "approve")).status).toBe(200);
});
});