File
Blob: tests/client/lib/active-page-model.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | getPageLoadFailureAction, |
| 4 | needsRestrictedAncestors, |
| 5 | type ActivePageSurface, |
| 6 | } from "@/client/lib/active-page-model"; |
| 7 | import { SESSION_MODES } from "@/client/lib/constants"; |
| 8 | import type { FailureKind } from "@/client/lib/classify-failure"; |
| 9 | |
| 10 | const SURFACES: ActivePageSurface[] = ["canonical", "shared"]; |
| 11 | |
| 12 | describe("getPageLoadFailureAction", () => { |
| 13 | describe("forbidden", () => { |
| 14 | it.each(SURFACES)("returns evict for %s surface regardless of connection", (surface) => { |
| 15 | expect(getPageLoadFailureAction("forbidden", true, SESSION_MODES.AUTHENTICATED, surface)).toBe("evict"); |
| 16 | expect(getPageLoadFailureAction("forbidden", false, SESSION_MODES.AUTHENTICATED, surface)).toBe("evict"); |
| 17 | expect(getPageLoadFailureAction("forbidden", true, SESSION_MODES.LOCAL_ONLY, surface)).toBe("evict"); |
| 18 | expect(getPageLoadFailureAction("forbidden", true, SESSION_MODES.EXPIRED, surface)).toBe("evict"); |
| 19 | }); |
| 20 | }); |
| 21 | |
| 22 | describe("not-found", () => { |
| 23 | it.each(SURFACES)("is terminal on %s surface", (surface) => { |
| 24 | expect(getPageLoadFailureAction("not-found", true, SESSION_MODES.AUTHENTICATED, surface)).toBe("terminal-gone"); |
| 25 | expect(getPageLoadFailureAction("not-found", false, SESSION_MODES.AUTHENTICATED, surface)).toBe("terminal-gone"); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe("network", () => { |
| 30 | it("falls back to cache on canonical surface", () => { |
| 31 | expect(getPageLoadFailureAction("network", true, SESSION_MODES.AUTHENTICATED, "canonical")).toBe( |
| 32 | "cache-fallback", |
| 33 | ); |
| 34 | expect(getPageLoadFailureAction("network", false, SESSION_MODES.AUTHENTICATED, "canonical")).toBe( |
| 35 | "cache-fallback", |
| 36 | ); |
| 37 | }); |
| 38 | |
| 39 | it("is terminal on share surface", () => { |
| 40 | expect(getPageLoadFailureAction("network", true, SESSION_MODES.AUTHENTICATED, "shared")).toBe("terminal-gone"); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe("server / unknown / auth-ambiguous", () => { |
| 45 | const kinds: FailureKind[] = ["server", "unknown", "auth-ambiguous"]; |
| 46 | |
| 47 | it.each(kinds)("is terminal for %s on canonical + online + authenticated", (kind) => { |
| 48 | expect(getPageLoadFailureAction(kind, true, SESSION_MODES.AUTHENTICATED, "canonical")).toBe("terminal-gone"); |
| 49 | }); |
| 50 | |
| 51 | it.each(kinds)("falls back to cache for %s when canonical + offline", (kind) => { |
| 52 | expect(getPageLoadFailureAction(kind, false, SESSION_MODES.AUTHENTICATED, "canonical")).toBe("cache-fallback"); |
| 53 | }); |
| 54 | |
| 55 | it.each(kinds)("falls back to cache for %s when canonical + LOCAL_ONLY", (kind) => { |
| 56 | expect(getPageLoadFailureAction(kind, true, SESSION_MODES.LOCAL_ONLY, "canonical")).toBe("cache-fallback"); |
| 57 | }); |
| 58 | |
| 59 | it.each(kinds)("is terminal for %s on share surface regardless of connection", (kind) => { |
| 60 | expect(getPageLoadFailureAction(kind, true, SESSION_MODES.AUTHENTICATED, "shared")).toBe("terminal-gone"); |
| 61 | expect(getPageLoadFailureAction(kind, false, SESSION_MODES.AUTHENTICATED, "shared")).toBe("terminal-gone"); |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("needsRestrictedAncestors", () => { |
| 67 | it("returns true for shared access mode", () => { |
| 68 | expect(needsRestrictedAncestors("shared", null)).toBe(true); |
| 69 | expect(needsRestrictedAncestors("shared", "member")).toBe(true); |
| 70 | }); |
| 71 | |
| 72 | it("returns true for guest role regardless of access mode", () => { |
| 73 | expect(needsRestrictedAncestors("member", "guest")).toBe(true); |
| 74 | expect(needsRestrictedAncestors(null, "guest")).toBe(true); |
| 75 | }); |
| 76 | |
| 77 | it("returns false for member access with non-guest role", () => { |
| 78 | expect(needsRestrictedAncestors("member", "owner")).toBe(false); |
| 79 | expect(needsRestrictedAncestors("member", "admin")).toBe(false); |
| 80 | expect(needsRestrictedAncestors("member", "member")).toBe(false); |
| 81 | }); |
| 82 | |
| 83 | it("returns false when both are null", () => { |
| 84 | expect(needsRestrictedAncestors(null, null)).toBe(false); |
| 85 | }); |
| 86 | }); |