import { describe, it, expect, beforeEach } from "vitest"; import { reportServerVersion, subscribeNewVersion, getNewVersion, BUILD_VERSION, __resetVersionStateForTest, } from "../admin/utils/appVersion"; /** * Client store backing the "new version available" banner. The server stamps * X-App-Version on every response; apiFetch feeds it here. A version that * differs from the one baked into THIS bundle (BUILD_VERSION) means a deploy * happened → latch it and notify subscribers (the banner). */ describe("appVersion store", () => { beforeEach(() => __resetVersionStateForTest()); it("ignores the same version, empty, or null", () => { reportServerVersion(BUILD_VERSION); reportServerVersion(""); reportServerVersion(null); reportServerVersion(undefined); expect(getNewVersion()).toBeNull(); }); it("latches + notifies once when the server reports a different version", () => { let notified = 0; const unsub = subscribeNewVersion(() => { notified++; }); reportServerVersion(`${BUILD_VERSION}-next`); expect(getNewVersion()).toBe(`${BUILD_VERSION}-next`); expect(notified).toBe(1); // Idempotent: once a new version is known, further reports don't re-notify // or overwrite (the banner is already showing). reportServerVersion("99.0.0"); expect(getNewVersion()).toBe(`${BUILD_VERSION}-next`); expect(notified).toBe(1); unsub(); }); });