|
| 1 | +import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection"; |
| 2 | +import { EnvironmentId, type KeybindingRule, type ServerConfig } from "@t3tools/contracts"; |
| 3 | +import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings"; |
| 4 | +import { Atom, AtomRegistry } from "effect/unstable/reactivity"; |
| 5 | +import { afterEach, describe, expect, it } from "vite-plus/test"; |
| 6 | + |
| 7 | +import { resolveShortcutCommand } from "../keybindings"; |
| 8 | +import { createShortcutKeybindingsAtom } from "./server"; |
| 9 | + |
| 10 | +const PRIMARY = EnvironmentId.make("primary"); |
| 11 | +const A = EnvironmentId.make("a"); |
| 12 | +const B = EnvironmentId.make("b"); |
| 13 | + |
| 14 | +// The reporter's swap: Cmd+N opens a local thread instead of a regular one. |
| 15 | +const SWAPPED: KeybindingRule[] = [{ key: "mod+n", command: "chat.newLocal" }]; |
| 16 | +const STALE: KeybindingRule[] = [{ key: "mod+n", command: "chat.newWithoutProject" }]; |
| 17 | + |
| 18 | +function config(rules: readonly KeybindingRule[]) { |
| 19 | + return { |
| 20 | + keybindings: compileResolvedKeybindingsConfig(rules), |
| 21 | + providers: [], |
| 22 | + } as unknown as ServerConfig; |
| 23 | +} |
| 24 | + |
| 25 | +function environment(phase: EnvironmentConnectionPhase, serverConfig: ServerConfig | null) { |
| 26 | + return { connection: { phase, error: null, traceId: null }, serverConfig }; |
| 27 | +} |
| 28 | + |
| 29 | +type Environment = ReturnType<typeof environment>; |
| 30 | + |
| 31 | +function makeHarness(input: { |
| 32 | + primaryEnvironmentId?: EnvironmentId | null; |
| 33 | + primaryConfig?: ServerConfig | null; |
| 34 | + environments?: ReadonlyArray<readonly [EnvironmentId, Environment]>; |
| 35 | +}) { |
| 36 | + const primaryEnvironmentIdAtom = Atom.make(input.primaryEnvironmentId ?? null); |
| 37 | + const primaryConfigAtom = Atom.make(input.primaryConfig ?? null); |
| 38 | + const environmentsAtom = Atom.make<ReadonlyMap<EnvironmentId, Environment>>( |
| 39 | + new Map(input.environments ?? []), |
| 40 | + ); |
| 41 | + const keybindingsAtom = createShortcutKeybindingsAtom({ |
| 42 | + primaryEnvironmentIdAtom, |
| 43 | + primaryConfigAtom, |
| 44 | + environmentsAtom, |
| 45 | + }); |
| 46 | + const registry = AtomRegistry.make(); |
| 47 | + registries.push(registry); |
| 48 | + // Keep the atom mounted, like the shortcut handlers do. |
| 49 | + registry.subscribe(keybindingsAtom, () => {}); |
| 50 | + return { |
| 51 | + registry, |
| 52 | + primaryEnvironmentIdAtom, |
| 53 | + primaryConfigAtom, |
| 54 | + environmentsAtom, |
| 55 | + keybindings: () => registry.get(keybindingsAtom), |
| 56 | + /** The command Cmd+N runs on macOS. */ |
| 57 | + cmdN: () => |
| 58 | + resolveShortcutCommand( |
| 59 | + { key: "n", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false }, |
| 60 | + registry.get(keybindingsAtom), |
| 61 | + { platform: "MacIntel" }, |
| 62 | + ), |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +const registries: AtomRegistry.AtomRegistry[] = []; |
| 67 | +afterEach(() => { |
| 68 | + for (const registry of registries.splice(0)) registry.dispose(); |
| 69 | +}); |
| 70 | + |
| 71 | +describe("createShortcutKeybindingsAtom", () => { |
| 72 | + it("follows the primary environment when it has a config", () => { |
| 73 | + const harness = makeHarness({ primaryEnvironmentId: PRIMARY, primaryConfig: config(SWAPPED) }); |
| 74 | + expect(harness.cmdN()).toBe("chat.newLocal"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("keeps the defaults while the primary config is loading", () => { |
| 78 | + const harness = makeHarness({ |
| 79 | + primaryEnvironmentId: PRIMARY, |
| 80 | + environments: [[B, environment("connected", config(SWAPPED))]], |
| 81 | + }); |
| 82 | + expect(harness.cmdN()).toBe("chat.new"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("follows the connected environment without a primary", () => { |
| 86 | + const harness = makeHarness({ environments: [[B, environment("connected", config(SWAPPED))]] }); |
| 87 | + expect(harness.cmdN()).toBe("chat.newLocal"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("skips an offline environment's cached config, as Settings does", () => { |
| 91 | + const harness = makeHarness({ |
| 92 | + environments: [ |
| 93 | + [A, environment("offline", config(STALE))], |
| 94 | + [B, environment("connected", config(SWAPPED))], |
| 95 | + ], |
| 96 | + }); |
| 97 | + expect(harness.cmdN()).toBe("chat.newLocal"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("keeps the defaults when no environment is connected", () => { |
| 101 | + const harness = makeHarness({ |
| 102 | + environments: [[A, environment("offline", config(STALE))]], |
| 103 | + }); |
| 104 | + expect(harness.cmdN()).toBe("chat.new"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("follows edits to the connected environment's keybindings", () => { |
| 108 | + const harness = makeHarness({ environments: [[B, environment("connected", config([]))]] }); |
| 109 | + expect(harness.cmdN()).toBe("chat.new"); |
| 110 | + |
| 111 | + harness.registry.set( |
| 112 | + harness.environmentsAtom, |
| 113 | + new Map([[B, environment("connected", config(SWAPPED))]]), |
| 114 | + ); |
| 115 | + expect(harness.cmdN()).toBe("chat.newLocal"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("keeps the same bindings when an unrelated config update arrives", () => { |
| 119 | + const remote = config(SWAPPED); |
| 120 | + const harness = makeHarness({ |
| 121 | + environments: [ |
| 122 | + [A, environment("offline", config([]))], |
| 123 | + [B, environment("connected", remote)], |
| 124 | + ], |
| 125 | + }); |
| 126 | + const before = harness.keybindings(); |
| 127 | + |
| 128 | + // A provider status update on B and a reconnect attempt on A. |
| 129 | + harness.registry.set( |
| 130 | + harness.environmentsAtom, |
| 131 | + new Map([ |
| 132 | + [A, environment("reconnecting", config([]))], |
| 133 | + [B, environment("connected", { ...remote, providers: [] })], |
| 134 | + ]), |
| 135 | + ); |
| 136 | + expect(harness.keybindings()).toBe(before); |
| 137 | + }); |
| 138 | + |
| 139 | + it("follows connection and primary changes without a reload", () => { |
| 140 | + const aConfig = config(STALE); |
| 141 | + const bConfig = config(SWAPPED); |
| 142 | + const harness = makeHarness({ |
| 143 | + environments: [ |
| 144 | + [A, environment("connected", aConfig)], |
| 145 | + [B, environment("connected", bConfig)], |
| 146 | + ], |
| 147 | + }); |
| 148 | + const setEnvironments = (a: EnvironmentConnectionPhase, b: EnvironmentConnectionPhase) => |
| 149 | + harness.registry.set( |
| 150 | + harness.environmentsAtom, |
| 151 | + new Map([ |
| 152 | + [A, environment(a, aConfig)], |
| 153 | + [B, environment(b, bConfig)], |
| 154 | + ]), |
| 155 | + ); |
| 156 | + |
| 157 | + // Both connected: the first in catalog order wins. |
| 158 | + expect(harness.cmdN()).toBe("chat.newWithoutProject"); |
| 159 | + |
| 160 | + setEnvironments("offline", "connected"); |
| 161 | + expect(harness.cmdN()).toBe("chat.newLocal"); |
| 162 | + |
| 163 | + setEnvironments("offline", "offline"); |
| 164 | + expect(harness.cmdN()).toBe("chat.new"); |
| 165 | + |
| 166 | + // A primary appears while its config is loading; B is back but must not leak in. |
| 167 | + setEnvironments("offline", "connected"); |
| 168 | + harness.registry.set(harness.primaryEnvironmentIdAtom, PRIMARY); |
| 169 | + expect(harness.cmdN()).toBe("chat.new"); |
| 170 | + |
| 171 | + harness.registry.set( |
| 172 | + harness.primaryConfigAtom, |
| 173 | + config([{ key: "mod+n", command: "sidebar.toggle" }]), |
| 174 | + ); |
| 175 | + expect(harness.cmdN()).toBe("sidebar.toggle"); |
| 176 | + }); |
| 177 | +}); |
0 commit comments