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