File
Blob: tests/client/lib/workspace-route-model.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | isWorkspaceReady, |
| 4 | hasWorkspaceIdentity, |
| 5 | isResolvingWorkspace, |
| 6 | type WorkspaceRouteState, |
| 7 | } from "@/client/lib/workspace-route-model"; |
| 8 | |
| 9 | const WS_ID = "ws-1"; |
| 10 | |
| 11 | const states: Record<string, WorkspaceRouteState> = { |
| 12 | loadingEmpty: { phase: "loading", workspaceId: null }, |
| 13 | loadingSeeded: { phase: "loading", workspaceId: WS_ID }, |
| 14 | readyMember: { phase: "ready", workspaceId: WS_ID, accessMode: "member" }, |
| 15 | readyShared: { phase: "ready", workspaceId: WS_ID, accessMode: "shared" }, |
| 16 | degradedStaleSharedNoIdentity: { |
| 17 | phase: "degraded", |
| 18 | workspaceId: null, |
| 19 | workspaceSlug: "my-ws", |
| 20 | reason: "stale-shared", |
| 21 | }, |
| 22 | degradedStaleShared: { phase: "degraded", workspaceId: WS_ID, workspaceSlug: "my-ws", reason: "stale-shared" }, |
| 23 | errorNotFound: { phase: "error", errorKind: "not-found", message: "Workspace not found" }, |
| 24 | errorNetwork: { phase: "error", errorKind: "network", message: "Failed to load workspace" }, |
| 25 | }; |
| 26 | |
| 27 | describe("isWorkspaceReady", () => { |
| 28 | const ready = ["readyMember", "readyShared"]; |
| 29 | const notReady = [ |
| 30 | "loadingEmpty", |
| 31 | "loadingSeeded", |
| 32 | "degradedStaleSharedNoIdentity", |
| 33 | "degradedStaleShared", |
| 34 | "errorNotFound", |
| 35 | "errorNetwork", |
| 36 | ]; |
| 37 | |
| 38 | it.each(ready)("returns true for %s", (key) => { |
| 39 | expect(isWorkspaceReady(states[key])).toBe(true); |
| 40 | }); |
| 41 | |
| 42 | it.each(notReady)("returns false for %s", (key) => { |
| 43 | expect(isWorkspaceReady(states[key])).toBe(false); |
| 44 | }); |
| 45 | |
| 46 | it("narrows to workspaceId + accessMode", () => { |
| 47 | const state = states.readyMember; |
| 48 | if (isWorkspaceReady(state)) { |
| 49 | expect(state.workspaceId).toBe(WS_ID); |
| 50 | expect(state.accessMode).toBe("member"); |
| 51 | } |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | describe("hasWorkspaceIdentity", () => { |
| 56 | const withIdentity = ["loadingSeeded", "readyMember", "readyShared", "degradedStaleShared"]; |
| 57 | const withoutIdentity = ["loadingEmpty", "degradedStaleSharedNoIdentity", "errorNotFound", "errorNetwork"]; |
| 58 | |
| 59 | it.each(withIdentity)("returns true for %s", (key) => { |
| 60 | expect(hasWorkspaceIdentity(states[key])).toBe(true); |
| 61 | }); |
| 62 | |
| 63 | it.each(withoutIdentity)("returns false for %s", (key) => { |
| 64 | expect(hasWorkspaceIdentity(states[key])).toBe(false); |
| 65 | }); |
| 66 | |
| 67 | it("narrows workspaceId to non-null", () => { |
| 68 | const state = states.degradedStaleShared; |
| 69 | if (hasWorkspaceIdentity(state)) { |
| 70 | expect(state.workspaceId).toBe(WS_ID); |
| 71 | } |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | describe("isResolvingWorkspace", () => { |
| 76 | const resolving = ["loadingEmpty", "loadingSeeded", "degradedStaleSharedNoIdentity", "degradedStaleShared"]; |
| 77 | const notResolving = ["readyMember", "readyShared", "errorNotFound", "errorNetwork"]; |
| 78 | |
| 79 | it.each(resolving)("returns true for %s", (key) => { |
| 80 | expect(isResolvingWorkspace(states[key])).toBe(true); |
| 81 | }); |
| 82 | |
| 83 | it.each(notResolving)("returns false for %s", (key) => { |
| 84 | expect(isResolvingWorkspace(states[key])).toBe(false); |
| 85 | }); |
| 86 | }); |