114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
import { submitMyAssessmentAnswer } from "./endpoints/assessment/assessment";
|
|
import {
|
|
AssessmentAnswerSubmission,
|
|
AssessmentItem,
|
|
ItemContent,
|
|
SubmitMyAssessmentAnswerBody,
|
|
} from "./schemas";
|
|
|
|
// These tests pin the generated assessment contract that the FSD adapters
|
|
// consume. They fail when regeneration drops an answer format or adds a field
|
|
// that would carry a score, a key, or a seed between the client and the server.
|
|
|
|
const itemId = "01928f7a-4a10-7b2c-9d3e-5f6a7b8c9d01";
|
|
|
|
const answers = {
|
|
choice: { format: "choice", option_ids: ["x9"] },
|
|
number: { format: "number", number: 254 },
|
|
string: { format: "string", text: "Retry-After" },
|
|
order: { format: "order", element_ids: ["a7", "b1", "c2", "d3"] },
|
|
find_error: { format: "find_error", line_numbers: [5] },
|
|
configuration: { format: "configuration", config: "interface Gi0/1" },
|
|
practice: { format: "practice", language: "python", source_code: "print(1)" },
|
|
} as const;
|
|
|
|
const bodySchemas = [
|
|
["AssessmentAnswerSubmission", AssessmentAnswerSubmission],
|
|
["SubmitMyAssessmentAnswerBody", SubmitMyAssessmentAnswerBody],
|
|
] as const;
|
|
|
|
describe.each(bodySchemas)("generated %s", (_name, schema) => {
|
|
it.each(Object.entries(answers))("accepts a %s answer", (format, answer) => {
|
|
const parsed = schema.parse({ expected_version: 1, item_id: itemId, answer });
|
|
|
|
expect(parsed.answer.format).toBe(format);
|
|
});
|
|
|
|
it("rejects an unknown answer format", () => {
|
|
const result = schema.safeParse({
|
|
expected_version: 1,
|
|
item_id: itemId,
|
|
answer: { format: "essay", text: "..." },
|
|
});
|
|
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("rejects version 0, which no attempt has", () => {
|
|
const result = schema.safeParse({
|
|
expected_version: 0,
|
|
item_id: itemId,
|
|
answer: answers.choice,
|
|
});
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error?.issues).toEqual([expect.objectContaining({ path: ["expected_version"] })]);
|
|
});
|
|
|
|
it("never carries a client score or seed in the parsed body", () => {
|
|
const parsed = schema.parse({
|
|
expected_version: 1,
|
|
item_id: itemId,
|
|
score: 1,
|
|
answer: { ...answers.practice, seed: 42 },
|
|
});
|
|
|
|
// Generated Zod strips unknown keys; the server rejects them with 400.
|
|
expect(parsed).not.toHaveProperty("score");
|
|
expect(parsed.answer).not.toHaveProperty("seed");
|
|
});
|
|
});
|
|
|
|
it("parses a practice item without hidden checks", () => {
|
|
const parsed = AssessmentItem.parse({
|
|
id: itemId,
|
|
sequence: 15,
|
|
prompt: "Реализуйте функцию merge_intervals.",
|
|
content: {
|
|
format: "practice",
|
|
languages: ["python"],
|
|
max_source_length: 20000,
|
|
public_examples: [{ input: "[[1,3],[2,6]]", expected_output: "[[1,6]]" }],
|
|
hidden_tests: [{ input: "secret" }],
|
|
},
|
|
});
|
|
|
|
expect(parsed.content.format).toBe("practice");
|
|
expect(parsed.content).not.toHaveProperty("hidden_tests");
|
|
});
|
|
|
|
// Every key of every variant of a union.
|
|
type KeysOf<T> = T extends unknown ? keyof T : never;
|
|
type ForbiddenKey =
|
|
| "score"
|
|
| "points"
|
|
| "seed"
|
|
| "key"
|
|
| "answer_key"
|
|
| "correct"
|
|
| "is_correct"
|
|
| "correct_option_ids"
|
|
| "hidden_tests"
|
|
| "family_id";
|
|
|
|
it("exposes answer and item DTOs without scores, keys, seeds, or correctness", () => {
|
|
expectTypeOf<Extract<KeysOf<AssessmentAnswerSubmission>, ForbiddenKey>>().toEqualTypeOf<never>();
|
|
expectTypeOf<
|
|
Extract<KeysOf<AssessmentAnswerSubmission["answer"]>, ForbiddenKey>
|
|
>().toEqualTypeOf<never>();
|
|
expectTypeOf<Extract<KeysOf<AssessmentItem>, ForbiddenKey>>().toEqualTypeOf<never>();
|
|
expectTypeOf<Extract<KeysOf<ItemContent>, ForbiddenKey>>().toEqualTypeOf<never>();
|
|
expectTypeOf(submitMyAssessmentAnswer).parameter(1).toEqualTypeOf<AssessmentAnswerSubmission>();
|
|
});
|