File size: 10,707 Bytes
9580dce | 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | /**
* Tests for POST /api/debate/feedback
*
* Covers:
* - 200 with skipped message when LANGSMITH_API_KEY is not configured
* - 400 for invalid JSON body
* - 400 for missing runId
* - 400 for non-string runId
* - 400 for missing scores
* - 400 for scores that is not an object
* - 400 when no valid dimension IDs or scores in [0,10] remain after filtering
* - 200 with submitted count for valid request
* - unknown dimension IDs are silently filtered out
* - scores outside [0, 10] are silently filtered out
* - non-numeric score values are silently filtered out
* - partial scores (subset of all dimensions) are accepted
* - submitAllHumanFeedback is called with the clean scores and runId
* - 500 on unexpected submitAllHumanFeedback error
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
// ---------------------------------------------------------------------------
// Mock LangSmith
// ---------------------------------------------------------------------------
const mockSubmitAllHumanFeedback = vi.fn();
vi.mock("@/lib/langsmith", () => ({
submitAllHumanFeedback: (...args: unknown[]) =>
mockSubmitAllHumanFeedback(...args),
}));
// ---------------------------------------------------------------------------
// Import route AFTER mocks
// ---------------------------------------------------------------------------
import { POST } from "../feedback/route";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function makeRequest(body: unknown): NextRequest {
return new NextRequest("http://localhost/api/debate/feedback", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
const VALID_RUN_ID = "550e8400-e29b-41d4-a716-446655440000";
const VALID_SCORES = {
"argument-structure": 7,
persuasiveness: 6,
"flaw-detection": 8,
};
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("POST /api/debate/feedback — LANGSMITH_API_KEY not set", () => {
beforeEach(() => {
delete process.env.LANGSMITH_API_KEY;
mockSubmitAllHumanFeedback.mockReset();
});
it("returns 200 with submitted=0 and skipped message when no API key", async () => {
const req = makeRequest({ runId: VALID_RUN_ID, scores: VALID_SCORES });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(0);
expect(typeof body.skipped).toBe("string");
expect(body.skipped).toContain("LANGSMITH_API_KEY");
});
it("does not call submitAllHumanFeedback when no API key", async () => {
const req = makeRequest({ runId: VALID_RUN_ID, scores: VALID_SCORES });
await POST(req);
expect(mockSubmitAllHumanFeedback).not.toHaveBeenCalled();
});
});
describe("POST /api/debate/feedback — validation", () => {
beforeEach(() => {
process.env.LANGSMITH_API_KEY = "test-key";
mockSubmitAllHumanFeedback.mockResolvedValue(undefined);
});
it("returns 400 for invalid JSON body", async () => {
const req = new NextRequest("http://localhost/api/debate/feedback", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "not-json{",
});
const res = await POST(req);
expect(res.status).toBe(400);
});
it("returns 400 when runId is missing", async () => {
const req = makeRequest({ scores: VALID_SCORES });
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("runId");
});
it("returns 400 when runId is not a string", async () => {
const req = makeRequest({ runId: 12345, scores: VALID_SCORES });
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("runId");
});
it("returns 400 when scores is missing", async () => {
const req = makeRequest({ runId: VALID_RUN_ID });
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("scores");
});
it("returns 400 when scores is an array", async () => {
const req = makeRequest({ runId: VALID_RUN_ID, scores: [7, 8, 9] });
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("scores");
});
it("returns 400 when scores is a string", async () => {
const req = makeRequest({ runId: VALID_RUN_ID, scores: "7/10" });
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("scores");
});
it("returns 400 when all scores are for unknown dimension IDs", async () => {
const req = makeRequest({
runId: VALID_RUN_ID,
scores: { "made-up-dimension": 7, "another-fake": 5 },
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("No valid scores");
});
it("returns 400 when all scores are out of range [0, 10]", async () => {
const req = makeRequest({
runId: VALID_RUN_ID,
scores: { "argument-structure": 11, persuasiveness: -1 },
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("No valid scores");
});
it("returns 400 when all scores are non-numeric", async () => {
const req = makeRequest({
runId: VALID_RUN_ID,
scores: { "argument-structure": "great", persuasiveness: true },
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toContain("No valid scores");
});
});
describe("POST /api/debate/feedback — success", () => {
beforeEach(() => {
process.env.LANGSMITH_API_KEY = "test-key";
mockSubmitAllHumanFeedback.mockResolvedValue(undefined);
});
it("returns 200 with correct submitted count", async () => {
const req = makeRequest({ runId: VALID_RUN_ID, scores: VALID_SCORES });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(Object.keys(VALID_SCORES).length);
});
it("calls submitAllHumanFeedback with correct runId and clean scores", async () => {
const req = makeRequest({ runId: VALID_RUN_ID, scores: VALID_SCORES });
await POST(req);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(
VALID_RUN_ID,
VALID_SCORES,
);
});
it("accepts a partial subset of dimension IDs", async () => {
const partial = { "argument-structure": 9 };
const req = makeRequest({ runId: VALID_RUN_ID, scores: partial });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(1);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(VALID_RUN_ID, partial);
});
it("accepts score of exactly 0 (lower bound)", async () => {
const req = makeRequest({
runId: VALID_RUN_ID,
scores: { "argument-structure": 0 },
});
const res = await POST(req);
expect(res.status).toBe(200);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(VALID_RUN_ID, {
"argument-structure": 0,
});
});
it("accepts score of exactly 10 (upper bound)", async () => {
const req = makeRequest({
runId: VALID_RUN_ID,
scores: { persuasiveness: 10 },
});
const res = await POST(req);
expect(res.status).toBe(200);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(VALID_RUN_ID, {
persuasiveness: 10,
});
});
it("silently filters out unknown dimension IDs, submits valid ones", async () => {
const mixed = {
"argument-structure": 7,
"made-up-dimension": 5,
};
const req = makeRequest({ runId: VALID_RUN_ID, scores: mixed });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(1);
// Only the valid dimension was forwarded
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(VALID_RUN_ID, {
"argument-structure": 7,
});
});
it("silently filters out out-of-range scores, submits valid ones", async () => {
const mixed = {
"argument-structure": 7,
persuasiveness: 11, // over max
"flaw-detection": -1, // below min
};
const req = makeRequest({ runId: VALID_RUN_ID, scores: mixed });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(1);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(VALID_RUN_ID, {
"argument-structure": 7,
});
});
it("silently filters non-numeric score values", async () => {
const mixed = {
"argument-structure": 8,
persuasiveness: "high", // non-numeric, filtered
};
const req = makeRequest({ runId: VALID_RUN_ID, scores: mixed });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(1);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(VALID_RUN_ID, {
"argument-structure": 8,
});
});
it("accepts all 8 scoring dimensions at once", async () => {
const allDimScores = {
"flaw-detection": 7,
"steelman-awareness": 6,
"devils-advocate-depth": 5,
"argument-structure": 8,
persuasiveness: 9,
"evidence-accuracy": 7,
"rebuttal-quality": 6,
"intellectual-honesty": 8,
};
const req = makeRequest({ runId: VALID_RUN_ID, scores: allDimScores });
const res = await POST(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.submitted).toBe(8);
expect(mockSubmitAllHumanFeedback).toHaveBeenCalledWith(
VALID_RUN_ID,
allDimScores,
);
});
});
describe("POST /api/debate/feedback — error handling", () => {
beforeEach(() => {
process.env.LANGSMITH_API_KEY = "test-key";
});
it("returns 500 when submitAllHumanFeedback throws", async () => {
mockSubmitAllHumanFeedback.mockRejectedValue(new Error("LangSmith unavailable"));
const req = makeRequest({ runId: VALID_RUN_ID, scores: VALID_SCORES });
const res = await POST(req);
expect(res.status).toBe(500);
});
});
|