Spaces:
Running
Running
File size: 8,249 Bytes
837e3ac | 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | import { afterEach, describe, expect, it, vi } from "vitest";
const generateLlmText = vi.fn();
vi.mock("@/lib/llm-client", () => ({
generateLlmText,
}));
afterEach(() => {
generateLlmText.mockReset();
});
describe("/api/chat route", () => {
it("returns resolved context and thinking summary for mock package asks", async () => {
const { POST } = await import("./route");
const seed = (await import("@/agentic_pm_demo_codex_plans/data/work-packages.seed.json"))
.default as Array<Record<string, unknown>>;
const response = await POST(
new Request("http://localhost/api/chat", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
messages: [
{ role: "user", content: "@SRS ask What does verification method mean?" },
],
workPackages: seed,
selectedWorkPackageId: "wp-srs",
parsedCommand: {
referencedPackageName: "System Requirements Specification",
mode: "ask",
instruction: "What does verification method mean?",
},
}),
}),
);
const payload = (await response.json()) as {
boardAction?: { type?: string };
resolvedContext?: {
scope?: string;
workPackageId?: string | null;
mode?: string;
provider?: string;
boardMutationPolicy?: string;
};
thinkingSummary?: string[];
};
expect(payload.boardAction?.type).toBe("none");
expect(payload.resolvedContext).toMatchObject({
scope: "package",
workPackageId: "wp-srs",
mode: "ask",
provider: "mock",
boardMutationPolicy: "none",
});
expect(payload.thinkingSummary?.join("\n")).toContain("Resolved scope to System Requirements Specification.");
expect(payload.thinkingSummary?.join("\n")).toContain("Using mock mode.");
});
it("falls back to package-scoped mock ask guidance when the live model call throws", async () => {
generateLlmText.mockRejectedValueOnce(new Error("Invalid JSON"));
const { POST } = await import("./route");
const seed = (await import("@/agentic_pm_demo_codex_plans/data/work-packages.seed.json"))
.default as Array<Record<string, unknown>>;
const response = await POST(
new Request("http://localhost/api/chat", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
messages: [
{ role: "user", content: "@SRS ask What does verification method mean?" },
],
workPackages: seed,
selectedWorkPackageId: "wp-srs",
parsedCommand: {
referencedPackageName: "System Requirements Specification",
mode: "ask",
instruction: "What does verification method mean?",
},
llmConfig: {
apiKey: "live-key",
baseUrl: "https://api.example.com/v1",
model: "gpt-test",
},
}),
}),
);
const payload = (await response.json()) as {
assistantMessage: string;
boardAction?: { type?: string };
resolvedContext?: {
scope?: string;
workPackageId?: string | null;
mode?: string;
provider?: string;
boardMutationPolicy?: string;
};
thinkingSummary?: string[];
};
expect(payload.assistantMessage).toContain("For SRS, the objective here is:");
expect(payload.assistantMessage).toContain("verification");
expect(payload.assistantMessage).not.toContain("Network or parsing error calling LLM");
expect(payload.boardAction?.type).toBe("none");
expect(payload.resolvedContext).toMatchObject({
scope: "package",
workPackageId: "wp-srs",
mode: "ask",
provider: "live",
boardMutationPolicy: "none",
});
expect(payload.thinkingSummary?.join("\n")).toContain(
"Provider call failed; preserving the package-scoped fallback.",
);
});
it("falls back to a scoped mock plan update when the live model call throws", async () => {
generateLlmText.mockRejectedValueOnce(new Error("Invalid JSON"));
const { POST } = await import("./route");
const seed = (await import("@/agentic_pm_demo_codex_plans/data/work-packages.seed.json"))
.default as Array<Record<string, unknown>>;
const response = await POST(
new Request("http://localhost/api/chat", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
messages: [
{ role: "user", content: "@SRS plan Break this into review steps." },
],
workPackages: seed,
selectedWorkPackageId: "wp-srs",
parsedCommand: {
referencedPackageName: "System Requirements Specification",
mode: "plan",
instruction: "Break this into review steps.",
},
llmConfig: {
apiKey: "live-key",
baseUrl: "https://api.example.com/v1",
model: "gpt-test",
},
}),
}),
);
const payload = (await response.json()) as {
assistantMessage: string;
boardAction?: {
type?: string;
workPackageId?: string | null;
fields?: { tasks?: unknown[]; status?: string };
};
resolvedContext?: {
scope?: string;
workPackageId?: string | null;
mode?: string;
provider?: string;
boardMutationPolicy?: string;
};
thinkingSummary?: string[];
};
expect(payload.assistantMessage).toContain("Planned next steps for SRS");
expect(payload.boardAction?.type).toBe("update");
expect(payload.boardAction?.workPackageId).toBe("wp-srs");
expect(payload.boardAction?.fields?.tasks?.length).toBeGreaterThan(0);
expect(payload.boardAction?.fields?.status).toBe("in_progress");
expect(payload.resolvedContext).toMatchObject({
scope: "package",
workPackageId: "wp-srs",
mode: "plan",
provider: "live",
boardMutationPolicy: "selected_package_only",
});
expect(payload.thinkingSummary?.join("\n")).toContain(
"Provider call failed; preserving the package-scoped fallback.",
);
});
it("neutralizes live package updates that target a different work package", async () => {
generateLlmText.mockResolvedValueOnce({
text: JSON.stringify({
assistantMessage: "Changed the wrong package.",
boardAction: {
type: "update",
workPackageId: "wp-final-concept",
fields: {
objective: "This should not land on Final Concept.",
},
},
}),
endpoint: "https://api.example.com/v1/chat/completions",
requestPreview: "request",
responsePreview: "response",
});
const { POST } = await import("./route");
const seed = (await import("@/agentic_pm_demo_codex_plans/data/work-packages.seed.json"))
.default as Array<Record<string, unknown>>;
const response = await POST(
new Request("http://localhost/api/chat", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
messages: [
{ role: "user", content: "@SRS change Add cybersecurity acceptance criteria." },
],
workPackages: seed,
selectedWorkPackageId: "wp-srs",
parsedCommand: {
referencedPackageName: "System Requirements Specification",
mode: "change",
instruction: "Add cybersecurity acceptance criteria.",
},
llmConfig: {
apiKey: "live-key",
baseUrl: "https://api.example.com/v1",
model: "gpt-test",
},
}),
}),
);
const payload = (await response.json()) as {
boardAction?: { type?: string; workPackageId?: string | null };
thinkingSummary?: string[];
};
expect(payload.boardAction).toMatchObject({
type: "none",
workPackageId: null,
});
expect(payload.thinkingSummary?.join("\n")).toContain(
"Blocked a board update that targeted a different work package.",
);
});
});
|