Open History to see the last commit
1/**
2 * Test helpers for constructing a `ProviderAdapterRegistryShape` mock from a
3 * kind-keyed adapter map.
4 *
5 * Tests historically assembled a `registry` object with only `getByProvider`
6 * + `listProviders` populated. Slice D grew the shape with `getByInstance`
7 * and `listInstances`; this helper fills both in from a single kind-keyed
8 * input so individual fixtures can stay concise.
9 *
10 * Non-default instance ids (e.g. `codex_personal`) are not addressable via
11 * the shim returned here — the legacy test fixtures only ever had
12 * single-instance-per-driver data anyway.
13 *
14 * @module provider/testUtils/providerAdapterRegistryMock
15 */
16import {
17 defaultInstanceIdForDriver,
18 ProviderDriverKind,
19 type ProviderInstanceId,
20} from "@t3tools/contracts";
21import * as Effect from "effect/Effect";
22import * as PubSub from "effect/PubSub";
23import * as Record from "effect/Record";
24import * as Result from "effect/Result";
25import * as Stream from "effect/Stream";
26
27import { ProviderUnsupportedError, type ProviderAdapterError } from "../Errors.ts";
28import type { ProviderAdapterShape } from "../Services/ProviderAdapter.ts";
29import type { ProviderAdapterRegistryShape } from "../Services/ProviderAdapterRegistry.ts";
30
31export type KindAdapterMap = Partial<
32 Record<ProviderDriverKind, ProviderAdapterShape<ProviderAdapterError>>
33>;
34
35/**
36 * Build a `ProviderAdapterRegistryShape` from a kind-keyed adapter map.
37 * Every adapter present in the map is addressable via both the legacy
38 * `getByProvider(kind)` path and the new `getByInstance(id)` path (where
39 * `id = defaultInstanceIdForDriver(kind)`).
40 */
41export const makeAdapterRegistryMock = (adapters: KindAdapterMap): ProviderAdapterRegistryShape => {
42 const byInstanceId = new Map<ProviderInstanceId, ProviderAdapterShape<ProviderAdapterError>>();
43 for (const [kind, adapter] of Object.entries(adapters)) {
44 if (!adapter) continue;
45 const driverKind = ProviderDriverKind.make(kind);
46 byInstanceId.set(defaultInstanceIdForDriver(driverKind), adapter);
47 }
48
49 const getByInstance: ProviderAdapterRegistryShape["getByInstance"] = (instanceId) => {
50 const adapter = byInstanceId.get(instanceId);
51 return adapter
52 ? Effect.succeed(adapter)
53 : Effect.fail(
54 new ProviderUnsupportedError({
55 provider: ProviderDriverKind.make(instanceId),
56 }),
57 );
58 };
59
60 return {
61 getByInstance,
62 getInstanceInfo: (instanceId) => {
63 const adapter = byInstanceId.get(instanceId);
64 if (!adapter) {
65 return Effect.fail(
66 new ProviderUnsupportedError({
67 provider: ProviderDriverKind.make(instanceId),
68 }),
69 );
70 }
71 return Effect.succeed({
72 instanceId,
73 driverKind: ProviderDriverKind.make(adapter.provider),
74 displayName: undefined,
75 enabled: true,
76 continuationIdentity: {
77 driverKind: ProviderDriverKind.make(adapter.provider),
78 continuationKey: `${adapter.provider}:instance:${instanceId}`,
79 },
80 });
81 },
82 listInstances: () => Effect.succeed(Array.from(byInstanceId.keys())),
83 listProviders: () =>
84 Effect.succeed(
85 Record.keys(
86 Record.filterMap(adapters, (adapter, kind) =>
87 adapter !== undefined ? Result.succeed(kind) : Result.failVoid,
88 ),
89 ),
90 ),
91 // Static test fixtures don't reload; an empty stream is enough to
92 // satisfy the shape. Tests exercising hot-reload build their own
93 // stream via the real `ProviderInstanceRegistry`.
94 streamChanges: Stream.empty,
95 subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), (pubsub) =>
96 PubSub.subscribe(pubsub),
97 ),
98 };
99};