|
| 1 | +import http from 'http' |
| 2 | +import zlib from 'zlib' |
| 3 | +import { afterEach, describe, expect, test, vi } from 'vitest' |
| 4 | + |
| 5 | +import { RetoolAPI } from './api' |
| 6 | + |
| 7 | +describe('RetoolAPI', () => { |
| 8 | + afterEach(() => { |
| 9 | + vi.restoreAllMocks() |
| 10 | + }) |
| 11 | + |
| 12 | + test('registerAgent uses globalThis.fetch', async () => { |
| 13 | + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue( |
| 14 | + new Response(JSON.stringify({ versionHash: 'abc' }), { |
| 15 | + status: 200, |
| 16 | + headers: { 'Content-Type': 'application/json' }, |
| 17 | + }), |
| 18 | + ) |
| 19 | + |
| 20 | + const api = new RetoolAPI({ |
| 21 | + hostUrl: 'https://example.retool.com', |
| 22 | + apiKey: 'token', |
| 23 | + pollingTimeoutMs: 1000, |
| 24 | + }) |
| 25 | + |
| 26 | + const response = await api.registerAgent({ |
| 27 | + resourceId: 'resource-id', |
| 28 | + environmentName: 'production', |
| 29 | + version: '0.0.1', |
| 30 | + agentUuid: 'agent-uuid', |
| 31 | + operations: {}, |
| 32 | + }) |
| 33 | + |
| 34 | + expect(fetchSpy).toHaveBeenCalledTimes(1) |
| 35 | + expect(fetchSpy.mock.calls[0][0]).toBe('https://example.retool.com/api/v1/retoolrpc/registerAgent') |
| 36 | + expect(response.ok).toBe(true) |
| 37 | + await expect(response.json()).resolves.toEqual({ versionHash: 'abc' }) |
| 38 | + }) |
| 39 | + |
| 40 | + // Regression for RE-2830: node-fetch@2 throws FetchError Premature close on some Node 24 |
| 41 | + // gzip responses. Native fetch must consume a gzip registerAgent body cleanly. |
| 42 | + // Node 18 under Vitest can take several seconds for a local fetch round-trip. |
| 43 | + test( |
| 44 | + 'registerAgent consumes a gzip Content-Encoding response body', |
| 45 | + async () => { |
| 46 | + const payload = JSON.stringify({ versionHash: 'gzip-version-hash' }) |
| 47 | + const gzipBody = zlib.gzipSync(payload) |
| 48 | + |
| 49 | + const server = http.createServer((req, res) => { |
| 50 | + // Drain the request body before responding so fetch does not stall on an unread POST. |
| 51 | + req.resume() |
| 52 | + req.on('end', () => { |
| 53 | + expect(req.method).toBe('POST') |
| 54 | + expect(req.url).toBe('/api/v1/retoolrpc/registerAgent') |
| 55 | + res.writeHead(200, { |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + 'Content-Encoding': 'gzip', |
| 58 | + 'Content-Length': String(gzipBody.length), |
| 59 | + }) |
| 60 | + res.end(gzipBody) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + await new Promise<void>((resolve) => { |
| 65 | + server.listen(0, '127.0.0.1', () => resolve()) |
| 66 | + }) |
| 67 | + |
| 68 | + const address = server.address() |
| 69 | + if (!address || typeof address === 'string') { |
| 70 | + throw new Error('Expected TCP server address') |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + const api = new RetoolAPI({ |
| 75 | + hostUrl: `http://127.0.0.1:${address.port}`, |
| 76 | + apiKey: 'token', |
| 77 | + pollingTimeoutMs: 1000, |
| 78 | + }) |
| 79 | + |
| 80 | + const response = await api.registerAgent({ |
| 81 | + resourceId: 'resource-id', |
| 82 | + environmentName: 'production', |
| 83 | + version: '0.0.1', |
| 84 | + agentUuid: 'agent-uuid', |
| 85 | + operations: {}, |
| 86 | + }) |
| 87 | + |
| 88 | + expect(response.ok).toBe(true) |
| 89 | + await expect(response.json()).resolves.toEqual({ versionHash: 'gzip-version-hash' }) |
| 90 | + } finally { |
| 91 | + await new Promise<void>((resolve, reject) => { |
| 92 | + server.close((error) => (error ? reject(error) : resolve())) |
| 93 | + }) |
| 94 | + } |
| 95 | + }, |
| 96 | + 20_000, |
| 97 | + ) |
| 98 | +}) |
0 commit comments