|
| 1 | +// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +import nodeFetch from 'node-fetch'; |
| 4 | + |
| 5 | +import { |
| 6 | + getNodeStreamAdapter, |
| 7 | + isAxiosStatic, |
| 8 | + parseEventChunk, |
| 9 | + generateChunks, |
| 10 | +} from '../../src/api/api-client/utils'; |
| 11 | + |
| 12 | +vi.mock('node-fetch', () => ({ |
| 13 | + default: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +describe('getNodeStreamAdapter', () => { |
| 17 | + it('should return undefined if streaming is false', () => { |
| 18 | + const adapter = getNodeStreamAdapter(false); |
| 19 | + expect(adapter).toBeUndefined(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return "fetch" if window.fetch is available', async () => { |
| 23 | + vi.stubGlobal('fetch', vi.fn()); |
| 24 | + const adapter = getNodeStreamAdapter(true); |
| 25 | + const mockResponse = { data: { val: 1 } }; |
| 26 | + (fetch as any).mockResolvedValueOnce(mockResponse); |
| 27 | + expect(adapter).toBe('fetch'); |
| 28 | + // @ts-expect-error skip |
| 29 | + const result = await fetch({ |
| 30 | + url: 'https://example.com/api', |
| 31 | + method: 'post', |
| 32 | + }); |
| 33 | + expect(result).toBe(mockResponse); |
| 34 | + vi.unstubAllGlobals(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return a function for node-fetch', async () => { |
| 38 | + vi.stubGlobal('fetch', undefined); |
| 39 | + const mockResponse = { |
| 40 | + status: 200, |
| 41 | + body: 'Hello, World!', |
| 42 | + headers: new Map<string, string>(), |
| 43 | + }; |
| 44 | + (nodeFetch as any).mockResolvedValueOnce(mockResponse); |
| 45 | + |
| 46 | + const adapter = getNodeStreamAdapter(true); |
| 47 | + expect(typeof adapter).toBe('function'); |
| 48 | + |
| 49 | + // @ts-expect-error skip |
| 50 | + const response = await adapter({ |
| 51 | + url: 'https://example.com/api', |
| 52 | + }); |
| 53 | + |
| 54 | + expect(nodeFetch).toHaveBeenCalledWith( |
| 55 | + 'https://example.com/api', |
| 56 | + expect.anything(), |
| 57 | + ); |
| 58 | + expect(response).toEqual({ |
| 59 | + data: 'Hello, World!', |
| 60 | + status: 200, |
| 61 | + statusText: undefined, |
| 62 | + headers: {}, |
| 63 | + config: { url: 'https://example.com/api' }, |
| 64 | + request: { method: 'GET', headers: {}, timeout: undefined }, |
| 65 | + }); |
| 66 | + vi.unstubAllGlobals(); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('isAxiosStatic', () => { |
| 71 | + it('should return true if the instance has Axios property', () => { |
| 72 | + const instance = { Axios: true }; |
| 73 | + expect(isAxiosStatic(instance)).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return false if the instance does not have Axios property', () => { |
| 77 | + const instance = {}; |
| 78 | + expect(isAxiosStatic(instance)).toBe(false); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('parseEventChunk', () => { |
| 83 | + it('should parse the event and data from the chunk', () => { |
| 84 | + const chunk = 'event: my-event\ndata: {"foo":"bar"}'; |
| 85 | + const result = parseEventChunk(chunk); |
| 86 | + expect(result).toEqual({ foo: 'bar' }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should throw an error if the event is "gateway-error"', () => { |
| 90 | + const chunk = 'event: gateway-error\ndata: Something went wrong'; |
| 91 | + expect(() => parseEventChunk(chunk)).toThrow('Something went wrong'); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('generateChunks', () => { |
| 96 | + it('should yield an empty object if stream is undefined or null', async () => { |
| 97 | + const parseChunk = vi.fn(); |
| 98 | + const generator = generateChunks(undefined, parseChunk); |
| 99 | + const result = await generator.next(); |
| 100 | + |
| 101 | + expect(result.value).toBeUndefined(); |
| 102 | + expect(parseChunk).toHaveBeenCalledWith(''); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should parse chunks from a readable stream', async () => { |
| 106 | + const parseChunk = vi.fn(chunk => chunk); |
| 107 | + const mockStream = new ReadableStream({ |
| 108 | + start(controller) { |
| 109 | + controller.enqueue( |
| 110 | + new Uint8Array([ |
| 111 | + 0x64, 0x61, 0x74, 0x61, 0x3a, 0x20, 0x31, 0x0a, 0x0a, |
| 112 | + ]), |
| 113 | + ); // 'data: 1\n\n' |
| 114 | + controller.enqueue( |
| 115 | + new Uint8Array([ |
| 116 | + 0x64, 0x61, 0x74, 0x61, 0x3a, 0x20, 0x32, 0x0a, 0x0a, |
| 117 | + ]), |
| 118 | + ); // 'data: 2\n\n' |
| 119 | + controller.close(); |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + const generator = generateChunks(mockStream, parseChunk); |
| 124 | + const result1 = await generator.next(); |
| 125 | + const result2 = await generator.next(); |
| 126 | + const result3 = await generator.next(); |
| 127 | + |
| 128 | + expect(result1.value).toBe('data: 1'); |
| 129 | + expect(result2.value).toBe('data: 2'); |
| 130 | + expect(result3.done).toBe(true); |
| 131 | + }); |
| 132 | +}); |
0 commit comments