import { describe, expect, it } from "vitest"; import { ApiRequestError } from "../../../shared/api"; import { closedNoticeDialog, confirmationError, decideNotice, noticeDialogReducer, type NoticeDialogState, } from "./notice-gate"; import { knownSignalNoticeVersion } from "./notice-content"; function conflict(status: number): ApiRequestError { return new ApiRequestError({ status, code: "state_conflict", title: "failure" }); } describe("decideNotice", () => { it("does not proceed without data from the server", () => { expect(decideNotice(undefined).kind).toBe("unavailable"); }); it("refuses to confirm a text version the screen does not know", () => { const decision = decideNotice({ notice_version: knownSignalNoticeVersion + 1, acknowledged: true, }); expect(decision.kind).toBe("outdated"); }); it("proceeds at once when the server already has the acknowledgement", () => { expect(decideNotice({ notice_version: knownSignalNoticeVersion, acknowledged: true })).toEqual({ kind: "proceed", }); }); it("asks for confirmation when the acknowledgement is missing", () => { expect(decideNotice({ notice_version: knownSignalNoticeVersion, acknowledged: false })).toEqual( { kind: "confirm" }, ); }); }); describe("confirmationError", () => { it("tells the candidate to reload after a version conflict", () => { expect(confirmationError(conflict(409))).toContain("Текст уведомления изменился"); }); it("asks to retry after any other failure", () => { expect(confirmationError(conflict(0))).toContain("Не удалось сохранить"); expect(confirmationError(new Error("x"))).toContain("Не удалось сохранить"); }); }); describe("noticeDialogReducer", () => { const open = noticeDialogReducer(closedNoticeDialog, { type: "open" }); it("opens and closes without a confirmation", () => { expect(open).toEqual({ open: true, busy: false, errorMessage: null }); expect(noticeDialogReducer(open, { type: "close" })).toEqual(closedNoticeDialog); }); it("ignores a second confirmation while one is being saved", () => { const busy = noticeDialogReducer(open, { type: "confirm" }); expect(busy.busy).toBe(true); expect(noticeDialogReducer(busy, { type: "confirm" })).toBe(busy); }); it("cannot be closed while the confirmation is being saved", () => { const busy: NoticeDialogState = noticeDialogReducer(open, { type: "confirm" }); expect(noticeDialogReducer(busy, { type: "close" })).toBe(busy); }); it("closes after the server stored the confirmation", () => { const busy = noticeDialogReducer(open, { type: "confirm" }); expect(noticeDialogReducer(busy, { type: "confirmed" })).toEqual(closedNoticeDialog); }); it("stays open with a message after a version conflict and can be closed again", () => { const busy = noticeDialogReducer(open, { type: "confirm" }); const failed = noticeDialogReducer(busy, { type: "failed", error: conflict(409) }); expect(failed.open).toBe(true); expect(failed.busy).toBe(false); expect(failed.errorMessage).toContain("Текст уведомления изменился"); expect(noticeDialogReducer(failed, { type: "close" })).toEqual(closedNoticeDialog); }); it("does not confirm a dialog that is closed", () => { expect(noticeDialogReducer(closedNoticeDialog, { type: "confirm" })).toBe(closedNoticeDialog); }); });