fix(ai): skip cross-provider-handoff tests when no API keys available

Tests were throwing errors instead of skipping on CI where no API keys
are configured. Now uses describe.skipIf() and it.skipIf() patterns
consistent with other tests in the package.
This commit is contained in:
Mario Zechner 2026-01-19 16:47:49 +01:00
parent 2f4b510c12
commit 68352a42cf

View file

@ -116,6 +116,20 @@ async function getApiKey(provider: string): Promise<string | undefined> {
return getEnvApiKey(provider); return getEnvApiKey(provider);
} }
/**
* Synchronous check for API key availability (env vars only, for skipIf)
*/
function hasApiKey(provider: string): boolean {
return !!getEnvApiKey(provider);
}
/**
* Check if any provider has API keys available (for skipIf at describe level)
*/
function hasAnyApiKey(): boolean {
return PROVIDER_MODEL_PAIRS.some((pair) => hasApiKey(pair.provider));
}
function dumpFailurePayload(params: { label: string; error: string; payload?: unknown; messages: Message[] }): void { function dumpFailurePayload(params: { label: string; error: string; payload?: unknown; messages: Message[] }): void {
const filename = `/tmp/pi-handoff-${params.label}-${Date.now()}.json`; const filename = `/tmp/pi-handoff-${params.label}-${Date.now()}.json`;
const body = { const body = {
@ -259,7 +273,7 @@ async function generateContext(
}; };
} }
describe("Cross-Provider Handoff", () => { describe.skipIf(!hasAnyApiKey())("Cross-Provider Handoff", () => {
let contexts: Record<string, CachedContext>; let contexts: Record<string, CachedContext>;
let availablePairs: ProviderModelPair[]; let availablePairs: ProviderModelPair[];
@ -272,14 +286,16 @@ describe("Cross-Provider Handoff", () => {
for (const pair of PROVIDER_MODEL_PAIRS) { for (const pair of PROVIDER_MODEL_PAIRS) {
const apiKey = await getApiKey(pair.provider); const apiKey = await getApiKey(pair.provider);
if (!apiKey) { if (!apiKey) {
throw new Error(`Missing auth for ${pair.provider}`); console.log(`[${pair.label}] Skipping - no auth for ${pair.provider}`);
continue;
} }
console.log(`[${pair.label}] Generating fixture...`); console.log(`[${pair.label}] Generating fixture...`);
const result = await generateContext(pair, apiKey); const result = await generateContext(pair, apiKey);
if (!result || result.messages.length < 4) { if (!result || result.messages.length < 4) {
throw new Error(`Failed to generate fixture for ${pair.label}`); console.log(`[${pair.label}] Failed to generate fixture, skipping`);
continue;
} }
contexts[pair.label] = { contexts[pair.label] = {
@ -297,15 +313,18 @@ describe("Cross-Provider Handoff", () => {
console.log(`\n=== ${availablePairs.length}/${PROVIDER_MODEL_PAIRS.length} contexts available ===\n`); console.log(`\n=== ${availablePairs.length}/${PROVIDER_MODEL_PAIRS.length} contexts available ===\n`);
}, 300000); }, 300000);
it("should have at least 2 fixtures to test handoffs", () => { it.skipIf(!hasAnyApiKey())("should have at least 2 fixtures to test handoffs", () => {
expect(Object.keys(contexts).length).toBeGreaterThanOrEqual(2); expect(Object.keys(contexts).length).toBeGreaterThanOrEqual(2);
}); });
it("should handle cross-provider handoffs for each target", async () => { it.skipIf(!hasAnyApiKey())(
"should handle cross-provider handoffs for each target",
async () => {
const contextLabels = Object.keys(contexts); const contextLabels = Object.keys(contexts);
if (contextLabels.length < 2) { if (contextLabels.length < 2) {
throw new Error("Not enough fixtures for handoff test"); console.log("Not enough fixtures for handoff test, skipping");
return;
} }
console.log("\n=== Testing Cross-Provider Handoffs ===\n"); console.log("\n=== Testing Cross-Provider Handoffs ===\n");
@ -350,7 +369,9 @@ describe("Cross-Provider Handoff", () => {
continue; continue;
} }
const model: Model<Api> = targetPair.apiOverride ? { ...baseModel, api: targetPair.apiOverride } : baseModel; const model: Model<Api> = targetPair.apiOverride
? { ...baseModel, api: targetPair.apiOverride }
: baseModel;
const supportsReasoning = model.reasoning === true; const supportsReasoning = model.reasoning === true;
console.log( console.log(
@ -419,5 +440,7 @@ describe("Cross-Provider Handoff", () => {
} }
expect(failures.length).toBe(0); expect(failures.length).toBe(0);
}, 600000); },
600000,
);
}); });