/** * UF-044 end to end: the Models screen's key check goes through this server's * own auth middleware and built-in proxy. Only the upstream Anthropic call is * faked, so a check that reaches the proxy without the session token fails here. */ import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import type { FastifyInstance } from 'fastify'; import { buildLocalServer } from '../../src/local/index.js'; import { injectWithAuth } from '../test-utils.js'; describe('probe-model through the built-in proxy (UF-044)', () => { let server: FastifyInstance; let tmpDir: string; const realFetch = globalThis.fetch; const priorTrustLocalhost = process.env.WAGGLE_TRUST_LOCALHOST; beforeAll(async () => { // The production default: loopback callers need the session token too. // vitest.setup.ts turns localhost trust on for the older suites. process.env.WAGGLE_TRUST_LOCALHOST = '0'; tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-probe-proxy-')); server = await buildLocalServer({ dataDir: tmpDir }); await server.listen({ port: 0, host: '127.0.0.1' }); server.vault.set('anthropic', 'sk-ant-test-key'); }); afterEach(() => vi.unstubAllGlobals()); afterAll(async () => { await server.close(); fs.rmSync(tmpDir, { recursive: true, force: true }); if (priorTrustLocalhost === undefined) delete process.env.WAGGLE_TRUST_LOCALHOST; else process.env.WAGGLE_TRUST_LOCALHOST = priorTrustLocalhost; }); it('verifies a working key', async () => { const upstream = vi.fn(async () => new Response(JSON.stringify({ id: 'msg_probe', type: 'message', role: 'assistant', model: 'claude-haiku-4-5', content: [{ type: 'text', text: 'h' }], stop_reason: 'max_tokens', usage: { input_tokens: 1, output_tokens: 1 }, }), { status: 200, headers: { 'content-type': 'application/json' } })); vi.stubGlobal('fetch', (input: RequestInfo | URL, init?: RequestInit) => { const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; return url.startsWith('https://api.anthropic.com/') ? upstream() : realFetch(input, init); }); const res = await injectWithAuth(server, { method: 'POST', url: '/api/settings/probe-model', payload: { model: 'claude-haiku-4-5' }, }); expect(JSON.parse(res.body)).toMatchObject({ configured: true, verified: true }); expect(upstream).toHaveBeenCalledTimes(1); }); });