Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import {
4 ProviderDriverKind,
5 ProviderInstanceId,
6 type ProviderInstallState,
7} from "@t3tools/contracts";
8import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Path from "effect/Path";
13import * as Stream from "effect/Stream";
14
15import * as ServerSettings from "../serverSettings.ts";
16import * as CodexInstallation from "./CodexInstallation.ts";
17import * as AntigravityInstallation from "./AntigravityInstallation.ts";
18import type { ProviderInstance } from "./ProviderDriver.ts";
19import { makeProviderInstallation } from "./providerInstallation.ts";
20import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
21import * as ProviderRegistry from "./ProviderRegistry.ts";
22
23const instanceId = ProviderInstanceId.make("antigravity");
24const driver = ProviderDriverKind.make("antigravity");
25const state: ProviderInstallState = {
26 driver,
27 operationId: null,
28 phase: "idle",
29 downloadedBytes: 0,
30 totalBytes: null,
31 version: null,
32 installedVersion: null,
33 canRemove: false,
34 message: null,
35};
36
37function instance(kind = driver, id = instanceId): ProviderInstance {
38 return {
39 instanceId: id,
40 driverKind: kind,
41 enabled: false,
42 displayName: undefined,
43 continuationIdentity: { driverKind: kind, continuationKey: instanceId },
44 get orchestrationAdapter(): never {
45 throw new Error("Installation must not start a provider session.");
46 },
47 get snapshot(): never {
48 throw new Error("Installation routing must not probe the provider.");
49 },
50 get textGeneration(): never {
51 throw new Error("Installation must not generate text.");
52 },
53 };
54}
55
56const makeHarness = Effect.fn("providerInstallation.test.makeHarness")(function* (
57 input: {
58 instance?: ProviderInstance;
59 settings?: Parameters<typeof ServerSettings.layerTest>[0];
60 } = {},
61) {
62 const calls: string[] = [];
63 let protectedPaths: ReadonlyArray<string> = [];
64 const configured = input.instance ?? instance();
65 const router = yield* makeProviderInstallation().pipe(
66 Effect.provide(
67 Layer.mergeAll(
68 ServerSettings.layerTest(input.settings),
69 Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
70 getInstance: (id) =>
71 Effect.succeed(id === configured.instanceId ? configured : undefined),
72 listInstances: Effect.succeed([configured]),
73 }),
74 Layer.mock(ProviderRegistry.ProviderRegistry)({
75 refreshInstance: () =>
76 Effect.sync(() => {
77 calls.push("refresh");
78 return [];
79 }),
80 }),
81 Layer.mock(CodexInstallation.CodexInstallation)({
82 managedDirectory: "/unused-managed-codex",
83 start: Effect.sync(() => {
84 calls.push("codex-start");
85 return { ...state, driver: ProviderDriverKind.make("codex") };
86 }),
87 cancel: () =>
88 Effect.sync(() => {
89 calls.push("codex-cancel");
90 return { ...state, driver: ProviderDriverKind.make("codex") };
91 }),
92 state: Effect.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
93 changes: Stream.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
94 remove: (paths) =>
95 Effect.sync(() => {
96 protectedPaths = paths ?? [];
97 calls.push("codex-remove");
98 }),
99 }),
100 Layer.mock(AntigravityInstallation.AntigravityInstallation)({
101 managedDirectory: "/unused-managed-runtime",
102 start: Effect.sync(() => {
103 calls.push("start");
104 return state;
105 }),
106 cancel: () =>
107 Effect.sync(() => {
108 calls.push("cancel");
109 return state;
110 }),
111 state: Effect.succeed(state),
112 changes: Stream.succeed(state),
113 remove: (paths) =>
114 Effect.sync(() => {
115 protectedPaths = paths ?? [];
116 calls.push("remove");
117 }),
118 }),
119 ),
120 ),
121 );
122 return { router, calls, protectedPaths: () => protectedPaths };
123});
124
125describe("provider installation routing", () => {
126 it.effect("allows explicit installation while the provider is disabled", () =>
127 Effect.gen(function* () {
128 const harness = yield* makeHarness();
129 yield* harness.router.start({ instanceId });
130 assert.deepEqual(harness.calls, ["start"]);
131 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
132 );
133
134 it.effect("rejects another driver and unknown instances before installation", () =>
135 Effect.gen(function* () {
136 const harness = yield* makeHarness({ instance: instance(ProviderDriverKind.make("codex")) });
137 const wrongDriver = yield* Effect.flip(harness.router.start({ instanceId }));
138 const missing = yield* Effect.flip(
139 harness.router.start({ instanceId: ProviderInstanceId.make("missing") }),
140 );
141 assert.equal(wrongDriver._tag, "ProviderSetupError");
142 assert.equal(missing._tag, "ProviderSetupError");
143 assert.deepEqual(harness.calls, []);
144 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
145 );
146
147 it.effect("keeps external installs manual without hiding shared install status", () =>
148 Effect.gen(function* () {
149 const harness = yield* makeHarness({
150 settings: { providers: { antigravity: { binaryPath: "/external/agy" } } },
151 });
152 const start = yield* Effect.flip(harness.router.start({ instanceId }));
153 const remove = yield* Effect.flip(harness.router.remove({ instanceId }));
154 assert.include(start.detail, "custom executable");
155 assert.include(remove.detail, "custom executable");
156 const observed = yield* Stream.runCollect(harness.router.subscribe({ instanceId }));
157 assert.deepEqual(Array.from(observed), [state]);
158 assert.deepEqual(harness.calls, []);
159 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
160 );
161
162 it.effect("routes a managed Codex instance through its own installer and refreshes removal", () =>
163 Effect.gen(function* () {
164 const codexId = ProviderInstanceId.make("codex");
165 const harness = yield* makeHarness({
166 instance: instance(ProviderDriverKind.make("codex"), codexId),
167 settings: { providers: { codex: { setupMode: "managed" } } },
168 });
169 assert.equal((yield* harness.router.start({ instanceId: codexId })).driver, "codex");
170 yield* harness.router.cancel({ instanceId: codexId, operationId: "operation" });
171 const observed = yield* Stream.runCollect(harness.router.subscribe({ instanceId: codexId }));
172 assert.equal(Array.from(observed)[0]?.driver, "codex");
173 yield* harness.router.remove({ instanceId: codexId });
174 assert.deepEqual(harness.calls, ["codex-start", "codex-cancel", "codex-remove", "refresh"]);
175 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
176 );
177
178 it.effect("keeps native Codex installation outside managed setup", () =>
179 Effect.gen(function* () {
180 const codexId = ProviderInstanceId.make("codex");
181 const harness = yield* makeHarness({
182 instance: instance(ProviderDriverKind.make("codex"), codexId),
183 settings: { providers: { codex: { setupMode: "existing" } } },
184 });
185 assert.include(
186 (yield* Effect.flip(harness.router.start({ instanceId: codexId }))).detail,
187 "Choose managed setup",
188 );
189 assert.deepEqual(harness.calls, []);
190 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
191 );
192
193 it.effect("protects another instance's binary found through its own PATH", () =>
194 Effect.gen(function* () {
195 const fs = yield* FileSystem.FileSystem;
196 const path = yield* Path.Path;
197 const platform = yield* HostProcessPlatform;
198 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-provider-install-route-" });
199 const binary = platform === "win32" ? "agy-test.exe" : "agy-test";
200 const executable = path.join(directory, binary);
201 yield* fs.writeFileString(executable, "test");
202 yield* fs.chmod(executable, 0o755);
203 const other = ProviderInstanceId.make("antigravity-work");
204 const harness = yield* makeHarness({
205 settings: {
206 providerInstances: {
207 [other]: {
208 driver,
209 config: { binaryPath: binary },
210 environment: [{ name: "PATH", value: directory, sensitive: false }],
211 },
212 },
213 },
214 });
215 yield* harness.router.remove({ instanceId });
216 assert.include(harness.protectedPaths(), executable);
217 assert.deepEqual(harness.calls, ["remove", "refresh"]);
218 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
219 );
220});