344 lines
12 KiB
TypeScript
344 lines
12 KiB
TypeScript
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
import { createOrganizationNeed, updateOrganizationNeed } from "./endpoints/need/need";
|
|
import {
|
|
CreateOrganizationNeedBody,
|
|
Need,
|
|
NeedCategoryRecommendation,
|
|
NeedCreate,
|
|
NeedUpdate,
|
|
SalaryRange,
|
|
UpdateOrganizationNeedBody,
|
|
} from "./schemas";
|
|
|
|
// These tests pin the generated need contract that the FSD adapters consume.
|
|
// They fail when regeneration drops a salary limit, an unknown state, or the
|
|
// explicit confirmation status of a field, or adds a request field that would
|
|
// let an employer set membership, a candidate's minimum, or a ready
|
|
// recommendation score.
|
|
|
|
const salary = {
|
|
salary_from: 120000,
|
|
salary_to: 160000,
|
|
currency: "RUB",
|
|
period: "month",
|
|
tax_basis: "net",
|
|
} as const;
|
|
|
|
const manualNeed = {
|
|
title: "Junior Go-разработчик для сервиса уведомлений",
|
|
specialization: { value: "backend", status: "confirmed" },
|
|
grade: { value: "junior", status: "confirmed" },
|
|
required_skills: [
|
|
{ name: "Go", status: "confirmed" },
|
|
{ name: "PostgreSQL", status: "confirmed" },
|
|
],
|
|
desired_skills: [{ name: "Docker", status: "confirmed" }],
|
|
tasks: [{ text: "Поддерживать и развивать сервис уведомлений", status: "confirmed" }],
|
|
work_formats: { value: ["hybrid"], status: "confirmed" },
|
|
location: { value: "Москва", status: "confirmed" },
|
|
salary: { value: salary, status: "confirmed" },
|
|
} as const;
|
|
|
|
const createSchemas = [
|
|
["NeedCreate", NeedCreate],
|
|
["CreateOrganizationNeedBody", CreateOrganizationNeedBody],
|
|
] as const;
|
|
|
|
describe.each(createSchemas)("generated %s", (_name, schema) => {
|
|
it("accepts a manual need without a vacancy", () => {
|
|
const parsed = schema.parse(manualNeed);
|
|
|
|
expect(parsed).not.toHaveProperty("vacancy_id");
|
|
expect(parsed.salary).toEqual({ value: salary, status: "confirmed" });
|
|
});
|
|
|
|
it("accepts a need from a title and a text only", () => {
|
|
const parsed = schema.parse({ title: "Backend", source_text: "Ищем бэкенд-разработчика." });
|
|
|
|
expect(parsed).toEqual({ title: "Backend", source_text: "Ищем бэкенд-разработчика." });
|
|
});
|
|
|
|
it("keeps an explicit null as a field without a value", () => {
|
|
const parsed = schema.parse({ title: "Backend", vacancy_id: null, grade: null, salary: null });
|
|
|
|
expect(parsed).toEqual({ title: "Backend", vacancy_id: null, grade: null, salary: null });
|
|
});
|
|
|
|
it("requires a title", () => {
|
|
const result = schema.safeParse({ specialization: { value: "backend", status: "confirmed" } });
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error?.issues).toEqual([expect.objectContaining({ path: ["title"] })]);
|
|
});
|
|
|
|
it.each([
|
|
["a bare value", "junior"],
|
|
["a value without a status", { value: "junior" }],
|
|
["an unknown status", { value: "junior", status: "unknown" }],
|
|
["a grade outside the scale", { value: "lead", status: "confirmed" }],
|
|
])("rejects a grade decision with %s", (_case, grade) => {
|
|
const result = schema.safeParse({ ...manualNeed, grade });
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error?.issues).toEqual([
|
|
expect.objectContaining({ path: expect.arrayContaining(["grade"]) }),
|
|
]);
|
|
});
|
|
|
|
it.each([
|
|
["a zero lower bound", { salary_from: 0 }, "salary_from"],
|
|
["a negative lower bound", { salary_from: -1000 }, "salary_from"],
|
|
["a bound that is not a multiple of 1000", { salary_to: 160500 }, "salary_to"],
|
|
["a fractional bound", { salary_to: 160000.5 }, "salary_to"],
|
|
["another currency", { currency: "USD" }, "currency"],
|
|
["an hourly period", { period: "hour" }, "period"],
|
|
["an unknown tax basis", { tax_basis: "after_tax" }, "tax_basis"],
|
|
])("rejects a salary with %s", (_case, change, field) => {
|
|
const range = { ...salary, ...change };
|
|
const result = schema.safeParse({
|
|
...manualNeed,
|
|
salary: { value: range, status: "confirmed" },
|
|
});
|
|
|
|
// The nullable salary is a union, so Zod reports some failures on the
|
|
// union and others on the field; the range schema itself names the field.
|
|
expect(result.success).toBe(false);
|
|
expect(result.error?.issues).toEqual([
|
|
expect.objectContaining({ path: expect.arrayContaining(["salary"]) }),
|
|
]);
|
|
expect(SalaryRange.safeParse(range).error?.issues).toEqual([
|
|
expect.objectContaining({ path: [field] }),
|
|
]);
|
|
});
|
|
|
|
it("rejects a salary without a tax basis or without a decision", () => {
|
|
const withoutBasis = {
|
|
salary_from: salary.salary_from,
|
|
salary_to: salary.salary_to,
|
|
currency: salary.currency,
|
|
period: salary.period,
|
|
};
|
|
|
|
expect(
|
|
schema.safeParse({ ...manualNeed, salary: { value: withoutBasis, status: "confirmed" } })
|
|
.success,
|
|
).toBe(false);
|
|
expect(schema.safeParse({ ...manualNeed, salary }).success).toBe(false);
|
|
});
|
|
|
|
it("leaves the cross-field salary rules to the server", () => {
|
|
// A < B and the 10-40% width compare two fields; the server answers 400
|
|
// with the pointer /salary/value/salary_to.
|
|
for (const [from, to] of [
|
|
[150000, 150000],
|
|
[100000, 200000],
|
|
]) {
|
|
const range = { ...salary, salary_from: from, salary_to: to };
|
|
|
|
expect(SalaryRange.safeParse(range).success).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("rejects an unknown item status and empty work formats", () => {
|
|
expect(
|
|
schema.safeParse({ ...manualNeed, required_skills: [{ name: "Go", status: "unknown" }] })
|
|
.success,
|
|
).toBe(false);
|
|
expect(
|
|
schema.safeParse({ ...manualNeed, work_formats: { value: [], status: "confirmed" } }).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("never carries membership, a candidate minimum, or a score in the parsed body", () => {
|
|
const parsed = schema.parse({
|
|
...manualNeed,
|
|
rec_score: 0.99,
|
|
members: [{ user_id: "01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01", role: "owner" }],
|
|
candidate_minimum: 130000,
|
|
salary: { value: { ...salary, candidate_minimum: 130000 }, status: "confirmed" },
|
|
});
|
|
|
|
// Generated Zod strips unknown keys; the server rejects them with 400.
|
|
expect(parsed).not.toHaveProperty("rec_score");
|
|
expect(parsed).not.toHaveProperty("members");
|
|
expect(parsed).not.toHaveProperty("candidate_minimum");
|
|
expect(parsed.salary?.value).not.toHaveProperty("candidate_minimum");
|
|
});
|
|
});
|
|
|
|
const updateSchemas = [
|
|
["NeedUpdate", NeedUpdate],
|
|
["UpdateOrganizationNeedBody", UpdateOrganizationNeedBody],
|
|
] as const;
|
|
|
|
describe.each(updateSchemas)("generated %s", (_name, schema) => {
|
|
it("confirms suggestions and keeps others open explicitly", () => {
|
|
const parsed = schema.parse({
|
|
expected_version: 1,
|
|
specialization: { value: "backend", status: "confirmed" },
|
|
grade: { value: "middle", status: "confirmed" },
|
|
domain: { value: "платежи", status: "extracted" },
|
|
required_skills: [{ name: "Temporal", status: "confirmed" }],
|
|
desired_skills: [{ name: "Kafka", status: "extracted" }],
|
|
salary: { value: { ...salary, tax_basis: "gross" }, status: "confirmed" },
|
|
});
|
|
|
|
expect(parsed.domain).toEqual({ value: "платежи", status: "extracted" });
|
|
expect(parsed.desired_skills).toEqual([{ name: "Kafka", status: "extracted" }]);
|
|
});
|
|
|
|
it("keeps null, which removes a requirement, apart from an absent field", () => {
|
|
const parsed = schema.parse({ expected_version: 3, grade: null, salary: null, tasks: [] });
|
|
|
|
expect(parsed).toEqual({ expected_version: 3, grade: null, salary: null, tasks: [] });
|
|
expect(parsed).not.toHaveProperty("specialization");
|
|
});
|
|
|
|
it("rejects a list sent as null", () => {
|
|
expect(schema.safeParse({ expected_version: 3, tasks: null }).success).toBe(false);
|
|
});
|
|
|
|
it.each([
|
|
["missing", {}],
|
|
["zero", { expected_version: 0 }],
|
|
["fractional", { expected_version: 1.5 }],
|
|
["text", { expected_version: "1" }],
|
|
])("rejects an expected_version that is %s", (_case, version) => {
|
|
const result = schema.safeParse({ ...version, grade: null });
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error?.issues).toEqual([expect.objectContaining({ path: ["expected_version"] })]);
|
|
});
|
|
});
|
|
|
|
it("parses a need whose unknown fields stay null instead of guessed values", () => {
|
|
const unknown = { status: "unknown", value: null, source: null } as const;
|
|
const parsed = Need.parse({
|
|
id: "01928f7a-3c4d-7e5f-8a6b-7c8d9e0f1a23",
|
|
organization_id: "01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01",
|
|
vacancy_id: null,
|
|
title: "Backend в команду платежей",
|
|
source_text: "Ищем бэкенд-разработчика. Обязательно: Go и Temporal.",
|
|
version: 1,
|
|
requirements_version: 1,
|
|
requirements: {
|
|
specialization: {
|
|
status: "extracted",
|
|
value: "backend",
|
|
source: {
|
|
origin: "extracted_from_text",
|
|
quote: "Ищем бэкенд-разработчика",
|
|
extracted_in_version: 1,
|
|
extraction_version: "need-rules.2026-10.1",
|
|
},
|
|
},
|
|
grade: unknown,
|
|
required_skills: [
|
|
{
|
|
name: "Temporal",
|
|
taxonomy_code: null,
|
|
status: "extracted",
|
|
source: {
|
|
origin: "extracted_from_text",
|
|
quote: "Обязательно: Go и Temporal",
|
|
extracted_in_version: 1,
|
|
extraction_version: "need-rules.2026-10.1",
|
|
},
|
|
},
|
|
],
|
|
desired_skills: [],
|
|
tasks: [],
|
|
domain: unknown,
|
|
work_formats: unknown,
|
|
location: unknown,
|
|
salary: unknown,
|
|
},
|
|
readiness: {
|
|
status: "incomplete",
|
|
missing_fields: [
|
|
{
|
|
field: "grade",
|
|
reason: "unknown",
|
|
question: "Какой грейд нужен: Intern, Junior, Middle или Senior?",
|
|
},
|
|
],
|
|
},
|
|
recommendations: {
|
|
status: "not_available",
|
|
requirements_version: 1,
|
|
policy_version: null,
|
|
algorithm_version: null,
|
|
computed_at: null,
|
|
categories: [],
|
|
},
|
|
created_at: "2026-10-06T09:00:00Z",
|
|
updated_at: "2026-10-06T09:00:00Z",
|
|
});
|
|
|
|
expect(parsed.requirements.grade).toEqual(unknown);
|
|
expect(parsed.requirements.salary.value).toBeNull();
|
|
expect(parsed.requirements.required_skills[0]?.taxonomy_code).toBeNull();
|
|
expect(parsed.requirements.required_skills[0]?.source.extracted_in_version).toBe(1);
|
|
});
|
|
|
|
it("parses a recommendation whose small group hides the score", () => {
|
|
const parsed = NeedCategoryRecommendation.parse({
|
|
specialization: "backend",
|
|
grade: "intern",
|
|
score: null,
|
|
skill_overlap: { share: 0.8, covered_skills: ["Go"], uncovered_skills: ["Docker"] },
|
|
money: { status: "withheld", share: null },
|
|
supply: "fewer_than_5",
|
|
});
|
|
|
|
expect(parsed.score).toBeNull();
|
|
expect(
|
|
NeedCategoryRecommendation.safeParse({ ...parsed, money: { status: "hidden", share: null } })
|
|
.success,
|
|
).toBe(false);
|
|
});
|
|
|
|
type ForbiddenRequestKey =
|
|
| "rec_score"
|
|
| "score"
|
|
| "recommendations"
|
|
| "candidate_minimum"
|
|
| "salary_minimum"
|
|
| "minimum"
|
|
| "members"
|
|
| "membership"
|
|
| "role"
|
|
| "organization_id"
|
|
| "version"
|
|
| "requirements_version"
|
|
| "readiness"
|
|
| "source"
|
|
| "origin"
|
|
| "quote";
|
|
|
|
type ItemOf<T> = T extends readonly (infer Item)[] ? Item : never;
|
|
|
|
it("exposes request DTOs without membership, candidate minimum, or recommendation fields", () => {
|
|
expectTypeOf<Extract<keyof NeedCreate, ForbiddenRequestKey>>().toEqualTypeOf<never>();
|
|
expectTypeOf<Extract<keyof NeedUpdate, ForbiddenRequestKey>>().toEqualTypeOf<never>();
|
|
expectTypeOf<
|
|
Extract<keyof NonNullable<NeedCreate["salary"]>, ForbiddenRequestKey>
|
|
>().toEqualTypeOf<never>();
|
|
expectTypeOf<
|
|
Extract<keyof NonNullable<NeedUpdate["salary"]>["value"], ForbiddenRequestKey>
|
|
>().toEqualTypeOf<never>();
|
|
expectTypeOf<
|
|
Extract<keyof NonNullable<NeedUpdate["grade"]>, ForbiddenRequestKey>
|
|
>().toEqualTypeOf<never>();
|
|
expectTypeOf<
|
|
Extract<keyof ItemOf<NonNullable<NeedUpdate["required_skills"]>>, ForbiddenRequestKey>
|
|
>().toEqualTypeOf<never>();
|
|
// A field decision always carries an explicit status, so a round-tripped
|
|
// form cannot confirm a suggestion without saying so.
|
|
expectTypeOf<NonNullable<NeedUpdate["grade"]>["status"]>().toEqualTypeOf<
|
|
"confirmed" | "extracted"
|
|
>();
|
|
expectTypeOf(createOrganizationNeed).parameter(1).toEqualTypeOf<NeedCreate>();
|
|
expectTypeOf(updateOrganizationNeed).parameter(2).toEqualTypeOf<NeedUpdate>();
|
|
});
|