240 lines
8.0 KiB
TypeScript
240 lines
8.0 KiB
TypeScript
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",
|
|
kind: "root",
|
|
root_attempt_id: null,
|
|
chain_deadline: null,
|
|
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 correctiveResult = {
|
|
...demoResult,
|
|
target_grade: "middle",
|
|
kind: "corrective",
|
|
root_attempt_id: "01928f7b-1111-7a2b-8c3d-4e5f6a7b8c9d",
|
|
chain_deadline: "2026-11-23T09:00:00Z",
|
|
};
|
|
|
|
const expressResult = {
|
|
attempt_id: "01928f7f-9999-7a2b-8c3d-4e5f6a7b8c9d",
|
|
specialization: null,
|
|
target_grade: null,
|
|
task_language: null,
|
|
kind: "demo_express",
|
|
mode: "demo_express",
|
|
versions: {
|
|
policy_version: 1,
|
|
bank_version: "express-demo.2026-10.1",
|
|
algorithm_version: "express-interactive.1",
|
|
},
|
|
outcome: "demonstration",
|
|
not_determined_reason: null,
|
|
confirmed_grade: null,
|
|
previous_confirmed_grade: null,
|
|
strength: null,
|
|
calibration_status: "uncalibrated",
|
|
limitations: ["demonstration_only"],
|
|
uncertainty: null,
|
|
basic_score: null,
|
|
practices: [],
|
|
gaps: [],
|
|
durations: { items_seconds: 260, practice_seconds: null },
|
|
next_steps: [],
|
|
summary: "This short run demonstrates the task flow.",
|
|
decided_at: "2026-10-08T10:05:00Z",
|
|
};
|
|
|
|
const pendingPracticeAttempt = {
|
|
id: "01928f7b-1111-7a2b-8c3d-4e5f6a7b8c9d",
|
|
version: 7,
|
|
kind: "root",
|
|
specialization: "backend",
|
|
target_grade: "middle",
|
|
task_language: "python",
|
|
root_attempt_id: null,
|
|
chain_deadline: null,
|
|
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,
|
|
},
|
|
],
|
|
};
|
|
|
|
const correctivePracticeAttempt = {
|
|
...pendingPracticeAttempt,
|
|
kind: "corrective",
|
|
root_attempt_id: "01928f7b-2222-7a2b-8c3d-4e5f6a7b8c9d",
|
|
chain_deadline: "2026-11-23T09:00:00Z",
|
|
};
|
|
|
|
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("requires an explicit null pair for a root result", () => {
|
|
expect(AssessmentResult.parse(demoResult)).toMatchObject({
|
|
kind: "root",
|
|
root_attempt_id: null,
|
|
chain_deadline: null,
|
|
});
|
|
|
|
const missingPair: Record<string, unknown> = { ...demoResult };
|
|
delete missingPair.chain_deadline;
|
|
|
|
expect(AssessmentResult.safeParse(missingPair).success).toBe(false);
|
|
expect(
|
|
AssessmentResult.safeParse({
|
|
...demoResult,
|
|
root_attempt_id: "01928f7b-1111-7a2b-8c3d-4e5f6a7b8c9d",
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("requires both correction-chain values together", () => {
|
|
expect(AssessmentResult.parse(correctiveResult).kind).toBe("corrective");
|
|
expect(AssessmentResult.safeParse({ ...correctiveResult, root_attempt_id: null }).success).toBe(
|
|
false,
|
|
);
|
|
expect(AssessmentResult.safeParse({ ...correctiveResult, chain_deadline: null }).success).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("keeps demo_express training results from confirming qualification", () => {
|
|
const parsed = AssessmentResult.parse(expressResult);
|
|
|
|
expect(parsed).toMatchObject({
|
|
kind: "demo_express",
|
|
outcome: "demonstration",
|
|
confirmed_grade: null,
|
|
specialization: null,
|
|
target_grade: null,
|
|
uncertainty: null,
|
|
basic_score: null,
|
|
});
|
|
expect(
|
|
AssessmentResult.safeParse({ ...expressResult, confirmed_grade: "junior" }).success,
|
|
).toBe(false);
|
|
expect(AssessmentResult.safeParse({ ...expressResult, outcome: "confirmed" }).success).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("rejects a result without calibration_status", () => {
|
|
const result: Record<string, unknown> = { ...demoResult };
|
|
delete result.calibration_status;
|
|
|
|
const parsed = AssessmentResult.safeParse(result);
|
|
|
|
expect(parsed.success).toBe(false);
|
|
expect(parsed.error?.issues[0]).toMatchObject({ code: "invalid_union" });
|
|
expect(JSON.stringify(parsed.error?.issues)).toContain('"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[0]).toMatchObject({ code: "invalid_union" });
|
|
expect(JSON.stringify(parsed.error?.issues)).toContain('"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("requires both correction-chain values together", () => {
|
|
expect(AssessmentAttempt.parse(correctivePracticeAttempt).kind).toBe("corrective");
|
|
expect(
|
|
AssessmentAttempt.safeParse({ ...correctivePracticeAttempt, root_attempt_id: null }).success,
|
|
).toBe(false);
|
|
expect(
|
|
AssessmentAttempt.safeParse({ ...correctivePracticeAttempt, chain_deadline: null }).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects an unknown attempt state", () => {
|
|
const parsed = AssessmentAttempt.safeParse({ ...pendingPracticeAttempt, state: "paused" });
|
|
|
|
expect(parsed.success).toBe(false);
|
|
expect(parsed.error?.issues[0]).toMatchObject({ code: "invalid_union" });
|
|
expect(JSON.stringify(parsed.error?.issues)).toContain('"state"');
|
|
});
|
|
});
|
|
|
|
it("types allow null instead of fabricated numbers", () => {
|
|
type Practice = AssessmentAttempt["practices"][number];
|
|
type ExpressResult = Extract<AssessmentResult, { kind: "demo_express" }>;
|
|
|
|
expectTypeOf<null>().toExtend<AssessmentResult["uncertainty"]>();
|
|
expectTypeOf<null>().toExtend<AssessmentResult["basic_score"]>();
|
|
expectTypeOf<null>().toExtend<AssessmentResult["strength"]>();
|
|
expectTypeOf<null>().toExtend<Practice["verdict"]>();
|
|
expectTypeOf<"uncalibrated">().toExtend<AssessmentResult["calibration_status"]>();
|
|
expectTypeOf<"pending">().toExtend<Practice["status"]>();
|
|
expectTypeOf<ExpressResult["confirmed_grade"]>().toEqualTypeOf<null>();
|
|
expectTypeOf<ExpressResult["target_grade"]>().toEqualTypeOf<null>();
|
|
expectTypeOf<
|
|
Extract<keyof ExpressResult, "percentile" | "qualification_evidence">
|
|
>().toEqualTypeOf<never>();
|
|
});
|