290 lines
9.9 KiB
TypeScript
290 lines
9.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type {
|
|
CandidateQuestionnaire,
|
|
ProblemFieldError,
|
|
SpecializationCatalog,
|
|
} from "../../../shared/api/schemas";
|
|
import {
|
|
addQuestionnaireSpecialization,
|
|
addQuestionnaireStackTag,
|
|
buildCandidateQuestionnaireUpdate,
|
|
changeQuestionnaireSpecialization,
|
|
mapQuestionnaireServerErrors,
|
|
patchQuestionnaireSpecialization,
|
|
removeQuestionnaireSpecialization,
|
|
toQuestionnaireDraft,
|
|
toggleQuestionnaireWorkFormat,
|
|
questionnaireStepOfField,
|
|
} from "./questionnaire-form";
|
|
|
|
const catalog: SpecializationCatalog = {
|
|
directions: [
|
|
{
|
|
code: "development",
|
|
title: "Разработка",
|
|
specializations: [
|
|
{
|
|
code: "backend",
|
|
title: "Backend",
|
|
test_availability: {
|
|
status: "available",
|
|
grades: ["junior"],
|
|
task_languages: ["python", "go"],
|
|
note: null,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "data",
|
|
title: "Данные",
|
|
specializations: [
|
|
{
|
|
code: "ds_ml",
|
|
title: "DS/ML",
|
|
test_availability: {
|
|
status: "in_development",
|
|
grades: [],
|
|
task_languages: [],
|
|
note: "Банк заданий готовится.",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
grades: [
|
|
{ code: "intern", title: "Стажер" },
|
|
{ code: "junior", title: "Junior" },
|
|
{ code: "middle", title: "Middle" },
|
|
{ code: "senior", title: "Senior" },
|
|
],
|
|
};
|
|
|
|
const emptyQuestionnaire: CandidateQuestionnaire = {
|
|
version: 0,
|
|
specializations: [],
|
|
work_context: null,
|
|
updated_at: null,
|
|
};
|
|
|
|
function completedDraft() {
|
|
const base = toQuestionnaireDraft(emptyQuestionnaire);
|
|
const item = base.specializations[0];
|
|
if (!item) throw new Error("The empty questionnaire draft should include one editing row.");
|
|
const selected = changeQuestionnaireSpecialization(base, item.id, "backend");
|
|
return {
|
|
...patchQuestionnaireSpecialization(selected, item.id, {
|
|
selfAssessedGrade: "junior",
|
|
taskLanguages: ["go"],
|
|
}),
|
|
workFormats: ["remote" as const],
|
|
};
|
|
}
|
|
|
|
describe("questionnaire form model", () => {
|
|
it("restores server fields and the first unsaved form starts at version zero", () => {
|
|
const empty = toQuestionnaireDraft(emptyQuestionnaire);
|
|
expect(empty.expectedVersion).toBe(0);
|
|
expect(empty.specializations).toHaveLength(1);
|
|
expect(empty.specializations[0]?.specialization).toBe("");
|
|
|
|
const stored: CandidateQuestionnaire = {
|
|
version: 3,
|
|
specializations: [
|
|
{
|
|
specialization: "backend",
|
|
self_assessed_grade: "junior",
|
|
task_languages: ["go"],
|
|
stack: ["PostgreSQL"],
|
|
experience: { commercial_months: 12, academic_months: 6 },
|
|
},
|
|
],
|
|
work_context: { work_formats: ["remote", "hybrid"] },
|
|
updated_at: "2026-10-07T12:00:00Z",
|
|
};
|
|
expect(toQuestionnaireDraft(stored)).toMatchObject({
|
|
expectedVersion: 3,
|
|
workFormats: ["remote", "hybrid"],
|
|
specializations: [
|
|
{
|
|
specialization: "backend",
|
|
selfAssessedGrade: "junior",
|
|
taskLanguages: ["go"],
|
|
stack: ["PostgreSQL"],
|
|
hasExperience: true,
|
|
commercialMonths: "12",
|
|
academicMonths: "6",
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it("builds the full Backend/Junior/Go questionnaire without requiring an FSP", () => {
|
|
const result = buildCandidateQuestionnaireUpdate(completedDraft(), catalog);
|
|
expect(result.issues).toEqual([]);
|
|
expect(result.data).toEqual({
|
|
expected_version: 0,
|
|
specializations: [
|
|
{
|
|
specialization: "backend",
|
|
self_assessed_grade: "junior",
|
|
task_languages: ["go"],
|
|
stack: [],
|
|
experience: null,
|
|
},
|
|
],
|
|
work_context: { work_formats: ["remote"] },
|
|
});
|
|
});
|
|
|
|
it("allows an in-development specialization as a self-assessed claim", () => {
|
|
const base = completedDraft();
|
|
const item = base.specializations[0];
|
|
if (!item) throw new Error("Expected one specialization row.");
|
|
const selected = changeQuestionnaireSpecialization(base, item.id, "ds_ml");
|
|
const developmentDraft = patchQuestionnaireSpecialization(selected, item.id, {
|
|
selfAssessedGrade: "senior",
|
|
taskLanguages: [],
|
|
});
|
|
const result = buildCandidateQuestionnaireUpdate(developmentDraft, catalog);
|
|
expect(result.issues).toEqual([]);
|
|
expect(result.data?.specializations[0]).toMatchObject({
|
|
specialization: "ds_ml",
|
|
self_assessed_grade: "senior",
|
|
task_languages: [],
|
|
});
|
|
expect(result.data?.specializations[0]).not.toHaveProperty("confirmed_grade");
|
|
});
|
|
|
|
it("rejects duplicate specializations, unavailable languages, and invalid months", () => {
|
|
const base = completedDraft();
|
|
const first = base.specializations[0];
|
|
if (!first) throw new Error("Expected one specialization row.");
|
|
let draft = addQuestionnaireSpecialization(base, "new-second");
|
|
draft = changeQuestionnaireSpecialization(draft, "new-second", "backend");
|
|
draft = patchQuestionnaireSpecialization(draft, "new-second", {
|
|
selfAssessedGrade: "junior",
|
|
});
|
|
draft = patchQuestionnaireSpecialization(draft, first.id, {
|
|
hasExperience: true,
|
|
commercialMonths: "601",
|
|
academicMonths: "-1",
|
|
taskLanguages: ["python"],
|
|
});
|
|
const restrictedCatalog: SpecializationCatalog = {
|
|
...catalog,
|
|
directions: catalog.directions.map((direction) => ({
|
|
...direction,
|
|
specializations: direction.specializations.map((specialization) =>
|
|
specialization.code === "backend"
|
|
? {
|
|
...specialization,
|
|
test_availability: {
|
|
...specialization.test_availability,
|
|
task_languages: ["go"],
|
|
},
|
|
}
|
|
: specialization,
|
|
),
|
|
})),
|
|
};
|
|
const result = buildCandidateQuestionnaireUpdate(draft, restrictedCatalog);
|
|
expect(result.data).toBeNull();
|
|
expect(result.issues.map((issue) => issue.field)).toContain("specializations.1.specialization");
|
|
expect(result.issues.map((issue) => issue.field)).toContain(
|
|
"specializations.0.commercialMonths",
|
|
);
|
|
expect(result.issues.map((issue) => issue.field)).toContain("specializations.0.academicMonths");
|
|
expect(result.issues.map((issue) => issue.field)).toContain("specializations.0.taskLanguages");
|
|
});
|
|
|
|
it("resets specialization-specific answers when the candidate changes the selected discipline", () => {
|
|
const base = completedDraft();
|
|
const item = base.specializations[0];
|
|
if (!item) throw new Error("Expected one specialization row.");
|
|
const changed = patchQuestionnaireSpecialization(base, item.id, {
|
|
stack: ["Go"],
|
|
hasExperience: true,
|
|
commercialMonths: "15",
|
|
academicMonths: "0",
|
|
});
|
|
const changedSpecialization = changeQuestionnaireSpecialization(changed, item.id, "ds_ml");
|
|
expect(changedSpecialization.specializations[0]).toMatchObject({
|
|
specialization: "ds_ml",
|
|
stack: [],
|
|
hasExperience: false,
|
|
commercialMonths: "",
|
|
academicMonths: "",
|
|
selfAssessedGrade: "",
|
|
taskLanguages: [],
|
|
});
|
|
});
|
|
|
|
it("keeps one or two specialization rows and at most three work formats", () => {
|
|
let draft = toQuestionnaireDraft(emptyQuestionnaire);
|
|
draft = addQuestionnaireSpecialization(draft, "second");
|
|
expect(draft.specializations).toHaveLength(2);
|
|
expect(addQuestionnaireSpecialization(draft, "third")).toBe(draft);
|
|
draft = removeQuestionnaireSpecialization(draft, "second");
|
|
expect(draft.specializations).toHaveLength(1);
|
|
expect(removeQuestionnaireSpecialization(draft, draft.specializations[0]?.id ?? "")).toBe(
|
|
draft,
|
|
);
|
|
|
|
draft = toggleQuestionnaireWorkFormat(draft, "remote");
|
|
draft = toggleQuestionnaireWorkFormat(draft, "office");
|
|
draft = toggleQuestionnaireWorkFormat(draft, "hybrid");
|
|
expect(toggleQuestionnaireWorkFormat(draft, "remote").workFormats).toEqual([
|
|
"office",
|
|
"hybrid",
|
|
]);
|
|
expect(toggleQuestionnaireWorkFormat(draft, "office").workFormats).toEqual([
|
|
"remote",
|
|
"hybrid",
|
|
]);
|
|
});
|
|
|
|
it("adds normalized stack tags and maps server pointers to the matching form field", () => {
|
|
const base = completedDraft();
|
|
const item = base.specializations[0];
|
|
if (!item) throw new Error("Expected one specialization row.");
|
|
const withInput = patchQuestionnaireSpecialization(base, item.id, { stackInput: " Go " });
|
|
const added = addQuestionnaireStackTag(withInput, item.id);
|
|
expect(added.issue).toBeNull();
|
|
expect(added.draft.specializations[0]?.stack).toEqual(["Go"]);
|
|
|
|
const serverErrors: ProblemFieldError[] = [
|
|
{
|
|
in: "body",
|
|
pointer: "/specializations/0/task_languages/0",
|
|
code: "invalid",
|
|
message: "invalid",
|
|
},
|
|
{
|
|
in: "body",
|
|
pointer: "/work_context/work_formats/0",
|
|
code: "invalid",
|
|
message: "invalid",
|
|
},
|
|
];
|
|
expect(mapQuestionnaireServerErrors(serverErrors).map((issue) => issue.field)).toEqual([
|
|
"specializations.0.taskLanguages",
|
|
"workFormats",
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("questionnaire steps", () => {
|
|
it("opens the step that owns a client or server issue", () => {
|
|
expect(questionnaireStepOfField("specializations")).toBe("specializations");
|
|
expect(questionnaireStepOfField("specializations.1.specialization")).toBe("specializations");
|
|
expect(questionnaireStepOfField("specializations.0.selfAssessedGrade")).toBe("details");
|
|
expect(questionnaireStepOfField("specializations.0.stack.3")).toBe("details");
|
|
expect(questionnaireStepOfField("specializations.1")).toBe("details");
|
|
expect(questionnaireStepOfField("workFormats")).toBe("work");
|
|
expect(questionnaireStepOfField("work_context.work_formats")).toBe("work");
|
|
expect(questionnaireStepOfField("form")).toBe("review");
|
|
expect(questionnaireStepOfField("expected_version")).toBe("review");
|
|
});
|
|
});
|