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

ws.test.ts

236 lines8.6 KB

1import { assert, it } from "@effect/vitest";
2import {
3 ORCHESTRATION_PROTOCOL_VERSION,
4 type ServerConfig,
5 type ServerConfigStreamEvent,
6} from "@t3tools/contracts";
7import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
8import * as ConfigProvider from "effect/ConfigProvider";
9import * as Deferred from "effect/Deferred";
10import * as Duration from "effect/Duration";
11import * as Effect from "effect/Effect";
12import * as Fiber from "effect/Fiber";
13import * as FileSystem from "effect/FileSystem";
14import * as Layer from "effect/Layer";
15import * as Path from "effect/Path";
16import * as Queue from "effect/Queue";
17import * as Stream from "effect/Stream";
18import * as TestClock from "effect/testing/TestClock";
19import { ChildProcessSpawner } from "effect/process";
20​
21import * as ExternalLauncher from "./process/externalLauncher.ts";
22import {
23 hasCompatibleOrchestrationProtocol,
24 resolveAvailableEditorsForConfig,
25 shouldUseBoundedThreadSnapshot,
26 withLateEditorConfig,
27} from "./ws.ts";
28​
29it("accepts only the current orchestration protocol before websocket RPC setup", () => {
30 assert.isTrue(
31 hasCompatibleOrchestrationProtocol(
32 new URL(`https://host.test/ws?orchestrationProtocol=${ORCHESTRATION_PROTOCOL_VERSION}`),
33 ),
34 );
35 assert.isFalse(hasCompatibleOrchestrationProtocol(new URL("https://host.test/ws")));
36 assert.isFalse(
37 hasCompatibleOrchestrationProtocol(
38 new URL(`https://host.test/ws?orchestrationProtocol=${ORCHESTRATION_PROTOCOL_VERSION - 1}`),
39 ),
40 );
41});
42​
43it("keeps full thread snapshot fallback unless the client opts into bounded history", () => {
44 assert.isFalse(shouldUseBoundedThreadSnapshot({}));
45 assert.isFalse(shouldUseBoundedThreadSnapshot({ acceptBoundedSnapshot: false }));
46 assert.isTrue(shouldUseBoundedThreadSnapshot({ acceptBoundedSnapshot: true }));
47});
48​
49it.effect("does not block server config when editor discovery never resolves", () =>
50 Effect.gen(function* () {
51 const discoveryInterrupted = yield* Deferred.make<void>();
52 const responseFiber = yield* resolveAvailableEditorsForConfig(
53 Effect.never.pipe(
54 Effect.onInterrupt(() => Deferred.succeed(discoveryInterrupted, undefined)),
55 ),
56 ).pipe(Effect.forkChild);
57​
58 yield* TestClock.adjust(Duration.seconds(5));
59​
60 const availableEditors = yield* Fiber.join(responseFiber);
61 yield* Deferred.await(discoveryInterrupted);
62 assert.deepEqual(availableEditors, []);
63 }),
64);
65​
66// Only the fields the late-editor fold reads or rewrites.
67const snapshotConfig = (fields: Partial<ServerConfig>) =>
68 ({ availableEditors: [], settings: {}, ...fields }) as unknown as ServerConfig;
69​
70const settingsUpdated = (settings: object): ServerConfigStreamEvent => ({
71 version: 1,
72 type: "settingsUpdated",
73 payload: { settings: settings as ServerConfig["settings"] },
74});
75​
76it.effect("resends late editors without rolling back updates already sent", () =>
77 Effect.gen(function* () {
78 const settingsSent = yield* Deferred.make<void>();
79 const events = yield* withLateEditorConfig(
80 snapshotConfig({ settings: { enableProviderUpdateChecks: true } as never }),
81 Stream.make(settingsUpdated({ enableProviderUpdateChecks: false })),
82 {
83 resolveAvailableEditors: () => Effect.succeed(["file-manager"]),
84 // Holds the late snapshot until the settings change has gone out.
85 resolveFileManagerRevealKind: () =>
86 Deferred.await(settingsSent).pipe(Effect.as("file-explorer" as const)),
87 },
88 ).pipe(
89 Stream.tap((event) =>
90 event.type === "settingsUpdated" ? Deferred.succeed(settingsSent, undefined) : Effect.void,
91 ),
92 Stream.runCollect,
93 );
94​
95 const [first, second] = Array.from(events);
96 assert.equal(events.length, 2);
97 assert.equal(first?.type, "settingsUpdated");
98 assert.equal(second?.type, "snapshot");
99 if (second?.type === "snapshot") {
100 assert.deepEqual(second.config.availableEditors, ["file-manager"]);
101 assert.equal(second.config.shellRevealInFileManagerKind, "file-explorer");
102 assert.deepEqual(second.config.settings, { enableProviderUpdateChecks: false } as never);
103 }
104 }),
105);
106​
107it.effect("sends no late snapshot when the scan matches the snapshot", () =>
108 Effect.gen(function* () {
109 const events = yield* withLateEditorConfig(
110 snapshotConfig({ availableEditors: ["vscode"] }),
111 Stream.empty,
112 {
113 resolveAvailableEditors: () => Effect.succeed(["vscode"]),
114 resolveFileManagerRevealKind: () => Effect.succeed(undefined),
115 },
116 ).pipe(Stream.runCollect);
117​
118 assert.equal(events.length, 0);
119 }),
120);
121​
122it.effect("resends a file manager reveal kind that missed the snapshot", () =>
123 Effect.gen(function* () {
124 const events = yield* withLateEditorConfig(
125 snapshotConfig({ availableEditors: ["file-manager"] }),
126 Stream.empty,
127 {
128 resolveAvailableEditors: () => Effect.succeed(["file-manager"]),
129 resolveFileManagerRevealKind: () => Effect.succeed("file-explorer"),
130 },
131 ).pipe(Stream.runCollect);
132​
133 const [late] = Array.from(events);
134 assert.equal(events.length, 1);
135 assert.equal(late?.type, "snapshot");
136 if (late?.type === "snapshot") {
137 assert.equal(late.config.shellRevealInFileManagerKind, "file-explorer");
138 }
139 }),
140);
141​
142// The real launcher on Windows over a filesystem whose probes park until
143// released, like a host too busy to finish discovery inside the snapshot timeout.
144const makeParkedWindowsLauncher = Effect.gen(function* () {
145 const parkedProbes = yield* Queue.unbounded<void>();
146 const release = yield* Deferred.make<void>();
147 const launcher = yield* ExternalLauncher.make.pipe(
148 Effect.provide(
149 Layer.mergeAll(
150 FileSystem.layerNoop({
151 stat: () =>
152 Queue.offer(parkedProbes, undefined).pipe(
153 Effect.andThen(Deferred.await(release)),
154 Effect.as({ type: "File" } as FileSystem.File.Info),
155 ),
156 }),
157 Path.layer,
158 Layer.succeed(
159 ChildProcessSpawner.ChildProcessSpawner,
160 ChildProcessSpawner.make(() => Effect.die("unexpected spawn")),
161 ),
162 ),
163 ),
164 );
165 const onWindows = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
166 effect.pipe(
167 Effect.provideService(HostProcessPlatform, "win32"),
168 Effect.provide(
169 ConfigProvider.layer(
170 ConfigProvider.fromEnv({
171 env: { PATH: "C:\\t3-late-editors-test", PATHEXT: ".EXE" },
172 }),
173 ),
174 ),
175 );
176 return {
177 editors: {
178 resolveAvailableEditors: () => onWindows(launcher.resolveAvailableEditors()),
179 resolveFileManagerRevealKind: () => onWindows(launcher.resolveFileManagerRevealKind()),
180 },
181 probeParked: Queue.take(parkedProbes),
182 releaseProbes: Deferred.succeed(release, undefined),
183 };
184});
185​
186it.effect("recovers editors after a real scan outlasts the config timeout", () =>
187 Effect.gen(function* () {
188 const { editors, probeParked, releaseProbes } = yield* makeParkedWindowsLauncher;
189​
190 const snapshotFiber = yield* resolveAvailableEditorsForConfig(
191 editors.resolveAvailableEditors(),
192 ).pipe(Effect.forkChild);
193 yield* probeParked;
194 yield* TestClock.adjust(Duration.seconds(5));
195 const snapshotEditors = yield* Fiber.join(snapshotFiber);
196 assert.deepEqual(snapshotEditors, []);
197​
198 const lateFiber = yield* withLateEditorConfig(
199 snapshotConfig({ availableEditors: snapshotEditors }),
200 Stream.empty,
201 editors,
202 ).pipe(Stream.runCollect, Effect.forkChild);
203 yield* releaseProbes;
204​
205 const [late] = Array.from(yield* Fiber.join(lateFiber));
206 assert.equal(late?.type, "snapshot");
207 if (late?.type === "snapshot") {
208 assert.equal(late.config.availableEditors.includes("vscode"), true);
209 }
210 }).pipe(Effect.scoped),
211);
212​
213it.effect("recovers a reveal kind whose real probe outlasts the config timeout", () =>
214 Effect.gen(function* () {
215 const { editors, probeParked, releaseProbes } = yield* makeParkedWindowsLauncher;
216​
217 // The snapshot's bounded probe timed out: file manager, but no reveal kind.
218 const lateFiber = yield* withLateEditorConfig(
219 snapshotConfig({ availableEditors: ["file-manager"] }),
220 Stream.empty,
221 {
222 resolveAvailableEditors: () => Effect.succeed(["file-manager"]),
223 resolveFileManagerRevealKind: editors.resolveFileManagerRevealKind,
224 },
225 ).pipe(Stream.runCollect, Effect.forkChild);
226 yield* probeParked;
227 yield* TestClock.adjust(Duration.seconds(6));
228 yield* releaseProbes;
229​
230 const [late] = Array.from(yield* Fiber.join(lateFiber));
231 assert.equal(late?.type, "snapshot");
232 if (late?.type === "snapshot") {
233 assert.equal(late.config.shellRevealInFileManagerKind, "file-explorer");
234 }
235 }).pipe(Effect.scoped),
236);