The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Deferred from "effect/Deferred"; |
| 5 | import * as Fiber from "effect/Fiber"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as PubSub from "effect/PubSub"; |
| 9 | import * as Ref from "effect/Ref"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import * as ServerConfig from "../config.ts"; |
| 12 | import * as ModelManifest from "./ModelManifest.ts"; |
| 13 | import * as ProviderRegistry from "./ProviderRegistry.ts"; |
| 14 | import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; |
| 15 | import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts"; |
| 16 | import * as ProviderInstanceRegistryMutator from "./ProviderInstanceRegistryMutator.ts"; |
| 17 | import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; |
| 18 | import { makeManualOnlyProviderMaintenanceCapabilities } from "@t3tools/provider-core/server/maintenanceResolver"; |
| 19 | import * as Schema from "effect/Schema"; |
| 20 | import { |
| 21 | applyProviderCompatibility, |
| 22 | ProviderCompatibilityPolicy, |
| 23 | resolveProviderCompatibility, |
| 24 | } from "./providerCompatibility.ts"; |
| 25 | import bundledManifest from "./model-manifest.json" with { type: "json" }; |
| 26 | |
| 27 | const decodeCompatibilityPolicies = Schema.decodeUnknownSync( |
| 28 | Schema.Array(ProviderCompatibilityPolicy), |
| 29 | ); |
| 30 | const driver = ProviderDriverKind.make("codex"); |
| 31 | const policy: ProviderCompatibilityPolicy = { |
| 32 | driver, |
| 33 | t3CodeRange: ">=0.0.0", |
| 34 | recommendedVersion: "2.0.0", |
| 35 | recommendedRange: ">=2.0.0 <3.0.0", |
| 36 | ranges: [ |
| 37 | { range: "<1.0.0", status: "broken" }, |
| 38 | { range: ">=1.0.0 <1.5.0", status: "unsupported" }, |
| 39 | { range: ">=1.5.0 <2.0.0", status: "graceful" }, |
| 40 | { range: ">=2.0.0 <3.0.0", status: "supported" }, |
| 41 | ], |
| 42 | }; |
| 43 | const provider: ServerProvider = { |
| 44 | driver, |
| 45 | instanceId: ProviderInstanceId.make("codex-work"), |
| 46 | enabled: true, |
| 47 | installed: true, |
| 48 | version: "0.9.0", |
| 49 | status: "error", |
| 50 | message: "Authentication failed", |
| 51 | checkedAt: "2026-09-22T00:00:00Z", |
| 52 | auth: { status: "unauthenticated" }, |
| 53 | models: [], |
| 54 | skills: [], |
| 55 | slashCommands: [], |
| 56 | }; |
| 57 | |
| 58 | describe("provider compatibility", () => { |
| 59 | it("keeps releases before V2 on the OpenCode 1 policy they shipped with", () => { |
| 60 | // Older builds fetch main's manifest; their V1 orchestrator cannot drive OpenCode 2. |
| 61 | const opencode = ProviderDriverKind.make("opencode"); |
| 62 | for (const [version, expected] of [ |
| 63 | ["1.14.19", "supported"], |
| 64 | ["2.0.18", "broken"], |
| 65 | ["1.14.18", "broken"], |
| 66 | ] as const) { |
| 67 | const advisory = resolveProviderCompatibility( |
| 68 | ModelManifest.BUNDLED_MODEL_MANIFEST.compatibility, |
| 69 | opencode, |
| 70 | version, |
| 71 | "0.0.45", |
| 72 | ); |
| 73 | assert.strictEqual(advisory?.status, expected, `OpenCode ${version} on 0.0.45`); |
| 74 | assert.strictEqual(advisory?.recommendedVersion, "1.14.19"); |
| 75 | } |
| 76 | // The current policy rates a prerelease or build by its base version. |
| 77 | for (const [version, expected] of [ |
| 78 | ["1.13.0-beta.1", "broken"], |
| 79 | ["1.13.0+abc123", "broken"], |
| 80 | ["2.1.0-rc.1", "supported"], |
| 81 | ] as const) { |
| 82 | assert.strictEqual( |
| 83 | resolveProviderCompatibility( |
| 84 | ModelManifest.BUNDLED_MODEL_MANIFEST.compatibility, |
| 85 | opencode, |
| 86 | version, |
| 87 | )?.status, |
| 88 | expected, |
| 89 | `OpenCode ${version}`, |
| 90 | ); |
| 91 | } |
| 92 | assert.isUndefined( |
| 93 | resolveProviderCompatibility( |
| 94 | ModelManifest.BUNDLED_MODEL_MANIFEST.compatibility, |
| 95 | ProviderDriverKind.make("pi"), |
| 96 | "1.0.0", |
| 97 | "0.0.45", |
| 98 | ), |
| 99 | ); |
| 100 | }); |
| 101 | |
| 102 | it("compares Cursor build dates without treating semver prereleases as stable", () => { |
| 103 | const cursor = ProviderDriverKind.make("cursor"); |
| 104 | const cursorPolicy: ProviderCompatibilityPolicy = { |
| 105 | driver: cursor, |
| 106 | t3CodeRange: policy.t3CodeRange, |
| 107 | versionPattern: "^(\\d{4}\\.\\d{2}\\.\\d{2})-[a-f0-9]+$", |
| 108 | ranges: [ |
| 109 | { range: "<2026.05.09", status: "unsupported" }, |
| 110 | { range: ">=2026.05.09", status: "supported" }, |
| 111 | ], |
| 112 | }; |
| 113 | for (const [version, expected] of [ |
| 114 | ["2026.05.08-a1b2c3d", "unsupported"], |
| 115 | ["2026.05.09-a1b2c3d", "supported"], |
| 116 | ["2026.09.22-f2b0fcd", "supported"], |
| 117 | ["2026.05.09", "supported"], |
| 118 | ["2026.05.09-beta.1", "unknown"], |
| 119 | ] as const) { |
| 120 | assert.strictEqual( |
| 121 | resolveProviderCompatibility([cursorPolicy], cursor, version)?.status, |
| 122 | expected, |
| 123 | ); |
| 124 | } |
| 125 | assert.strictEqual( |
| 126 | resolveProviderCompatibility([policy], driver, "2.0.0-a1b2c3d")?.status, |
| 127 | "unknown", |
| 128 | ); |
| 129 | }); |
| 130 | |
| 131 | it("marks Antigravity below 1.5 broken and recommends 1.5.12 in the bundled manifest", () => { |
| 132 | const policies = decodeCompatibilityPolicies(bundledManifest.compatibility); |
| 133 | const antigravity = ProviderDriverKind.make("antigravity"); |
| 134 | for (const version of [ |
| 135 | "1.1.1", |
| 136 | "1.3.0", |
| 137 | "1.4.1", |
| 138 | "1.4.5", |
| 139 | "1.4.9", |
| 140 | "agy_acp_server_1.4.5", |
| 141 | "1.4.5-beta", |
| 142 | ]) { |
| 143 | const advisory = resolveProviderCompatibility(policies, antigravity, version); |
| 144 | assert.strictEqual(advisory?.status, "broken", version); |
| 145 | } |
| 146 | const supported = resolveProviderCompatibility(policies, antigravity, "1.5.11"); |
| 147 | assert.strictEqual(supported?.status, "supported"); |
| 148 | assert.strictEqual(supported?.recommendedRange, ">=1.5.12 <2"); |
| 149 | // Stable releases before 0.1.10 cannot install the CLI, so they keep the policy they shipped with. |
| 150 | assert.strictEqual( |
| 151 | resolveProviderCompatibility(policies, antigravity, "1.4.5", "0.1.9")?.status, |
| 152 | "supported", |
| 153 | ); |
| 154 | }); |
| 155 | |
| 156 | it("classifies boundaries and treats unlisted versions and release tags as unknown", () => { |
| 157 | for (const [version, expected] of [ |
| 158 | ["0.9.9", "broken"], |
| 159 | ["1.0.0", "unsupported"], |
| 160 | ["1.5.0", "graceful"], |
| 161 | ["2.0.0", "supported"], |
| 162 | ["v2.0.0", "supported"], |
| 163 | ["3.0.0", "unknown"], |
| 164 | ["2.0.0-beta.1", "unknown"], |
| 165 | [null, "unknown"], |
| 166 | ] as const) { |
| 167 | assert.strictEqual(resolveProviderCompatibility([policy], driver, version)?.status, expected); |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | it("applies policies only within their T3 Code version range", () => { |
| 172 | const scopedPolicy = { ...policy, t3CodeRange: ">=0.0.42 <0.1.0" }; |
| 173 | for (const version of ["0.0.42", "0.0.45"]) { |
| 174 | assert.strictEqual( |
| 175 | resolveProviderCompatibility([scopedPolicy], driver, "0.9.0", version)?.status, |
| 176 | "broken", |
| 177 | ); |
| 178 | } |
| 179 | for (const version of ["0.0.41", "0.1.0"]) { |
| 180 | assert.isUndefined(resolveProviderCompatibility([scopedPolicy], driver, "0.9.0", version)); |
| 181 | } |
| 182 | }); |
| 183 | |
| 184 | it("relaxes remote policy without losing probe errors, and falls back when a policy is omitted", () => { |
| 185 | const broken = applyProviderCompatibility(provider, [], [policy]); |
| 186 | assert.strictEqual(broken.compatibilityAdvisory?.status, "broken"); |
| 187 | const relaxed = { ...policy, ranges: [{ range: ">=0.0.0", status: "supported" as const }] }; |
| 188 | const supported = applyProviderCompatibility(broken, [relaxed], [policy]); |
| 189 | assert.strictEqual(supported.compatibilityAdvisory?.status, "supported"); |
| 190 | assert.strictEqual(supported.status, "error"); |
| 191 | assert.strictEqual(supported.message, "Authentication failed"); |
| 192 | assert.strictEqual( |
| 193 | applyProviderCompatibility(supported, [{ ...policy, t3CodeRange: "<0.0.0" }], [policy]) |
| 194 | .compatibilityAdvisory?.status, |
| 195 | "broken", |
| 196 | ); |
| 197 | const removed = applyProviderCompatibility(supported, [], []); |
| 198 | assert.isUndefined(removed.compatibilityAdvisory); |
| 199 | assert.strictEqual(removed.status, "error"); |
| 200 | assert.strictEqual(removed.message, "Authentication failed"); |
| 201 | assert.isUndefined( |
| 202 | applyProviderCompatibility({ ...broken, enabled: false }, [], [policy]).compatibilityAdvisory, |
| 203 | ); |
| 204 | assert.isUndefined( |
| 205 | applyProviderCompatibility({ ...broken, installed: false }, [], [policy]) |
| 206 | .compatibilityAdvisory, |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | it("rejects invalid ranges and recommendations outside the first supported match", () => { |
| 211 | const decode = Schema.decodeUnknownSync(ProviderCompatibilityPolicy); |
| 212 | assert.doesNotThrow(() => decode(policy)); |
| 213 | const prefixed = decode({ |
| 214 | ...policy, |
| 215 | t3CodeRange: ">=v0.0.42 <v0.1", |
| 216 | recommendedRange: "^v2", |
| 217 | ranges: [{ range: ">=v2.0 <v3", status: "supported" }], |
| 218 | }); |
| 219 | assert.strictEqual( |
| 220 | resolveProviderCompatibility([prefixed], driver, "2.0.0", "0.0.45")?.status, |
| 221 | "supported", |
| 222 | ); |
| 223 | assert.strictEqual( |
| 224 | resolveProviderCompatibility([prefixed], driver, "3.0.0", "0.0.45")?.status, |
| 225 | "unknown", |
| 226 | ); |
| 227 | for (const invalid of [ |
| 228 | { ...policy, t3CodeRange: "*" }, |
| 229 | { ...policy, recommendedVersion: "3.0.0" }, |
| 230 | { ...policy, ranges: [{ range: ">=2.0.0 garbage", status: "supported" }] }, |
| 231 | { ...policy, recommendedVersion: "2.0.0; echo unsafe" }, |
| 232 | { ...policy, ranges: [{ range: ">=0.0.0", status: "broken" }, ...policy.ranges] }, |
| 233 | ]) |
| 234 | assert.throws(() => decode(invalid)); |
| 235 | }); |
| 236 | }); |
| 237 | |
| 238 | it.effect("a remote policy refresh preserves a newer health result on the registry stream", () => |
| 239 | Effect.gen(function* () { |
| 240 | const started = yield* Deferred.make<void>(); |
| 241 | const release = yield* Deferred.make<void>(); |
| 242 | const health = yield* Ref.make<ServerProvider>({ |
| 243 | ...provider, |
| 244 | status: "ready" as ServerProvider["status"], |
| 245 | message: "Healthy", |
| 246 | }); |
| 247 | const manifest = yield* Ref.make<ModelManifest.ModelManifestData>({ |
| 248 | version: 1, |
| 249 | currentModels: {}, |
| 250 | compatibility: [policy], |
| 251 | }); |
| 252 | const instance: ProviderInstance = { |
| 253 | instanceId: provider.instanceId, |
| 254 | driverKind: driver, |
| 255 | enabled: true, |
| 256 | displayName: undefined, |
| 257 | continuationIdentity: { driverKind: driver, continuationKey: "test-codex" }, |
| 258 | snapshot: { |
| 259 | getSnapshot: Ref.get(health), |
| 260 | refresh: Ref.get(health), |
| 261 | streamChanges: Stream.empty, |
| 262 | applyUsageLimits: () => Effect.void, |
| 263 | resolveMaintenance: () => |
| 264 | Effect.succeed( |
| 265 | makeManualOnlyProviderMaintenanceCapabilities({ provider: driver, packageName: null }), |
| 266 | ), |
| 267 | }, |
| 268 | orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"], |
| 269 | textGeneration: {} as ProviderInstance["textGeneration"], |
| 270 | }; |
| 271 | const refresh = Deferred.succeed(started, undefined).pipe( |
| 272 | Effect.andThen(Deferred.await(release)), |
| 273 | Effect.andThen( |
| 274 | Ref.updateAndGet(manifest, (current) => ({ |
| 275 | ...current, |
| 276 | compatibility: [ |
| 277 | { ...policy, ranges: [{ range: ">=0.0.0", status: "supported" as const }] }, |
| 278 | ], |
| 279 | })), |
| 280 | ), |
| 281 | ); |
| 282 | const layerDependencies = Layer.mergeAll( |
| 283 | ProviderLatestVersions.layer, |
| 284 | Layer.succeed(ModelManifest.ModelManifest, { |
| 285 | current: Ref.get(manifest), |
| 286 | refresh, |
| 287 | forceRefresh: refresh, |
| 288 | refreshOnDemand: refresh, |
| 289 | refreshInBackground: Effect.void, |
| 290 | }), |
| 291 | Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, { |
| 292 | getInstance: (id) => Effect.succeed(id === instance.instanceId ? instance : undefined), |
| 293 | listInstances: Effect.succeed([instance]), |
| 294 | listUnavailable: Effect.succeed([]), |
| 295 | streamChanges: Stream.empty, |
| 296 | subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), PubSub.subscribe), |
| 297 | }), |
| 298 | ServerConfig.layerTest(process.cwd(), { prefix: "compatibility-registry-test" }).pipe( |
| 299 | Layer.provideMerge(NodeServices.layer), |
| 300 | ), |
| 301 | ); |
| 302 | yield* Effect.gen(function* () { |
| 303 | const registry = yield* ProviderRegistry.ProviderRegistry; |
| 304 | yield* Deferred.await(started); |
| 305 | assert.strictEqual( |
| 306 | (yield* registry.getProviders)[0]?.compatibilityAdvisory?.status, |
| 307 | "broken", |
| 308 | ); |
| 309 | yield* Ref.set(health, provider); |
| 310 | const latestHealth = yield* registry.refreshInstance(provider.instanceId); |
| 311 | assert.strictEqual(latestHealth[0]?.message, "Authentication failed"); |
| 312 | const supported = yield* Stream.toPull( |
| 313 | registry.streamChanges.pipe( |
| 314 | Stream.filter((snapshots) => snapshots[0]?.compatibilityAdvisory?.status === "supported"), |
| 315 | ), |
| 316 | ); |
| 317 | const subscribed = yield* supported.pipe(Effect.forkScoped({ startImmediately: true })); |
| 318 | yield* Deferred.succeed(release, undefined); |
| 319 | const updated = (yield* Fiber.join(subscribed))[0]?.[0]; |
| 320 | assert.strictEqual(updated?.compatibilityAdvisory?.status, "supported"); |
| 321 | assert.strictEqual(updated?.status, "error"); |
| 322 | assert.strictEqual(updated?.message, "Authentication failed"); |
| 323 | }).pipe( |
| 324 | Effect.provide( |
| 325 | ProviderRegistry.layer.pipe( |
| 326 | Layer.provide(layerDependencies), |
| 327 | Layer.provide( |
| 328 | Layer.mock(ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator)({}), |
| 329 | ), |
| 330 | ), |
| 331 | ), |
| 332 | ); |
| 333 | }), |
| 334 | ); |