Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadActionMenu.logic.test.ts

131 lines5.2 KB

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 isSnoozed: false,
11 canSnoozeNow: true,
12 isRegeneratingTitle: false,
13 isRunning: false,
14 supports: { settlement: true, snooze: true, pinning: true, titleRegeneration: true },
15 snoozePresets: [
16 { id: "hour", label: "In 1 hour", whenLabel: "3:00 PM", snoozedUntil: "2026-08-07T15:00:00Z" },
17 ],
18};
19​
20function ids(state: ThreadActionMenuState): string[] {
21 return buildThreadActionMenuItems(state).map((item) => item.id);
23​
24function allIds(state: ThreadActionMenuState): string[] {
25 const flatten = (items: ReturnType<typeof buildThreadActionMenuItems>): string[] =>
26 items.flatMap((item) => [item.id, ...(item.children ? flatten(item.children) : [])]);
27 return flatten(buildThreadActionMenuItems(state));
29​
30describe("buildThreadActionMenuItems", () => {
31 it("hides lifecycle items when the environment lacks the capabilities", () => {
32 expect(
33 ids({
34 ...baseState,
35 supports: { settlement: false, snooze: false, pinning: false, titleRegeneration: false },
36 }),
37 ).toEqual(["rename", "mark-unread", "copy", "project-settings", "archive", "delete"]);
38 });
39​
40 it("groups project settings with utility actions before archive", () => {
41 const items = buildThreadActionMenuItems(baseState);
42 const copyIndex = items.findIndex((item) => item.id === "copy");
43 expect(items[copyIndex + 1]).toMatchObject({
44 id: "project-settings",
45 label: "Project settings",
46 icon: "settings",
47 });
48 expect(items[copyIndex + 2]?.id).toBe("archive");
49 });
50​
51 it("offers project filtering only for surfaces with a scoped thread list", () => {
52 expect(ids(baseState)).not.toContain("filter-by-project");
53 expect(
54 buildThreadActionMenuItems({
55 ...baseState,
56 projectFilter: { label: "Beta Project", isActive: false },
57 }).find((item) => item.id === "filter-by-project"),
58 ).toMatchObject({ label: "Filter by Beta Project", icon: "folder-tree" });
59 });
60​
61 it("offers the way back to all projects once the list is scoped", () => {
62 const items = buildThreadActionMenuItems({
63 ...baseState,
64 projectFilter: { label: "Beta Project", isActive: true },
65 });
66 const filterIndex = items.findIndex((candidate) => candidate.id === "filter-by-project");
67 expect(items[filterIndex]).toMatchObject({ label: "Show all projects", icon: "folder-tree" });
68 expect(items[filterIndex - 1]?.id).toBe("mark-unread");
69 expect(items[filterIndex + 1]?.id).toBe("copy");
70 });
71​
72 it("includes branch items only for threads with a branch", () => {
73 const withBranch = allIds({ ...baseState, branch: "feat/menu" });
74 expect(withBranch).toContain("new-thread-on-branch");
75 expect(withBranch).toContain("copy-branch");
76 expect(allIds(baseState)).not.toContain("new-thread-on-branch");
77 expect(allIds(baseState)).not.toContain("copy-branch");
78 });
79​
80 it("flips lifecycle labels with thread state", () => {
81 expect(ids({ ...baseState, isPinned: true, isSettled: true, isSnoozed: true })).toEqual(
82 expect.arrayContaining(["unpin", "unsettle", "unsnooze"]),
83 );
84 expect(ids(baseState)).toEqual(expect.arrayContaining(["pin", "settle", "snooze"]));
85 });
86​
87 it("disables snooze when the thread cannot snooze, keeping presets visible", () => {
88 const snooze = buildThreadActionMenuItems({ ...baseState, canSnoozeNow: false }).find(
89 (item) => item.id === "snooze",
90 );
91 expect(snooze?.disabled).toBe(true);
92 expect(snooze?.children?.map((child) => child.id)).toEqual(["snooze:hour", "snooze:custom"]);
93 });
94​
95 it("disables title regeneration while one is in flight", () => {
96 const item = buildThreadActionMenuItems({ ...baseState, isRegeneratingTitle: true }).find(
97 (candidate) => candidate.id === "regenerate-title",
98 );
99 expect(item).toMatchObject({ label: "Regenerating…", disabled: true });
100 });
101​
102 it("marks delete as destructive and keeps it last", () => {
103 const items = buildThreadActionMenuItems({ ...baseState, branch: "main" });
104 expect(items.at(-1)).toMatchObject({ id: "delete", destructive: true });
105 });
106 it("offers archive as a non-destructive action right before delete", () => {
107 const items = buildThreadActionMenuItems(baseState);
108 const archiveItem = items.at(-2);
109 expect(archiveItem?.id).toBe("archive");
110 expect(archiveItem?.icon).toBe("archive");
111 expect(archiveItem?.separatorBefore).toBe(true);
112 expect(archiveItem?.destructive).toBeFalsy();
113 expect(items.at(-1)?.id).toBe("delete");
114 });
115​
116 it("keeps archive available even when the environment lacks every other capability", () => {
117 expect(
118 ids({
119 ...baseState,
120 supports: { settlement: false, snooze: false, pinning: false, titleRegeneration: false },
121 }),
122 ).toContain("archive");
123 });
124​
125 it("disables archive while the thread is running", () => {
126 const archiveItem = buildThreadActionMenuItems({ ...baseState, isRunning: true }).find(
127 (item) => item.id === "archive",
128 );
129 expect(archiveItem?.disabled).toBe(true);
130 });
131});