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

KeybindingsSettings.environment.test.tsx

143 lines4.6 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { visitElements } from "../../test/reactElementTree";
5import { reactHookHarness as hooks } from "../../test/reactHookHarness";
6​
7const state = vi.hoisted(() => ({
8 canOperate: false,
9 canWriteSettings: true,
10 openEditor: vi.fn(),
11 upsert: vi.fn(),
12 remove: vi.fn(),
13}));
14​
15vi.mock("react", async (importOriginal) => {
16 const actual = await importOriginal<typeof import("react")>();
17 const { reactHookHarness } = await import("../../test/reactHookHarness");
18 return {
19 ...actual,
20 useCallback: reactHookHarness.useCallback,
21 useEffect: () => undefined,
22 useMemo: reactHookHarness.useMemo,
23 useRef: reactHookHarness.useRef,
24 useState: reactHookHarness.useState,
25 };
26});
27​
28vi.mock("react/compiler-runtime", async () => {
29 const { reactHookHarness } = await import("../../test/reactHookHarness");
30 return { c: reactHookHarness.useMemoCache };
31});
32​
33vi.mock("@effect/atom-react", () => ({
34 useAtomValue: (atom: string) => (atom === "config-path" ? "/fixture/keybindings.json" : []),
35}));
36​
37vi.mock("@tanstack/react-router", () => ({ useLocation: () => "" }));
38vi.mock("../ProjectFavicon", () => ({ ProjectFavicon: () => null }));
39vi.mock("../../state/server", () => ({
40 primaryServerKeybindingsAtom: "keybindings",
41 primaryServerKeybindingsConfigPathAtom: "config-path",
42 primaryServerAvailableEditorsAtom: "editors",
43 serverEnvironment: { upsertKeybinding: state.upsert, removeKeybinding: state.remove },
44}));
45​
46vi.mock("../../state/environments", () => ({
47 usePrimaryEnvironment: () => ({ environmentId: "primary-settings" }),
48}));
49​
50vi.mock("./SettingsScopeContext", () => ({
51 useSettingsScope: () => {
52 const environment = {
53 environmentId: "primary-settings",
54 serverConfig: {
55 keybindings: [],
56 keybindingsConfigPath: "/fixture/keybindings.json",
57 availableEditors: [],
58 },
59 };
60 return { environment, connectedEnvironments: [environment] };
61 },
62}));
63​
64vi.mock("../../state/session", () => {
65 const hasScope = (environmentId: EnvironmentId | null, scope: string) =>
66 environmentId === "primary-settings" &&
67 (scope === "orchestration:operate"
68 ? state.canOperate
69 : scope === "settings:write" && state.canWriteSettings);
70 return {
71 useEnvironmentScope: hasScope,
72 readEnvironmentScope: hasScope,
73 useEnvironmentsWithScope: () => new Set(state.canWriteSettings ? ["primary-settings"] : []),
74 };
75});
76​
77vi.mock("../../state/use-atom-command", () => ({
78 useAtomCommand: (command: unknown) => command,
79}));
80​
81vi.mock("../../editorPreferences", () => ({
82 useOpenInPreferredEditor: (environmentId: EnvironmentId) => (path: string) =>
83 state.openEditor({ environmentId, path }),
84}));
85​
86import { KeybindingsSettingsPanel } from "./KeybindingsSettings";
87​
88function renderPanel() {
89 hooks.beginRender();
90 return KeybindingsSettingsPanel();
92​
93function openButton(panel: unknown) {
94 const button = visitElements(
95 panel,
96 (element) => element.props["aria-label"] === "Open keybindings.json",
97 );
98 if (!button) throw new Error("Missing Open keybindings.json action.");
99 return button;
101​
102describe("KeybindingsSettings editor permission", () => {
103 beforeEach(() => {
104 hooks.reset();
105 state.canOperate = false;
106 state.canWriteSettings = true;
107 state.openEditor.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
108 state.upsert.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
109 state.remove.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
110 });
111​
112 it("rejects editor launches when settings writes are the only write grant", () => {
113 const button = openButton(renderPanel());
114 (button.props.onClick as () => void)();
115​
116 expect(state.openEditor).not.toHaveBeenCalled();
117 expect(button.props.disabled).toBe(true);
118 });
119​
120 it("opens the primary environment's file after operate is granted without settings writes", () => {
121 state.canWriteSettings = false;
122 renderPanel();
123 state.canOperate = true;
124 const button = openButton(renderPanel());
125 expect(button.props.disabled).toBe(false);
126 (button.props.onClick as () => void)();
127​
128 expect(state.openEditor).toHaveBeenCalledWith({
129 environmentId: "primary-settings",
130 path: "/fixture/keybindings.json",
131 });
132 });
133​
134 it("rejects a queued editor launch after operate is revoked", () => {
135 state.canOperate = true;
136 const open = openButton(renderPanel()).props.onClick as () => void;
137 state.canOperate = false;
138 open();
139​
140 expect(state.openEditor).not.toHaveBeenCalled();
141 expect(openButton(renderPanel()).props.disabled).toBe(true);
142 });
143});