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

fileContextMenu.test.ts

135 lines5.0 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import { resolveFileContextMenuAbsolutePath, useFileContextMenu } from "./fileContextMenu";
5import type { RemoteOpenResolution } from "./remoteOpen";
6​
7const state = vi.hoisted(() => ({
8 remoteOpen: { state: { mode: "local-exec" }, isResolved: true } as RemoteOpenResolution,
9 openInEditor: vi.fn(async () => ({ _tag: "Success" })),
10 openRemoteEditorUrl: vi.fn(async (_url: string) => true),
11}));
12​
13vi.mock("@effect/atom-react", () => ({
14 useAtomValue: () => ({
15 availableEditors: ["cursor", "file-manager"],
16 shellRevealInFileManager: true,
17 shellRevealInFileManagerKind: "xdg-open",
18 environment: { platform: { os: "linux" } },
19 }),
20}));
21vi.mock("gt-react", async (importOriginal) => ({
22 ...(await importOriginal<typeof import("gt-react")>()),
23 useMessages: () => (text: string) => text,
24}));
25vi.mock(import("./state/session"), async (original) => ({
26 ...(await original()),
27 useEnvironmentScope: () => true,
28}));
29vi.mock("./editorMenuIcons", () => ({ useEditorMenuIcons: () => new Map() }));
30vi.mock("./state/use-atom-command", () => ({ useAtomCommand: () => state.openInEditor }));
31vi.mock(import("./remoteOpen"), async (original) => ({
32 ...(await original()),
33 useRemoteOpenResolution: () => state.remoteOpen,
34 useRemoteCapableEditors: () => ["vscode"],
35 openRemoteEditorUrl: state.openRemoteEditorUrl,
36}));
37​
38const BASE_TARGET = {
39 environmentId: EnvironmentId.make("environment-local"),
40 filePath: "src/index.ts",
41 workspaceRoot: "/workspace/project",
42};
43​
44describe("resolveFileContextMenuAbsolutePath", () => {
45 it("strips the repository prefix when the repo root is nested in the workspace", () => {
46 expect(
47 resolveFileContextMenuAbsolutePath({
48 ...BASE_TARGET,
49 workspaceRoot: "/workspace/project/packages/app",
50 repositoryRoot: "/workspace/project",
51 filePath: "packages/app/src/index.ts",
52 }),
53 ).toBe("/workspace/project/packages/app/src/index.ts");
54 });
55​
56 it("returns null for paths outside the workspace when a repository root is set", () => {
57 expect(
58 resolveFileContextMenuAbsolutePath({
59 ...BASE_TARGET,
60 workspaceRoot: "/workspace/project/packages/app",
61 repositoryRoot: "/workspace/project",
62 filePath: "other/src/index.ts",
63 }),
64 ).toBeNull();
65 });
66​
67 it("rejects absolute paths without a workspace root, matching diff path resolution", () => {
68 expect(
69 resolveFileContextMenuAbsolutePath({
70 ...BASE_TARGET,
71 workspaceRoot: undefined,
72 filePath: "/absolute/src/index.ts",
73 }),
74 ).toBeNull();
75 });
76});
77​
78describe("useFileContextMenu", () => {
79 it.each<[string, RemoteOpenResolution]>([
80 ["remote-unavailable", { state: { mode: "remote-unavailable" }, isResolved: true }],
81 ["unresolved", { state: { mode: "local-exec" }, isResolved: false }],
82 ])("offers no open actions when remote open is %s", async (_mode, resolution) => {
83 state.remoteOpen = resolution;
84 state.openInEditor.mockClear();
85 const menu = useFileContextMenu(BASE_TARGET.environmentId);
86​
87 expect(menu.buildItems(BASE_TARGET)).toEqual([]);
88 await menu.activate("open", BASE_TARGET);
89 await menu.activate("editor:vscode", BASE_TARGET);
90 expect(state.openInEditor).not.toHaveBeenCalled();
91 });
92​
93 it("opens files over SSH remote links instead of on the server host", async () => {
94 state.remoteOpen = {
95 state: { mode: "remote-links", host: { kind: "ssh-alias", host: "box", username: "me" } },
96 isResolved: true,
97 };
98 state.openInEditor.mockClear();
99 state.openRemoteEditorUrl.mockClear();
100 const menu = useFileContextMenu(BASE_TARGET.environmentId);
101​
102 expect(menu.buildItems(BASE_TARGET)).toEqual([
103 {
104 id: "open-with",
105 label: "Open with",
106 children: [{ id: "editor:vscode", label: "VS Code" }],
107 },
108 ]);
109 await menu.activate("open", BASE_TARGET);
110 await menu.activate("editor:vscode", BASE_TARGET);
111 await menu.activate("editor:vscode", { ...BASE_TARGET, filePath: "src", isDirectory: true });
112 expect(state.openInEditor).not.toHaveBeenCalled();
113 expect(state.openRemoteEditorUrl.mock.calls).toEqual([
114 ["vscode://vscode-remote/ssh-remote+me%40box/workspace/project/src/index.ts:1:1"],
115 ["vscode://vscode-remote/ssh-remote+me%40box/workspace/project/src?windowId=_blank"],
116 ]);
117 });
118​
119 it("opens files on the server host when remote open is local-exec", async () => {
120 state.remoteOpen = { state: { mode: "local-exec" }, isResolved: true };
121 state.openInEditor.mockClear();
122 const menu = useFileContextMenu(BASE_TARGET.environmentId);
123​
124 expect(menu.buildItems(BASE_TARGET).map((item) => item.id)).toEqual([
125 "open",
126 "reveal-in-folder",
127 "open-with",
128 ]);
129 await menu.activate("open", BASE_TARGET);
130 expect(state.openInEditor).toHaveBeenCalledWith({
131 environmentId: BASE_TARGET.environmentId,
132 input: { cwd: "/workspace/project/src/index.ts", editor: "file-manager" },
133 });
134 });
135});