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

fileContextMenu.test.ts

145 lines5.3 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 const gt = await importOriginal<typeof import("gt-react")>();
23 return { ...gt, useMessages: () => gt.mFallback };
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("keeps a literal tilde folder inside the repository", () => {
46 expect(
47 resolveFileContextMenuAbsolutePath({
48 ...BASE_TARGET,
49 filePath: "~/notes.txt",
50 workspaceRoot: "/home/alex/repo",
51 }),
52 ).toBe("/home/alex/repo/~/notes.txt");
53 });
54​
55 it("strips the repository prefix when the repo root is nested in the workspace", () => {
56 expect(
57 resolveFileContextMenuAbsolutePath({
58 ...BASE_TARGET,
59 workspaceRoot: "/workspace/project/packages/app",
60 repositoryRoot: "/workspace/project",
61 filePath: "packages/app/src/index.ts",
62 }),
63 ).toBe("/workspace/project/packages/app/src/index.ts");
64 });
65​
66 it("returns null for paths outside the workspace when a repository root is set", () => {
67 expect(
68 resolveFileContextMenuAbsolutePath({
69 ...BASE_TARGET,
70 workspaceRoot: "/workspace/project/packages/app",
71 repositoryRoot: "/workspace/project",
72 filePath: "other/src/index.ts",
73 }),
74 ).toBeNull();
75 });
76​
77 it("rejects absolute paths without a workspace root, matching diff path resolution", () => {
78 expect(
79 resolveFileContextMenuAbsolutePath({
80 ...BASE_TARGET,
81 workspaceRoot: undefined,
82 filePath: "/absolute/src/index.ts",
83 }),
84 ).toBeNull();
85 });
86});
87​
88describe("useFileContextMenu", () => {
89 it.each<[string, RemoteOpenResolution]>([
90 ["remote-unavailable", { state: { mode: "remote-unavailable" }, isResolved: true }],
91 ["unresolved", { state: { mode: "local-exec" }, isResolved: false }],
92 ])("offers no open actions when remote open is %s", async (_mode, resolution) => {
93 state.remoteOpen = resolution;
94 state.openInEditor.mockClear();
95 const menu = useFileContextMenu(BASE_TARGET.environmentId);
96​
97 expect(menu.buildItems(BASE_TARGET)).toEqual([]);
98 await menu.activate("open", BASE_TARGET);
99 await menu.activate("editor:vscode", BASE_TARGET);
100 expect(state.openInEditor).not.toHaveBeenCalled();
101 });
102​
103 it("opens files over SSH remote links instead of on the server host", async () => {
104 state.remoteOpen = {
105 state: { mode: "remote-links", host: { kind: "ssh-alias", host: "box", username: "me" } },
106 isResolved: true,
107 };
108 state.openInEditor.mockClear();
109 state.openRemoteEditorUrl.mockClear();
110 const menu = useFileContextMenu(BASE_TARGET.environmentId);
111​
112 expect(menu.buildItems(BASE_TARGET)).toEqual([
113 {
114 id: "open-with",
115 label: "Open with",
116 children: [{ id: "editor:vscode", label: "VS Code" }],
117 },
118 ]);
119 await menu.activate("open", BASE_TARGET);
120 await menu.activate("editor:vscode", BASE_TARGET);
121 await menu.activate("editor:vscode", { ...BASE_TARGET, filePath: "src", isDirectory: true });
122 expect(state.openInEditor).not.toHaveBeenCalled();
123 expect(state.openRemoteEditorUrl.mock.calls).toEqual([
124 ["vscode://vscode-remote/ssh-remote+me%40box/workspace/project/src/index.ts:1:1"],
125 ["vscode://vscode-remote/ssh-remote+me%40box/workspace/project/src?windowId=_blank"],
126 ]);
127 });
128​
129 it("opens files on the server host when remote open is local-exec", async () => {
130 state.remoteOpen = { state: { mode: "local-exec" }, isResolved: true };
131 state.openInEditor.mockClear();
132 const menu = useFileContextMenu(BASE_TARGET.environmentId);
133​
134 expect(menu.buildItems(BASE_TARGET).map((item) => item.id)).toEqual([
135 "open",
136 "reveal-in-folder",
137 "open-with",
138 ]);
139 await menu.activate("open", BASE_TARGET);
140 expect(state.openInEditor).toHaveBeenCalledWith({
141 environmentId: BASE_TARGET.environmentId,
142 input: { cwd: "/workspace/project/src/index.ts", editor: "file-manager" },
143 });
144 });
145});