import { Buffer } from 'node:buffer' import { expectAssignable, expectNotAssignable, expectType } from 'tsd' import { Agent, Dispatcher, MockAgent, SnapshotAgent, setGlobalDispatcher } from '../..' import { SnapshotRecorder } from '../../types/snapshot-agent' // Basic constructor tests expectAssignable(new SnapshotAgent()) expectAssignable(new SnapshotAgent({})) expectAssignable(new SnapshotAgent({ mode: 'record' })) expectAssignable(new SnapshotAgent({ mode: 'playback' })) expectAssignable(new SnapshotAgent({ mode: 'update' })) expectAssignable(new SnapshotAgent({ snapshotPath: './snapshots.json' })) // Constructor with basic options expectAssignable(new SnapshotAgent({ mode: 'record', snapshotPath: './snapshots.json', maxSnapshots: 100, autoFlush: true, flushInterval: 5000 })) // Constructor with matching options expectAssignable(new SnapshotAgent({ mode: 'playback', snapshotPath: './snapshots.json', matchHeaders: ['content-type', 'accept'], ignoreHeaders: ['authorization', 'x-api-key'], excludeHeaders: ['set-cookie', 'authorization'], matchBody: true, normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), matchQuery: false, normalizeQuery: (query: URLSearchParams) => query.toString(), caseSensitive: false })) // Constructor with all options combined expectAssignable(new SnapshotAgent({ mode: 'update', snapshotPath: './snapshots.json', maxSnapshots: 50, autoFlush: true, flushInterval: 10000, matchHeaders: ['content-type'], ignoreHeaders: ['user-agent'], excludeHeaders: ['cookie'], matchBody: false, normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), matchQuery: true, normalizeQuery: (query: URLSearchParams) => query.toString(), caseSensitive: true, connections: 5, // MockAgent option enableCallHistory: true // MockAgent option })) // SnapshotAgent extends MockAgent expectAssignable(new SnapshotAgent()) expectAssignable(new SnapshotAgent()) { const snapshotAgent = new SnapshotAgent() expectAssignable(setGlobalDispatcher(snapshotAgent)) // Original snapshot methods expectType>(snapshotAgent.saveSnapshots()) expectType>(snapshotAgent.saveSnapshots('./custom.json')) expectType>(snapshotAgent.loadSnapshots()) expectType>(snapshotAgent.loadSnapshots('./custom.json')) expectType(snapshotAgent.getRecorder()) expectType(snapshotAgent.getMode()) expectType(snapshotAgent.clearSnapshots()) // New snapshot management methods expectType(snapshotAgent.resetCallCounts()) expectType(snapshotAgent.deleteSnapshot({})) expectType(snapshotAgent.getSnapshotInfo({})) expectType(snapshotAgent.replaceSnapshots([])) // Inherited MockAgent methods expectType>(snapshotAgent.close()) expectType(snapshotAgent.deactivate()) expectType(snapshotAgent.activate()) expectType(snapshotAgent.enableNetConnect()) expectType(snapshotAgent.disableNetConnect()) } { // Constructor with agent option const agent = new Agent() const snapshotAgent = new SnapshotAgent({ mode: 'record', agent, maxSnapshots: 25 }) expectAssignable(snapshotAgent) } { // SnapshotRecorder standalone usage - basic options const recorder = new SnapshotRecorder() expectType>(recorder.record({}, {})) expectType(recorder.findSnapshot({})) expectType>(recorder.loadSnapshots()) expectType>(recorder.loadSnapshots('./file.json')) expectType>(recorder.saveSnapshots()) expectType>(recorder.saveSnapshots('./file.json')) expectType(recorder.clear()) expectType(recorder.getSnapshots()) expectType(recorder.size()) // New methods expectType(recorder.resetCallCounts()) expectType(recorder.deleteSnapshot({})) expectType(recorder.getSnapshotInfo({})) expectType(recorder.replaceSnapshots([])) expectType(recorder.destroy()) } { // SnapshotRecorder with full options const recorder = new SnapshotRecorder({ snapshotPath: './test.json', mode: 'record', maxSnapshots: 100, autoFlush: true, flushInterval: 30000, matchHeaders: ['accept', 'content-type'], ignoreHeaders: ['user-agent'], excludeHeaders: ['authorization', 'cookie'], matchBody: true, normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), matchQuery: true, normalizeQuery: (query: URLSearchParams) => query.toString(), caseSensitive: false }) expectAssignable(recorder) } // Test Options interfaces completeness expectAssignable({}) expectAssignable({ snapshotPath: './test.json' }) expectAssignable({ mode: 'playback', maxSnapshots: 50 }) expectAssignable({ autoFlush: true, flushInterval: 15000 }) expectAssignable({ matchHeaders: ['content-type'], ignoreHeaders: ['authorization'], excludeHeaders: ['set-cookie'] }) expectAssignable({ matchBody: false, normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), matchQuery: true, normalizeQuery: (query: URLSearchParams) => query.toString(), caseSensitive: true }) expectAssignable({}) expectAssignable({ mode: 'update', snapshotPath: './snapshots.json', connections: 2 // MockAgent option }) { // New snapshot structure with responses array const snapshot: SnapshotRecorder.Snapshot = { request: { method: 'GET', url: 'https://api.example.com/test', headers: { authorization: 'Bearer token' }, body: '{"test": "data"}' }, responses: [{ statusCode: 200, headers: { 'content-type': 'application/json' }, body: 'eyJ0ZXN0IjoiZGF0YSJ9', // base64 trailers: {} }], callCount: 0, timestamp: '2024-01-01T00:00:00.000Z' } expectType(snapshot.request.method) expectType(snapshot.request.url) expectType>(snapshot.request.headers) expectType(snapshot.request.body) expectType body: string trailers: Record }>>(snapshot.responses) expectType(snapshot.callCount) expectType(snapshot.timestamp) } { // Sequential responses support const sequentialSnapshot: SnapshotRecorder.Snapshot = { request: { method: 'POST', url: 'https://api.example.com/sequence', headers: { 'content-type': 'application/json' } }, responses: [ { statusCode: 201, headers: { 'content-type': 'application/json' }, body: '{"id": 1}', trailers: {} }, { statusCode: 200, headers: { 'content-type': 'application/json' }, body: '{"id": 1, "status": "updated"}', trailers: {} }, { statusCode: 204, headers: {}, body: '', trailers: {} } ], callCount: 2, timestamp: '2024-01-01T00:00:00.000Z' } expectAssignable(sequentialSnapshot) } { // SnapshotInfo interface test const info: SnapshotRecorder.SnapshotInfo = { hash: 'abc123def456', request: { method: 'PUT', url: 'https://api.example.com/update', headers: { 'content-type': 'application/json' }, body: '{"data": "value"}' }, responseCount: 3, callCount: 1, timestamp: '2024-01-01T00:00:00.000Z' } expectType(info.hash) expectType<{ method: string url: string headers: Record body?: string }>(info.request) expectType(info.responseCount) expectType(info.callCount) expectType(info.timestamp) } { // SnapshotData interface for replacement const snapshotData: SnapshotRecorder.SnapshotData = { hash: 'replacement-hash', snapshot: { request: { method: 'DELETE', url: 'https://api.example.com/delete/123', headers: { authorization: 'Bearer token' } }, responses: [{ statusCode: 204, headers: {}, body: '', trailers: {} }], callCount: 0, timestamp: '2024-01-01T00:00:00.000Z' } } expectType(snapshotData.hash) expectType(snapshotData.snapshot) } { // Array operations for replaceSnapshots const snapshotDataArray: SnapshotRecorder.SnapshotData[] = [ { hash: 'hash1', snapshot: { request: { method: 'GET', url: 'https://example.com/1', headers: {} }, responses: [{ statusCode: 200, headers: {}, body: 'response1', trailers: {} }], callCount: 0, timestamp: '2024-01-01T00:00:00.000Z' } }, { hash: 'hash2', snapshot: { request: { method: 'POST', url: 'https://example.com/2', headers: {} }, responses: [ { statusCode: 201, headers: {}, body: 'created', trailers: {} }, { statusCode: 200, headers: {}, body: 'updated', trailers: {} } ], callCount: 1, timestamp: '2024-01-01T01:00:00.000Z' } } ] const agent = new SnapshotAgent() const recorder = new SnapshotRecorder() expectType(agent.replaceSnapshots(snapshotDataArray)) expectType(recorder.replaceSnapshots(snapshotDataArray)) } { // Test method parameter types const agent = new SnapshotAgent() const requestOpts = { origin: 'https://api.example.com', path: '/test', method: 'GET', headers: { accept: 'application/json' } } expectType(agent.deleteSnapshot(requestOpts)) expectType(agent.getSnapshotInfo(requestOpts)) const recorder = new SnapshotRecorder() expectType(recorder.deleteSnapshot(requestOpts)) expectType(recorder.getSnapshotInfo(requestOpts)) } { // Test optional body in request const snapshotWithoutBody: SnapshotRecorder.Snapshot = { request: { method: 'GET', url: 'https://api.example.com/get', headers: { accept: 'application/json' } // body is optional }, responses: [{ statusCode: 200, headers: { 'content-type': 'application/json' }, body: 'response-data', trailers: {} }], callCount: 0, timestamp: '2024-01-01T00:00:00.000Z' } expectAssignable(snapshotWithoutBody) } { // Test mode type constraints const recordAgent = new SnapshotAgent({ mode: 'record' }) const playbackAgent = new SnapshotAgent({ mode: 'playback', snapshotPath: './test.json' }) const updateAgent = new SnapshotAgent({ mode: 'update', snapshotPath: './test.json' }) expectType(recordAgent.getMode()) expectType(playbackAgent.getMode()) expectType(updateAgent.getMode()) } { // Test array types for header configuration const matchHeaders: string[] = ['content-type', 'accept', 'authorization'] const ignoreHeaders: string[] = ['user-agent', 'x-request-id'] const excludeHeaders: string[] = ['set-cookie', 'authorization'] expectAssignable({ matchHeaders, ignoreHeaders, excludeHeaders }) expectAssignable({ matchHeaders, ignoreHeaders, excludeHeaders }) } // Test boolean configuration options expectAssignable({ matchBody: true, normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), matchQuery: false, normalizeQuery: (query: URLSearchParams) => query.toString(), caseSensitive: true, autoFlush: false }) expectAssignable({ matchBody: false, normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), matchQuery: true, normalizeQuery: (query: URLSearchParams) => query.toString(), caseSensitive: false, autoFlush: true }) // Test number configuration options expectAssignable({ maxSnapshots: 100, flushInterval: 30000 }) expectAssignable({ maxSnapshots: 50, flushInterval: 15000 }) // Test filtering options expectAssignable({ shouldRecord: (requestOpts) => requestOpts.path.startsWith('/api'), shouldPlayback: (requestOpts) => !requestOpts.path.includes('admin'), excludeUrls: ['/health', /\/private\/.*/, /token=/] }) expectAssignable({ shouldRecord: (requestOpts) => requestOpts.method === 'GET', shouldPlayback: (requestOpts) => requestOpts.path !== '/forbidden', excludeUrls: ['secret', /admin/] }) // Test exact declared types for normalizeBody and normalizeQuery expectAssignable({ normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), normalizeQuery: (query: URLSearchParams) => query.toString() }) expectAssignable({ normalizeBody: (body: string | Buffer | null | undefined) => String(body ?? ''), normalizeQuery: (query: URLSearchParams) => query.toString() }) // normalizeBody and normalizeQuery must return string expectNotAssignable({ normalizeBody: (body: string | Buffer | null | undefined) => body }) expectNotAssignable({ normalizeQuery: (query: URLSearchParams) => query })