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