From 391c8a6cec8943171a607e73c665f098816fcfcb Mon Sep 17 00:00:00 2001 From: shira Date: Tue, 6 Oct 2026 07:42:14 +0300 Subject: [PATCH] test(frontend): pin the generated assessment response contract --- .../api/assessment-response-contract.test.ts | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 frontend/src/shared/api/assessment-response-contract.test.ts diff --git a/frontend/src/shared/api/assessment-response-contract.test.ts b/frontend/src/shared/api/assessment-response-contract.test.ts new file mode 100644 index 0000000..cfd1fb7 --- /dev/null +++ b/frontend/src/shared/api/assessment-response-contract.test.ts @@ -0,0 +1,127 @@ +import { describe, expect, expectTypeOf, it } from "vitest"; +import { AssessmentAttempt, AssessmentResult } from "./schemas"; + +// These tests pin the generated response contract of the assessment client: +// a response that drops calibration_status or reports an unknown attempt state +// must fail the check, and the types must let the UI show an unfinished +// practice and a missing calibration without inventing numbers. The fixtures +// follow the AssessmentDemoBasicResult and AssessmentAttemptResumed examples +// of the source contract. + +const demoResult = { + attempt_id: "01928f7a-3c4d-7e5f-8a9b-0c1d2e3f4a5b", + specialization: "backend", + target_grade: "junior", + task_language: "go", + mode: "demo_basic", + versions: { + policy_version: 1, + bank_version: "backend-demo.2026-10.1", + algorithm_version: "basic-raw-score.1", + }, + outcome: "confirmed", + not_determined_reason: null, + confirmed_grade: "junior", + previous_confirmed_grade: null, + strength: null, + calibration_status: "uncalibrated", + limitations: ["uncalibrated_thresholds", "demo_bank", "practice_not_included", "unproctored"], + uncertainty: null, + basic_score: { points: 9, max_points: 12, pass_points: 8 }, + practices: [], + gaps: [], + durations: { items_seconds: 1260, practice_seconds: null }, + next_steps: [{ action: "take_higher_grade", grade: "middle", available_at: null }], + decided_at: "2026-10-08T10:24:00Z", +}; + +const pendingPracticeAttempt = { + id: "01928f7b-1111-7a2b-8c3d-4e5f6a7b8c9d", + version: 7, + specialization: "backend", + target_grade: "middle", + task_language: "python", + mode: "adaptive", + state: "practice_pending", + versions: { + policy_version: 2, + bank_version: "backend.2026-11.3", + algorithm_version: "irt-1pl-eap.1", + }, + started_at: "2026-11-20T09:00:00Z", + expires_at: "2026-11-23T09:00:00Z", + sessions: { used: 1, max: 2, idle_timeout_seconds: 1800 }, + finished_at: null, + progress: { answered_items: 25, max_items: 25 }, + current_item: null, + practices: [ + { + kind: "main", + item_id: "01928f7b-2222-7a2b-8c3d-4e5f6a7b8c99", + status: "pending", + submitted_at: "2026-11-20T10:00:00Z", + verdict: null, + failure_reason: null, + }, + ], +}; + +describe("generated AssessmentResult", () => { + it("accepts a demo result without uncertainty or strength numbers", () => { + const parsed = AssessmentResult.parse(demoResult); + + expect(parsed.calibration_status).toBe("uncalibrated"); + expect(parsed.uncertainty).toBeNull(); + expect(parsed.strength).toBeNull(); + }); + + it("rejects a result without calibration_status", () => { + const result: Record = { ...demoResult }; + delete result.calibration_status; + + const parsed = AssessmentResult.safeParse(result); + + expect(parsed.success).toBe(false); + expect(parsed.error?.issues).toEqual([ + expect.objectContaining({ path: ["calibration_status"] }), + ]); + }); + + it("rejects an unknown calibration status", () => { + const parsed = AssessmentResult.safeParse({ ...demoResult, calibration_status: "estimated" }); + + expect(parsed.success).toBe(false); + expect(parsed.error?.issues).toEqual([ + expect.objectContaining({ code: "invalid_value", path: ["calibration_status"] }), + ]); + }); +}); + +describe("generated AssessmentAttempt", () => { + it("accepts an unfinished practice without a verdict", () => { + const parsed = AssessmentAttempt.parse(pendingPracticeAttempt); + + expect(parsed.practices[0]?.status).toBe("pending"); + expect(parsed.practices[0]?.verdict).toBeNull(); + }); + + it("rejects an unknown attempt state", () => { + const parsed = AssessmentAttempt.safeParse({ ...pendingPracticeAttempt, state: "paused" }); + + expect(parsed.success).toBe(false); + expect(parsed.error?.issues).toEqual([ + expect.objectContaining({ code: "invalid_value", path: ["state"] }), + ]); + }); +}); + +it("types allow null instead of fabricated numbers", () => { + type Practice = AssessmentAttempt["practices"][number]; + + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); + expectTypeOf().toExtend(); + expectTypeOf<"uncalibrated">().toExtend(); + expectTypeOf<"pending">().toExtend(); +});