File
Blob: tests/client/components/workspace/page-view-load.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { getPageLoadTarget } from "@/client/lib/page-load-target"; |
| 3 | import { SESSION_MODES } from "@/client/lib/constants"; |
| 4 | import type { WorkspaceRouteState } from "@/client/lib/workspace-route-model"; |
| 5 | import { createPage, createWorkspace } from "@tests/client/util/fixtures"; |
| 6 | |
| 7 | const workspace = createWorkspace({ id: "ws-1", slug: "docs" }); |
| 8 | const cachedPage = createPage({ id: "page-1", workspace_id: workspace.id }); |
| 9 | |
| 10 | describe("getPageLoadTarget", () => { |
| 11 | it("uses cached page content for degraded routes when offline and both metadata and Yjs content are cached", () => { |
| 12 | const route: WorkspaceRouteState = { |
| 13 | phase: "degraded", |
| 14 | workspaceId: null, |
| 15 | workspaceSlug: "docs", |
| 16 | reason: "stale-shared", |
| 17 | }; |
| 18 | |
| 19 | expect( |
| 20 | getPageLoadTarget({ |
| 21 | surface: "canonical", |
| 22 | route, |
| 23 | online: false, |
| 24 | sessionMode: SESSION_MODES.AUTHENTICATED, |
| 25 | cachedPage, |
| 26 | docCached: true, |
| 27 | workspaceId: cachedPage.workspace_id, |
| 28 | }), |
| 29 | ).toBe("cached-page"); |
| 30 | }); |
| 31 | |
| 32 | it("uses cached page content for stale-shared routes in local-only mode", () => { |
| 33 | const route: WorkspaceRouteState = { |
| 34 | phase: "degraded", |
| 35 | workspaceId: workspace.id, |
| 36 | workspaceSlug: workspace.slug, |
| 37 | reason: "stale-shared", |
| 38 | }; |
| 39 | |
| 40 | expect( |
| 41 | getPageLoadTarget({ |
| 42 | surface: "canonical", |
| 43 | route, |
| 44 | online: true, |
| 45 | sessionMode: SESSION_MODES.LOCAL_ONLY, |
| 46 | cachedPage, |
| 47 | docCached: true, |
| 48 | workspaceId: cachedPage.workspace_id, |
| 49 | }), |
| 50 | ).toBe("cached-page"); |
| 51 | }); |
| 52 | |
| 53 | it("shows offline unavailable when metadata exists but the cached Yjs doc does not", () => { |
| 54 | const route: WorkspaceRouteState = { |
| 55 | phase: "degraded", |
| 56 | workspaceId: null, |
| 57 | workspaceSlug: "docs", |
| 58 | reason: "stale-shared", |
| 59 | }; |
| 60 | |
| 61 | expect( |
| 62 | getPageLoadTarget({ |
| 63 | surface: "canonical", |
| 64 | route, |
| 65 | online: false, |
| 66 | sessionMode: SESSION_MODES.AUTHENTICATED, |
| 67 | cachedPage, |
| 68 | docCached: false, |
| 69 | workspaceId: cachedPage.workspace_id, |
| 70 | }), |
| 71 | ).toBe("offline-unavailable"); |
| 72 | }); |
| 73 | |
| 74 | it("uses cached page content for degraded routes when online and authenticated if metadata and Yjs content are cached", () => { |
| 75 | const route: WorkspaceRouteState = { |
| 76 | phase: "degraded", |
| 77 | workspaceId: null, |
| 78 | workspaceSlug: "docs", |
| 79 | reason: "stale-shared", |
| 80 | }; |
| 81 | |
| 82 | expect( |
| 83 | getPageLoadTarget({ |
| 84 | surface: "canonical", |
| 85 | route, |
| 86 | online: true, |
| 87 | sessionMode: SESSION_MODES.AUTHENTICATED, |
| 88 | cachedPage, |
| 89 | docCached: true, |
| 90 | workspaceId: cachedPage.workspace_id, |
| 91 | }), |
| 92 | ).toBe("cached-page"); |
| 93 | }); |
| 94 | |
| 95 | it("surfaces cache-unavailable when degraded routes are online but lack a cached Yjs doc", () => { |
| 96 | const route: WorkspaceRouteState = { |
| 97 | phase: "degraded", |
| 98 | workspaceId: null, |
| 99 | workspaceSlug: "docs", |
| 100 | reason: "stale-shared", |
| 101 | }; |
| 102 | |
| 103 | expect( |
| 104 | getPageLoadTarget({ |
| 105 | surface: "canonical", |
| 106 | route, |
| 107 | online: true, |
| 108 | sessionMode: SESSION_MODES.AUTHENTICATED, |
| 109 | cachedPage, |
| 110 | docCached: false, |
| 111 | workspaceId: cachedPage.workspace_id, |
| 112 | }), |
| 113 | ).toBe("cache-unavailable"); |
| 114 | }); |
| 115 | |
| 116 | it("loads live page data once the workspace route is ready", () => { |
| 117 | const route: WorkspaceRouteState = { |
| 118 | phase: "ready", |
| 119 | workspaceId: workspace.id, |
| 120 | accessMode: "member", |
| 121 | }; |
| 122 | |
| 123 | expect( |
| 124 | getPageLoadTarget({ |
| 125 | surface: "canonical", |
| 126 | route, |
| 127 | online: true, |
| 128 | sessionMode: SESSION_MODES.AUTHENTICATED, |
| 129 | cachedPage, |
| 130 | docCached: true, |
| 131 | workspaceId: workspace.id, |
| 132 | }), |
| 133 | ).toBe("live"); |
| 134 | }); |
| 135 | }); |