import { describe, expect, it } from 'vitest'; import { HttpRequest, HttpResponse, PartialBy, ThymianFormat, type ThymianHttpRequest, ThymianHttpResponse, } from '../../src'; describe('ThymianFormat', () => { const transactions: [ PartialBy, PartialBy, ][] = [ [ { type: 'http-request', host: 'localhost', port: 8080, protocol: 'http', path: '/hello', method: 'get', headers: { 'x-api-key': { required: true, schema: { type: 'string', }, style: { explode: false, style: 'label', }, }, }, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', label: '', }, { type: 'http-response', headers: {}, mediaType: 'text/plain', statusCode: 200, }, ], ]; it('should be identical for identical sources', () => { const format1 = new ThymianFormat(); const format2 = new ThymianFormat(); for (const transaction of transactions) { format1.addHttpTransaction(transaction[0], transaction[1], ''); format2.addHttpTransaction(transaction[0], transaction[1], ''); } expect(format1.export()).toMatchObject(format2.export()); }); it('should not be identically for different sources', () => { const format1 = new ThymianFormat(); const format2 = new ThymianFormat(); for (const transaction of transactions) { format1.addHttpTransaction(transaction[0], transaction[1], ''); format2.addHttpTransaction( transaction[0], { ...transaction[1], mediaType: 'application/json', }, '', ); } expect(format1.export()).not.toMatchObject(format2.export()); }); it('should produce same hash for identical sources', () => { const format1 = new ThymianFormat(); const format2 = new ThymianFormat(); for (const transaction of transactions) { format1.addHttpTransaction(transaction[0], transaction[1], ''); format2.addHttpTransaction(transaction[0], transaction[1], ''); } expect(format1.toHash()).toBe(format2.toHash()); }); it('should not produce same hash for different sources', () => { const format1 = new ThymianFormat(); const format2 = new ThymianFormat(); for (const transaction of transactions) { format1.addHttpTransaction(transaction[0], transaction[1], ''); format2.addHttpTransaction( transaction[0], { ...transaction[1], mediaType: 'application/json', }, '', ); } expect(format1.toHash()).not.toBe(format2.toHash()); }); describe('fromHttpTransactions', () => { it('should create a valid ThymianFormat graph for given HTTP transactions', () => { const transactions: [HttpRequest, HttpResponse][] = [ [ { origin: 'https://api.example.com', path: '/users', method: 'GET' }, { statusCode: 200, headers: {}, body: '[]', trailers: {}, duration: 50, }, ], [ { origin: 'https://api.example.com', path: '/posts', method: 'POST' }, { statusCode: 201, headers: {}, body: '{}', trailers: {}, duration: 75, }, ], ]; const thymianFormat = ThymianFormat.fromHttpTransactions( transactions, '', ); expect(thymianFormat).toBeInstanceOf(ThymianFormat); const httpTransactions = thymianFormat.getThymianHttpTransactions(); expect(httpTransactions.length).toBe(2); }); it('should handle invalid transactions gracefully', () => { const transactions = [ // Missing response in a transaction [ { origin: 'https://api.example.com', path: '/users', method: 'GET' }, null, ], ]; expect(() => ThymianFormat.fromHttpTransactions( transactions as [HttpRequest, HttpResponse][], '', ), ).toThrow(); }); }); describe('matchTransaction', () => { function createParameterizedFormat() { const format = new ThymianFormat(); const [, , transactionId] = format.addHttpTransaction( { type: 'http-request', host: 'api.example.com', port: 443, protocol: 'https', path: '/users/{id}', method: 'GET', headers: {}, queryParameters: {}, cookies: {}, pathParameters: { id: { name: 'id', in: 'path', required: true, schema: { type: 'integer', }, }, }, mediaType: '', label: '', }, { type: 'http-response', headers: { 'content-type': { required: true, schema: { type: 'string', }, style: { explode: false, style: 'simple', }, }, }, mediaType: 'application/json', statusCode: 200, }, 'test-source', ); return { format, transactionId }; } it('should match a transaction by templated path while keeping other constraints', () => { const { format, transactionId } = createParameterizedFormat(); const result = format.matchTransaction( { origin: 'https://api.example.com', path: '/users/not-a-number', method: 'get', headers: {}, }, { statusCode: 200, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeDefined(); expect(result?.[0]).toBe(transactionId); }); it('should still match exact paths', () => { const { format, transactionId } = createParameterizedFormat(); const result = format.matchTransaction( { origin: 'https://api.example.com', path: '/users/{id}', method: 'get', headers: {}, }, { statusCode: 200, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeDefined(); expect(result?.[0]).toBe(transactionId); }); it('should not match a transaction when the method differs', () => { const { format } = createParameterizedFormat(); const result = format.matchTransaction( { origin: 'https://api.example.com', path: '/users/123', method: 'post', headers: {}, }, { statusCode: 200, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeUndefined(); }); it('should ignore charset parameter', () => { const format = new ThymianFormat(); const [, , transactionId] = format.addHttpTransaction( { cookies: {}, headers: {}, host: 'localhost', label: '', mediaType: '', method: 'get', path: '/api/users', pathParameters: {}, port: 3000, protocol: 'http', queryParameters: {}, sourceName: '', type: 'http-request', }, { headers: {}, label: '', mediaType: 'application/json', sourceName: '', statusCode: 200, type: 'http-response', schema: { type: 'object', properties: { name: { type: 'string' } } }, }, '', ); const [matchedTransactionID] = format.matchTransaction( { method: 'get', origin: 'http://localhost:3000', path: '/api/users', headers: {}, body: undefined, }, { statusCode: 200, headers: { 'content-type': 'application/json; charset=utf-8', }, body: '{"name": "matthyk"}', bodyEncoding: undefined, trailers: {}, duration: 0, }, ) ?? []; expect(matchedTransactionID).toEqual(transactionId); }); it('should match a transaction with a different origin when it is the only candidate', () => { const { format, transactionId } = createParameterizedFormat(); const result = format.matchTransaction( { origin: 'https://other.example.com', path: '/users/123', method: 'get', headers: {}, }, { statusCode: 200, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeDefined(); expect(result?.[0]).toBe(transactionId); }); it('should not match a transaction when the response status differs', () => { const { format } = createParameterizedFormat(); const result = format.matchTransaction( { origin: 'https://api.example.com', path: '/users/123', method: 'get', headers: {}, }, { statusCode: 404, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeUndefined(); }); it('should not match a transaction when the request media type differs', () => { const format = new ThymianFormat(); format.addHttpTransaction( { type: 'http-request', host: 'api.example.com', port: 443, protocol: 'https', path: '/users/{id}', method: 'POST', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: 'application/json', label: '', }, { type: 'http-response', headers: {}, mediaType: 'application/json', statusCode: 200, }, 'test-source', ); const result = format.matchTransaction( { origin: 'https://api.example.com', path: '/users/123', method: 'post', headers: { 'content-type': 'text/plain', }, }, { statusCode: 200, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeUndefined(); }); it('should not match a transaction when the response media type differs', () => { const { format } = createParameterizedFormat(); const result = format.matchTransaction( { origin: 'https://api.example.com', path: '/users/123', method: 'get', headers: {}, }, { statusCode: 200, headers: { 'content-type': 'text/plain', }, trailers: {}, duration: 42, }, ); expect(result).toBeUndefined(); }); it('should not match when origin fallback produces multiple candidates', () => { const format = new ThymianFormat(); format.addHttpTransaction( { type: 'http-request', host: 'api-a.example.com', port: 443, protocol: 'https', path: '/users/{id}', method: 'GET', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', label: '', }, { type: 'http-response', headers: {}, mediaType: 'application/json', statusCode: 200, }, 'test-source-a', ); format.addHttpTransaction( { type: 'http-request', host: 'api-b.example.com', port: 443, protocol: 'https', path: '/users/{id}', method: 'GET', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', label: '', }, { type: 'http-response', headers: {}, mediaType: 'application/json', statusCode: 200, }, 'test-source-b', ); const result = format.matchTransaction( { origin: 'https://other.example.com', path: '/users/123', method: 'get', headers: {}, }, { statusCode: 200, headers: { 'content-type': 'application/json', }, trailers: {}, duration: 42, }, ); expect(result).toBeUndefined(); }); }); describe('filter', () => { it('should filter transactions based on expression', () => { const format = new ThymianFormat(); format.addHttpTransaction( { ...transactions[0][0], method: 'get' }, transactions[0][1], 'source1', ); format.addHttpTransaction( { ...transactions[0][0], method: 'post' }, transactions[0][1], 'source1', ); const filtered = format.filter({ type: 'method', method: 'get', kind: 'request', }); expect(filtered).toBeInstanceOf(ThymianFormat); expect(filtered.getThymianHttpTransactions().length).toBe(1); expect(filtered.getThymianHttpTransactions()[0].thymianReq.method).toBe( 'get', ); }); it('should filter using callback function', () => { const format = new ThymianFormat(); format.addHttpTransaction( { ...transactions[0][0], method: 'get' }, transactions[0][1], 'source1', ); format.addHttpTransaction( { ...transactions[0][0], method: 'post' }, transactions[0][1], 'source1', ); const filtered = format.filter((transaction) => { return transaction.thymianReq.method === 'get'; }); expect(filtered.getThymianHttpTransactions().length).toBe(1); }); it('should remove orphaned nodes after filtering', () => { const format = new ThymianFormat(); const [reqId1] = format.addHttpTransaction( { ...transactions[0][0], method: 'get' }, transactions[0][1], 'source1', ); const [reqId2] = format.addHttpTransaction( { ...transactions[0][0], method: 'post' }, transactions[0][1], 'source1', ); const filtered = format.filter({ type: 'method', method: 'get', kind: 'request', }); expect(filtered.graph.hasNode(reqId1)).toBe(true); expect(filtered.graph.hasNode(reqId2)).toBe(false); }); it('should return empty format when no transactions match', () => { const format = new ThymianFormat(); format.addHttpTransaction( { ...transactions[0][0], method: 'get' }, transactions[0][1], 'source1', ); const filtered = format.filter({ type: 'method', method: 'delete', kind: 'request', }); expect(filtered.getThymianHttpTransactions().length).toBe(0); }); it('should return new ThymianFormat instance', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const filtered = format.filter({ type: 'method', method: 'get', kind: 'request', }); expect(filtered).not.toBe(format); expect(filtered).toBeInstanceOf(ThymianFormat); }); }); describe('getThymianHttpRequestsWithResponses', () => { it('should return requests with their responses', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const result = format.getThymianHttpRequestsWithResponses(); expect(result.length).toBe(1); expect(result[0][0]).toMatchObject( expect.objectContaining({ type: 'http-request' }), ); expect(result[0][1].length).toBe(1); expect(result[0][1][0]).toMatchObject( expect.objectContaining({ type: 'http-response' }), ); }); it('should include request and response IDs', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const result = format.getThymianHttpRequestsWithResponses(); expect(result[0][2]).toBeDefined(); expect(result[0][3]).toHaveLength(1); expect(typeof result[0][2]).toBe('string'); expect(typeof result[0][3][0]).toBe('string'); }); it('should return empty array for empty format', () => { const format = new ThymianFormat(); const result = format.getThymianHttpRequestsWithResponses(); expect(result).toEqual([]); }); }); describe('getThymianHttpRequestsWithIds', () => { it('should return requests with their IDs', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const result = format.getThymianHttpRequestsWithIds(); expect(result.length).toBe(1); expect(result[0][0]).toMatchObject( expect.objectContaining({ type: 'http-request' }), ); expect(typeof result[0][1]).toBe('string'); }); it('should return empty array for empty format', () => { const format = new ThymianFormat(); const result = format.getThymianHttpRequestsWithIds(); expect(result).toEqual([]); }); }); describe('getThymianHttpRequests', () => { it('should return all HTTP requests', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); format.addHttpTransaction( { ...transactions[0][0], method: 'post' }, transactions[0][1], 'source1', ); const result = format.getThymianHttpRequests(); expect(result.length).toBe(2); result.forEach((req) => { expect(req.type).toBe('http-request'); }); }); it('should return empty array for empty format', () => { const format = new ThymianFormat(); const result = format.getThymianHttpRequests(); expect(result).toEqual([]); }); }); describe('getThymianHttpTransactionById', () => { it('should return transaction by ID', () => { const format = new ThymianFormat(); const [, , transactionId] = format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const result = format.getThymianHttpTransactionById(transactionId); expect(result).toBeDefined(); expect(result?.transactionId).toBe(transactionId); expect(result?.thymianReq.type).toBe('http-request'); expect(result?.thymianRes.type).toBe('http-response'); }); it('should return undefined for non-existent ID', () => { const format = new ThymianFormat(); const result = format.getThymianHttpTransactionById('non-existent-id'); expect(result).toBeUndefined(); }); }); describe('toHash', () => { it('should generate consistent hash for same format', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const hash1 = format.toHash(); const hash2 = format.toHash(); expect(hash1).toBe(hash2); }); it('should generate different hashes for different formats', () => { const format1 = new ThymianFormat(); format1.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const format2 = new ThymianFormat(); format2.addHttpTransaction( { ...transactions[0][0], method: 'post' }, transactions[0][1], 'source1', ); expect(format1.toHash()).not.toBe(format2.toHash()); }); it('should ignore labels, source names, and source locations', () => { const format1 = new ThymianFormat(); const [reqId1, resId1, transactionId1] = format1.addHttpTransaction( transactions[0][0], transactions[0][1], '/Users/alice/project/openapi.yaml', ); format1.graph.setNodeAttribute(reqId1, 'label', 'Alice request label'); format1.graph.setNodeAttribute(resId1, 'label', 'Alice response label'); format1.graph.setNodeAttribute(reqId1, 'sourceLocation', { path: '/Users/alice/project/openapi.yaml', position: { line: 1, column: 2, offset: 3 }, }); format1.graph.setNodeAttribute(resId1, 'sourceLocation', { path: '/Users/alice/project/openapi.yaml', position: { line: 4, column: 5, offset: 6 }, }); format1.graph.setEdgeAttribute( transactionId1, 'label', 'Alice transaction label', ); format1.graph.setEdgeAttribute(transactionId1, 'sourceLocation', { path: '/Users/alice/project/openapi.yaml', position: { line: 7, column: 8, offset: 9 }, }); const format2 = new ThymianFormat(); const [reqId2, resId2, transactionId2] = format2.addHttpTransaction( transactions[0][0], transactions[0][1], 'C:\\Users\\bob\\project\\openapi.yaml', ); format2.graph.setNodeAttribute(reqId2, 'label', 'Bob request label'); format2.graph.setNodeAttribute(resId2, 'label', 'Bob response label'); format2.graph.setNodeAttribute(reqId2, 'sourceLocation', { path: 'C:\\Users\\bob\\project\\openapi.yaml', position: { line: 10, column: 11, offset: 12 }, }); format2.graph.setNodeAttribute(resId2, 'sourceLocation', { path: 'C:\\Users\\bob\\project\\openapi.yaml', position: { line: 13, column: 14, offset: 15 }, }); format2.graph.setEdgeAttribute( transactionId2, 'label', 'Bob transaction label', ); format2.graph.setEdgeAttribute(transactionId2, 'sourceLocation', { path: 'C:\\Users\\bob\\project\\openapi.yaml', position: { line: 16, column: 17, offset: 18 }, }); expect(format1.toHash()).toBe(format2.toHash()); expect(format1.export().attributes.hash).toBe( format2.export().attributes.hash, ); }); it('should generate stable IDs for labels, source names, and source locations', () => { const format1 = new ThymianFormat(); const ids1 = format1.addHttpTransaction( { ...transactions[0][0], label: 'Request label A', sourceLocation: { path: '/Users/alice/project/openapi.yaml' }, }, { ...transactions[0][1], label: 'Response label A', sourceLocation: { path: '/Users/alice/project/openapi.yaml' }, }, '/Users/alice/project/openapi.yaml', ); const format2 = new ThymianFormat(); const ids2 = format2.addHttpTransaction( { ...transactions[0][0], label: 'Request label B', sourceLocation: { path: 'C:\\Users\\bob\\project\\openapi.yaml' }, }, { ...transactions[0][1], label: 'Response label B', sourceLocation: { path: 'C:\\Users\\bob\\project\\openapi.yaml' }, }, 'C:\\Users\\bob\\project\\openapi.yaml', ); expect(ids1).toStrictEqual(ids2); }); it('should recursively ignore labels, source names, and source locations', () => { const format1 = new ThymianFormat(); format1.addRequest({ ...transactions[0][0], sourceName: 'source1', extensions: { metadata: { sourceName: '/Users/alice/project/openapi.yaml', sourceLocation: { path: '/Users/alice/project/openapi.yaml' }, label: 'Alice metadata label', }, }, }); const format2 = new ThymianFormat(); format2.addRequest({ ...transactions[0][0], sourceName: 'source2', extensions: { metadata: { sourceName: 'C:\\Users\\bob\\project\\openapi.yaml', sourceLocation: { path: 'C:\\Users\\bob\\project\\openapi.yaml' }, label: 'Bob metadata label', }, }, }); expect(format1.toHash()).toBe(format2.toHash()); }); it('should ignore source attribution on imported formats', () => { const format = new ThymianFormat(); format.addHttpTransaction( transactions[0][0], transactions[0][1], '/Users/alice/project/openapi.yaml', ); const exported1 = format.export(); const exported2 = structuredClone(exported1); exported1.nodes.forEach((node) => { node.attributes.sourceName = '/Users/alice/project/openapi.yaml'; node.attributes.sourceLocation = { path: '/Users/alice/project/openapi.yaml', }; }); exported1.edges.forEach((edge) => { edge.attributes.sourceName = '/Users/alice/project/openapi.yaml'; edge.attributes.sourceLocation = { path: '/Users/alice/project/openapi.yaml', }; }); exported2.nodes.forEach((node) => { node.attributes.sourceName = 'C:\\Users\\bob\\project\\openapi.yaml'; node.attributes.sourceLocation = { path: 'C:\\Users\\bob\\project\\openapi.yaml', }; }); exported2.edges.forEach((edge) => { edge.attributes.sourceName = 'C:\\Users\\bob\\project\\openapi.yaml'; edge.attributes.sourceLocation = { path: 'C:\\Users\\bob\\project\\openapi.yaml', }; }); expect(ThymianFormat.import(exported1).toHash()).toBe( ThymianFormat.import(exported2).toHash(), ); }); it('should include security schemes and HTTP links in semantic hashes', () => { const format1 = new ThymianFormat(); const [reqId1, resId1] = format1.addHttpTransaction( transactions[0][0], transactions[0][1], '/Users/alice/project/openapi.yaml', ); const securityId1 = format1.addSecurityScheme({ scheme: 'api-key', in: 'header', sourceName: '/Users/alice/project/openapi.yaml', sourceLocation: { path: '/Users/alice/project/openapi.yaml' }, }); format1.addEdge(reqId1, securityId1, { type: 'is-secured', sourceName: '/Users/alice/project/openapi.yaml', sourceLocation: { path: '/Users/alice/project/openapi.yaml' }, }); format1.addHttpLink(resId1, reqId1, { sourceName: '/Users/alice/project/openapi.yaml', sourceLocation: { path: '/Users/alice/project/openapi.yaml' }, }); const format2 = new ThymianFormat(); const [reqId2, resId2] = format2.addHttpTransaction( transactions[0][0], transactions[0][1], 'C:\\Users\\bob\\project\\openapi.yaml', ); const securityId2 = format2.addSecurityScheme({ scheme: 'api-key', in: 'header', sourceName: 'C:\\Users\\bob\\project\\openapi.yaml', sourceLocation: { path: 'C:\\Users\\bob\\project\\openapi.yaml' }, }); format2.addEdge(reqId2, securityId2, { type: 'is-secured', sourceName: 'C:\\Users\\bob\\project\\openapi.yaml', sourceLocation: { path: 'C:\\Users\\bob\\project\\openapi.yaml' }, }); format2.addHttpLink(resId2, reqId2, { sourceName: 'C:\\Users\\bob\\project\\openapi.yaml', sourceLocation: { path: 'C:\\Users\\bob\\project\\openapi.yaml' }, }); expect(format1.toHash()).toBe(format2.toHash()); const format3 = new ThymianFormat(); const [reqId3] = format3.addHttpTransaction( transactions[0][0], transactions[0][1], '/Users/alice/project/openapi.yaml', ); const securityId3 = format3.addSecurityScheme({ scheme: 'api-key', in: 'query', sourceName: '/Users/alice/project/openapi.yaml', }); format3.addEdge(reqId3, securityId3, { type: 'is-secured', sourceName: '/Users/alice/project/openapi.yaml', }); expect(format1.toHash()).not.toBe(format3.toHash()); }); }); describe('import with sourceName', () => { it('should set sourceName on nodes when not present in import', () => { const format1 = new ThymianFormat(); format1.addHttpTransaction( transactions[0][0], transactions[0][1], 'original', ); const exported = format1.export(); const format2 = ThymianFormat.import(exported, 'new-source'); const requests = format2.getThymianHttpRequests(); requests.forEach((req) => { expect(req.sourceName).toBe('original'); }); }); it('should preserve existing sourceName in import', () => { const format1 = new ThymianFormat(); format1.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const exported = format1.export(); const format2 = ThymianFormat.import(exported); const requests = format2.getThymianHttpRequests(); requests.forEach((req) => { expect(req.sourceName).toBe('source1'); }); }); }); describe('addNode with deterministic IDs', () => { it('should not create duplicate nodes with same content', () => { const format = new ThymianFormat(); const node = { type: 'http-request' as const, host: 'localhost', port: 8080, protocol: 'http' as const, path: '/test', method: 'get', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', label: 'GET http://localhost:8080/test', sourceName: 'source1', }; const id1 = format.addNode(node); const id2 = format.addNode(node); expect(id1).toBe(id2); expect(format.graph.order).toBe(1); }); it('should throw error when throwIfExists is true', () => { const format = new ThymianFormat(); const node = { type: 'http-request' as const, host: 'localhost', port: 8080, protocol: 'http' as const, path: '/test', method: 'get', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', label: 'GET http://localhost:8080/test', sourceName: 'source1', }; format.addNode(node); expect(() => format.addNode(node, undefined, { throwIfExists: true }), ).toThrow(); }); }); describe('addEdge with deterministic IDs', () => { it('should not create duplicate edges with same content', () => { const format = new ThymianFormat(); const [reqId, resId] = format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const edge = { type: 'http-link' as const, sourceName: 'source1', }; const edgeId1 = format.addEdge(reqId, resId, edge); const edgeId2 = format.addEdge(reqId, resId, edge); expect(edgeId1).toBe(edgeId2); }); it('should throw error when throwIfExists is true', () => { const format = new ThymianFormat(); const [reqId, resId] = format.addHttpTransaction( transactions[0][0], transactions[0][1], 'source1', ); const edge = { type: 'http-link' as const, sourceName: 'source1', }; format.addEdge(reqId, resId, edge); expect(() => format.addEdge(reqId, resId, edge, { throwIfExists: true }), ).toThrow(); }); }); describe('addResponseToRequest', () => { it('should create unique response nodes for identical responses on different requests', () => { const format = new ThymianFormat(); const request1: PartialBy = { type: 'http-request', host: 'localhost', port: 8080, protocol: 'http', path: '/endpoint-a', method: 'get', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', }; const request2: PartialBy = { type: 'http-request', host: 'localhost', port: 8080, protocol: 'http', path: '/endpoint-b', method: 'get', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', }; const identicalResponse: PartialBy< ThymianHttpResponse, 'label' | 'sourceName' > = { type: 'http-response', headers: {}, mediaType: '', statusCode: 200, description: 'OK', }; const reqId1 = format.addRequest({ ...request1, sourceName: 'source1' }); const reqId2 = format.addRequest({ ...request2, sourceName: 'source1' }); const [resId1] = format.addResponseToRequest(reqId1, { ...identicalResponse, sourceName: 'source1', }); const [resId2] = format.addResponseToRequest(reqId2, { ...identicalResponse, sourceName: 'source1', }); // Response IDs should be different even though response content is identical expect(resId1).not.toBe(resId2); // Both response nodes should exist in the graph expect(format.graph.hasNode(resId1)).toBe(true); expect(format.graph.hasNode(resId2)).toBe(true); // Total nodes should be 4 (2 requests + 2 responses) expect(format.graph.order).toBe(4); }); it('should throw error when adding response to non-existent request', () => { const format = new ThymianFormat(); const response: PartialBy = { type: 'http-response', headers: {}, mediaType: '', statusCode: 200, sourceName: 'source1', }; expect(() => { format.addResponseToRequest('non-existent-request-id', response); }).toThrow(); }); it('should return both response ID and transaction ID', () => { const format = new ThymianFormat(); const request: PartialBy = { type: 'http-request', host: 'localhost', port: 8080, protocol: 'http', path: '/test', method: 'get', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: '', sourceName: 'source1', }; const response: PartialBy = { type: 'http-response', headers: {}, mediaType: '', statusCode: 200, sourceName: 'source1', }; const reqId = format.addRequest(request); const [resId, transactionId] = format.addResponseToRequest( reqId, response, ); // Should return valid IDs expect(typeof resId).toBe('string'); expect(typeof transactionId).toBe('string'); expect(resId.length).toBeGreaterThan(0); expect(transactionId.length).toBeGreaterThan(0); // Response and transaction should exist in graph expect(format.graph.hasNode(resId)).toBe(true); expect(format.graph.hasEdge(transactionId)).toBe(true); // Transaction should connect request to response const [sourceId, targetId] = format.graph.extremities(transactionId); expect(sourceId).toBe(reqId); expect(targetId).toBe(resId); }); }); describe('addHttpTransaction', () => { it('should create transaction using addResponseToRequest internally', () => { const format = new ThymianFormat(); const request: PartialBy = { type: 'http-request', host: 'localhost', port: 8080, protocol: 'http', path: '/test', method: 'post', headers: {}, queryParameters: {}, cookies: {}, pathParameters: {}, mediaType: 'application/json', }; const response: PartialBy = { type: 'http-response', headers: {}, mediaType: 'application/json', statusCode: 201, }; const [reqId, resId, transactionId] = format.addHttpTransaction( request, response, 'test-source', ); // All IDs should be valid expect(typeof reqId).toBe('string'); expect(typeof resId).toBe('string'); expect(typeof transactionId).toBe('string'); // All nodes and edges should exist expect(format.graph.hasNode(reqId)).toBe(true); expect(format.graph.hasNode(resId)).toBe(true); expect(format.graph.hasEdge(transactionId)).toBe(true); // Verify the transaction connects request to response const transaction = format.getThymianHttpTransactionById(transactionId); expect(transaction).toBeDefined(); expect(transaction?.thymianReqId).toBe(reqId); expect(transaction?.thymianResId).toBe(resId); }); it('should properly inherit sourceName in response when using addHttpTransaction', () => { const format = new ThymianFormat(); const [reqId, resId] = format.addHttpTransaction( transactions[0][0], transactions[0][1], 'custom-source', ); const req = format.getNode(reqId); const res = format.getNode(resId); expect(req?.sourceName).toBe('custom-source'); expect(res?.sourceName).toBe('custom-source'); }); }); });