|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + ProviderDriverKind, |
| 4 | + ProviderInstanceId, |
| 5 | + ThreadId, |
| 6 | + type HostPowerSnapshot, |
| 7 | + type ProviderInstanceConfig, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import * as DateTime from "effect/DateTime"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Layer from "effect/Layer"; |
| 12 | +import * as PubSub from "effect/PubSub"; |
| 13 | +import * as Stream from "effect/Stream"; |
| 14 | +import { HttpClient, HttpClientResponse } from "effect/unstable/http"; |
| 15 | + |
| 16 | +import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
| 17 | +import * as HostPowerMonitor from "../background/HostPowerMonitor.ts"; |
| 18 | +import * as ServerSettings from "../serverSettings.ts"; |
| 19 | +import * as PistacheSource from "./PistacheSource.ts"; |
| 20 | +import { metricsFixture, snapshotFixture } from "./pistacheApi.fixture.ts"; |
| 21 | + |
| 22 | +const hostPower: HostPowerSnapshot = { |
| 23 | + source: "unknown", |
| 24 | + idle: "unknown", |
| 25 | + idleSeconds: null, |
| 26 | + locked: "unknown", |
| 27 | + suspended: false, |
| 28 | + onBattery: "unknown", |
| 29 | + lowPowerMode: "unknown", |
| 30 | + thermalState: "unknown", |
| 31 | + stale: true, |
| 32 | + updatedAt: DateTime.makeUnsafe("2026-09-05T20:30:00.000Z"), |
| 33 | +}; |
| 34 | + |
| 35 | +const hostPowerLayer = Layer.effect( |
| 36 | + HostPowerMonitor.HostPowerMonitor, |
| 37 | + Effect.gen(function* () { |
| 38 | + const changes = yield* PubSub.sliding<HostPowerSnapshot>(1); |
| 39 | + return HostPowerMonitor.HostPowerMonitor.of({ |
| 40 | + snapshot: Effect.succeed(hostPower), |
| 41 | + report: (next) => PubSub.publish(changes, next).pipe(Effect.asVoid), |
| 42 | + streamChanges: Stream.fromPubSub(changes), |
| 43 | + }); |
| 44 | + }), |
| 45 | +); |
| 46 | + |
| 47 | +const V1_CONFIG = JSON.stringify({ |
| 48 | + provider: { |
| 49 | + pistache: { |
| 50 | + npm: "@ai-sdk/openai-compatible", |
| 51 | + options: { baseURL: "http://router.test:8080/v1" }, |
| 52 | + }, |
| 53 | + }, |
| 54 | +}); |
| 55 | +const V2_CONFIG = JSON.stringify({ |
| 56 | + providers: { pistache: { settings: { baseURL: "http://router.test:8080/v1/" } } }, |
| 57 | +}); |
| 58 | + |
| 59 | +function instance( |
| 60 | + environment: ReadonlyArray<{ name: string; value: string; sensitive?: boolean }>, |
| 61 | +): ProviderInstanceConfig { |
| 62 | + return { |
| 63 | + driver: ProviderDriverKind.make("opencode"), |
| 64 | + environment: environment.map((variable) => ({ |
| 65 | + name: variable.name, |
| 66 | + value: variable.value, |
| 67 | + sensitive: variable.sensitive ?? false, |
| 68 | + })), |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +function harness(instances: Record<string, ProviderInstanceConfig>) { |
| 73 | + const requests: string[] = []; |
| 74 | + const http = HttpClient.make((request) => |
| 75 | + Effect.sync(() => { |
| 76 | + const url = new URL(request.url); |
| 77 | + requests.push(url.origin + url.pathname); |
| 78 | + return HttpClientResponse.fromWeb( |
| 79 | + request, |
| 80 | + Response.json(url.pathname === "/api/v1/metrics" ? metricsFixture : snapshotFixture), |
| 81 | + ); |
| 82 | + }), |
| 83 | + ); |
| 84 | + const settings = ServerSettings.layerTest({ providerInstances: instances }); |
| 85 | + const layer = PistacheSource.layer.pipe( |
| 86 | + Layer.provide( |
| 87 | + Layer.mergeAll( |
| 88 | + Layer.succeed(HttpClient.HttpClient, http), |
| 89 | + settings, |
| 90 | + BackgroundPolicy.layer.pipe(Layer.provide(Layer.merge(hostPowerLayer, settings))), |
| 91 | + ), |
| 92 | + ), |
| 93 | + ); |
| 94 | + return { requests, layer }; |
| 95 | +} |
| 96 | + |
| 97 | +const configured = ( |
| 98 | + environment: ReadonlyArray<{ name: string; value: string; sensitive?: boolean }>, |
| 99 | +) => ({ |
| 100 | + [ProviderInstanceId.make("pistache")]: instance(environment), |
| 101 | +}); |
| 102 | + |
| 103 | +describe("resolvePistacheConfig", () => { |
| 104 | + const resolve = (instances: Record<string, ProviderInstanceConfig>) => |
| 105 | + Effect.gen(function* () { |
| 106 | + const settings = yield* ServerSettings.ServerSettingsService; |
| 107 | + return PistacheSource.resolvePistacheConfig(yield* settings.getSettings); |
| 108 | + }).pipe(Effect.provide(ServerSettings.layerTest({ providerInstances: instances }))); |
| 109 | + |
| 110 | + it.effect("derives the origin from an OpenCode 1.x config and strips /v1", () => |
| 111 | + Effect.gen(function* () { |
| 112 | + const config = yield* resolve( |
| 113 | + configured([ |
| 114 | + { name: "OPENCODE_CONFIG_CONTENT", value: V1_CONFIG }, |
| 115 | + { name: "PISTACHE_API_KEY", value: "secret", sensitive: true }, |
| 116 | + ]), |
| 117 | + ); |
| 118 | + expect(config).toEqual({ baseUrl: "http://router.test:8080", token: "secret" }); |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + it.effect("derives the origin from an OpenCode 2.x config", () => |
| 123 | + Effect.gen(function* () { |
| 124 | + const config = yield* resolve( |
| 125 | + configured([ |
| 126 | + { name: "OPENCODE_CONFIG_CONTENT", value: V2_CONFIG }, |
| 127 | + { name: "PISTACHE_API_KEY", value: "secret", sensitive: true }, |
| 128 | + ]), |
| 129 | + ); |
| 130 | + expect(config?.baseUrl).toBe("http://router.test:8080"); |
| 131 | + }), |
| 132 | + ); |
| 133 | + |
| 134 | + it.effect("prefers an explicit PISTACHE_BASE_URL", () => |
| 135 | + Effect.gen(function* () { |
| 136 | + const config = yield* resolve( |
| 137 | + configured([ |
| 138 | + { name: "OPENCODE_CONFIG_CONTENT", value: V1_CONFIG }, |
| 139 | + { name: "PISTACHE_BASE_URL", value: "https://pistache.example/" }, |
| 140 | + { name: "PISTACHE_API_KEY", value: "secret", sensitive: true }, |
| 141 | + ]), |
| 142 | + ); |
| 143 | + expect(config?.baseUrl).toBe("https://pistache.example"); |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
| 147 | + it.effect("is null without a key, without a URL, or without the instance", () => |
| 148 | + Effect.gen(function* () { |
| 149 | + expect( |
| 150 | + yield* resolve(configured([{ name: "OPENCODE_CONFIG_CONTENT", value: V1_CONFIG }])), |
| 151 | + ).toBeNull(); |
| 152 | + expect( |
| 153 | + yield* resolve(configured([{ name: "PISTACHE_API_KEY", value: "secret" }])), |
| 154 | + ).toBeNull(); |
| 155 | + expect( |
| 156 | + yield* resolve({ |
| 157 | + [ProviderInstanceId.make("other")]: instance([ |
| 158 | + { name: "PISTACHE_BASE_URL", value: "http://router.test" }, |
| 159 | + { name: "PISTACHE_API_KEY", value: "secret" }, |
| 160 | + ]), |
| 161 | + }), |
| 162 | + ).toBeNull(); |
| 163 | + }), |
| 164 | + ); |
| 165 | +}); |
| 166 | + |
| 167 | +describe("PistacheSource", () => { |
| 168 | + it.effect("reports unconfigured and makes no requests without a pistache instance", () => |
| 169 | + Effect.gen(function* () { |
| 170 | + const test = harness({}); |
| 171 | + const source = yield* PistacheSource.PistacheSource.pipe(Effect.provide(test.layer)); |
| 172 | + yield* source.refresh; |
| 173 | + expect(yield* source.overview).toEqual({ |
| 174 | + configured: false, |
| 175 | + fetchedAt: null, |
| 176 | + error: null, |
| 177 | + rows: [], |
| 178 | + windows: [], |
| 179 | + alerts: [], |
| 180 | + usage: [], |
| 181 | + }); |
| 182 | + expect(yield* source.threadHistory(ThreadId.make("thread-a"))).toMatchObject({ |
| 183 | + configured: false, |
| 184 | + decisions: [], |
| 185 | + }); |
| 186 | + expect(test.requests).toEqual([]); |
| 187 | + }), |
| 188 | + ); |
| 189 | + |
| 190 | + it.effect("serves the overview and per-thread decisions from one read", () => |
| 191 | + Effect.gen(function* () { |
| 192 | + const test = harness( |
| 193 | + configured([ |
| 194 | + { name: "OPENCODE_CONFIG_CONTENT", value: V1_CONFIG }, |
| 195 | + { name: "PISTACHE_API_KEY", value: "secret", sensitive: true }, |
| 196 | + ]), |
| 197 | + ); |
| 198 | + const source = yield* PistacheSource.PistacheSource.pipe(Effect.provide(test.layer)); |
| 199 | + yield* source.refresh; |
| 200 | + const overview = yield* source.overview; |
| 201 | + expect(overview.configured).toBe(true); |
| 202 | + expect(overview.error).toBeNull(); |
| 203 | + expect(overview.rows).toHaveLength(5); |
| 204 | + expect(overview.windows[0]?.remainingFraction).toBe(0.42); |
| 205 | + expect(overview.alerts).toHaveLength(1); |
| 206 | + expect(overview.usage[0]?.id).toBe("jev_calls"); |
| 207 | + |
| 208 | + const history = yield* source.threadHistory(ThreadId.make("thread-a")); |
| 209 | + expect(history.configured).toBe(true); |
| 210 | + expect(history.decisions.map((decision) => decision.requestId)).toEqual(["req-1"]); |
| 211 | + expect(history.decisions[0]).not.toHaveProperty("threadId"); |
| 212 | + expect(yield* source.threadHistory(ThreadId.make("thread-none"))).toMatchObject({ |
| 213 | + decisions: [], |
| 214 | + }); |
| 215 | + expect(new Set(test.requests)).toEqual( |
| 216 | + new Set([ |
| 217 | + "http://router.test:8080/api/v1/metrics", |
| 218 | + "http://router.test:8080/api/v1/snapshot", |
| 219 | + ]), |
| 220 | + ); |
| 221 | + }), |
| 222 | + ); |
| 223 | +}); |