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

threadActionMenu.logic.test.ts

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