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

server.test.ts

166 lines6.1 KB

1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import { EnvironmentId, type KeybindingRule, type ServerConfig } from "@t3tools/contracts";
3import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
4import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
5import { afterEach, describe, expect, it, vi } from "vite-plus/test";
6​
7import { resolveShortcutCommand } from "../keybindings";
8import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
9import {
10 createShortcutKeybindingsAtom,
11 primaryRelayTracingAllowedAtom,
12 serverEnvironment,
13} from "./server";
14import { environmentSession } from "./session";
15​
16const PRIMARY = EnvironmentId.make("primary");
17const A = EnvironmentId.make("a");
18const B = EnvironmentId.make("b");
19​
20// The reporter's swap: Cmd+N opens a local thread instead of a regular one.
21const SWAPPED: KeybindingRule[] = [{ key: "mod+n", command: "chat.newLocal" }];
22const STALE: KeybindingRule[] = [{ key: "mod+n", command: "chat.newWithoutProject" }];
23​
24function config(rules: readonly KeybindingRule[], relayTracingAllowed?: boolean) {
25 return {
26 keybindings: compileResolvedKeybindingsConfig(rules),
27 providers: [],
28 observability: { relayTracingAllowed },
29 } as unknown as ServerConfig;
31​
32function environment(phase: EnvironmentConnectionPhase, serverConfig: ServerConfig | null) {
33 return { connection: { phase, error: null, traceId: null }, serverConfig };
35​
36type Environment = ReturnType<typeof environment>;
37​
38function makeHarness(input: {
39 primaryEnvironmentId?: EnvironmentId | null;
40 primaryConfig?: ServerConfig | null;
41 environments?: ReadonlyArray<readonly [EnvironmentId, Environment]>;
42}) {
43 const primaryEnvironmentIdAtom = Atom.make(input.primaryEnvironmentId ?? null);
44 const primaryConfigAtom = Atom.make(input.primaryConfig ?? null);
45 const environmentsAtom = Atom.make<ReadonlyMap<EnvironmentId, Environment>>(
46 new Map(input.environments ?? []),
47 );
48 const keybindingsAtom = createShortcutKeybindingsAtom({
49 primaryEnvironmentIdAtom,
50 primaryConfigAtom,
51 environmentsAtom,
52 });
53 const registry = AtomRegistry.make();
54 registries.push(registry);
55 // Keep the atom mounted, like the shortcut handlers do.
56 registry.subscribe(keybindingsAtom, () => {});
57 return {
58 registry,
59 primaryEnvironmentIdAtom,
60 primaryConfigAtom,
61 environmentsAtom,
62 keybindings: () => registry.get(keybindingsAtom),
63 cmdN: () =>
64 resolveShortcutCommand(
65 { key: "n", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false },
66 registry.get(keybindingsAtom),
67 { platform: "MacIntel" },
68 ),
69 };
71​
72const registries: AtomRegistry.AtomRegistry[] = [];
73afterEach(() => {
74 for (const registry of registries.splice(0)) registry.dispose();
75});
76​
77describe("createShortcutKeybindingsAtom", () => {
78 it("updates shortcuts from the first connected, configured environment without a primary", () => {
79 const harness = makeHarness({});
80 const aConfig = config(STALE);
81 const bConfig = config(SWAPPED);
82 const edited = config([{ key: "mod+n", command: "sidebar.toggle" }]);
83 expect(harness.cmdN()).toBe("chat.new");
84​
85 // One mounted consumer follows config loading, edits, disconnects and reconnects.
86 for (const [a, b, command] of [
87 [environment("offline", aConfig), environment("connected", bConfig), "chat.newLocal"],
88 [environment("connected", null), environment("connected", bConfig), "chat.newLocal"],
89 [
90 environment("connected", aConfig),
91 environment("connected", bConfig),
92 "chat.newWithoutProject",
93 ],
94 [environment("connected", edited), environment("connected", bConfig), "sidebar.toggle"],
95 [environment("offline", edited), environment("connected", bConfig), "chat.newLocal"],
96 [environment("offline", edited), environment("offline", bConfig), "chat.new"],
97 [environment("reconnecting", edited), environment("offline", bConfig), "chat.new"],
98 [
99 environment("connected", aConfig),
100 environment("offline", bConfig),
101 "chat.newWithoutProject",
102 ],
103 ] as const) {
104 harness.registry.set(
105 harness.environmentsAtom,
106 new Map([
107 [A, a],
108 [B, b],
109 ]),
110 );
111 expect(harness.cmdN()).toBe(command);
112 }
113 });
114​
115 it("uses the primary config or defaults until the primary is removed", () => {
116 const harness = makeHarness({
117 environments: [[B, environment("connected", config(SWAPPED))]],
118 });
119 expect(harness.cmdN()).toBe("chat.newLocal");
120​
121 harness.registry.set(harness.primaryEnvironmentIdAtom, PRIMARY);
122 expect(harness.cmdN()).toBe("chat.new");
123​
124 harness.registry.set(harness.primaryConfigAtom, config(STALE));
125 expect(harness.cmdN()).toBe("chat.newWithoutProject");
126​
127 harness.registry.set(harness.primaryConfigAtom, null);
128 expect(harness.cmdN()).toBe("chat.new");
129​
130 harness.registry.set(harness.primaryEnvironmentIdAtom, null);
131 expect(harness.cmdN()).toBe("chat.newLocal");
132 });
133});
134​
135it("clears relay tracing permission when the primary session ends", () => {
136 const allowed = config([], true);
137 const sessionConfig = Atom.make<ServerConfig | null>(allowed);
138 using _session = vi
139 .spyOn(environmentSession, "initialConfigValueAtom")
140 .mockReturnValue(sessionConfig);
141 // The previous session's config stream can retain its last live value across disconnect.
142 const lastProjection = serverEnvironment.configProjection({ environmentId: PRIMARY, input: {} });
143 const registry = AtomRegistry.make({
144 initialValues: [
145 [primaryEnvironmentIdAtom, PRIMARY],
146 [
147 lastProjection,
148 AsyncResult.success({
149 config: allowed,
150 latestEvent: { type: "snapshot", config: allowed },
151 source: "live",
152 }),
153 ],
154 ],
155 });
156 registries.push(registry);
157 const answers: Array<boolean | null> = [];
158 registry.subscribe(primaryRelayTracingAllowedAtom, (answer) => answers.push(answer), {
159 immediate: true,
160 });
161​
162 registry.set(sessionConfig, null);
163 registry.set(sessionConfig, config([], false));
164​
165 expect(answers).toEqual([true, null, false]);
166});