|
| 1 | +import { convertReadableStreamToArray } from '@ai-sdk/provider-utils/test'; |
1 | 2 | import assert from 'node:assert';
|
2 | 3 | import { z } from 'zod';
|
3 | 4 | import { MockLanguageModelV1 } from '../test/mock-language-model-v1';
|
@@ -84,3 +85,36 @@ describe('result.object', () => {
|
84 | 85 | assert.deepStrictEqual(result.object, { content: 'Hello, world!' });
|
85 | 86 | });
|
86 | 87 | });
|
| 88 | + |
| 89 | +describe('result.toJsonResponse', () => { |
| 90 | + it('should return JSON response', async () => { |
| 91 | + const result = await generateObject({ |
| 92 | + model: new MockLanguageModelV1({ |
| 93 | + doGenerate: async ({ prompt, mode }) => { |
| 94 | + return { |
| 95 | + ...dummyResponseValues, |
| 96 | + text: `{ "content": "Hello, world!" }`, |
| 97 | + }; |
| 98 | + }, |
| 99 | + }), |
| 100 | + schema: z.object({ content: z.string() }), |
| 101 | + mode: 'json', |
| 102 | + prompt: 'prompt', |
| 103 | + }); |
| 104 | + |
| 105 | + const response = result.toJsonResponse(); |
| 106 | + |
| 107 | + assert.strictEqual(response.status, 200); |
| 108 | + assert.strictEqual( |
| 109 | + response.headers.get('Content-Type'), |
| 110 | + 'application/json; charset=utf-8', |
| 111 | + ); |
| 112 | + |
| 113 | + assert.deepStrictEqual( |
| 114 | + await convertReadableStreamToArray( |
| 115 | + response.body!.pipeThrough(new TextDecoderStream()), |
| 116 | + ), |
| 117 | + ['{"content":"Hello, world!"}'], |
| 118 | + ); |
| 119 | + }); |
| 120 | +}); |
0 commit comments