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

threadActionMenu.logic.test.ts

98 lines3.8 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", "archive", "delete"]);
37 });
38​
39 it("includes branch items only for threads with a branch", () => {
40 const withBranch = allIds({ ...baseState, branch: "feat/menu" });
41 expect(withBranch).toContain("new-thread-on-branch");
42 expect(withBranch).toContain("copy-branch");
43 expect(allIds(baseState)).not.toContain("new-thread-on-branch");
44 expect(allIds(baseState)).not.toContain("copy-branch");
45 });
46​
47 it("flips lifecycle labels with thread state", () => {
48 expect(ids({ ...baseState, isPinned: true, isSettled: true, isSnoozed: true })).toEqual(
49 expect.arrayContaining(["unpin", "unsettle", "unsnooze"]),
50 );
51 expect(ids(baseState)).toEqual(expect.arrayContaining(["pin", "settle", "snooze"]));
52 });
53​
54 it("disables snooze when the thread cannot snooze, keeping presets visible", () => {
55 const snooze = buildThreadActionMenuItems({ ...baseState, canSnoozeNow: false }).find(
56 (item) => item.id === "snooze",
57 );
58 expect(snooze?.disabled).toBe(true);
59 expect(snooze?.children?.map((child) => child.id)).toEqual(["snooze:hour"]);
60 });
61​
62 it("disables title regeneration while one is in flight", () => {
63 const item = buildThreadActionMenuItems({ ...baseState, isRegeneratingTitle: true }).find(
64 (candidate) => candidate.id === "regenerate-title",
65 );
66 expect(item).toMatchObject({ label: "Regenerating…", disabled: true });
67 });
68​
69 it("marks delete as destructive and keeps it last", () => {
70 const items = buildThreadActionMenuItems({ ...baseState, branch: "main" });
71 expect(items.at(-1)).toMatchObject({ id: "delete", destructive: true });
72 });
73 it("offers archive as a non-destructive action right before delete", () => {
74 const items = buildThreadActionMenuItems(baseState);
75 const archiveItem = items.at(-2);
76 expect(archiveItem?.id).toBe("archive");
77 expect(archiveItem?.icon).toBe("archive");
78 expect(archiveItem?.separatorBefore).toBe(true);
79 expect(archiveItem?.destructive).toBeFalsy();
80 expect(items.at(-1)?.id).toBe("delete");
81 });
82​
83 it("keeps archive available even when the environment lacks every other capability", () => {
84 expect(
85 ids({
86 ...baseState,
87 supports: { settlement: false, snooze: false, pinning: false, titleRegeneration: false },
88 }),
89 ).toContain("archive");
90 });
91​
92 it("disables archive while the thread is running", () => {
93 const archiveItem = buildThreadActionMenuItems({ ...baseState, isRunning: true }).find(
94 (item) => item.id === "archive",
95 );
96 expect(archiveItem?.disabled).toBe(true);
97 });
98});