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

providerInstallation.test.ts

175 lines6.4 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 * as HostProcess 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 "@t3tools/provider-core/server/driver";
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 };
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("keeps external installs manual without hiding shared install status", () =>
127 Effect.gen(function* () {
128 const harness = yield* makeHarness({
129 settings: {
130 providerInstances: {
131 [ProviderInstanceId.make("antigravity")]: {
132 driver: ProviderDriverKind.make("antigravity"),
133 config: { binaryPath: "/external/agy" },
134 },
135 },
136 },
137 });
138 const start = yield* Effect.flip(harness.router.start({ instanceId }));
139 const remove = yield* Effect.flip(harness.router.remove({ instanceId }));
140 assert.include(start.detail, "custom executable");
141 assert.include(remove.detail, "custom executable");
142 const observed = yield* Stream.runCollect(harness.router.subscribe({ instanceId }));
143 assert.deepEqual(Array.from(observed), [state]);
144 assert.deepEqual(harness.calls, []);
145 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
146 );
147​
148 it.effect("protects another instance's binary found through its own PATH", () =>
149 Effect.gen(function* () {
150 const fs = yield* FileSystem.FileSystem;
151 const path = yield* Path.Path;
152 const platform = yield* HostProcess.Platform;
153 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-provider-install-route-" });
154 const binary = platform === "win32" ? "agy-test.exe" : "agy-test";
155 const executable = path.join(directory, binary);
156 yield* fs.writeFileString(executable, "test");
157 yield* fs.chmod(executable, 0o755);
158 const other = ProviderInstanceId.make("antigravity-work");
159 const harness = yield* makeHarness({
160 settings: {
161 providerInstances: {
162 [other]: {
163 driver,
164 config: { binaryPath: binary },
165 environment: [{ name: "PATH", value: directory, sensitive: false }],
166 },
167 },
168 },
169 });
170 yield* harness.router.remove({ instanceId });
171 assert.include(harness.protectedPaths(), executable);
172 assert.deepEqual(harness.calls, ["remove", "refresh"]);
173 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
174 );
175});