import { describe, expect, it } from "vitest"; import { ApiRequestError, invalidResponseCode } from "../../../shared/api"; import { describeResponseFailure } from "./response-failure"; function problem(status: number, code: string, pointer?: string): ApiRequestError { return new ApiRequestError({ status, code: code as ApiRequestError["code"], title: "Problem", fieldErrors: pointer ? [{ in: "body", pointer, code: "invalid", message: "Invalid." }] : [], }); } describe("describing a failed answer", () => { it("treats a stale version as a reason to read the invitation again, for both 412 and 409", () => { for (const error of [problem(412, "version_conflict"), problem(409, "version_conflict")]) { const failure = describeResponseFailure("accept", error); expect(failure).toMatchObject({ kind: "stale-terms", refresh: true }); expect(failure.message).toContain("Приглашение изменилось"); } }); it("shows the final state for a state conflict instead of a retry", () => { expect(describeResponseFailure("accept", problem(409, "state_conflict"))).toMatchObject({ kind: "final-state", refresh: true, }); }); it("keeps the same key for an outcome that is unknown or temporary", () => { const retryable = [ new ApiRequestError({ status: 0, code: "network_error", title: "" }), problem(500, "internal"), new ApiRequestError({ status: 200, code: invalidResponseCode, title: "" }), problem(409, "idempotency_in_progress"), problem(429, "rate_limited"), new TypeError("not an API error"), ]; for (const error of retryable) { expect(describeResponseFailure("decline", error)).toMatchObject({ kind: "retry", refresh: false, }); } }); it("sends a consent pointer to the offer, and any other validation error to the page", () => { expect( describeResponseFailure("accept", problem(400, "validation_failed", "/consent_c4/fields/1")), ).toMatchObject({ kind: "consent", refresh: true }); expect( describeResponseFailure("accept", problem(400, "validation_failed", "/terms/salary")), ).toMatchObject({ kind: "blocked", refresh: true }); }); it("separates the session, CSRF, and missing-invitation cases", () => { expect(describeResponseFailure("accept", problem(403, "csrf_check_failed"))).toMatchObject({ kind: "csrf", refresh: true, }); expect(describeResponseFailure("accept", problem(403, "forbidden"))).toMatchObject({ kind: "access", refresh: true, }); expect(describeResponseFailure("decline", problem(404, "not_found"))).toMatchObject({ kind: "not-found", refresh: true, }); }); it("never shows server text: the messages are the product's own", () => { const failure = describeResponseFailure("decline", problem(500, "internal")); expect(failure.message).toContain("Сервер не подтвердил результат"); expect(failure.message).toContain("тем же ключом"); }); });