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

providerInstallation.test.ts

169 lines6.3 KB

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 { layerTest as settingsLayerTest } from "../serverSettings.ts";
16import { AntigravityInstallation } from "./AntigravityInstallation.ts";
17import type { ProviderInstance } from "./ProviderDriver.ts";
18import { makeProviderInstallation } from "./providerInstallation.ts";
19import { ProviderInstanceRegistry } from "./Services/ProviderInstanceRegistry.ts";
20import { ProviderRegistry } from "./Services/ProviderRegistry.ts";
21​
22const instanceId = ProviderInstanceId.make("antigravity");
23const driver = ProviderDriverKind.make("antigravity");
24const state: ProviderInstallState = {
25 driver,
26 operationId: null,
27 phase: "idle",
28 downloadedBytes: 0,
29 totalBytes: null,
30 version: null,
31 installedVersion: null,
32 canRemove: false,
33 message: null,
34};
35​
36function instance(kind = driver): ProviderInstance {
37 return {
38 instanceId,
39 driverKind: kind,
40 enabled: false,
41 displayName: undefined,
42 continuationIdentity: { driverKind: kind, continuationKey: instanceId },
43 get adapter(): never {
44 throw new Error("Installation must not start a provider session.");
45 },
46 get snapshot(): never {
47 throw new Error("Installation routing must not probe the provider.");
48 },
49 get textGeneration(): never {
50 throw new Error("Installation must not generate text.");
51 },
52 };
54​
55const makeHarness = Effect.fn("providerInstallation.test.makeHarness")(function* (
56 input: {
57 instance?: ProviderInstance;
58 settings?: Parameters<typeof settingsLayerTest>[0];
59 } = {},
60) {
61 const calls: string[] = [];
62 let protectedPaths: ReadonlyArray<string> = [];
63 const configured = input.instance ?? instance();
64 const router = yield* makeProviderInstallation().pipe(
65 Effect.provide(
66 Layer.mergeAll(
67 settingsLayerTest(input.settings),
68 Layer.mock(ProviderInstanceRegistry)({
69 getInstance: (id) =>
70 Effect.succeed(id === configured.instanceId ? configured : undefined),
71 listInstances: Effect.succeed([configured]),
72 }),
73 Layer.mock(ProviderRegistry)({
74 refreshInstance: () =>
75 Effect.sync(() => {
76 calls.push("refresh");
77 return [];
78 }),
79 }),
80 Layer.mock(AntigravityInstallation)({
81 managedDirectory: "/unused-managed-runtime",
82 start: Effect.sync(() => {
83 calls.push("start");
84 return state;
85 }),
86 cancel: () =>
87 Effect.sync(() => {
88 calls.push("cancel");
89 return state;
90 }),
91 state: Effect.succeed(state),
92 changes: Stream.succeed(state),
93 remove: (paths) =>
94 Effect.sync(() => {
95 protectedPaths = paths ?? [];
96 calls.push("remove");
97 }),
98 }),
99 ),
100 ),
101 );
102 return { router, calls, protectedPaths: () => protectedPaths };
103});
104​
105describe("provider installation routing", () => {
106 it.effect("allows explicit installation while the provider is disabled", () =>
107 Effect.gen(function* () {
108 const harness = yield* makeHarness();
109 yield* harness.router.start({ instanceId });
110 assert.deepEqual(harness.calls, ["start"]);
111 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
112 );
113​
114 it.effect("rejects another driver and unknown instances before installation", () =>
115 Effect.gen(function* () {
116 const harness = yield* makeHarness({ instance: instance(ProviderDriverKind.make("codex")) });
117 const wrongDriver = yield* Effect.flip(harness.router.start({ instanceId }));
118 const missing = yield* Effect.flip(
119 harness.router.start({ instanceId: ProviderInstanceId.make("missing") }),
120 );
121 assert.equal(wrongDriver._tag, "ProviderSetupError");
122 assert.equal(missing._tag, "ProviderSetupError");
123 assert.deepEqual(harness.calls, []);
124 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
125 );
126​
127 it.effect("keeps external installs manual without hiding shared install status", () =>
128 Effect.gen(function* () {
129 const harness = yield* makeHarness({
130 settings: { providers: { antigravity: { binaryPath: "/external/agy" } } },
131 });
132 const start = yield* Effect.flip(harness.router.start({ instanceId }));
133 const remove = yield* Effect.flip(harness.router.remove({ instanceId }));
134 assert.include(start.detail, "custom executable");
135 assert.include(remove.detail, "custom executable");
136 const observed = yield* Stream.runCollect(harness.router.subscribe({ instanceId }));
137 assert.deepEqual(Array.from(observed), [state]);
138 assert.deepEqual(harness.calls, []);
139 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
140 );
141​
142 it.effect("protects another instance's binary found through its own PATH", () =>
143 Effect.gen(function* () {
144 const fs = yield* FileSystem.FileSystem;
145 const path = yield* Path.Path;
146 const platform = yield* HostProcessPlatform;
147 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-provider-install-route-" });
148 const binary = platform === "win32" ? "agy-test.exe" : "agy-test";
149 const executable = path.join(directory, binary);
150 yield* fs.writeFileString(executable, "test");
151 yield* fs.chmod(executable, 0o755);
152 const other = ProviderInstanceId.make("antigravity-work");
153 const harness = yield* makeHarness({
154 settings: {
155 providerInstances: {
156 [other]: {
157 driver,
158 config: { binaryPath: binary },
159 environment: [{ name: "PATH", value: directory, sensitive: false }],
160 },
161 },
162 },
163 });
164 yield* harness.router.remove({ instanceId });
165 assert.include(harness.protectedPaths(), executable);
166 assert.deepEqual(harness.calls, ["remove", "refresh"]);
167 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
168 );
169});