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 * as HostProcess from "@t3tools/shared/HostProcess";
9import * as Effect from "effect/Effect";
10import * as Deferred from "effect/Deferred";
11import * as SubscriptionRef from "effect/SubscriptionRef";
12import * as FileSystem from "effect/FileSystem";
13import * as Layer from "effect/Layer";
14import * as Path from "effect/Path";
15import * as Stream from "effect/Stream";
16
17import * as ServerSettings from "../serverSettings.ts";
18import * as CodexInstallation from "./CodexInstallation.ts";
19import * as AntigravityInstallation from "./AntigravityInstallation.ts";
20import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
21import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
22import * as ProviderInstallation from "./providerInstallation.ts";
23import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
24import * as ProviderRegistry from "./ProviderRegistry.ts";
25
26const instanceId = ProviderInstanceId.make("antigravity");
27const driver = ProviderDriverKind.make("antigravity");
28const state: ProviderInstallState = {
29 driver,
30 operationId: null,
31 phase: "idle",
32 downloadedBytes: 0,
33 totalBytes: null,
34 version: null,
35 installedVersion: null,
36 canRemove: false,
37 message: null,
38};
39
40function instance(kind = driver, id = instanceId): ProviderInstance {
41 return {
42 instanceId: id,
43 driverKind: kind,
44 enabled: false,
45 displayName: undefined,
46 continuationIdentity: { driverKind: kind, continuationKey: instanceId },
47 get orchestrationAdapter(): never {
48 throw new Error("Installation must not start a provider session.");
49 },
50 get snapshot(): never {
51 throw new Error("Installation routing must not probe the provider.");
52 },
53 get textGeneration(): never {
54 throw new Error("Installation must not generate text.");
55 },
56 };
57}
58
59const makeHarness = Effect.fn("providerInstallation.test.makeHarness")(function* (
60 input: {
61 instance?: ProviderInstance;
62 settings?: Parameters<typeof ServerSettings.layerTest>[0];
63 } = {},
64) {
65 const calls: string[] = [];
66 const refreshed = yield* Deferred.make<void>();
67 const observed = yield* Deferred.make<void>();
68 const listening = yield* Deferred.make<void>();
69 const installationState = yield* SubscriptionRef.make(state);
70 let protectedPaths: ReadonlyArray<string> = [];
71 const configured = input.instance ?? instance();
72 const dependencies = Layer.mergeAll(
73 ServerSettings.layerTest(input.settings),
74 Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
75 getInstance: (id) => Effect.succeed(id === configured.instanceId ? configured : undefined),
76 listInstances: Effect.succeed([configured]),
77 }),
78 Layer.mock(ProviderRegistry.ProviderRegistry)({
79 refreshInstance: () =>
80 Effect.sync(() => {
81 calls.push("refresh");
82 return [];
83 }).pipe(Effect.tap(() => Deferred.succeed(refreshed, undefined))),
84 }),
85 Layer.mock(CodexInstallation.CodexInstallation)({
86 managedDirectory: "/unused-managed-codex",
87 start: Effect.sync(() => {
88 calls.push("codex-start");
89 return { ...state, driver: ProviderDriverKind.make("codex") };
90 }),
91 cancel: () =>
92 Effect.sync(() => {
93 calls.push("codex-cancel");
94 return { ...state, driver: ProviderDriverKind.make("codex") };
95 }),
96 state: Effect.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
97 changes: Stream.succeed({ ...state, driver: ProviderDriverKind.make("codex") }),
98 remove: (paths) =>
99 Effect.sync(() => {
100 protectedPaths = paths ?? [];
101 calls.push("codex-remove");
102 }),
103 }),
104 Layer.mock(AntigravityInstallation.AntigravityInstallation)({
105 managedDirectory: "/unused-managed-runtime",
106 start: Effect.sync(() => {
107 calls.push("start");
108 return { ...state, operationId: "install-1", phase: "downloading" as const };
109 }).pipe(Effect.tap((started) => SubscriptionRef.set(installationState, started))),
110 cancel: () =>
111 Effect.sync(() => {
112 calls.push("cancel");
113 return state;
114 }),
115 state: Effect.succeed(state),
116 changes: input.instance?.refreshModels
117 ? SubscriptionRef.changes(installationState).pipe(
118 Stream.tap(() => Deferred.succeed(listening, undefined)),
119 Stream.ensuring(Deferred.succeed(observed, undefined)),
120 )
121 : Stream.succeed(state),
122 remove: (paths) =>
123 Effect.sync(() => {
124 protectedPaths = paths ?? [];
125 calls.push("remove");
126 }),
127 }),
128 );
129 const context = yield* Layer.build(ProviderInstallation.layer.pipe(Layer.provide(dependencies)));
130 const connect = Effect.service(ProviderInstallation.ProviderInstallation).pipe(
131 Effect.provide(context),
132 );
133 const router = yield* connect;
134 return {
135 router,
136 connect,
137 listening,
138 calls,
139 refreshed,
140 observed,
141 installationState,
142 protectedPaths: () => protectedPaths,
143 };
144});
145
146describe("provider installation routing", () => {
147 it.effect.each([
148 { phase: "succeeded", enabled: true, checkFails: false, expectedChecks: 1 },
149 { phase: "succeeded", enabled: true, checkFails: true, expectedChecks: 1 },
150 { phase: "succeeded", enabled: false, checkFails: false, expectedChecks: 0 },
151 { phase: "cancelled", enabled: true, checkFails: false, expectedChecks: 0 },
152 { phase: "failed", enabled: true, checkFails: false, expectedChecks: 0 },
153 ] as const)(
154 "checks account access once after installation is $phase, enabled=$enabled, checkFails=$checkFails",
155 ({ phase, enabled, checkFails, expectedChecks }) =>
156 Effect.gen(function* () {
157 let checks = 0;
158 const harness = yield* makeHarness({
159 instance: Object.assign(instance(), {
160 enabled,
161 refreshModels: () =>
162 Effect.sync(() => {
163 checks++;
164 }).pipe(
165 Effect.andThen(
166 checkFails
167 ? Effect.fail(
168 new ProviderDriverError({
169 driver,
170 instanceId,
171 detail: "Sign in required.",
172 }),
173 )
174 : Effect.void,
175 ),
176 ),
177 }),
178 });
179 yield* Effect.gen(function* () {
180 const first = yield* harness.connect;
181 const second = yield* harness.connect;
182 yield* Effect.all([first.start({ instanceId }), second.start({ instanceId })], {
183 concurrency: "unbounded",
184 });
185 yield* Deferred.await(harness.listening);
186 }).pipe(Effect.scoped);
187 assert.isFalse(yield* Deferred.isDone(harness.observed));
188 assert.strictEqual(checks, 0);
189 yield* SubscriptionRef.set(harness.installationState, {
190 ...state,
191 operationId: "install-1",
192 phase,
193 installedVersion: phase === "succeeded" ? "1.5.1" : null,
194 });
195 yield* Deferred.await(harness.observed);
196 if (expectedChecks > 0) yield* Deferred.await(harness.refreshed);
197 assert.strictEqual(checks, expectedChecks);
198 assert.deepEqual(
199 harness.calls,
200 expectedChecks > 0 ? ["start", "start", "refresh"] : ["start", "start"],
201 );
202 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
203 );
204
205 it.effect("keeps external installs manual without hiding shared install status", () =>
206 Effect.gen(function* () {
207 const harness = yield* makeHarness({
208 settings: {
209 providerInstances: {
210 [ProviderInstanceId.make("antigravity")]: {
211 driver: ProviderDriverKind.make("antigravity"),
212 config: { binaryPath: "/external/agy" },
213 },
214 },
215 },
216 });
217 const start = yield* Effect.flip(harness.router.start({ instanceId }));
218 const remove = yield* Effect.flip(harness.router.remove({ instanceId }));
219 assert.include(start.detail, "custom executable");
220 assert.include(remove.detail, "custom executable");
221 const observed = yield* Stream.runCollect(harness.router.subscribe({ instanceId }));
222 assert.deepEqual(Array.from(observed), [state]);
223 assert.deepEqual(harness.calls, []);
224 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
225 );
226
227 it.effect("protects another instance's binary found through its own PATH", () =>
228 Effect.gen(function* () {
229 const fs = yield* FileSystem.FileSystem;
230 const path = yield* Path.Path;
231 const platform = yield* HostProcess.Platform;
232 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-provider-install-route-" });
233 const binary = platform === "win32" ? "agy-test.exe" : "agy-test";
234 const executable = path.join(directory, binary);
235 yield* fs.writeFileString(executable, "test");
236 yield* fs.chmod(executable, 0o755);
237 const other = ProviderInstanceId.make("antigravity-work");
238 const harness = yield* makeHarness({
239 settings: {
240 providerInstances: {
241 [other]: {
242 driver,
243 config: { binaryPath: binary },
244 environment: [{ name: "PATH", value: directory, sensitive: false }],
245 },
246 },
247 },
248 });
249 yield* harness.router.remove({ instanceId });
250 assert.include(harness.protectedPaths(), executable);
251 assert.deepEqual(harness.calls, ["remove", "refresh"]);
252 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
253 );
254});