Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { providerSeeds } from '#/db/seed-providers.ts'
4
5import { ANTHROPIC_SPEC_URL, anthropicProvider } from './anthropic.ts'
6import { OPENAI_SPEC_URL } from './openai.ts'
7import { sha256Text, stainlessSpecUrlFromStats } from './types.ts'
8
9describe('stainlessSpecUrlFromStats', () => {
10 it('reads the openapi_spec_url field', () => {
11 expect(
12 stainlessSpecUrlFromStats(
13 [
14 'configured_endpoints: 131',
15 'openapi_spec_url: https://example.test/spec.yml',
16 'openapi_spec_hash: abc',
17 ].join('\n'),
18 'anthropic',
19 ),
20 ).toBe('https://example.test/spec.yml')
21 })
22
23 it('throws when the field is missing', () => {
24 expect(() =>
25 stainlessSpecUrlFromStats('config_hash: abc\n', 'openai'),
26 ).toThrow(/openai \.stats\.yml: couldn't find openapi_spec_url/)
27 })
28})
29
30describe('openai spec source', () => {
31 it('seeds the Castiron transformed spec, not the lagging public export', () => {
32 const openai = providerSeeds.find((p) => p.id === 'openai')
33 expect(openai?.specSourceUrl).toBe(OPENAI_SPEC_URL)
34 expect(OPENAI_SPEC_URL).toContain('openai-node')
35 expect(OPENAI_SPEC_URL).toContain('openapi.transformed.yml')
36 })
37})
38
39describe('anthropic fetchSpec (Stainless-bundled mock-spec.json.gz)', () => {
40 async function gzip(text: string): Promise<ArrayBuffer> {
41 return new Response(
42 new Blob([text]).stream().pipeThrough(new CompressionStream('gzip')),
43 ).arrayBuffer()
44 }
45
46 async function withBody<T>(
47 body: BodyInit | null,
48 status: number,
49 run: () => Promise<T>,
50 ): Promise<T> {
51 const original = globalThis.fetch
52 globalThis.fetch = ((url: string) =>
53 Promise.resolve(
54 String(url) === ANTHROPIC_SPEC_URL
55 ? new Response(body, { status })
56 : new Response('not found', { status: 404 }),
57 )) as typeof fetch
58 try {
59 return await run()
60 } finally {
61 globalThis.fetch = original
62 }
63 }
64
65 it('seeds the bundled spec URL', () => {
66 const anthropic = providerSeeds.find((p) => p.id === 'anthropic')
67 expect(anthropic?.specSourceUrl).toBe(ANTHROPIC_SPEC_URL)
68 })
69
70 it('gunzips the spec and hashes the JSON as specRevision', async () => {
71 const json = JSON.stringify({
72 openapi: '3.1.0',
73 info: { title: 'Anthropic API' },
74 paths: { '/v1/messages': { post: {} } },
75 })
76 const fetched = await withBody(await gzip(json), 200, () =>
77 anthropicProvider.fetchSpec({}),
78 )
79 expect(fetched.specs[0]?.info?.title).toBe('Anthropic API')
80 expect(fetched.sources).toEqual([
81 { url: ANTHROPIC_SPEC_URL, hash: await sha256Text(json) },
82 ])
83 expect(fetched.specRevision).toBe(await sha256Text(json))
84 })
85
86 it('throws on 404', async () => {
87 await expect(
88 withBody('nope', 404, () => anthropicProvider.fetchSpec({})),
89 ).rejects.toThrow(/404/)
90 })
91
92 it('throws when the bytes are not gzip', async () => {
93 await expect(
94 withBody('{"openapi":"3.1.0"}', 200, () =>
95 anthropicProvider.fetchSpec({}),
96 ),
97 ).rejects.toThrow(/anthropic: bundled spec .* is not gzipped JSON/)
98 })
99
100 it('throws when the document is not OpenAPI', async () => {
101 await expect(
102 withBody(await gzip('{"hello":"world"}'), 200, () =>
103 anthropicProvider.fetchSpec({}),
104 ),
105 ).rejects.toThrow(/anthropic: bundled spec .* is not OpenAPI/)
106 })
107})