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

threadActionMenu.logic.test.ts

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