File size: 2,867 Bytes
eb3f11e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | // Provider contract helpers expose reusable provider plugin contract test setup.
import { describe, expect, it } from "vitest";
import {
providerContractLoadError,
resolveProviderContractProvidersForPluginIds,
} from "../../plugins/contracts/registry.js";
import { resolveBundledExplicitProviderContractsFromPublicArtifacts } from "../../plugins/provider-contract-public-artifacts.js";
import type { ProviderPlugin } from "../provider-model-shared.js";
import { installProviderPluginContractSuite } from "./provider-contract-suites.js";
type ProviderContractEntry = {
pluginId: string;
provider: ProviderPlugin;
};
function providerMatchesManifestId(provider: ProviderPlugin, providerId: string): boolean {
return (
provider.id === providerId ||
(provider.aliases ?? []).includes(providerId) ||
(provider.hookAliases ?? []).includes(providerId)
);
}
function resolveProviderContractProvidersFromPublicArtifact(
pluginId: string,
): ProviderContractEntry[] | null {
return resolveBundledExplicitProviderContractsFromPublicArtifacts({ onlyPluginIds: [pluginId] });
}
export function describeProviderContracts(pluginId: string) {
let providerEntries: ProviderContractEntry[] | undefined;
const resolveProviderEntries = (): ProviderContractEntry[] => {
if (providerEntries) {
return providerEntries;
}
const publicArtifactProviders = resolveProviderContractProvidersFromPublicArtifact(pluginId);
if (publicArtifactProviders) {
providerEntries = publicArtifactProviders;
return providerEntries;
}
providerEntries = resolveProviderContractProvidersForPluginIds([pluginId]).map((provider) => ({
pluginId,
provider,
}));
return providerEntries;
};
const resolveProviderIds = (): string[] =>
resolveProviderEntries().map((entry) => entry.provider.id);
describe(`${pluginId} provider contract registry load`, () => {
it("loads bundled providers without import-time registry failure", () => {
const providers = resolveProviderEntries();
expect(providerContractLoadError).toBeUndefined();
expect(providers.length).toBeGreaterThan(0);
});
});
for (const providerId of resolveProviderIds()) {
describe(`${pluginId}:${providerId} provider contract`, () => {
// Resolve provider entries lazily so the non-isolated extension runner
// does not race provider contract collection against other file imports.
installProviderPluginContractSuite({
provider: () => {
const entry = resolveProviderEntries().find((entryLocal) =>
providerMatchesManifestId(entryLocal.provider, providerId),
);
if (!entry) {
throw new Error(`provider contract entry missing for ${pluginId}:${providerId}`);
}
return entry.provider;
},
});
});
}
}
|