1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import type { Workspace, WorkspaceEntry } from "@dispatch/wire";
import { render, screen, waitFor } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import type { LocalStore } from "../../../adapters/local-storage";
import type { WorkspaceHttp, WorkspaceResult } from "../adapter/http";
import type { TileLayoutData, TileLayoutEntry } from "../logic/tile-layout";
import { createTileLayoutStore } from "../tile-layout-store.svelte";
import WorkspacesHome from "./WorkspacesHome.svelte";
function ws(overrides: Partial<WorkspaceEntry> = {}): WorkspaceEntry {
return {
id: "a",
title: "A",
defaultCwd: null,
defaultComputerId: null,
starred: false,
createdAt: 1,
lastActivityAt: 2,
conversationCount: 1,
...overrides,
};
}
/** A fake WorkspaceHttp edge with a controllable list (no mocks of our modules). */
function fakeHttp(list: WorkspaceEntry[] = [ws()]): WorkspaceHttp {
return {
list: vi.fn(async (): Promise<WorkspaceEntry[]> => list),
// Create-on-miss: the backend persists the new workspace, so the next
// `list` serves it too.
ensure: vi.fn(async (id: string): Promise<WorkspaceResult<Workspace>> => {
if (!list.some((w) => w.id === id)) list.push(ws({ id, title: id }));
return { ok: true, value: ws({ id, title: id }) };
}),
get: vi.fn(),
setTitle: vi.fn(),
setDefaultCwd: vi.fn(),
setDefaultComputer: vi.fn(),
star: vi.fn(),
unstar: vi.fn(),
delete: vi.fn(async (): Promise<WorkspaceResult<{ closedCount: number }>> => {
// Mirrors the backend: the workspace leaves the list.
list.splice(0);
return { ok: true, value: { closedCount: 0 } };
}),
} as unknown as WorkspaceHttp;
}
function memoryLayout(initial: TileLayoutData | null = null): LocalStore<TileLayoutData> & {
data: TileLayoutData | null;
} {
const store = {
data: initial,
load: () => store.data,
save: (value: TileLayoutData) => {
store.data = value;
},
clear: () => {
store.data = null;
},
};
return store;
}
describe("WorkspacesHome (tile canvas)", () => {
it("renders one tile per workspace, in the persisted tile order (not the list order)", async () => {
const list = [ws({ id: "a", title: "A" }), ws({ id: "b", title: "B" })];
const layout = memoryLayout({
version: 1,
tiles: [
{ id: "b", order: 0, colSpan: 1, rowSpan: 1 },
{ id: "a", order: 1, colSpan: 1, rowSpan: 1 },
],
});
render(WorkspacesHome, {
props: {
store: (await import("../store.svelte")).createWorkspaceStore(fakeHttp(list)),
onNavigate: vi.fn(),
computers: [],
tiles: createTileLayoutStore(layout),
},
});
await screen.findByRole("button", { name: "Open workspace B" });
const wrappers = document.querySelectorAll("[data-tile-id]");
expect([...wrappers].map((el) => el.getAttribute("data-tile-id"))).toEqual(["b", "a"]);
});
it("clicking a tile navigates to the workspace path", async () => {
const user = userEvent.setup();
const onNavigate = vi.fn();
render(WorkspacesHome, {
props: {
store: (await import("../store.svelte")).createWorkspaceStore(fakeHttp()),
onNavigate,
computers: [],
tiles: createTileLayoutStore(memoryLayout()),
},
});
const tile = await screen.findByRole("button", { name: "Open workspace A" });
await user.click(tile);
expect(onNavigate).toHaveBeenCalledWith("/a");
});
it("creating a workspace from the header does NOT navigate; the new tile enters rename mode", async () => {
const user = userEvent.setup();
const list = [ws({ id: "a", title: "A" })];
const onNavigate = vi.fn();
const store = (await import("../store.svelte")).createWorkspaceStore(fakeHttp(list));
render(WorkspacesHome, {
props: {
store,
onNavigate,
computers: [],
tiles: createTileLayoutStore(memoryLayout()),
},
});
await screen.findByRole("button", { name: "Open workspace A" });
await user.click(screen.getByRole("button", { name: "New workspace" }));
await user.type(screen.getByRole("textbox", { name: "New workspace" }), "fresh-ws{Enter}");
await waitFor(() => {
expect(screen.getByLabelText("Workspace title", { selector: "input" })).toBeInTheDocument();
});
expect(onNavigate).not.toHaveBeenCalled();
// The backend was asked to create it (create-on-miss).
expect(store.list.map((w) => w.id)).toContain("fresh-ws");
});
it("right-clicking a tile opens the action bar; Delete keeps the confirm + store call", async () => {
const user = userEvent.setup();
const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(true);
const list = [ws({ id: "a", title: "A" })];
const store = (await import("../store.svelte")).createWorkspaceStore(fakeHttp(list));
render(WorkspacesHome, {
props: {
store,
onNavigate: vi.fn(),
computers: [],
tiles: createTileLayoutStore(memoryLayout()),
},
});
const tile = await screen.findByRole("button", { name: "Open workspace A" });
await user.pointer({ keys: "[MouseRight]", target: tile });
const deleteButton = await screen.findByRole("button", { name: "Delete" });
await user.click(deleteButton);
expect(confirmSpy).toHaveBeenCalledWith(expect.stringContaining('Delete workspace "A"?'));
await waitFor(() => {
expect(store.list.length).toBe(0);
});
confirmSpy.mockRestore();
});
it("workspaces unknown to the layout append at the end (deep-link auto-create)", async () => {
const list = [ws({ id: "a", title: "A" }), ws({ id: "b", title: "B" })];
const layout = memoryLayout({
version: 1,
tiles: [{ id: "b", order: 0, colSpan: 1, rowSpan: 1 }],
});
render(WorkspacesHome, {
props: {
store: (await import("../store.svelte")).createWorkspaceStore(fakeHttp(list)),
onNavigate: vi.fn(),
computers: [],
tiles: createTileLayoutStore(layout),
},
});
await screen.findByRole("button", { name: "Open workspace B" });
await screen.findByRole("button", { name: "Open workspace A" });
const wrappers = document.querySelectorAll("[data-tile-id]");
expect([...wrappers].map((el) => el.getAttribute("data-tile-id"))).toEqual(["b", "a"]);
// And the sync persisted the appended entry.
const entries = (layout.data?.tiles ?? []).map((tile: TileLayoutEntry) => tile.id);
expect(entries).toEqual(["b", "a"]);
});
});
|