Skip to content
File

Blob: tests/client/lib/root-workspace-gateway.test.ts

typescript97 lines
1import { describe, expect, it } from "vitest";
2import { createWorkspace } from "@tests/client/util/fixtures";
3import { resolveRootWorkspaceDecision } from "@/client/lib/root-workspace-gateway";
4 
5describe("resolveRootWorkspaceDecision", () => {
6 it("prefers the last visited workspace when it exists in the live list", () => {
7 const preferred = createWorkspace({ id: "ws-2", slug: "preferred" });
8 const liveWorkspaces = [createWorkspace({ id: "ws-1", slug: "first" }), preferred];
9 
10 expect(
11 resolveRootWorkspaceDecision({
12 lastVisitedWorkspaceId: "ws-2",
13 cachedWorkspaces: [],
14 liveWorkspaces,
15 }),
16 ).toEqual({
17 kind: "redirect",
18 workspace: preferred,
19 });
20 });
21 
22 it("falls back to the first live workspace when the last visited workspace is stale", () => {
23 const liveWorkspace = createWorkspace({ id: "ws-1", slug: "first" });
24 
25 expect(
26 resolveRootWorkspaceDecision({
27 lastVisitedWorkspaceId: "ws-stale",
28 cachedWorkspaces: [],
29 liveWorkspaces: [liveWorkspace, createWorkspace({ id: "ws-2", slug: "second" })],
30 }),
31 ).toEqual({
32 kind: "redirect",
33 workspace: liveWorkspace,
34 });
35 });
36 
37 it("returns empty when the live list is confirmed empty", () => {
38 expect(
39 resolveRootWorkspaceDecision({
40 lastVisitedWorkspaceId: "ws-1",
41 cachedWorkspaces: [createWorkspace()],
42 liveWorkspaces: [],
43 }),
44 ).toEqual({ kind: "empty" });
45 });
46 
47 it("falls back to the cached last visited workspace when the live request fails", () => {
48 const cachedWorkspace = createWorkspace({ id: "ws-2", slug: "cached-preferred" });
49 
50 expect(
51 resolveRootWorkspaceDecision({
52 lastVisitedWorkspaceId: "ws-2",
53 cachedWorkspaces: [createWorkspace({ id: "ws-1", slug: "first" }), cachedWorkspace],
54 liveWorkspaces: null,
55 }),
56 ).toEqual({
57 kind: "redirect",
58 workspace: cachedWorkspace,
59 });
60 });
61 
62 it("returns unavailable when the cached last visited workspace is missing", () => {
63 expect(
64 resolveRootWorkspaceDecision({
65 lastVisitedWorkspaceId: "ws-stale",
66 cachedWorkspaces: [],
67 liveWorkspaces: null,
68 }),
69 ).toEqual({ kind: "unavailable" });
70 });
71 
72 it("falls back to first cached workspace when no lastVisitedWorkspaceId", () => {
73 const cachedWorkspace = createWorkspace({ id: "ws-1", slug: "first" });
74 
75 expect(
76 resolveRootWorkspaceDecision({
77 lastVisitedWorkspaceId: null,
78 cachedWorkspaces: [cachedWorkspace],
79 liveWorkspaces: null,
80 }),
81 ).toEqual({
82 kind: "redirect",
83 workspace: cachedWorkspace,
84 });
85 });
86 
87 it("returns unavailable when there is no live response and no cached workspace", () => {
88 expect(
89 resolveRootWorkspaceDecision({
90 lastVisitedWorkspaceId: null,
91 cachedWorkspaces: [],
92 liveWorkspaces: null,
93 }),
94 ).toEqual({ kind: "unavailable" });
95 });
96});