Files
t3code/apps/web/src/fileContextMenu.test.ts
T
Alex Southwellandgithub-actions[bot] 9051ed9c39 feat(web): open diff files from a right-click context menu (#11842)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-09-17 17:30:23 -07:00

103 lines
3.2 KiB
TypeScript

import { EnvironmentId } from "@t3tools/contracts";
import * as NodeAssert from "node:assert/strict";
import { describe, expect, it } from "vite-plus/test";
import { buildFileContextMenuItems, resolveFileContextMenuAbsolutePath } from "./fileContextMenu";
const BASE_TARGET = {
environmentId: EnvironmentId.make("environment-local"),
filePath: "src/index.ts",
workspaceRoot: "/workspace/project",
};
const EMPTY_CAPABILITIES = {
revealLabel: undefined,
canOpenDefault: false,
editorIds: [],
};
describe("resolveFileContextMenuAbsolutePath", () => {
it("joins workspace-relative diff paths onto the workspace root", () => {
expect(resolveFileContextMenuAbsolutePath(BASE_TARGET)).toBe("/workspace/project/src/index.ts");
});
it("strips the repository prefix when the repo root is nested in the workspace", () => {
expect(
resolveFileContextMenuAbsolutePath({
...BASE_TARGET,
workspaceRoot: "/workspace/project/packages/app",
repositoryRoot: "/workspace/project",
filePath: "packages/app/src/index.ts",
}),
).toBe("/workspace/project/packages/app/src/index.ts");
});
it("returns null for paths outside the workspace when a repository root is set", () => {
expect(
resolveFileContextMenuAbsolutePath({
...BASE_TARGET,
workspaceRoot: "/workspace/project/packages/app",
repositoryRoot: "/workspace/project",
filePath: "other/src/index.ts",
}),
).toBeNull();
});
it("rejects absolute paths without a workspace root, matching diff path resolution", () => {
expect(
resolveFileContextMenuAbsolutePath({
...BASE_TARGET,
workspaceRoot: undefined,
filePath: "/absolute/src/index.ts",
}),
).toBeNull();
});
});
describe("buildFileContextMenuItems", () => {
it("offers open, reveal, and an open-with submenu when all are available", () => {
const items = buildFileContextMenuItems({
hasAbsolutePath: true,
capabilities: {
revealLabel: "Reveal in Finder",
canOpenDefault: true,
editorIds: ["vscode", "cursor", "file-manager"],
},
});
expect(items.map((item) => item.id)).toEqual(["open", "reveal-in-folder", "open-with"]);
expect(items[0]).toMatchObject({ label: "Open" });
expect(items[1]).toMatchObject({ label: "Reveal in Finder" });
const openWith = items[2];
NodeAssert.ok(openWith);
expect(openWith.children?.map((child) => child.id)).toEqual(["editor:vscode", "editor:cursor"]);
});
it("offers only the reveal item when just reveal is enabled", () => {
const items = buildFileContextMenuItems({
hasAbsolutePath: true,
capabilities: {
revealLabel: "Reveal in File Explorer",
canOpenDefault: false,
editorIds: [],
},
});
expect(items.map((item) => item.id)).toEqual(["reveal-in-folder"]);
expect(items[0]).toMatchObject({ label: "Reveal in File Explorer" });
});
it("offers nothing when the path cannot be resolved", () => {
expect(
buildFileContextMenuItems({
hasAbsolutePath: false,
capabilities: {
revealLabel: "Reveal in Finder",
canOpenDefault: true,
editorIds: ["vscode"],
},
}),
).toEqual([]);
});
});