/** * @file Coverage gap tests for SocketSdk class API methods. Targets uncovered * lines in socket-sdk-class.ts including: * * - getApi response type handling (#handleQueryResponseData) * - sendApi additional paths * - additional method success paths (exportOpenVEX, rescanFullScan, * getEnabledEntitlements, getOrgAlertFullScans) */ import { describe, expect, it } from 'vitest' import { SocketSdk } from '../../src/index.mts' import { setupLocalHttpServer } from '../utils/local-server.mts' import type { HttpResponse } from '@socketsecurity/lib/http-request/response-types' import type { SocketSdkGenericResult } from '../../src/index.mts' import type { IncomingMessage, ServerResponse } from 'node:http' // --------------------------------------------------------------------------- // getApi with different response types (covers #handleQueryResponseData) // --------------------------------------------------------------------------- describe('SocketSdk - getApi response type handling', () => { const getBaseUrl = setupLocalHttpServer( (req: IncomingMessage, res: ServerResponse) => { const url = req.url || '' if (url.includes('/raw-endpoint')) { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('raw data') } else if (url.includes('/text-endpoint')) { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('Hello, text!') } else if (url.includes('/json-endpoint')) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ key: 'value', count: 42 })) } else if (url.includes('/text-nothrow')) { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('some text data') } else if (url.includes('/default-endpoint')) { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('data') } else if (url.includes('/large-text')) { res.writeHead(200, { 'Content-Type': 'text/plain' }) res.end('x'.repeat(10_000)) } else if (url.includes('/utf8-text')) { res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }) res.end('Hello 世界 Мир 🌍') } else { res.writeHead(404) res.end() } }, ) it('should return raw response when responseType is "response" and throws=false', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = (await client.getApi('raw-endpoint', { responseType: 'response', throws: false, })) as SocketSdkGenericResult expect(result.success).toBe(true) if (!result.success) { return } expect(result.data).toBeDefined() expect(result.status).toBe(200) }) it('should return text when responseType is "text" and throws=true', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getApi('text-endpoint', { responseType: 'text', }) expect(result).toBe('Hello, text!') }) it('should return JSON when responseType is "json" and throws=true', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getApi<{ count: number; key: string }>( 'json-endpoint', { responseType: 'json' }, ) expect(result).toEqual({ key: 'value', count: 42 }) }) it('should return text in non-throwing mode with status', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = (await client.getApi('text-nothrow', { responseType: 'text', throws: false, })) as SocketSdkGenericResult expect(result.success).toBe(true) if (!result.success) { return } expect(result.data).toBe('some text data') expect(result.status).toBe(200) }) it('should handle default responseType (response) without options', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getApi('default-endpoint') expect(result).toBeDefined() expect((result as HttpResponse).status).toBe(200) }) it('should handle large text responses within limit', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getApi('large-text', { responseType: 'text', }) expect(result).toBe('x'.repeat(10_000)) }) it('should handle multi-byte UTF-8 text', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getApi('utf8-text', { responseType: 'text', }) expect(result).toBe('Hello 世界 Мир 🌍') }) }) // --------------------------------------------------------------------------- // sendApi additional coverage (local HTTP server) // --------------------------------------------------------------------------- describe('SocketSdk - sendApi additional paths', () => { const getBaseUrl = setupLocalHttpServer( (req: IncomingMessage, res: ServerResponse) => { const url = req.url || '' // Consume POST/PUT body let body = '' req.on('data', (chunk: Buffer) => { body += chunk.toString() }) req.on('end', () => { if (url.includes('/items') && req.method === 'POST') { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ id: 1, status: 'created' })) } else if (url.includes('/bad-items') && req.method === 'POST') { res.writeHead(400, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ error: { message: 'Bad request' } })) } else { res.writeHead(404) res.end() } }) }, ) it('should send POST with default method when not specified', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.sendApi<{ id: number; status: string }>( 'items', { body: { name: 'test' } }, ) expect(result).toEqual({ id: 1, status: 'created' }) }) it('should return success result in non-throwing mode', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = (await client.sendApi<{ id: number; status: string }>( 'items', { body: { name: 'test' }, throws: false, }, )) as SocketSdkGenericResult<{ id: number; status: string }> expect(result.success).toBe(true) if (!result.success) { return } expect(result.data).toEqual({ id: 1, status: 'created' }) expect(result.status).toBe(200) }) it('should throw on error when throws=true (default)', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) await expect( client.sendApi('bad-items', { body: { bad: true } }), ).rejects.toThrow() }) it('should return error result in non-throwing mode on failure', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = (await client.sendApi('bad-items', { body: {}, throws: false, })) as SocketSdkGenericResult expect(result.success).toBe(false) }) }) // --------------------------------------------------------------------------- // Additional method success paths (prevent coverage regression) // --------------------------------------------------------------------------- describe('SocketSdk - additional method coverage', () => { const getBaseUrl = setupLocalHttpServer( (req: IncomingMessage, res: ServerResponse) => { const url = req.url || '' let body = '' req.on('data', (chunk: Buffer) => { body += chunk.toString() }) req.on('end', () => { if (url.includes('/export/openvex/')) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ document: { '@context': 'openvex' } })) } else if ( url.includes('/full-scans/') && url.includes('/rescan') && req.method === 'POST' ) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ id: 'scan-456', status: 'pending' })) } else if (url.includes('/entitlements')) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end( JSON.stringify({ items: [ { enabled: true, key: 'feature-a' }, { enabled: false, key: 'feature-b' }, { enabled: true, key: 'feature-c' }, { enabled: true, key: '' }, ], }), ) } else if (url.includes('/alert-full-scan-search')) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ results: [{ id: 'alert-1' }] })) } else { res.writeHead(404) res.end() } }) }, ) it('should export OpenVEX successfully', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.exportOpenVEX('test-org', 'vex-123') expect(result.success).toBe(true) if (!result.success) { return } expect(result.data).toBeDefined() }) it('should rescan a full scan', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.rescanFullScan('test-org', 'scan-123') expect(result.success).toBe(true) if (!result.success) { return } expect(result.data).toBeDefined() }) it('should get enabled entitlements filtered', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getEnabledEntitlements('test-org') expect(result).toEqual(['feature-a', 'feature-c']) }) it('should get full scan alerts', async () => { const client = new SocketSdk('test-token', { baseUrl: `${getBaseUrl()}/v0/`, retries: 0, }) const result = await client.getOrgAlertFullScans('test-org', { alertKey: 'malware', }) expect(result.success).toBe(true) if (!result.success) { return } expect(result.data).toBeDefined() }) })