import { describe, expect, it } from 'vitest'; import type { KeycloakConfig } from '../config.ts'; import { checkKeycloakConnection } from '../keycloak-check.ts'; const kc: KeycloakConfig = { url: 'https://kc.test', realm: 'corp', clientId: 'api-gateway', issuer: 'https://kc.test/realms/corp', jwksUrl: 'https://kc.test/realms/corp/protocol/openid-connect/certs', }; const reply = (status: number, body: unknown): typeof fetch => async () => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }); describe('启动自检(手册第 5 步)', () => { it('issuer 一致 → 回 issuer 与 clientId', async () => { await expect(checkKeycloakConnection(kc, reply(200, { issuer: kc.issuer }))).resolves.toEqual({ issuer: kc.issuer, clientId: 'api-gateway', }); }); it('issuer 不一致 → 抛,话里带期望 / 实际 / KC_HOSTNAME', async () => { await expect(checkKeycloakConnection(kc, reply(200, { issuer: 'http://localhost:8080/realms/corp' }))).rejects.toThrow( /KC_HOSTNAME/, ); }); it('发现文档拉不到 → 抛带状态码', async () => { await expect(checkKeycloakConnection(kc, reply(503, {}))).rejects.toThrow(/503/); }); });