Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5
6const state = vi.hoisted(() => ({
7 canOperate: false,
8 canWriteSettings: true,
9 connected: true,
10 openEditor: vi.fn(),
11}));
12
13vi.mock(import("@tanstack/react-router"), async (original) => ({
14 ...(await original()),
15 useLocation: (() => "") as never,
16}));
17vi.mock(import("./SettingsScopeContext"), async (original) => ({
18 ...(await original()),
19 useSettingsScope: () => {
20 const environment = {
21 environmentId: "primary-settings",
22 serverConfig: {
23 keybindings: [],
24 keybindingsConfigPath: "/fixture/keybindings.json",
25 availableEditors: [],
26 },
27 };
28 return (
29 state.connected
30 ? { environment, connectedEnvironments: [environment] }
31 : { environment: null, connectedEnvironments: [] }
32 ) as never;
33 },
34}));
35vi.mock(import("../../state/session"), async (original) => {
36 const hasScope = (environmentId: EnvironmentId | null, scope: string) =>
37 environmentId === "primary-settings" &&
38 (scope === "orchestration:operate"
39 ? state.canOperate
40 : scope === "settings:write" && state.canWriteSettings);
41 return {
42 ...(await original()),
43 useEnvironmentScope: hasScope as never,
44 readEnvironmentScope: hasScope as never,
45 useEnvironmentsWithScope: (() =>
46 new Set(state.canWriteSettings ? ["primary-settings"] : [])) as never,
47 };
48});
49vi.mock(import("../../editorPreferences"), async (original) => ({
50 ...(await original()),
51 useOpenInPreferredEditor: ((environmentId: EnvironmentId) => (path: string) =>
52 state.openEditor({ environmentId, path })) as never,
53}));
54
55import { KeybindingsSettingsPanel } from "./KeybindingsSettings";
56
57let root: Root;
58let container: HTMLDivElement;
59
60const render = () => act(async () => root.render(<KeybindingsSettingsPanel />));
61const openButton = () => {
62 const button = container.querySelector<HTMLButtonElement>(
63 'button[aria-label="Open keybindings.json"]',
64 );
65 expect(button).not.toBeNull();
66 return button!;
67};
68
69beforeEach(() => {
70 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
71 state.canOperate = false;
72 state.canWriteSettings = true;
73 state.connected = true;
74 state.openEditor.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
75 container = document.createElement("div");
76 document.body.append(container);
77 root = createRoot(container);
78});
79afterEach(async () => {
80 await act(async () => root.unmount());
81 container.remove();
82 vi.unstubAllGlobals();
83});
84
85describe("KeybindingsSettings editor permission", () => {
86 it("rejects editor launches when settings writes are the only write grant", async () => {
87 await render();
88 expect(openButton().disabled).toBe(true);
89 await act(async () => openButton().click());
90 expect(state.openEditor).not.toHaveBeenCalled();
91 });
92
93 it("opens the primary environment's file after operate is granted without settings writes", async () => {
94 state.canWriteSettings = false;
95 await render();
96 state.canOperate = true;
97 await render();
98 expect(openButton().disabled).toBe(false);
99 await act(async () => openButton().click());
100
101 expect(state.openEditor).toHaveBeenCalledWith({
102 environmentId: "primary-settings",
103 path: "/fixture/keybindings.json",
104 });
105 });
106
107 it("rejects a queued editor launch after operate is revoked", async () => {
108 state.canOperate = true;
109 await render();
110 const button = openButton();
111 state.canOperate = false;
112 // The rendered button is still enabled; the click must recheck the live grant.
113 await act(async () => button.click());
114
115 expect(state.openEditor).not.toHaveBeenCalled();
116 await render();
117 expect(openButton().disabled).toBe(true);
118 });
119});
120
121describe("KeybindingsSettings write notice", () => {
122 it("asks for a connected environment when none is connected", async () => {
123 state.connected = false;
124 await render();
125 expect(container.textContent).toContain("Connect an environment to change keybindings.");
126 });
127
128 it("names the permission when the connection cannot write settings", async () => {
129 state.canWriteSettings = false;
130 await render();
131 expect(container.textContent).toContain(
132 "This connection can view keybindings but cannot change them.",
133 );
134 });
135});