Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 buildDraftActionMenuItems,
5 buildThreadActionMenuItems,
6 type ThreadActionMenuState,
7} from "./threadActionMenu.logic";
8
9const baseState: ThreadActionMenuState = {
10 canOperate: true,
11 branch: null,
12 projectFilter: null,
13 isPinned: false,
14 isSettled: false,
15 autoSettleEnabled: true,
16 isSnoozed: false,
17 canSnoozeNow: true,
18 isRegeneratingTitle: false,
19 isRunning: false,
20 supports: {
21 settlement: true,
22 autoSettleOptOut: true,
23 snooze: true,
24 pinning: true,
25 titleRegeneration: true,
26 },
27 snoozePresets: [
28 { id: "hour", label: "In 1 hour", whenLabel: "3:00 PM", snoozedUntil: "2026-08-07T15:00:00Z" },
29 ],
30};
31
32function ids(state: ThreadActionMenuState): string[] {
33 return buildThreadActionMenuItems(state).map((item) => item.id);
34}
35
36function allIds(state: ThreadActionMenuState): string[] {
37 const flatten = (items: ReturnType<typeof buildThreadActionMenuItems>): string[] =>
38 items.flatMap((item) => [item.id, ...(item.children ? flatten(item.children) : [])]);
39 return flatten(buildThreadActionMenuItems(state));
40}
41
42describe("buildThreadActionMenuItems", () => {
43 it.each([false, true])(
44 "disables both lifecycle directions without permission (reversed: %s)",
45 (reversed) => {
46 const items = buildThreadActionMenuItems({
47 ...baseState,
48 canOperate: false,
49 isPinned: reversed,
50 isSettled: reversed,
51 isSnoozed: reversed,
52 });
53 const expected = reversed
54 ? [
55 "unpin",
56 "unsettle",
57 "unsnooze",
58 "rename",
59 "regenerate-title",
60 "auto-settle",
61 "archive",
62 "delete",
63 ]
64 : [
65 "pin",
66 "settle",
67 "snooze",
68 "rename",
69 "regenerate-title",
70 "auto-settle",
71 "archive",
72 "delete",
73 ];
74 expect(items.filter((item) => item.disabled).map((item) => item.id)).toEqual(expected);
75 expect(
76 items.find((item) => item.id === "snooze")?.children?.every((child) => child.disabled) ??
77 true,
78 ).toBe(true);
79 },
80 );
81
82 it("preserves local actions and restores mutations after a grant", () => {
83 const denied = buildThreadActionMenuItems({ ...baseState, canOperate: false, branch: "main" });
84 expect(denied.filter((item) => !item.disabled).map((item) => item.id)).toEqual([
85 "new-thread-on-branch",
86 "mark-unread",
87 "copy",
88 "project-settings",
89 ]);
90 const allowed = buildThreadActionMenuItems({ ...baseState, canOperate: true });
91 expect(allowed.every((item) => !item.disabled)).toBe(true);
92 });
93
94 it("hides lifecycle items when the environment lacks the capabilities", () => {
95 expect(
96 ids({
97 ...baseState,
98 supports: {
99 settlement: false,
100 autoSettleOptOut: false,
101 snooze: false,
102 pinning: false,
103 titleRegeneration: false,
104 },
105 }),
106 ).toEqual(["rename", "mark-unread", "copy", "project-settings", "archive", "delete"]);
107 });
108
109 it("groups project settings with utility actions before archive", () => {
110 const items = buildThreadActionMenuItems(baseState);
111 const copyIndex = items.findIndex((item) => item.id === "copy");
112 expect(items[copyIndex + 1]).toMatchObject({
113 id: "project-settings",
114 label: "Project settings",
115 icon: "settings",
116 });
117 expect(items[copyIndex + 2]?.id).toBe("archive");
118 });
119
120 it("offers project filtering only for surfaces with a scoped thread list", () => {
121 expect(ids(baseState)).not.toContain("filter-by-project");
122 expect(
123 buildThreadActionMenuItems({
124 ...baseState,
125 projectFilter: { label: "Beta Project", isActive: false },
126 }).find((item) => item.id === "filter-by-project"),
127 ).toMatchObject({ label: "Filter by Beta Project", icon: "folder-tree" });
128 });
129
130 it("offers the way back to all projects once the list is scoped", () => {
131 const items = buildThreadActionMenuItems({
132 ...baseState,
133 projectFilter: { label: "Beta Project", isActive: true },
134 });
135 const filterIndex = items.findIndex((candidate) => candidate.id === "filter-by-project");
136 expect(items[filterIndex]).toMatchObject({ label: "Show all projects", icon: "folder-tree" });
137 expect(items[filterIndex - 1]?.id).toBe("mark-unread");
138 expect(items[filterIndex + 1]?.id).toBe("auto-settle");
139 });
140
141 it("includes branch items only for threads with a branch", () => {
142 const withBranch = allIds({ ...baseState, branch: "feat/menu" });
143 expect(withBranch).toContain("new-thread-on-branch");
144 expect(withBranch).toContain("copy-branch");
145 expect(allIds(baseState)).not.toContain("new-thread-on-branch");
146 expect(allIds(baseState)).not.toContain("copy-branch");
147 });
148
149 it("flips lifecycle labels with thread state", () => {
150 expect(ids({ ...baseState, isPinned: true, isSettled: true, isSnoozed: true })).toEqual(
151 expect.arrayContaining(["unpin", "unsettle", "unsnooze"]),
152 );
153 expect(ids(baseState)).toEqual(expect.arrayContaining(["pin", "settle", "snooze"]));
154 });
155
156 it("offers auto-settle as a submenu with the current option checked", () => {
157 const find = (state: ThreadActionMenuState) =>
158 buildThreadActionMenuItems(state).find((item) => item.id === "auto-settle");
159 const on = find(baseState);
160 expect(on?.label).toBe("Auto-settle behavior");
161 expect(on?.children?.map((child) => [child.id, child.checked])).toEqual([
162 ["auto-settle:enabled", true],
163 ["auto-settle:disabled", false],
164 ]);
165 const off = find({ ...baseState, autoSettleEnabled: false });
166 expect(off?.children?.map((child) => child.checked)).toEqual([false, true]);
167 // Sits with the per-thread settings after Mark unread, not the lifecycle verbs.
168 const items = buildThreadActionMenuItems(baseState);
169 expect(items[items.findIndex((item) => item.id === "mark-unread") + 1]?.id).toBe("auto-settle");
170 expect(
171 ids({ ...baseState, supports: { ...baseState.supports, autoSettleOptOut: false } }),
172 ).not.toContain("auto-settle");
173 });
174
175 it("disables snooze when the thread cannot snooze, keeping presets visible", () => {
176 const snooze = buildThreadActionMenuItems({ ...baseState, canSnoozeNow: false }).find(
177 (item) => item.id === "snooze",
178 );
179 expect(snooze?.disabled).toBe(true);
180 expect(snooze?.children?.map((child) => child.id)).toEqual(["snooze:hour", "snooze:custom"]);
181 });
182
183 it("disables title regeneration while one is in flight", () => {
184 const item = buildThreadActionMenuItems({ ...baseState, isRegeneratingTitle: true }).find(
185 (candidate) => candidate.id === "regenerate-title",
186 );
187 expect(item).toMatchObject({ label: "Regenerating…", disabled: true });
188 });
189
190 it("marks delete as destructive and keeps it last", () => {
191 const items = buildThreadActionMenuItems({ ...baseState, branch: "main" });
192 expect(items.at(-1)).toMatchObject({ id: "delete", destructive: true });
193 });
194 it("offers archive as a non-destructive action right before delete", () => {
195 const items = buildThreadActionMenuItems(baseState);
196 const archiveItem = items.at(-2);
197 expect(archiveItem?.id).toBe("archive");
198 expect(archiveItem?.icon).toBe("archive");
199 expect(archiveItem?.separatorBefore).toBe(true);
200 expect(archiveItem?.destructive).toBeFalsy();
201 expect(items.at(-1)?.id).toBe("delete");
202 });
203
204 it("keeps archive available even when the environment lacks every other capability", () => {
205 expect(
206 ids({
207 ...baseState,
208 supports: {
209 settlement: false,
210 autoSettleOptOut: false,
211 snooze: false,
212 pinning: false,
213 titleRegeneration: false,
214 },
215 }),
216 ).toContain("archive");
217 });
218
219 it("disables archive while the thread is running", () => {
220 const archiveItem = buildThreadActionMenuItems({ ...baseState, isRunning: true }).find(
221 (item) => item.id === "archive",
222 );
223 expect(archiveItem?.disabled).toBe(true);
224 });
225});
226
227describe("buildDraftActionMenuItems", () => {
228 it("offers only the copy values the draft has", () => {
229 const items = buildDraftActionMenuItems({ hasPath: false, hasBranch: true, hasProject: true });
230 expect(items[0]).toMatchObject({ id: "copy", disabled: false });
231 expect(items[0]?.children?.map((item) => item.id)).toEqual(["copy-branch"]);
232
233 const noCopy = buildDraftActionMenuItems({
234 hasPath: false,
235 hasBranch: false,
236 hasProject: true,
237 });
238 expect(noCopy[0]).toMatchObject({ id: "copy", disabled: true, children: [] });
239 });
240
241 it("drops project settings without a project and keeps discard last", () => {
242 const items = buildDraftActionMenuItems({ hasPath: true, hasBranch: false, hasProject: false });
243 expect(items.map((item) => item.id)).toEqual(["copy", "discard"]);
244 expect(items.at(-1)).toMatchObject({ label: "Discard draft", destructive: true });
245 });
246});