Open History to see the last commit
1import { describe, it, assert } from "@effect/vitest";
2import {
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6 type ServerProviderUpdateState,
7} from "@t3tools/contracts";
8import { ServerProviderUpdateError } from "@t3tools/contracts";
9import * as Cause from "effect/Cause";
10import * as Effect from "effect/Effect";
11import * as Exit from "effect/Exit";
12import * as Fiber from "effect/Fiber";
13import * as Layer from "effect/Layer";
14import * as Ref from "effect/Ref";
15import * as Schema from "effect/Schema";
16import * as Sink from "effect/Sink";
17import * as Stream from "effect/Stream";
18import { HttpClient, HttpClientResponse } from "effect/unstable/http";
19import { ChildProcessSpawner } from "effect/unstable/process";
20import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
21import { SpawnExecutableResolution } from "@t3tools/shared/shell";
22
23import { ProviderRegistry, type ProviderRegistryShape } from "./Services/ProviderRegistry.ts";
24import * as ProviderMaintenanceRunner from "./providerMaintenanceRunner.ts";
25import {
26 makeProviderMaintenanceCapabilities,
27 ProviderVersionCache,
28 type ProviderMaintenanceCapabilities,
29} from "./providerMaintenance.ts";
30const isServerProviderUpdateError = Schema.is(ServerProviderUpdateError);
31
32const CODEX_DRIVER = ProviderDriverKind.make("codex");
33const CURSOR_DRIVER = ProviderDriverKind.make("cursor");
34const OPENCODE_DRIVER = ProviderDriverKind.make("opencode");
35const CODEX_INSTANCE_ID = ProviderInstanceId.make("codex");
36const CURSOR_INSTANCE_ID = ProviderInstanceId.make("cursor");
37const OPENCODE_INSTANCE_ID = ProviderInstanceId.make("opencode");
38const encoder = new TextEncoder();
39
40// Pin a non-win32 platform so `resolveSpawnCommand` is a no-op and the raw
41// `{ command, args }` assertions below hold deterministically on any host
42// (including Windows). Windows-specific resolution is covered by the dedicated
43// win32 case at the end of this suite.
44const NonWindowsPlatform = Layer.succeed(HostProcessPlatform, "linux");
45
46function lifecycleFor(provider: ProviderDriverKind): ProviderMaintenanceCapabilities {
47 if (provider === CURSOR_DRIVER) {
48 return makeProviderMaintenanceCapabilities({
49 provider,
50 packageName: null,
51 updateExecutable: "cursor-agent",
52 updateArgs: ["update"],
53 updateLockKey: "cursor-agent",
54 });
55 }
56 return makeProviderMaintenanceCapabilities({
57 provider,
58 packageName: provider === OPENCODE_DRIVER ? "opencode-ai" : "@openai/codex",
59 updateExecutable: "npm",
60 updateArgs:
61 provider === OPENCODE_DRIVER
62 ? ["install", "-g", "opencode-ai@latest"]
63 : ["install", "-g", "@openai/codex@latest"],
64 updateLockKey: "npm-global",
65 });
66}
67
68const baseProvider: ServerProvider = {
69 instanceId: CODEX_INSTANCE_ID,
70 driver: CODEX_DRIVER,
71 enabled: true,
72 installed: true,
73 version: null,
74 status: "ready",
75 auth: { status: "authenticated" },
76 checkedAt: "2026-04-10T00:00:00.000Z",
77 models: [],
78 slashCommands: [],
79 skills: [],
80};
81
82const baseCursorProvider: ServerProvider = {
83 ...baseProvider,
84 instanceId: CURSOR_INSTANCE_ID,
85 driver: CURSOR_DRIVER,
86};
87
88const baseOpenCodeProvider: ServerProvider = {
89 ...baseProvider,
90 instanceId: OPENCODE_INSTANCE_ID,
91 driver: OPENCODE_DRIVER,
92};
93
94const latestVersionHttpClient = (version: string) =>
95 Layer.succeed(
96 HttpClient.HttpClient,
97 HttpClient.make((request) =>
98 Effect.succeed(
99 HttpClientResponse.fromWeb(
100 request,
101 Response.json({ version }, { headers: { "content-type": "application/json" } }),
102 ),
103 ),
104 ),
105 );
106
107function mockHandle(result: {
108 readonly stdout?: string;
109 readonly stderr?: string;
110 readonly code?: number;
111 readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode>;
112}) {
113 return ChildProcessSpawner.makeHandle({
114 pid: ChildProcessSpawner.ProcessId(1),
115 exitCode: result.exitCode ?? Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)),
116 isRunning: Effect.succeed(false),
117 kill: () => Effect.void,
118 unref: Effect.succeed(Effect.void),
119 stdin: Sink.drain,
120 stdout: Stream.make(encoder.encode(result.stdout ?? "")),
121 stderr: Stream.make(encoder.encode(result.stderr ?? "")),
122 all: Stream.empty,
123 getInputFd: () => Sink.drain,
124 getOutputFd: () => Stream.empty,
125 });
126}
127
128function mockSpawnerLayer(
129 handler: (
130 command: string,
131 args: ReadonlyArray<string>,
132 ) => {
133 readonly stdout?: string;
134 readonly stderr?: string;
135 readonly code?: number;
136 readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode>;
137 },
138) {
139 return Layer.succeed(
140 ChildProcessSpawner.ChildProcessSpawner,
141 ChildProcessSpawner.make((command) => {
142 const childProcess = command as unknown as {
143 readonly command: string;
144 readonly args: ReadonlyArray<string>;
145 };
146 return Effect.succeed(mockHandle(handler(childProcess.command, childProcess.args)));
147 }),
148 );
149}
150
151function makeRegistry(
152 initialProviders: ServerProvider | ReadonlyArray<ServerProvider> = baseProvider,
153) {
154 return Effect.gen(function* () {
155 const providersRef = yield* Ref.make<ReadonlyArray<ServerProvider>>(
156 Array.isArray(initialProviders) ? initialProviders : [initialProviders],
157 );
158 const updateStatesRef = yield* Ref.make<ReadonlyArray<ServerProviderUpdateState>>([]);
159
160 const setProviderMaintenanceActionState = Effect.fn(
161 "providerMaintenanceRunner.test.setProviderMaintenanceActionState",
162 )(function* (input: {
163 readonly instanceId: ProviderInstanceId;
164 readonly action: "update";
165 readonly state: ServerProviderUpdateState | null;
166 }) {
167 const updateState = input.state;
168 if (updateState) {
169 yield* Ref.update(updateStatesRef, (states) => [...states, updateState]);
170 }
171 return yield* Ref.updateAndGet(providersRef, (providers) =>
172 providers.map((candidate) => {
173 if (candidate.instanceId !== input.instanceId) {
174 return candidate;
175 }
176 if (!updateState) {
177 const { updateState: _updateState, ...providerWithoutUpdateState } = candidate;
178 return providerWithoutUpdateState;
179 }
180 return {
181 ...candidate,
182 updateState,
183 };
184 }),
185 );
186 });
187
188 const registry: ProviderRegistryShape = {
189 getProviders: Ref.get(providersRef),
190 refresh: () => Ref.get(providersRef),
191 refreshInstance: () => Ref.get(providersRef),
192 refreshWorkspaceSnapshot: () => Ref.get(providersRef),
193 getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
194 Effect.succeed(lifecycleFor(provider)),
195 setProviderMaintenanceActionState,
196 streamChanges: Stream.empty,
197 };
198
199 return {
200 registry,
201 updateStatesRef,
202 };
203 });
204}
205
206const makeTestRunner = (registry: ProviderRegistryShape) =>
207 Effect.service(ProviderMaintenanceRunner.ProviderMaintenanceRunner).pipe(
208 Effect.provide(
209 ProviderMaintenanceRunner.layer.pipe(
210 Layer.provide(
211 Layer.mergeAll(
212 Layer.succeed(ProviderRegistry, registry),
213 Layer.succeed(ProviderVersionCache, new Map()),
214 ),
215 ),
216 ),
217 ),
218 );
219
220describe("providerMaintenanceRunner", () => {
221 it.effect("runs the allowlisted provider update command and records success", () => {
222 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
223 return Effect.gen(function* () {
224 const { registry, updateStatesRef } = yield* makeRegistry(baseCursorProvider);
225 const updater = yield* makeTestRunner(registry);
226
227 const result = yield* updater.updateProvider(CURSOR_DRIVER);
228 assert.deepStrictEqual(calls, [
229 {
230 command: "cursor-agent",
231 args: ["update"],
232 },
233 ]);
234 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
235 assert.deepStrictEqual(
236 (yield* Ref.get(updateStatesRef)).map((state) => state.status),
237 ["queued", "running", "succeeded"],
238 );
239 }).pipe(
240 Effect.provide(
241 Layer.mergeAll(
242 NonWindowsPlatform,
243 latestVersionHttpClient("0.0.0"),
244 mockSpawnerLayer((command, args) => {
245 calls.push({ command, args });
246 return { stdout: "updated" };
247 }),
248 ),
249 ),
250 );
251 });
252
253 it.effect("uses the resolved provider capabilities when choosing the update executable", () => {
254 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
255 return Effect.gen(function* () {
256 const { registry } = yield* makeRegistry({
257 ...baseProvider,
258 versionAdvisory: {
259 status: "behind_latest",
260 currentVersion: "2.0.14",
261 latestVersion: "2.1.123",
262 updateCommand: "bun i -g @anthropic-ai/claude-code@latest",
263 canUpdate: true,
264 checkedAt: "2026-04-30T12:00:00.000Z",
265 message: "Update available.",
266 },
267 });
268 const updater = yield* makeTestRunner({
269 ...registry,
270 getProviderMaintenanceCapabilitiesForInstance: () =>
271 Effect.succeed(
272 makeProviderMaintenanceCapabilities({
273 provider: CODEX_DRIVER,
274 packageName: "@openai/codex",
275 updateExecutable: "bun",
276 updateArgs: ["i", "-g", "@openai/codex@latest"],
277 updateLockKey: "bun-global",
278 }),
279 ),
280 });
281
282 yield* updater.updateProvider(CODEX_DRIVER);
283 assert.deepStrictEqual(calls, [
284 {
285 command: "bun",
286 args: ["i", "-g", "@openai/codex@latest"],
287 },
288 ]);
289 }).pipe(
290 Effect.provide(
291 Layer.mergeAll(
292 NonWindowsPlatform,
293 latestVersionHttpClient("0.0.0"),
294 mockSpawnerLayer((command, args) => {
295 calls.push({ command, args });
296 return { stdout: "updated" };
297 }),
298 ),
299 ),
300 );
301 });
302
303 it.effect(
304 "runs update commands through Effect ChildProcess when no test runner is injected",
305 () => {
306 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
307 return Effect.gen(function* () {
308 const { registry } = yield* makeRegistry(baseProvider);
309 const runner = yield* makeTestRunner(registry);
310
311 const result = yield* runner.updateProvider(CODEX_DRIVER);
312
313 assert.deepStrictEqual(calls, [
314 {
315 command: "npm",
316 args: ["install", "-g", "@openai/codex@latest"],
317 },
318 ]);
319 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
320 }).pipe(
321 Effect.provide(
322 Layer.mergeAll(
323 NonWindowsPlatform,
324 latestVersionHttpClient("0.0.0"),
325 mockSpawnerLayer((command, args) => {
326 calls.push({ command, args });
327 return { stdout: "updated" };
328 }),
329 ),
330 ),
331 );
332 },
333 );
334
335 it.effect("updates a single provider instance without touching sibling instances", () => {
336 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
337 return Effect.gen(function* () {
338 const personalInstanceId = ProviderInstanceId.make("codex_personal");
339 const workInstanceId = ProviderInstanceId.make("codex_work");
340 const refreshedInstanceIds: Array<ProviderInstanceId> = [];
341 const { registry } = yield* makeRegistry([
342 {
343 ...baseProvider,
344 instanceId: personalInstanceId,
345 version: "0.124.0-alpha.3",
346 },
347 {
348 ...baseProvider,
349 instanceId: workInstanceId,
350 version: "0.124.0-alpha.3",
351 },
352 ]);
353 const updater = yield* makeTestRunner({
354 ...registry,
355 getProviderMaintenanceCapabilitiesForInstance: (instanceId, provider) =>
356 Effect.succeed(
357 makeProviderMaintenanceCapabilities({
358 provider,
359 packageName: "@openai/codex-instance-test",
360 updateExecutable: "vp",
361 updateArgs: ["i", "-g", "@openai/codex"],
362 updateLockKey: "vite-plus-global",
363 }),
364 ).pipe(
365 Effect.tap(() => Effect.sync(() => assert.strictEqual(instanceId, personalInstanceId))),
366 ),
367 refreshInstance: (instanceId) =>
368 registry.refreshInstance(instanceId).pipe(
369 Effect.tap(() =>
370 Effect.sync(() => {
371 refreshedInstanceIds.push(instanceId);
372 }),
373 ),
374 ),
375 });
376
377 const result = yield* updater.updateProvider({
378 provider: CODEX_DRIVER,
379 instanceId: personalInstanceId,
380 });
381
382 assert.deepStrictEqual(calls, [
383 {
384 command: "vp",
385 args: ["i", "-g", "@openai/codex"],
386 },
387 ]);
388 assert.deepStrictEqual(refreshedInstanceIds, [personalInstanceId]);
389 assert.strictEqual(result.providers[0]?.instanceId, personalInstanceId);
390 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
391 assert.strictEqual(result.providers[1]?.instanceId, workInstanceId);
392 assert.strictEqual(result.providers[1]?.updateState, undefined);
393 }).pipe(
394 Effect.provide(
395 Layer.mergeAll(
396 NonWindowsPlatform,
397 latestVersionHttpClient("0.124.0-alpha.3"),
398 mockSpawnerLayer((command, args) => {
399 calls.push({ command, args });
400 return { stdout: "updated" };
401 }),
402 ),
403 ),
404 );
405 });
406
407 it.effect("records command failure output in provider update state", () =>
408 Effect.gen(function* () {
409 const { registry } = yield* makeRegistry();
410 const updater = yield* makeTestRunner(registry);
411
412 const result = yield* updater.updateProvider(CODEX_DRIVER);
413 const updateState = result.providers[0]?.updateState;
414
415 assert.strictEqual(updateState?.status, "failed");
416 assert.strictEqual(updateState?.message, "Update command exited with code 1.");
417 assert.include(updateState?.output ?? "", "permission denied");
418 }).pipe(
419 Effect.provide(
420 Layer.mergeAll(
421 NonWindowsPlatform,
422 latestVersionHttpClient("0.0.0"),
423 mockSpawnerLayer(() => ({ stderr: "permission denied", code: 1 })),
424 ),
425 ),
426 ),
427 );
428
429 it.effect(
430 "marks successful commands as unchanged when the refreshed provider is still outdated",
431 () =>
432 Effect.gen(function* () {
433 const { registry } = yield* makeRegistry({
434 ...baseProvider,
435 installed: true,
436 version: "0.1.0",
437 });
438 const updater = yield* makeTestRunner(registry);
439
440 const result = yield* updater.updateProvider(CODEX_DRIVER);
441
442 assert.strictEqual(result.providers[0]?.updateState?.status, "unchanged");
443 assert.include(result.providers[0]?.updateState?.message ?? "", "still detects");
444 }).pipe(
445 Effect.provide(
446 Layer.mergeAll(
447 NonWindowsPlatform,
448 latestVersionHttpClient("9.9.9"),
449 mockSpawnerLayer(() => ({ stdout: "updated" })),
450 ),
451 ),
452 ),
453 );
454
455 it.effect("prevents concurrent updates for the same provider", () => {
456 const startedLatch: { resolve: () => void } = { resolve: () => {} };
457 const releaseLatch: { resolve: () => void } = { resolve: () => {} };
458 const started = new Promise<void>((resolve) => {
459 startedLatch.resolve = resolve;
460 });
461 const release = new Promise<void>((resolve) => {
462 releaseLatch.resolve = resolve;
463 });
464 return Effect.gen(function* () {
465 const { registry } = yield* makeRegistry();
466 const updater = yield* makeTestRunner(registry);
467
468 const first = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
469 yield* Effect.promise(() => started);
470
471 const second = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.exit);
472 assert.strictEqual(Exit.isFailure(second), true);
473 if (Exit.isFailure(second)) {
474 const error = Cause.squash(second.cause);
475 assert.strictEqual(isServerProviderUpdateError(error), true);
476 if (isServerProviderUpdateError(error)) {
477 assert.include(error.reason, "already running");
478 }
479 }
480
481 releaseLatch.resolve();
482 yield* Fiber.join(first);
483 }).pipe(
484 Effect.provide(
485 Layer.mergeAll(
486 NonWindowsPlatform,
487 latestVersionHttpClient("0.0.0"),
488 mockSpawnerLayer(() => {
489 startedLatch.resolve();
490 return {
491 stdout: "updated",
492 exitCode: Effect.promise(() => release).pipe(
493 Effect.as(ChildProcessSpawner.ExitCode(0)),
494 ),
495 };
496 }),
497 ),
498 ),
499 );
500 });
501
502 it.effect("serializes different providers that share the same update lock key", () => {
503 const firstStartedLatch: { resolve: () => void } = { resolve: () => {} };
504 const releaseFirstLatch: { resolve: () => void } = { resolve: () => {} };
505 const firstStarted = new Promise<void>((resolve) => {
506 firstStartedLatch.resolve = resolve;
507 });
508 const releaseFirst = new Promise<void>((resolve) => {
509 releaseFirstLatch.resolve = resolve;
510 });
511 const calls: Array<string> = [];
512 return Effect.gen(function* () {
513 const { registry } = yield* makeRegistry([baseProvider, baseOpenCodeProvider]);
514 const updater = yield* makeTestRunner({
515 ...registry,
516 getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
517 Effect.succeed(
518 makeProviderMaintenanceCapabilities({
519 provider,
520 packageName: provider === OPENCODE_DRIVER ? "opencode-ai" : "@openai/codex",
521 updateExecutable: "npm",
522 updateArgs:
523 provider === OPENCODE_DRIVER
524 ? ["install", "-g", "opencode-ai@latest"]
525 : ["install", "-g", "@openai/codex@latest"],
526 updateLockKey: "npm-global",
527 }),
528 ),
529 });
530
531 const first = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
532 yield* Effect.promise(() => firstStarted);
533
534 const second = yield* updater.updateProvider(OPENCODE_DRIVER).pipe(Effect.forkScoped);
535 let providersWhileQueued: ReadonlyArray<ServerProvider> = [];
536 for (let attempt = 0; attempt < 20; attempt += 1) {
537 providersWhileQueued = yield* registry.getProviders;
538 const queuedStatus = providersWhileQueued.find(
539 (provider) => provider.instanceId === OPENCODE_INSTANCE_ID,
540 )?.updateState?.status;
541 if (queuedStatus === "queued") {
542 break;
543 }
544 yield* Effect.yieldNow;
545 }
546 assert.deepStrictEqual(calls, ["install -g @openai/codex@latest"]);
547 assert.strictEqual(
548 providersWhileQueued.find((provider) => provider.instanceId === OPENCODE_INSTANCE_ID)
549 ?.updateState?.status,
550 "queued",
551 );
552
553 releaseFirstLatch.resolve();
554 yield* Fiber.join(first);
555 yield* Fiber.join(second);
556 assert.deepStrictEqual(calls, [
557 "install -g @openai/codex@latest",
558 "install -g opencode-ai@latest",
559 ]);
560 }).pipe(
561 Effect.provide(
562 Layer.mergeAll(
563 NonWindowsPlatform,
564 latestVersionHttpClient("0.0.0"),
565 mockSpawnerLayer((_command, args) => {
566 calls.push(args.join(" "));
567 if (calls.length === 1) {
568 firstStartedLatch.resolve();
569 return {
570 stdout: "updated",
571 exitCode: Effect.promise(() => releaseFirst).pipe(
572 Effect.as(ChildProcessSpawner.ExitCode(0)),
573 ),
574 };
575 }
576 return { stdout: "updated" };
577 }),
578 ),
579 ),
580 );
581 });
582
583 it.effect("accepts arbitrary driver-provided update lock keys", () => {
584 const calls: Array<string> = [];
585 return Effect.gen(function* () {
586 const { registry } = yield* makeRegistry(baseProvider);
587 const updater = yield* makeTestRunner({
588 ...registry,
589 getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
590 Effect.succeed(
591 makeProviderMaintenanceCapabilities({
592 provider,
593 packageName: "@openai/codex",
594 updateExecutable: "npm",
595 updateArgs: ["install", "-g", "@openai/codex@latest"],
596 updateLockKey: "unknown-lock-key",
597 }),
598 ),
599 });
600
601 const result = yield* updater.updateProvider(CODEX_DRIVER);
602 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
603 assert.deepStrictEqual(calls, ["install -g @openai/codex@latest"]);
604 }).pipe(
605 Effect.provide(
606 Layer.mergeAll(
607 NonWindowsPlatform,
608 latestVersionHttpClient("0.0.0"),
609 mockSpawnerLayer((_command, args) => {
610 calls.push(args.join(" "));
611 return { stdout: "updated" };
612 }),
613 ),
614 ),
615 );
616 });
617
618 it.effect(
619 "releases the running-provider marker when interrupted after queuing but before the lock run starts",
620 () =>
621 Effect.gen(function* () {
622 const { registry } = yield* makeRegistry(baseProvider);
623 let blockQueuedState = true;
624 const queuedStateWrittenLatch: { resolve: () => void } = { resolve: () => {} };
625 const releaseQueuedStateLatch: { resolve: () => void } = { resolve: () => {} };
626 const queuedStateWritten = new Promise<void>((resolve) => {
627 queuedStateWrittenLatch.resolve = resolve;
628 });
629 const releaseQueuedState = new Promise<void>((resolve) => {
630 releaseQueuedStateLatch.resolve = resolve;
631 });
632
633 const updater = yield* makeTestRunner({
634 ...registry,
635 setProviderMaintenanceActionState: Effect.fn(
636 "providerMaintenanceRunner.test.blockQueuedState",
637 )(function* (input) {
638 const providers = yield* registry.setProviderMaintenanceActionState(input);
639 if (input.state?.status === "queued" && blockQueuedState) {
640 queuedStateWrittenLatch.resolve();
641 yield* Effect.promise(() => releaseQueuedState);
642 }
643 return providers;
644 }),
645 });
646
647 const first = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
648 yield* Effect.promise(() => queuedStateWritten);
649 blockQueuedState = false;
650
651 yield* Fiber.interrupt(first);
652 releaseQueuedStateLatch.resolve();
653
654 const second = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.exit);
655 assert.strictEqual(Exit.isSuccess(second), true);
656 if (Exit.isSuccess(second)) {
657 assert.strictEqual(second.value.providers[0]?.updateState?.status, "succeeded");
658 }
659 }).pipe(
660 Effect.provide(
661 Layer.mergeAll(
662 NonWindowsPlatform,
663 latestVersionHttpClient("0.0.0"),
664 mockSpawnerLayer(() => ({ stdout: "updated" })),
665 ),
666 ),
667 ),
668 );
669
670 it.effect("resolves npm to a .cmd shim and routes through the shell on win32", () => {
671 const captured: Array<{
672 readonly command: string;
673 readonly args: ReadonlyArray<string>;
674 readonly shell: boolean | string | undefined;
675 }> = [];
676 return Effect.gen(function* () {
677 const { registry } = yield* makeRegistry(baseProvider);
678 const runner = yield* makeTestRunner(registry);
679
680 const result = yield* runner.updateProvider(CODEX_DRIVER);
681
682 // On win32, resolveSpawnCommand resolves `npm` to the `.cmd` shim and
683 // routes the spawn through cmd.exe (shell: true), escaping every arg.
684 assert.strictEqual(captured.length, 1);
685 const call = captured[0];
686 assert.ok(call, "expected the spawner to be invoked once");
687 // The resolved command is the escaped `.cmd` path. Asserting the precise
688 // escaped string is brittle, so verify it carries the resolved shim and
689 // that shell mode was used.
690 assert.match(call.command, /npm\.cmd/i);
691 assert.strictEqual(call.shell, true);
692 // Args are escaped for cmd.exe shell mode (each quoted) but still carry
693 // the original install command (`install -g @openai/codex@latest`) in order.
694 assert.strictEqual(call.args.length, 3);
695 assert.match(call.args[0] ?? "", /install/);
696 assert.match(call.args[1] ?? "", /-g/);
697 assert.match(call.args[2] ?? "", /@openai\/codex@latest/);
698 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
699 }).pipe(
700 Effect.provide(
701 Layer.mergeAll(
702 Layer.succeed(HostProcessPlatform, "win32"),
703 Layer.succeed(HostProcessEnvironment, {
704 PATH: "C:\\fake\\npm",
705 PATHEXT: ".COM;.EXE;.BAT;.CMD",
706 }),
707 Layer.succeed(SpawnExecutableResolution, (command) =>
708 command === "npm" ? "C:\\fake\\npm\\npm.cmd" : undefined,
709 ),
710 latestVersionHttpClient("0.0.0"),
711 Layer.succeed(
712 ChildProcessSpawner.ChildProcessSpawner,
713 ChildProcessSpawner.make((command) => {
714 const childProcess = command as unknown as {
715 readonly command: string;
716 readonly args: ReadonlyArray<string>;
717 readonly options: { readonly shell?: boolean | string | undefined };
718 };
719 captured.push({
720 command: childProcess.command,
721 args: childProcess.args,
722 shell: childProcess.options.shell,
723 });
724 return Effect.succeed(mockHandle({ stdout: "updated" }));
725 }),
726 ),
727 ),
728 ),
729 );
730 });
731});