Download src/app/api/debate/__tests__/feedback.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/debate/__tests__/feedback.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/app/api/debate/__tests__/feedback.test.ts
-
curl -L -o feedback.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/app/api/debate/__tests__/feedback.test.ts
10.7 kB
| /** | |
| * 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); | |
| }); | |
| }); | |