import { readFileSync, readdirSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, expectTypeOf, it } from "vitest"; import { setMyPublication, setMySearchStatus } from "./endpoints/privacy/privacy"; import { AnonymousCardContent, CardField, CardPreview, HiddenOrganization, PrivacySettings, Publication, PublicationUpdate, SearchStatus, SearchStatusUpdate, } from "./schemas"; // These tests pin the generated visibility contract that the // privacy settings screen consumes. They fail when regeneration lets a // request name a consent or a time, lets a card field name a name, a contact, // or the salary, lets the anonymous card gain a contact, mixes the search // status with a consent, or leaks a setting into an organization DTO. const consentId = "01928f7a-7a8b-7c9d-8e0f-1a2b3c4d5e60"; const closedPublication = { state: "closed", closed_reason: "never_published", fields: [], consent_id: null, published_at: null, version: 0, } as const; const publishedPublication = { state: "published", closed_reason: null, fields: ["categories", "stack"], consent_id: consentId, published_at: "2026-10-06T09:10:00Z", version: 1, } as const; const notLooking = { value: "not_looking", lapsed: false, confirmed_at: null, lapses_at: null, version: 0, } as const; const lookingStatus = { value: "looking", lapsed: false, confirmed_at: "2026-10-06T09:12:00Z", lapses_at: "2026-11-05T09:12:00Z", version: 1, } as const; describe("generated PrivacySettings", () => { it("reads a new candidate as closed, not looking, and hiding nobody, all at version 0", () => { const parsed = PrivacySettings.parse({ publication: closedPublication, search_status: notLooking, hidden_organizations_total: 0, }); expect(parsed.publication).toMatchObject({ state: "closed", fields: [], version: 0 }); expect(parsed.publication.consent_id).toBeNull(); expect(parsed.search_status.value).toBe("not_looking"); }); it("reads a published profile with its fields, its C2 record, and a confirmed status", () => { const settings = { publication: publishedPublication, search_status: lookingStatus, hidden_organizations_total: 1, }; expect(PrivacySettings.parse(settings)).toEqual(settings); }); it.each([ ["a state outside closed and published", { ...publishedPublication, state: "draft" }], ["a closing reason outside the enum", { ...closedPublication, closed_reason: "expired" }], ["a consent identifier that is not a UUID", { ...publishedPublication, consent_id: "c2" }], ["a negative version", { ...publishedPublication, version: -1 }], ])("rejects a publication with %s", (_case, publication) => { expect(Publication.safeParse(publication).success).toBe(false); }); }); describe("generated PublicationUpdate", () => { const update = { expected_version: 0, state: "published", fields: ["categories", "stack"] }; it("accepts a publication and a closing", () => { expect(PublicationUpdate.parse(update)).toEqual(update); expect(PublicationUpdate.parse({ expected_version: 1, state: "closed", fields: [] })).toEqual({ expected_version: 1, state: "closed", fields: [], }); }); it.each(["full_name", "email", "phone", "telegram", "contacts", "salary_minimum", "user_id"])( "has no card field %s, so no request can publish it", (field) => { expect(CardField.safeParse(field).success).toBe(false); expect(PublicationUpdate.safeParse({ ...update, fields: [field] }).success).toBe(false); }, ); it.each([ ["a missing version", { state: "closed", fields: [] }], ["a negative version", { ...update, expected_version: -1 }], ["a fractional version", { ...update, expected_version: 1.5 }], ["a missing field set", { expected_version: 0, state: "closed" }], ["more fields than there are groups", { ...update, fields: [...CardField.options, "stack"] }], ["an unknown state", { ...update, state: "hidden" }], ])("rejects a body with %s", (_case, body) => { expect(PublicationUpdate.safeParse(body).success).toBe(false); }); it("never carries a consent, a time, or a version of the result in the parsed body", () => { const parsed = PublicationUpdate.parse({ ...update, consent_id: consentId, published_at: "2026-10-06T09:10:00Z", version: 5, }); // Generated Zod strips unknown keys; the server rejects them with 400. expect(Object.keys(parsed).sort()).toEqual(["expected_version", "fields", "state"]); expectTypeOf().toEqualTypeOf<{ expected_version: number; state: "closed" | "published"; fields: Array< | "categories" | "stack" | "experience" | "soft_skills" | "location" | "relocation" | "self_assessment" | "work_formats" | "salary_compatibility" >; }>(); }); it("takes the update as the only argument of the generated call", () => { expectTypeOf(setMyPublication).parameter(0).toEqualTypeOf(); }); }); describe("generated SearchStatus", () => { it("is not a consent: it names no consent, publication, or organization", () => { expect(Object.keys(SearchStatus.shape).sort()).toEqual([ "confirmed_at", "lapsed", "lapses_at", "value", "version", ]); expect(Object.keys(SearchStatusUpdate.shape).sort()).toEqual(["expected_version", "value"]); }); it("stores the confirmation of looking and a lapse that falls to the non-public status", () => { expect(SearchStatus.parse(lookingStatus)).toEqual(lookingStatus); // A lapsed status holds as not_looking and still shows when it was confirmed. const lapsed = { ...lookingStatus, value: "not_looking", lapsed: true }; expect(SearchStatus.parse(lapsed)).toEqual(lapsed); }); it.each([ ["a value outside looking and not_looking", { expected_version: 0, value: "open" }], ["a missing version", { value: "looking" }], ["a negative version", { expected_version: -1, value: "looking" }], ])("rejects an update with %s", (_case, body) => { expect(SearchStatusUpdate.safeParse(body).success).toBe(false); }); it("never carries a time, an interval, or a policy version in the parsed update", () => { const parsed = SearchStatusUpdate.parse({ expected_version: 0, value: "looking", confirmed_at: "2026-10-06T09:12:00Z", lapses_at: "2030-01-01T00:00:00Z", interval_days: 3650, policy_version_id: consentId, }); expect(Object.keys(parsed).sort()).toEqual(["expected_version", "value"]); expectTypeOf(setMySearchStatus).parameter(0).toEqualTypeOf(); }); }); describe("generated HiddenOrganization", () => { it("belongs to the candidate and names the organization and the time", () => { const hidden = { organization: { id: "01928f7a-1a2b-7c3d-8e4f-5a6b7c8d9e01", name: "ООО Пример Платежи" }, hidden_at: "2026-10-06T09:20:00Z", }; expect(HiddenOrganization.parse(hidden)).toEqual(hidden); expect( HiddenOrganization.safeParse({ ...hidden, organization: { ...hidden.organization, id: "x" } }) .success, ).toBe(false); }); }); describe("generated AnonymousCardContent", () => { const card = { pseudonym: `cv1_${"a".repeat(43)}`, categories: [ { specialization: "backend", grade: "middle", strength: "standard", basis: "synthetic_demo" }, ], stack: ["postgresql", "kafka"], self_assessment: [ { specialization: "network_engineer", confirmation_status: "unconfirmed", declared_grade: "junior", }, ], experience_band: "1_3", work_formats: null, salary_compatibility: null, }; it("holds only the closed set of card properties and reads an unpublished one as null", () => { expect(AnonymousCardContent.parse(card)).toEqual(card); expect(Object.keys(AnonymousCardContent.shape).sort()).toEqual([ "categories", "experience_band", "pseudonym", "salary_compatibility", "self_assessment", "stack", "work_formats", ]); }); it("never keeps a name, a contact, a salary, or an identifier of the candidate", () => { const parsed = AnonymousCardContent.parse({ ...card, full_name: "Anna Sokolova", email: "anna@example.com", phone: "+79000000000", telegram: "@anna", salary_minimum: 180000, user_id: consentId, }); expect(Object.keys(parsed).sort()).toEqual(Object.keys(card).sort()); }); it("is the card of a preview that is null while the profile is closed", () => { const closed = { state: "closed", fields: [], card: null, evidence: null, generated_at: "2026-10-06T09:30:00Z", }; expect(CardPreview.parse(closed)).toEqual(closed); expect( CardPreview.parse({ ...closed, state: "published", fields: ["stack"], card, evidence: { items: [ { skill: "backend", grade: "junior", trust: 1, basis: "synthetic_demo", calibration_status: "uncalibrated", observed_year: 2026, }, ], }, }), ).toMatchObject({ card: { stack: ["postgresql", "kafka"] }, evidence: { items: [{ skill: "backend", trust: 1 }] }, }); }); }); // A setting of a candidate is private. The check reads the generated files: // the privacy schemas carry no profile, contact, or consent field, and the // organization, invitation, need, and profile sides carry no setting. describe("privacy schemas", () => { const apiDirectory = resolve(dirname(fileURLToPath(import.meta.url))); const sourcesIn = (directory: string) => readdirSync(join(apiDirectory, directory), { recursive: true, encoding: "utf8" }) .filter((name) => name.endsWith(".ts")) .map((name) => ({ name: join(directory, name), source: readFileSync(join(apiDirectory, directory, name), "utf8"), })); const privacySide = [...sourcesIn(join("schemas", "privacy"))]; const otherSide = ["organization", "invitation", "need", "candidate", "consent"].flatMap( (tag) => [...sourcesIn(join("schemas", tag)), ...sourcesIn(join("endpoints", tag))], ); // Generated object keys that only the visibility settings have, matched as // quoted keys because endpoint comments quote contract prose. const privacyOnly = [ '"hidden_organizations_total":', '"lapses_at":', '"closed_reason":', '"experience_months":', '"hidden_at":', ]; // Generated object keys that the private profile, a contact disclosure, or // a consent record has and no visibility schema may. const forbiddenOnPrivacySide = [ '"full_name":', '"email":', '"phone":', '"telegram":', '"salary_minimum":', '"confirmed_categories":', '"text_version":', '"invitation_id":', "\"fields\": zod.array(zod.enum(['email'", ]; it("finds the generated files of both sides", () => { expect(privacySide.length).toBeGreaterThan(20); expect(otherSide.length).toBeGreaterThan(50); }); it("is not reachable from an organization, invitation, need, profile, or consent schema or endpoint", () => { const leaks = otherSide.flatMap((file) => privacyOnly .filter((marker) => file.source.includes(marker)) .map((marker) => `${file.name}: ${marker}`), ); expect(leaks).toEqual([]); }); it("carries no name, contact, salary, profile claim, or consent field", () => { const leaks = privacySide.flatMap((file) => forbiddenOnPrivacySide .filter((marker) => file.source.includes(marker)) .map((marker) => `${file.name}: ${marker}`), ); expect(leaks).toEqual([]); }); });