-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcontext-menu.test.ts
More file actions
216 lines (194 loc) · 6.98 KB
/
Copy pathcontext-menu.test.ts
File metadata and controls
216 lines (194 loc) · 6.98 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import type {
ContextMenuAction,
ContextMenuItem,
IContextMenu,
ShowContextMenuOptions,
} from "@posthog/platform/context-menu";
import type { ConfirmOptions, IDialog } from "@posthog/platform/dialog";
import { describe, expect, it } from "vitest";
import { ContextMenuService } from "./context-menu";
import type { IContextMenuExternalApps } from "./identifiers";
import type { TaskContextMenuInput } from "./schemas";
class FakeContextMenu implements IContextMenu {
lastItems: ContextMenuItem[] = [];
lastOptions?: ShowContextMenuOptions;
private shownResolve!: () => void;
readonly shown = new Promise<void>((resolve) => {
this.shownResolve = resolve;
});
show(items: ContextMenuItem[], options?: ShowContextMenuOptions): void {
this.lastItems = items;
this.lastOptions = options;
this.shownResolve();
}
}
const noExternalApps: IContextMenuExternalApps = {
getDetectedApps: async () => [],
getLastUsed: async () => ({}),
};
function dialogReturning(response: number): IDialog {
return {
confirm: async (_options: ConfirmOptions) => response,
} as IDialog;
}
function labels(items: ContextMenuItem[]): string[] {
return items
.filter((i): i is ContextMenuAction => !("separator" in i))
.map((i) => i.label);
}
function findItem(items: ContextMenuItem[], label: string): ContextMenuAction {
const item = items.find(
(i): i is ContextMenuAction => !("separator" in i) && i.label === label,
);
if (!item) throw new Error(`menu item "${label}" not found`);
return item;
}
function makeService(menu: IContextMenu, dialog: IDialog = dialogReturning(1)) {
return new ContextMenuService(noExternalApps, dialog, menu);
}
const baseTask: TaskContextMenuInput = {
taskTitle: "Task",
isPinned: false,
isSuspended: false,
isInCommandCenter: false,
hasEmptyCommandCenterCell: true,
};
describe("ContextMenuService.showTaskContextMenu", () => {
it("shows Pin/Unpin based on isPinned", async () => {
const menu = new FakeContextMenu();
const pinned = makeService(menu).showTaskContextMenu({
...baseTask,
isPinned: true,
});
await menu.shown;
expect(labels(menu.lastItems)).toContain("Unpin");
expect(labels(menu.lastItems)).not.toContain("Pin");
findItem(menu.lastItems, "Unpin").click();
expect(await pinned).toEqual({ action: { type: "pin" } });
});
it("only offers Suspend when the task has a worktree", async () => {
const withWt = new FakeContextMenu();
makeService(withWt).showTaskContextMenu({
...baseTask,
worktreePath: "/wt",
});
await withWt.shown;
expect(labels(withWt.lastItems)).toContain("Suspend");
const noWt = new FakeContextMenu();
makeService(noWt).showTaskContextMenu({ ...baseTask, folderPath: "/f" });
await noWt.shown;
expect(labels(noWt.lastItems)).not.toContain("Suspend");
});
it("labels Suspend as Unsuspend when already suspended", async () => {
const menu = new FakeContextMenu();
makeService(menu).showTaskContextMenu({
...baseTask,
worktreePath: "/wt",
isSuspended: true,
});
await menu.shown;
expect(labels(menu.lastItems)).toContain("Unsuspend");
expect(labels(menu.lastItems)).not.toContain("Suspend");
});
it("offers Stop task only for a stoppable run", async () => {
const running = new FakeContextMenu();
const result = makeService(running).showTaskContextMenu({
...baseTask,
canStop: true,
});
await running.shown;
findItem(running.lastItems, "Stop task").click();
expect(await result).toEqual({ action: { type: "stop" } });
const idle = new FakeContextMenu();
makeService(idle).showTaskContextMenu(baseTask);
await idle.shown;
expect(labels(idle.lastItems)).not.toContain("Stop task");
});
it("hides Add to Command Center when already in it", async () => {
const inCc = new FakeContextMenu();
makeService(inCc).showTaskContextMenu({
...baseTask,
isInCommandCenter: true,
});
await inCc.shown;
expect(labels(inCc.lastItems)).not.toContain("Add to Command Center");
});
it("disables Add to Command Center when there is no empty cell", async () => {
const menu = new FakeContextMenu();
makeService(menu).showTaskContextMenu({
...baseTask,
isInCommandCenter: false,
hasEmptyCommandCenterCell: false,
});
await menu.shown;
expect(findItem(menu.lastItems, "Add to Command Center").enabled).toBe(
false,
);
});
it("offers Delete for active tasks and resolves without an inline confirm", async () => {
const menu = new FakeContextMenu();
// dialogReturning(0) would cancel any inline confirm; Delete must resolve
// anyway because confirmation happens downstream in TaskDeletionService.
const result = makeService(menu, dialogReturning(0)).showTaskContextMenu(
baseTask,
);
await menu.shown;
findItem(menu.lastItems, "Delete").click();
expect(await result).toEqual({ action: { type: "delete" } });
});
it("resolves to null when the menu is dismissed", async () => {
const menu = new FakeContextMenu();
const result = makeService(menu).showTaskContextMenu(baseTask);
await menu.shown;
menu.lastOptions?.onDismiss?.();
expect(await result).toEqual({ action: null });
});
it("gates a confirm-protected item on dialog confirmation", async () => {
const confirmed = new FakeContextMenu();
const okResult = makeService(
confirmed,
dialogReturning(1),
).showTaskContextMenu(baseTask);
await confirmed.shown;
findItem(confirmed.lastItems, "Archive prior tasks").click();
expect(await okResult).toEqual({ action: { type: "archive-prior" } });
const cancelled = new FakeContextMenu();
const cancelResult = makeService(
cancelled,
dialogReturning(0),
).showTaskContextMenu(baseTask);
await cancelled.shown;
findItem(cancelled.lastItems, "Archive prior tasks").click();
expect(await cancelResult).toEqual({ action: null });
});
});
describe("ContextMenuService.showBulkTaskContextMenu", () => {
it("labels the archive action with the task count and gates on confirm", async () => {
const menu = new FakeContextMenu();
const result = makeService(
menu,
dialogReturning(1),
).showBulkTaskContextMenu({ taskCount: 3 });
await menu.shown;
expect(labels(menu.lastItems)).toEqual(["Archive 3 tasks"]);
findItem(menu.lastItems, "Archive 3 tasks").click();
expect(await result).toEqual({ action: { type: "archive" } });
});
});
describe("ContextMenuService.confirmDeleteTask", () => {
it("returns confirmed=true/false from the dialog response", async () => {
const menu = new FakeContextMenu();
expect(
await makeService(menu, dialogReturning(1)).confirmDeleteTask({
taskTitle: "x",
hasWorktree: true,
}),
).toEqual({ confirmed: true });
expect(
await makeService(menu, dialogReturning(0)).confirmDeleteTask({
taskTitle: "x",
hasWorktree: false,
}),
).toEqual({ confirmed: false });
});
});