Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 EnvironmentId,
4 ThreadId,
5 type ContextMenuItem,
6} from "@t3tools/contracts";
7import { AsyncResult } from "effect/reactivity";
8import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
10import type { ThreadActionMenuId } from "../components/threadActionMenu.logic";
11
12function deferred<T>() {
13 let resolve!: (value: T) => void;
14 const promise = new Promise<T>((complete) => {
15 resolve = complete;
16 });
17 return { promise, resolve };
18}
19
20const state = vi.hoisted(() => ({
21 granted: new Set<string>(),
22 effects: [] as string[],
23 completed: deferred<void>(),
24 show: vi.fn<
25 (
26 items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>>,
27 position: { x: number; y: number },
28 ) => Promise<ThreadActionMenuId | null>
29 >(),
30}));
31
32function recordEffect(action: string) {
33 state.effects.push(action);
34 state.completed.resolve();
35}
36
37vi.mock("../components/CustomSnoozeDialog", () => ({ requestCustomSnooze: vi.fn() }));
38vi.mock("react", () => ({
39 useCallback: (callback: unknown) => callback,
40 useMemo: (factory: () => unknown) => factory(),
41}));
42vi.mock("@tanstack/react-router", () => ({
43 useRouter: () => ({ navigate: async () => recordEffect("project-settings") }),
44}));
45vi.mock("../state/session", () => ({
46 readEnvironmentScope: (environmentId: string, scope: string) =>
47 scope === AuthOrchestrationOperateScope && state.granted.has(environmentId),
48}));
49vi.mock("../state/entities", () => ({
50 readEnvironmentServerConfigLoaded: () => true,
51 readEnvironmentSupportsAutoSettleOptOut: () => true,
52 readThreadShell: () => ({
53 id: "thread",
54 environmentId: "secondary",
55 projectId: "project",
56 title: "Thread",
57 branch: "main",
58 worktreePath: null,
59 runtime: null,
60 latestRun: null,
61 }),
62 useProjects: () => [{ id: "project", environmentId: "secondary" }],
63}));
64vi.mock("../state/environments", () => ({ usePrimaryEnvironmentId: () => "primary" }));
65vi.mock("../state/threads", () => ({ threadEnvironment: { updateMetadata: "metadata" } }));
66vi.mock("../state/use-atom-command", () => ({
67 useAtomCommand: () => async () => {
68 recordEffect("metadata");
69 return AsyncResult.success(undefined);
70 },
71}));
72vi.mock("../localApi", () => ({
73 readLocalApi: () => ({
74 contextMenu: { show: state.show, close: () => {} },
75 dialogs: {
76 confirm: async () => {
77 recordEffect("confirm");
78 return false;
79 },
80 },
81 }),
82}));
83vi.mock("@t3tools/client-runtime/state/project-grouping", () => ({
84 deriveLogicalProjectKeyFromSettings: () => "project",
85 derivePhysicalProjectKey: () => "project",
86 selectProjectGroupingSettings: () => ({}),
87}));
88vi.mock("../sidebarProjectGrouping", () => ({
89 buildPhysicalToLogicalProjectKeyMap: () => new Map(),
90}));
91vi.mock("../uiStateStore", () => ({
92 useUiStateStore: (select: (store: unknown) => unknown) =>
93 select({
94 markThreadUnread: () => recordEffect("mark-unread"),
95 }),
96}));
97vi.mock("../components/ui/toast", () => ({
98 stackedThreadToast: (toast: unknown) => toast,
99 toastManager: { add: () => state.completed.resolve() },
100}));
101vi.mock("../components/Sidebar.snooze", () => ({
102 resolveSnoozePresets: () => [
103 { id: "hour", label: "In 1 hour", whenLabel: "3 PM", snoozedUntil: "2099-01-01T00:00:00Z" },
104 ],
105 snoozeWakeDescription: () => "later",
106}));
107vi.mock("./useCopyToClipboard", () => ({
108 useCopyToClipboard: () => ({ copyToClipboard: () => recordEffect("copy") }),
109}));
110vi.mock("./useHandleNewThread", () => ({
111 useNewThreadHandler: () => async () => recordEffect("draft"),
112}));
113vi.mock("./useSettings", () => ({
114 useClientSettings: (select: (settings: unknown) => unknown) =>
115 select({
116 confirmThreadDelete: true,
117 confirmThreadArchive: true,
118 timestampFormat: "12-hour",
119 }),
120}));
121vi.mock("./useThreadActions", () => ({
122 useThreadActions: () =>
123 Object.fromEntries(
124 [
125 "markThreadUnread",
126 "settleThread",
127 "unsettleThread",
128 "snoozeThread",
129 "unsnoozeThread",
130 "pinThread",
131 "confirmAndUnpinThread",
132 "archiveThread",
133 "deleteThread",
134 ].map((action) => [
135 action,
136 async () => {
137 recordEffect(action === "markThreadUnread" ? "mark-unread" : action);
138 return AsyncResult.success(undefined);
139 },
140 ]),
141 ),
142}));
143
144import { useThreadActionMenu } from "./useThreadActionMenu";
145
146const target = {
147 environmentId: EnvironmentId.make("secondary"),
148 threadId: ThreadId.make("thread"),
149};
150const position = { x: 10, y: 20 };
151const createMenu = () =>
152 useThreadActionMenu({
153 threadRef: target,
154 projectCwd: "/project",
155 onStartRename: () => recordEffect("rename"),
156 canShareWithGuest: false,
157 });
158
159beforeEach(() => {
160 state.granted = new Set(["primary"]);
161 state.effects = [];
162 state.completed = deferred<void>();
163 state.show.mockReset().mockResolvedValue(null);
164});
165
166describe("thread menu permissions", () => {
167 it.each(["rename", "regenerate-title", "delete", "pin", "settle", "archive"] as const)(
168 "%s rechecks after the native menu closes",
169 async (action) => {
170 state.granted.add("secondary");
171 const choice = deferred<ThreadActionMenuId | null>();
172 state.show.mockReturnValue(choice.promise);
173 createMenu().openMenu(position);
174 state.granted.delete("secondary");
175 choice.resolve(action);
176 await state.completed.promise;
177 expect(state.effects).toEqual([]);
178 },
179 );
180});