Download src/lib/__tests__/langsmith.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 17.2 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/langsmith.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/__tests__/langsmith.test.ts
-
curl -L -o langsmith.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/__tests__/langsmith.test.ts
17.2 kB
| /** | |
| * Tests for src/lib/langsmith.ts | |
| * | |
| * Covers: | |
| * langsmithClient singleton | |
| * - exports a Client instance | |
| * - is the same object on repeated access (singleton) | |
| * createRunTracer | |
| * - calls langsmithClient.createRun with a valid UUID id | |
| * - passes name, inputs, outputs to createRun | |
| * - defaults run_type to "llm" when not provided | |
| * - uses the provided runType when given | |
| * - includes end_time as a valid ISO string | |
| * - returns the run ID string | |
| * - returns a different ID on each call (UUID uniqueness) | |
| * - uses provided id when given (pre-generated run ID) | |
| * - returns provided id (not a new UUID) when id is specified | |
| * submitDimensionFeedback | |
| * - calls langsmithClient.createFeedback with runId and dimensionId | |
| * - normalises score: divides by 10 (0-10 → 0-1) | |
| * - normalises score: clamps above 10 to 1.0 | |
| * - normalises score: clamps below 0 to 0.0 | |
| * - normalises score: exact boundary 10 → 1.0 | |
| * - normalises score: exact boundary 0 → 0.0 | |
| * - sets a comment containing the dimensionId | |
| * submitAllDimensionFeedback | |
| * - calls submitDimensionFeedback once per entry in dimensionScores | |
| * - passes correct runId and dimensionId to each call | |
| * - passes correct score to each call | |
| * - resolves when all dimensions succeed | |
| * - rejects if any dimension call throws | |
| * submitHumanFeedback | |
| * - calls langsmithClient.createFeedback with "human_<dimensionId>" key | |
| * - normalises score: 0-10 → 0-1 | |
| * - clamps above 10 to 1.0 | |
| * - clamps below 0 to 0.0 | |
| * - score 0 → 0.0 (exact lower bound) | |
| * - score 10 → 1.0 (exact upper bound) | |
| * - includes a comment mentioning the dimensionId | |
| * submitAllHumanFeedback | |
| * - calls submitHumanFeedback once per entry | |
| * - passes the correct runId to each call | |
| * - uses "human_<dim>" key for each entry | |
| * - handles empty scores without calling createFeedback | |
| * - rejects if any individual submission throws | |
| * flushTraces | |
| * - resolves after a brief delay (smoke test — no real assertion) | |
| */ | |
| import { describe, it, expect, vi, beforeEach } from "vitest"; | |
| // --------------------------------------------------------------------------- | |
| // Mock the LangSmith Client | |
| // --------------------------------------------------------------------------- | |
| // Use vi.hoisted so the vars are available inside the hoisted vi.mock factory | |
| const { mockCreateRun, mockCreateFeedback } = vi.hoisted(() => ({ | |
| mockCreateRun: vi.fn().mockResolvedValue(undefined), | |
| mockCreateFeedback: vi.fn().mockResolvedValue(undefined), | |
| })); | |
| vi.mock("langsmith", () => { | |
| class Client { | |
| createRun = mockCreateRun; | |
| createFeedback = mockCreateFeedback; | |
| } | |
| return { Client }; | |
| }); | |
| // Reset mock call history before each test, but keep the same mock instance | |
| beforeEach(() => { | |
| mockCreateRun.mockClear(); | |
| mockCreateFeedback.mockClear(); | |
| }); | |
| // Import after the mock is in place | |
| import { | |
| langsmithClient, | |
| createRunTracer, | |
| submitDimensionFeedback, | |
| submitAllDimensionFeedback, | |
| submitHumanFeedback, | |
| submitAllHumanFeedback, | |
| flushTraces, | |
| } from "../langsmith"; | |
| // --------------------------------------------------------------------------- | |
| // langsmithClient singleton | |
| // --------------------------------------------------------------------------- | |
| describe("langsmithClient singleton", () => { | |
| it("exports a client-like object", () => { | |
| expect(langsmithClient).toBeDefined(); | |
| expect(typeof langsmithClient.createRun).toBe("function"); | |
| expect(typeof langsmithClient.createFeedback).toBe("function"); | |
| }); | |
| it("is the same object on repeated imports (singleton)", async () => { | |
| // Re-import to verify singleton pattern | |
| const { langsmithClient: second } = await import("../langsmith"); | |
| expect(second).toBe(langsmithClient); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // createRunTracer | |
| // --------------------------------------------------------------------------- | |
| describe("createRunTracer", () => { | |
| it("calls createRun on the client", async () => { | |
| await createRunTracer({ | |
| name: "test-run", | |
| inputs: { question: "hello" }, | |
| outputs: { answer: "world" }, | |
| }); | |
| expect(mockCreateRun).toHaveBeenCalledOnce(); | |
| }); | |
| it("passes a valid UUID as the run id", async () => { | |
| await createRunTracer({ | |
| name: "uuid-test", | |
| inputs: {}, | |
| outputs: {}, | |
| }); | |
| const call = mockCreateRun.mock.calls[0][0] as { id: string }; | |
| // UUIDs are 36 chars with dashes: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
| expect(call.id).toMatch( | |
| /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i, | |
| ); | |
| }); | |
| it("returns the same run ID that was passed to createRun", async () => { | |
| const returnedId = await createRunTracer({ | |
| name: "id-match", | |
| inputs: {}, | |
| outputs: {}, | |
| }); | |
| const call = mockCreateRun.mock.calls[0][0] as { id: string }; | |
| expect(returnedId).toBe(call.id); | |
| }); | |
| it("returns a string", async () => { | |
| const id = await createRunTracer({ name: "n", inputs: {}, outputs: {} }); | |
| expect(typeof id).toBe("string"); | |
| }); | |
| it("passes name to createRun", async () => { | |
| await createRunTracer({ | |
| name: "debate-judge-round", | |
| inputs: {}, | |
| outputs: {}, | |
| }); | |
| const call = mockCreateRun.mock.calls[0][0] as { name: string }; | |
| expect(call.name).toBe("debate-judge-round"); | |
| }); | |
| it("passes inputs and outputs to createRun", async () => { | |
| const inputs = { topic: "AI ethics", round: 1 }; | |
| const outputs = { scores: { clarity: 7 } }; | |
| await createRunTracer({ name: "n", inputs, outputs }); | |
| const call = mockCreateRun.mock.calls[0][0] as { | |
| inputs: typeof inputs; | |
| outputs: typeof outputs; | |
| }; | |
| expect(call.inputs).toEqual(inputs); | |
| expect(call.outputs).toEqual(outputs); | |
| }); | |
| it("defaults run_type to 'llm' when not provided", async () => { | |
| await createRunTracer({ name: "n", inputs: {}, outputs: {} }); | |
| const call = mockCreateRun.mock.calls[0][0] as { run_type: string }; | |
| expect(call.run_type).toBe("llm"); | |
| }); | |
| it("uses the provided runType when given", async () => { | |
| await createRunTracer({ | |
| name: "n", | |
| inputs: {}, | |
| outputs: {}, | |
| runType: "chain", | |
| }); | |
| const call = mockCreateRun.mock.calls[0][0] as { run_type: string }; | |
| expect(call.run_type).toBe("chain"); | |
| }); | |
| it("includes end_time as a valid ISO string", async () => { | |
| await createRunTracer({ name: "n", inputs: {}, outputs: {} }); | |
| const call = mockCreateRun.mock.calls[0][0] as { end_time: string }; | |
| expect(() => new Date(call.end_time).toISOString()).not.toThrow(); | |
| expect(call.end_time).toMatch(/^\d{4}-\d{2}-\d{2}T/); | |
| }); | |
| it("returns a different ID on each call", async () => { | |
| const id1 = await createRunTracer({ name: "n", inputs: {}, outputs: {} }); | |
| const id2 = await createRunTracer({ name: "n", inputs: {}, outputs: {} }); | |
| expect(id1).not.toBe(id2); | |
| }); | |
| it("propagates createRun errors", async () => { | |
| mockCreateRun.mockRejectedValueOnce(new Error("network error")); | |
| await expect( | |
| createRunTracer({ name: "n", inputs: {}, outputs: {} }), | |
| ).rejects.toThrow("network error"); | |
| }); | |
| it("uses the provided id when given", async () => { | |
| const prebuiltId = "550e8400-e29b-41d4-a716-446655440000"; | |
| await createRunTracer({ name: "n", inputs: {}, outputs: {}, id: prebuiltId }); | |
| const call = mockCreateRun.mock.calls[0][0] as { id: string }; | |
| expect(call.id).toBe(prebuiltId); | |
| }); | |
| it("returns the provided id (not a new UUID) when id is specified", async () => { | |
| const prebuiltId = "550e8400-e29b-41d4-a716-446655440001"; | |
| const result = await createRunTracer({ name: "n", inputs: {}, outputs: {}, id: prebuiltId }); | |
| expect(result).toBe(prebuiltId); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // submitDimensionFeedback | |
| // --------------------------------------------------------------------------- | |
| describe("submitDimensionFeedback", () => { | |
| it("calls createFeedback with runId and dimensionId", async () => { | |
| await submitDimensionFeedback("run-123", "flaw-detection", 7); | |
| expect(mockCreateFeedback).toHaveBeenCalledOnce(); | |
| const [runId, dimId] = mockCreateFeedback.mock.calls[0]; | |
| expect(runId).toBe("run-123"); | |
| expect(dimId).toBe("flaw-detection"); | |
| }); | |
| it("normalises score: 7 becomes 0.7", async () => { | |
| await submitDimensionFeedback("run-123", "flaw-detection", 7); | |
| const payload = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(payload.score).toBeCloseTo(0.7, 5); | |
| }); | |
| it("normalises score: 0 becomes 0.0", async () => { | |
| await submitDimensionFeedback("run-abc", "dim", 0); | |
| const payload = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(payload.score).toBe(0); | |
| }); | |
| it("normalises score: 10 becomes 1.0", async () => { | |
| await submitDimensionFeedback("run-abc", "dim", 10); | |
| const payload = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(payload.score).toBe(1.0); | |
| }); | |
| it("clamps score above 10 to 1.0", async () => { | |
| await submitDimensionFeedback("run-abc", "dim", 15); | |
| const payload = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(payload.score).toBe(1.0); | |
| }); | |
| it("clamps score below 0 to 0.0", async () => { | |
| await submitDimensionFeedback("run-abc", "dim", -5); | |
| const payload = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(payload.score).toBe(0.0); | |
| }); | |
| it("sets a comment containing the dimensionId", async () => { | |
| await submitDimensionFeedback("run-xyz", "argument-structure", 8); | |
| const payload = mockCreateFeedback.mock.calls[0][2] as { comment: string }; | |
| expect(payload.comment).toContain("argument-structure"); | |
| }); | |
| it("propagates createFeedback errors", async () => { | |
| mockCreateFeedback.mockRejectedValueOnce(new Error("rate limit")); | |
| await expect( | |
| submitDimensionFeedback("run-err", "dim", 5), | |
| ).rejects.toThrow("rate limit"); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // submitAllDimensionFeedback | |
| // --------------------------------------------------------------------------- | |
| describe("submitAllDimensionFeedback", () => { | |
| it("calls createFeedback once per dimension", async () => { | |
| const scores = { | |
| "flaw-detection": 7, | |
| "steelman-awareness": 8, | |
| "argument-structure": 6, | |
| }; | |
| await submitAllDimensionFeedback("run-all", scores); | |
| expect(mockCreateFeedback).toHaveBeenCalledTimes(3); | |
| }); | |
| it("passes the correct runId to each call", async () => { | |
| await submitAllDimensionFeedback("run-id-check", { | |
| "persuasiveness": 5, | |
| "rebuttal-quality": 9, | |
| }); | |
| for (const call of mockCreateFeedback.mock.calls) { | |
| expect(call[0]).toBe("run-id-check"); | |
| } | |
| }); | |
| it("passes the correct dimension ID to each call", async () => { | |
| const scores = { "persuasiveness": 5, "intellectual-honesty": 9 }; | |
| await submitAllDimensionFeedback("run-dims", scores); | |
| const calledDims = mockCreateFeedback.mock.calls.map((c) => c[1]); | |
| expect(calledDims).toContain("persuasiveness"); | |
| expect(calledDims).toContain("intellectual-honesty"); | |
| }); | |
| it("passes the correct normalised score to each call", async () => { | |
| await submitAllDimensionFeedback("run-scores", { | |
| "flaw-detection": 3, | |
| "evidence-accuracy": 10, | |
| }); | |
| const scoresByDim: Record<string, number> = {}; | |
| for (const call of mockCreateFeedback.mock.calls) { | |
| const dimId = call[1] as string; | |
| const payload = call[2] as { score: number }; | |
| scoresByDim[dimId] = payload.score; | |
| } | |
| expect(scoresByDim["flaw-detection"]).toBeCloseTo(0.3, 5); | |
| expect(scoresByDim["evidence-accuracy"]).toBeCloseTo(1.0, 5); | |
| }); | |
| it("resolves when all dimensions succeed", async () => { | |
| await expect( | |
| submitAllDimensionFeedback("run-ok", { "flaw-detection": 5 }), | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("rejects if any individual dimension call throws", async () => { | |
| mockCreateFeedback.mockResolvedValueOnce(undefined); | |
| mockCreateFeedback.mockRejectedValueOnce(new Error("feedback error")); | |
| await expect( | |
| submitAllDimensionFeedback("run-fail", { | |
| "dim-a": 5, | |
| "dim-b": 7, | |
| }), | |
| ).rejects.toThrow("feedback error"); | |
| }); | |
| it("handles empty dimensionScores without calling createFeedback", async () => { | |
| await submitAllDimensionFeedback("run-empty", {}); | |
| expect(mockCreateFeedback).not.toHaveBeenCalled(); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // submitHumanFeedback | |
| // --------------------------------------------------------------------------- | |
| describe("submitHumanFeedback", () => { | |
| it("calls createFeedback with 'human_<dimensionId>' as the key", async () => { | |
| await submitHumanFeedback("run-abc", "argument-structure", 7); | |
| expect(mockCreateFeedback).toHaveBeenCalledOnce(); | |
| const [runId, key] = mockCreateFeedback.mock.calls[0]; | |
| expect(runId).toBe("run-abc"); | |
| expect(key).toBe("human_argument-structure"); | |
| }); | |
| it("normalises score: 7 becomes 0.7", async () => { | |
| await submitHumanFeedback("run-abc", "persuasiveness", 7); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(opts.score).toBeCloseTo(0.7, 5); | |
| }); | |
| it("normalises score: 0 becomes 0.0", async () => { | |
| await submitHumanFeedback("run-abc", "flaw-detection", 0); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(opts.score).toBe(0); | |
| }); | |
| it("normalises score: 10 becomes 1.0", async () => { | |
| await submitHumanFeedback("run-abc", "flaw-detection", 10); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(opts.score).toBe(1); | |
| }); | |
| it("clamps score above 10 to 1.0", async () => { | |
| await submitHumanFeedback("run-abc", "steelman-awareness", 15); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(opts.score).toBe(1); | |
| }); | |
| it("clamps score below 0 to 0.0", async () => { | |
| await submitHumanFeedback("run-abc", "rebuttal-quality", -5); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(opts.score).toBe(0); | |
| }); | |
| it("sets a comment mentioning the dimensionId", async () => { | |
| await submitHumanFeedback("run-abc", "intellectual-honesty", 6); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { comment: string }; | |
| expect(opts.comment).toContain("intellectual-honesty"); | |
| }); | |
| it("propagates createFeedback errors", async () => { | |
| mockCreateFeedback.mockRejectedValueOnce(new Error("feedback error")); | |
| await expect(submitHumanFeedback("run-abc", "persuasiveness", 5)).rejects.toThrow( | |
| "feedback error", | |
| ); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // submitAllHumanFeedback | |
| // --------------------------------------------------------------------------- | |
| describe("submitAllHumanFeedback", () => { | |
| it("calls createFeedback once per dimension", async () => { | |
| await submitAllHumanFeedback("run-xyz", { | |
| "argument-structure": 7, | |
| persuasiveness: 8, | |
| }); | |
| expect(mockCreateFeedback).toHaveBeenCalledTimes(2); | |
| }); | |
| it("uses 'human_<dim>' key for each entry", async () => { | |
| await submitAllHumanFeedback("run-xyz", { | |
| "flaw-detection": 5, | |
| "rebuttal-quality": 6, | |
| }); | |
| const keys = mockCreateFeedback.mock.calls.map( | |
| (c: unknown[]) => c[1] as string, | |
| ); | |
| expect(keys).toContain("human_flaw-detection"); | |
| expect(keys).toContain("human_rebuttal-quality"); | |
| }); | |
| it("passes the correct runId to each call", async () => { | |
| await submitAllHumanFeedback("run-feedback-test", { "argument-structure": 9 }); | |
| const runId = mockCreateFeedback.mock.calls[0][0]; | |
| expect(runId).toBe("run-feedback-test"); | |
| }); | |
| it("passes the correct normalised score to each call", async () => { | |
| await submitAllHumanFeedback("run-xyz", { persuasiveness: 4 }); | |
| const opts = mockCreateFeedback.mock.calls[0][2] as { score: number }; | |
| expect(opts.score).toBeCloseTo(0.4, 5); | |
| }); | |
| it("handles empty scores without calling createFeedback", async () => { | |
| await submitAllHumanFeedback("run-xyz", {}); | |
| expect(mockCreateFeedback).not.toHaveBeenCalled(); | |
| }); | |
| it("rejects if any individual submission throws", async () => { | |
| mockCreateFeedback.mockRejectedValueOnce(new Error("LangSmith down")); | |
| await expect( | |
| submitAllHumanFeedback("run-xyz", { "argument-structure": 7 }), | |
| ).rejects.toThrow("LangSmith down"); | |
| }); | |
| }); | |
| // --------------------------------------------------------------------------- | |
| // flushTraces | |
| // --------------------------------------------------------------------------- | |
| describe("flushTraces", () => { | |
| it("resolves without throwing", async () => { | |
| await expect(flushTraces()).resolves.toBeUndefined(); | |
| }); | |
| }); | |