/** * @file Tests covering non-error-path gaps across file-upload, http-client, and * utils source files. Targets: * * - file-upload.ts: createUploadRequest hooks, createRequestBodyForFilepaths * multi-file and relative path resolution * - http-client.ts: getResponseJson JSON error branches (non-JSON content-type, * HTML response, 502/503 response body) * - utils.ts lines 146-151: promiseWithResolvers polyfill branch */ import { createServer } from 'node:http' import { mkdtempSync, writeFileSync } from 'node:fs' import os from 'node:os' import path from 'node:path' import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { createRequestBodyForFilepaths, createUploadRequest, } from '../../src/file-upload.mts' import { createGetRequest, getResponseJson } from '../../src/http-client.mts' import { promiseWithResolvers } from '../../src/utils.mts' import { setupLocalHttpServer } from '../utils/local-server.mts' import type { IncomingMessage, Server, ServerResponse } from 'node:http' import { safeDelete } from '@socketsecurity/lib-stable/fs/safe' // ============================================================================= // 1. file-upload.ts — createUploadRequest hooks and multi-file form data // ============================================================================= describe('file-upload createUploadRequest with hooks', () => { let tmpDir: string beforeAll(() => { tmpDir = mkdtempSync(path.join(os.tmpdir(), 'sdk-fup-hooks-')) writeFileSync(path.join(tmpDir, 'a.json'), '{"a":1}') writeFileSync(path.join(tmpDir, 'b.json'), '{"b":2}') }) afterAll(async () => { await safeDelete(tmpDir) }) // Local server to accept multipart uploads const getBaseUrl = setupLocalHttpServer( (req: IncomingMessage, res: ServerResponse) => { // Consume body req.on('data', () => {}) req.on('end', () => { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ uploaded: true })) }) }, ) it('should call onRequest and onResponse hooks during upload', async () => { let requestCalled = false let responseCalled = false const hooks = { onRequest: () => { requestCalled = true }, onResponse: () => { responseCalled = true }, } const form = createRequestBodyForFilepaths( [path.join(tmpDir, 'a.json')], tmpDir, ) const response = await createUploadRequest(getBaseUrl(), '/upload', form, { hooks, }) expect(response.status).toBe(200) expect(requestCalled).toBe(true) expect(responseCalled).toBe(true) }) }) // ============================================================================= // 2. http-client.ts — getResponseJson JSON error detail branches // (non-JSON content-type, HTML response, 502/503 body) // ============================================================================= describe('getResponseJson enhanced error branches', () => { let server: Server let baseUrl: string beforeAll(async () => { server = createServer((req, res) => { const url = req.url || '' if (url.includes('/wrong-content-type')) { // Return invalid JSON with non-JSON content-type res.writeHead(200, { 'Content-Type': 'text/html' }) res.end('not json data') } else if (url.includes('/html-response')) { // Return HTML that starts with '<', with application/json content-type res.writeHead(200, { 'Content-Type': 'application/json' }) res.end('
Error') } else if (url.includes('/bad-gateway')) { // Return 502 Bad Gateway text in JSON content-type res.writeHead(200, { 'Content-Type': 'application/json' }) res.end('502 Bad Gateway nginx') } else if (url.includes('/service-unavailable')) { // Return 503 Service Unavailable text in JSON content-type res.writeHead(200, { 'Content-Type': 'application/json' }) res.end('503 Service Unavailable') } else if (url.includes('/long-invalid-json')) { // Return >200 chars of invalid JSON to test preview truncation res.writeHead(200, { 'Content-Type': 'application/json' }) res.end('x'.repeat(300)) } else { res.writeHead(404) res.end() } }) await new Promise