Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

bedrock-sdk-spec.test.ts

108 lines3.6 KB

1import { describe, expect, it } from 'vitest'
2​
3import { classifyAndBundle } from '../ingest/sync.ts'
4import { provider } from './adapters/amazon-bedrock.ts'
5import sdk from './fixtures/bedrock-converse-native-sdk.json'
6import {
7 BEDROCK_RUNTIME_PATHS,
8 bedrockConverseSpec,
9} from './bedrock-sdk-spec.ts'
10​
11const spec = bedrockConverseSpec(sdk)
12​
13function isRecord(value: unknown): value is Record<string, unknown> {
14 return typeof value === 'object' && value !== null && !Array.isArray(value)
16​
17function record(value: unknown, label: string): Record<string, unknown> {
18 if (!isRecord(value)) throw new Error(`missing ${label}`)
19 return value
21​
22function schema(name: string): Record<string, unknown> {
23 return record(spec.components?.schemas?.[name], `schema ${name}`)
25​
26describe('bedrock runtime paths', () => {
27 it('publishes converse, converse-stream, invoke, and invoke-with-response-stream', () => {
28 expect(Object.keys(spec.paths ?? {})).toEqual([...BEDROCK_RUNTIME_PATHS])
29 const bundled = classifyAndBundle(provider, {
30 specs: [spec],
31 sources: [],
32 outputStrategy: 'post-200',
33 })
34 expect(bundled.warnings).toEqual([])
35 expect(bundled.endpoints.map((endpoint) => endpoint.path)).toEqual([
36 ...BEDROCK_RUNTIME_PATHS,
37 ])
38 expect(
39 new Set(bundled.endpoints.map((endpoint) => endpoint.activity)),
40 ).toEqual(new Set(['chat']))
41 })
42​
43 it('publishes one ConverseStream event per message, including stream errors', () => {
44 const events = schema('ConverseStreamOutput')
45 expect(events).toMatchObject({ minProperties: 1, maxProperties: 1 })
46 expect(Object.keys(record(events.properties, 'events'))).toEqual([
47 'messageStart',
48 'contentBlockStart',
49 'contentBlockDelta',
50 'contentBlockStop',
51 'messageStop',
52 'metadata',
53 'internalServerException',
54 'modelStreamErrorException',
55 'validationException',
56 'throttlingException',
57 'serviceUnavailableException',
58 ])
59 const stream = bundledOutput('/model/{modelId}/converse-stream')
60 expect(stream.responseContentTypes).toEqual([
61 'application/vnd.amazon.eventstream',
62 ])
63 expect(stream.output).toMatchObject({
64 minProperties: 1,
65 maxProperties: 1,
66 })
67 expect(record(stream.output?.properties, 'stream events')).toHaveProperty(
68 'messageStart',
69 )
70 expect(record(stream.output?.$defs, 'stream defs')).toHaveProperty(
71 'InternalServerException',
72 )
73 })
74​
75 it('publishes InvokeModel as the raw JSON document, and its stream as events', () => {
76 const invoke = bundledOutput('/model/{modelId}/invoke')
77 expect(typeof invoke.input?.description).toBe('string')
78 expect(invoke.input).not.toHaveProperty('contentEncoding')
79 expect(invoke.input).not.toHaveProperty('type')
80 expect(invoke.responseContentTypes).toBeNull()
81​
82 const streaming = bundledOutput(
83 '/model/{modelId}/invoke-with-response-stream',
84 )
85 expect(streaming.input).not.toHaveProperty('contentEncoding')
86 expect(streaming.responseContentTypes).toEqual([
87 'application/vnd.amazon.eventstream',
88 ])
89 expect(
90 Object.keys(
91 record(schema('ResponseStream').properties, 'response stream'),
92 ),
93 ).toContain('chunk')
94 expect(
95 record(streaming.output?.properties, 'invoke events'),
96 ).toHaveProperty('chunk')
97 })
98})
99​
100function bundledOutput(path: string) {
101 const endpoint = classifyAndBundle(provider, {
102 specs: [spec],
103 sources: [],
104 outputStrategy: 'post-200',
105 }).endpoints.find((item) => item.path === path)
106 if (!endpoint?.output) throw new Error(`no bundled output for ${path}`)
107 return endpoint