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 getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
193 Effect.succeed(lifecycleFor(provider)),
194 setProviderMaintenanceActionState,
195 streamChanges: Stream.empty,
196 };
197
198 return {
199 registry,
200 updateStatesRef,
201 };
202 });
203}
204
205const makeTestRunner = (registry: ProviderRegistryShape) =>
206 Effect.service(ProviderMaintenanceRunner.ProviderMaintenanceRunner).pipe(
207 Effect.provide(
208 ProviderMaintenanceRunner.layer.pipe(
209 Layer.provide(
210 Layer.mergeAll(
211 Layer.succeed(ProviderRegistry, registry),
212 Layer.succeed(ProviderVersionCache, new Map()),
213 ),
214 ),
215 ),
216 ),
217 );
218
219describe("providerMaintenanceRunner", () => {
220 it.effect("runs the allowlisted provider update command and records success", () => {
221 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
222 return Effect.gen(function* () {
223 const { registry, updateStatesRef } = yield* makeRegistry(baseCursorProvider);
224 const updater = yield* makeTestRunner(registry);
225
226 const result = yield* updater.updateProvider(CURSOR_DRIVER);
227 assert.deepStrictEqual(calls, [
228 {
229 command: "cursor-agent",
230 args: ["update"],
231 },
232 ]);
233 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
234 assert.deepStrictEqual(
235 (yield* Ref.get(updateStatesRef)).map((state) => state.status),
236 ["queued", "running", "succeeded"],
237 );
238 }).pipe(
239 Effect.provide(
240 Layer.mergeAll(
241 NonWindowsPlatform,
242 latestVersionHttpClient("0.0.0"),
243 mockSpawnerLayer((command, args) => {
244 calls.push({ command, args });
245 return { stdout: "updated" };
246 }),
247 ),
248 ),
249 );
250 });
251
252 it.effect("uses the resolved provider capabilities when choosing the update executable", () => {
253 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
254 return Effect.gen(function* () {
255 const { registry } = yield* makeRegistry({
256 ...baseProvider,
257 versionAdvisory: {
258 status: "behind_latest",
259 currentVersion: "2.0.14",
260 latestVersion: "2.1.123",
261 updateCommand: "bun i -g @anthropic-ai/claude-code@latest",
262 canUpdate: true,
263 checkedAt: "2026-04-30T12:00:00.000Z",
264 message: "Update available.",
265 },
266 });
267 const updater = yield* makeTestRunner({
268 ...registry,
269 getProviderMaintenanceCapabilitiesForInstance: () =>
270 Effect.succeed(
271 makeProviderMaintenanceCapabilities({
272 provider: CODEX_DRIVER,
273 packageName: "@openai/codex",
274 updateExecutable: "bun",
275 updateArgs: ["i", "-g", "@openai/codex@latest"],
276 updateLockKey: "bun-global",
277 }),
278 ),
279 });
280
281 yield* updater.updateProvider(CODEX_DRIVER);
282 assert.deepStrictEqual(calls, [
283 {
284 command: "bun",
285 args: ["i", "-g", "@openai/codex@latest"],
286 },
287 ]);
288 }).pipe(
289 Effect.provide(
290 Layer.mergeAll(
291 NonWindowsPlatform,
292 latestVersionHttpClient("0.0.0"),
293 mockSpawnerLayer((command, args) => {
294 calls.push({ command, args });
295 return { stdout: "updated" };
296 }),
297 ),
298 ),
299 );
300 });
301
302 it.effect(
303 "runs update commands through Effect ChildProcess when no test runner is injected",
304 () => {
305 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
306 return Effect.gen(function* () {
307 const { registry } = yield* makeRegistry(baseProvider);
308 const runner = yield* makeTestRunner(registry);
309
310 const result = yield* runner.updateProvider(CODEX_DRIVER);
311
312 assert.deepStrictEqual(calls, [
313 {
314 command: "npm",
315 args: ["install", "-g", "@openai/codex@latest"],
316 },
317 ]);
318 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
319 }).pipe(
320 Effect.provide(
321 Layer.mergeAll(
322 NonWindowsPlatform,
323 latestVersionHttpClient("0.0.0"),
324 mockSpawnerLayer((command, args) => {
325 calls.push({ command, args });
326 return { stdout: "updated" };
327 }),
328 ),
329 ),
330 );
331 },
332 );
333
334 it.effect("updates a single provider instance without touching sibling instances", () => {
335 const calls: Array<{ command: string; args: ReadonlyArray<string> }> = [];
336 return Effect.gen(function* () {
337 const personalInstanceId = ProviderInstanceId.make("codex_personal");
338 const workInstanceId = ProviderInstanceId.make("codex_work");
339 const refreshedInstanceIds: Array<ProviderInstanceId> = [];
340 const { registry } = yield* makeRegistry([
341 {
342 ...baseProvider,
343 instanceId: personalInstanceId,
344 version: "0.124.0-alpha.3",
345 },
346 {
347 ...baseProvider,
348 instanceId: workInstanceId,
349 version: "0.124.0-alpha.3",
350 },
351 ]);
352 const updater = yield* makeTestRunner({
353 ...registry,
354 getProviderMaintenanceCapabilitiesForInstance: (instanceId, provider) =>
355 Effect.succeed(
356 makeProviderMaintenanceCapabilities({
357 provider,
358 packageName: "@openai/codex-instance-test",
359 updateExecutable: "vp",
360 updateArgs: ["i", "-g", "@openai/codex"],
361 updateLockKey: "vite-plus-global",
362 }),
363 ).pipe(
364 Effect.tap(() => Effect.sync(() => assert.strictEqual(instanceId, personalInstanceId))),
365 ),
366 refreshInstance: (instanceId) =>
367 registry.refreshInstance(instanceId).pipe(
368 Effect.tap(() =>
369 Effect.sync(() => {
370 refreshedInstanceIds.push(instanceId);
371 }),
372 ),
373 ),
374 });
375
376 const result = yield* updater.updateProvider({
377 provider: CODEX_DRIVER,
378 instanceId: personalInstanceId,
379 });
380
381 assert.deepStrictEqual(calls, [
382 {
383 command: "vp",
384 args: ["i", "-g", "@openai/codex"],
385 },
386 ]);
387 assert.deepStrictEqual(refreshedInstanceIds, [personalInstanceId]);
388 assert.strictEqual(result.providers[0]?.instanceId, personalInstanceId);
389 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
390 assert.strictEqual(result.providers[1]?.instanceId, workInstanceId);
391 assert.strictEqual(result.providers[1]?.updateState, undefined);
392 }).pipe(
393 Effect.provide(
394 Layer.mergeAll(
395 NonWindowsPlatform,
396 latestVersionHttpClient("0.124.0-alpha.3"),
397 mockSpawnerLayer((command, args) => {
398 calls.push({ command, args });
399 return { stdout: "updated" };
400 }),
401 ),
402 ),
403 );
404 });
405
406 it.effect("records command failure output in provider update state", () =>
407 Effect.gen(function* () {
408 const { registry } = yield* makeRegistry();
409 const updater = yield* makeTestRunner(registry);
410
411 const result = yield* updater.updateProvider(CODEX_DRIVER);
412 const updateState = result.providers[0]?.updateState;
413
414 assert.strictEqual(updateState?.status, "failed");
415 assert.strictEqual(updateState?.message, "Update command exited with code 1.");
416 assert.include(updateState?.output ?? "", "permission denied");
417 }).pipe(
418 Effect.provide(
419 Layer.mergeAll(
420 NonWindowsPlatform,
421 latestVersionHttpClient("0.0.0"),
422 mockSpawnerLayer(() => ({ stderr: "permission denied", code: 1 })),
423 ),
424 ),
425 ),
426 );
427
428 it.effect(
429 "marks successful commands as unchanged when the refreshed provider is still outdated",
430 () =>
431 Effect.gen(function* () {
432 const { registry } = yield* makeRegistry({
433 ...baseProvider,
434 installed: true,
435 version: "0.1.0",
436 });
437 const updater = yield* makeTestRunner(registry);
438
439 const result = yield* updater.updateProvider(CODEX_DRIVER);
440
441 assert.strictEqual(result.providers[0]?.updateState?.status, "unchanged");
442 assert.include(result.providers[0]?.updateState?.message ?? "", "still detects");
443 }).pipe(
444 Effect.provide(
445 Layer.mergeAll(
446 NonWindowsPlatform,
447 latestVersionHttpClient("9.9.9"),
448 mockSpawnerLayer(() => ({ stdout: "updated" })),
449 ),
450 ),
451 ),
452 );
453
454 it.effect("prevents concurrent updates for the same provider", () => {
455 const startedLatch: { resolve: () => void } = { resolve: () => {} };
456 const releaseLatch: { resolve: () => void } = { resolve: () => {} };
457 const started = new Promise<void>((resolve) => {
458 startedLatch.resolve = resolve;
459 });
460 const release = new Promise<void>((resolve) => {
461 releaseLatch.resolve = resolve;
462 });
463 return Effect.gen(function* () {
464 const { registry } = yield* makeRegistry();
465 const updater = yield* makeTestRunner(registry);
466
467 const first = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
468 yield* Effect.promise(() => started);
469
470 const second = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.exit);
471 assert.strictEqual(Exit.isFailure(second), true);
472 if (Exit.isFailure(second)) {
473 const error = Cause.squash(second.cause);
474 assert.strictEqual(isServerProviderUpdateError(error), true);
475 if (isServerProviderUpdateError(error)) {
476 assert.include(error.reason, "already running");
477 }
478 }
479
480 releaseLatch.resolve();
481 yield* Fiber.join(first);
482 }).pipe(
483 Effect.provide(
484 Layer.mergeAll(
485 NonWindowsPlatform,
486 latestVersionHttpClient("0.0.0"),
487 mockSpawnerLayer(() => {
488 startedLatch.resolve();
489 return {
490 stdout: "updated",
491 exitCode: Effect.promise(() => release).pipe(
492 Effect.as(ChildProcessSpawner.ExitCode(0)),
493 ),
494 };
495 }),
496 ),
497 ),
498 );
499 });
500
501 it.effect("serializes different providers that share the same update lock key", () => {
502 const firstStartedLatch: { resolve: () => void } = { resolve: () => {} };
503 const releaseFirstLatch: { resolve: () => void } = { resolve: () => {} };
504 const firstStarted = new Promise<void>((resolve) => {
505 firstStartedLatch.resolve = resolve;
506 });
507 const releaseFirst = new Promise<void>((resolve) => {
508 releaseFirstLatch.resolve = resolve;
509 });
510 const calls: Array<string> = [];
511 return Effect.gen(function* () {
512 const { registry } = yield* makeRegistry([baseProvider, baseOpenCodeProvider]);
513 const updater = yield* makeTestRunner({
514 ...registry,
515 getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
516 Effect.succeed(
517 makeProviderMaintenanceCapabilities({
518 provider,
519 packageName: provider === OPENCODE_DRIVER ? "opencode-ai" : "@openai/codex",
520 updateExecutable: "npm",
521 updateArgs:
522 provider === OPENCODE_DRIVER
523 ? ["install", "-g", "opencode-ai@latest"]
524 : ["install", "-g", "@openai/codex@latest"],
525 updateLockKey: "npm-global",
526 }),
527 ),
528 });
529
530 const first = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
531 yield* Effect.promise(() => firstStarted);
532
533 const second = yield* updater.updateProvider(OPENCODE_DRIVER).pipe(Effect.forkScoped);
534 let providersWhileQueued: ReadonlyArray<ServerProvider> = [];
535 for (let attempt = 0; attempt < 20; attempt += 1) {
536 providersWhileQueued = yield* registry.getProviders;
537 const queuedStatus = providersWhileQueued.find(
538 (provider) => provider.instanceId === OPENCODE_INSTANCE_ID,
539 )?.updateState?.status;
540 if (queuedStatus === "queued") {
541 break;
542 }
543 yield* Effect.yieldNow;
544 }
545 assert.deepStrictEqual(calls, ["install -g @openai/codex@latest"]);
546 assert.strictEqual(
547 providersWhileQueued.find((provider) => provider.instanceId === OPENCODE_INSTANCE_ID)
548 ?.updateState?.status,
549 "queued",
550 );
551
552 releaseFirstLatch.resolve();
553 yield* Fiber.join(first);
554 yield* Fiber.join(second);
555 assert.deepStrictEqual(calls, [
556 "install -g @openai/codex@latest",
557 "install -g opencode-ai@latest",
558 ]);
559 }).pipe(
560 Effect.provide(
561 Layer.mergeAll(
562 NonWindowsPlatform,
563 latestVersionHttpClient("0.0.0"),
564 mockSpawnerLayer((_command, args) => {
565 calls.push(args.join(" "));
566 if (calls.length === 1) {
567 firstStartedLatch.resolve();
568 return {
569 stdout: "updated",
570 exitCode: Effect.promise(() => releaseFirst).pipe(
571 Effect.as(ChildProcessSpawner.ExitCode(0)),
572 ),
573 };
574 }
575 return { stdout: "updated" };
576 }),
577 ),
578 ),
579 );
580 });
581
582 it.effect("accepts arbitrary driver-provided update lock keys", () => {
583 const calls: Array<string> = [];
584 return Effect.gen(function* () {
585 const { registry } = yield* makeRegistry(baseProvider);
586 const updater = yield* makeTestRunner({
587 ...registry,
588 getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
589 Effect.succeed(
590 makeProviderMaintenanceCapabilities({
591 provider,
592 packageName: "@openai/codex",
593 updateExecutable: "npm",
594 updateArgs: ["install", "-g", "@openai/codex@latest"],
595 updateLockKey: "unknown-lock-key",
596 }),
597 ),
598 });
599
600 const result = yield* updater.updateProvider(CODEX_DRIVER);
601 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
602 assert.deepStrictEqual(calls, ["install -g @openai/codex@latest"]);
603 }).pipe(
604 Effect.provide(
605 Layer.mergeAll(
606 NonWindowsPlatform,
607 latestVersionHttpClient("0.0.0"),
608 mockSpawnerLayer((_command, args) => {
609 calls.push(args.join(" "));
610 return { stdout: "updated" };
611 }),
612 ),
613 ),
614 );
615 });
616
617 it.effect(
618 "releases the running-provider marker when interrupted after queuing but before the lock run starts",
619 () =>
620 Effect.gen(function* () {
621 const { registry } = yield* makeRegistry(baseProvider);
622 let blockQueuedState = true;
623 const queuedStateWrittenLatch: { resolve: () => void } = { resolve: () => {} };
624 const releaseQueuedStateLatch: { resolve: () => void } = { resolve: () => {} };
625 const queuedStateWritten = new Promise<void>((resolve) => {
626 queuedStateWrittenLatch.resolve = resolve;
627 });
628 const releaseQueuedState = new Promise<void>((resolve) => {
629 releaseQueuedStateLatch.resolve = resolve;
630 });
631
632 const updater = yield* makeTestRunner({
633 ...registry,
634 setProviderMaintenanceActionState: Effect.fn(
635 "providerMaintenanceRunner.test.blockQueuedState",
636 )(function* (input) {
637 const providers = yield* registry.setProviderMaintenanceActionState(input);
638 if (input.state?.status === "queued" && blockQueuedState) {
639 queuedStateWrittenLatch.resolve();
640 yield* Effect.promise(() => releaseQueuedState);
641 }
642 return providers;
643 }),
644 });
645
646 const first = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.forkScoped);
647 yield* Effect.promise(() => queuedStateWritten);
648 blockQueuedState = false;
649
650 yield* Fiber.interrupt(first);
651 releaseQueuedStateLatch.resolve();
652
653 const second = yield* updater.updateProvider(CODEX_DRIVER).pipe(Effect.exit);
654 assert.strictEqual(Exit.isSuccess(second), true);
655 if (Exit.isSuccess(second)) {
656 assert.strictEqual(second.value.providers[0]?.updateState?.status, "succeeded");
657 }
658 }).pipe(
659 Effect.provide(
660 Layer.mergeAll(
661 NonWindowsPlatform,
662 latestVersionHttpClient("0.0.0"),
663 mockSpawnerLayer(() => ({ stdout: "updated" })),
664 ),
665 ),
666 ),
667 );
668
669 it.effect("resolves npm to a .cmd shim and routes through the shell on win32", () => {
670 const captured: Array<{
671 readonly command: string;
672 readonly args: ReadonlyArray<string>;
673 readonly shell: boolean | string | undefined;
674 }> = [];
675 return Effect.gen(function* () {
676 const { registry } = yield* makeRegistry(baseProvider);
677 const runner = yield* makeTestRunner(registry);
678
679 const result = yield* runner.updateProvider(CODEX_DRIVER);
680
681 // On win32, resolveSpawnCommand resolves `npm` to the `.cmd` shim and
682 // routes the spawn through cmd.exe (shell: true), escaping every arg.
683 assert.strictEqual(captured.length, 1);
684 const call = captured[0];
685 assert.ok(call, "expected the spawner to be invoked once");
686 // The resolved command is the escaped `.cmd` path. Asserting the precise
687 // escaped string is brittle, so verify it carries the resolved shim and
688 // that shell mode was used.
689 assert.match(call.command, /npm\.cmd/i);
690 assert.strictEqual(call.shell, true);
691 // Args are escaped for cmd.exe shell mode (each quoted) but still carry
692 // the original install command (`install -g @openai/codex@latest`) in order.
693 assert.strictEqual(call.args.length, 3);
694 assert.match(call.args[0] ?? "", /install/);
695 assert.match(call.args[1] ?? "", /-g/);
696 assert.match(call.args[2] ?? "", /@openai\/codex@latest/);
697 assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded");
698 }).pipe(
699 Effect.provide(
700 Layer.mergeAll(
701 Layer.succeed(HostProcessPlatform, "win32"),
702 Layer.succeed(HostProcessEnvironment, {
703 PATH: "C:\\fake\\npm",
704 PATHEXT: ".COM;.EXE;.BAT;.CMD",
705 }),
706 Layer.succeed(SpawnExecutableResolution, (command) =>
707 command === "npm" ? "C:\\fake\\npm\\npm.cmd" : undefined,
708 ),
709 latestVersionHttpClient("0.0.0"),
710 Layer.succeed(
711 ChildProcessSpawner.ChildProcessSpawner,
712 ChildProcessSpawner.make((command) => {
713 const childProcess = command as unknown as {
714 readonly command: string;
715 readonly args: ReadonlyArray<string>;
716 readonly options: { readonly shell?: boolean | string | undefined };
717 };
718 captured.push({
719 command: childProcess.command,
720 args: childProcess.args,
721 shell: childProcess.options.shell,
722 });
723 return Effect.succeed(mockHandle({ stdout: "updated" }));
724 }),
725 ),
726 ),
727 ),
728 );
729 });
730});