Initial commit: add .gitignore and README
This commit is contained in:
141
tests/unit/exports/containers/raw-iso-container.test.ts
Normal file
141
tests/unit/exports/containers/raw-iso-container.test.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* Unit tests for Raw ISO 20022 Container
|
||||
*/
|
||||
|
||||
import { RawISOContainer } from '@/exports/containers/raw-iso-container';
|
||||
import { ISOMessage, MessageType, MessageStatus } from '@/models/message';
|
||||
import { PaymentIdentityMap } from '@/exports/types';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
describe('RawISOContainer', () => {
|
||||
const createTestMessage = (): ISOMessage => {
|
||||
const uetr = uuidv4();
|
||||
return {
|
||||
id: uuidv4(),
|
||||
paymentId: uuidv4(),
|
||||
messageType: MessageType.PACS_008,
|
||||
uetr,
|
||||
msgId: 'MSG-12345',
|
||||
xmlContent: `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
|
||||
<FIToFICstmrCdtTrf>
|
||||
<GrpHdr>
|
||||
<MsgId>MSG-12345</MsgId>
|
||||
<CreDtTm>${new Date().toISOString()}</CreDtTm>
|
||||
</GrpHdr>
|
||||
<CdtTrfTxInf>
|
||||
<PmtId>
|
||||
<EndToEndId>E2E-123</EndToEndId>
|
||||
<UETR>${uetr}</UETR>
|
||||
</PmtId>
|
||||
<IntrBkSttlmAmt Ccy="USD">1000.00</IntrBkSttlmAmt>
|
||||
</CdtTrfTxInf>
|
||||
</FIToFICstmrCdtTrf>
|
||||
</Document>`,
|
||||
xmlHash: 'test-hash',
|
||||
status: MessageStatus.VALIDATED,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
};
|
||||
|
||||
describe('exportMessage', () => {
|
||||
it('should export ISO 20022 message without modification', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await RawISOContainer.exportMessage(message);
|
||||
|
||||
expect(exported).toContain('urn:iso:std:iso:20022');
|
||||
expect(exported).toContain('FIToFICstmrCdtTrf');
|
||||
expect(exported).toContain(message.uetr);
|
||||
});
|
||||
|
||||
it('should ensure UETR is present when ensureUETR is true', async () => {
|
||||
const message = createTestMessage();
|
||||
// Remove UETR from XML
|
||||
message.xmlContent = message.xmlContent.replace(/<UETR>.*?<\/UETR>/, '');
|
||||
|
||||
const identityMap: PaymentIdentityMap = {
|
||||
paymentId: message.paymentId,
|
||||
uetr: message.uetr,
|
||||
ledgerJournalIds: [],
|
||||
internalTransactionIds: [],
|
||||
};
|
||||
|
||||
const exported = await RawISOContainer.exportMessage(message, identityMap, {
|
||||
ensureUETR: true,
|
||||
});
|
||||
|
||||
expect(exported).toContain(message.uetr);
|
||||
});
|
||||
|
||||
it('should normalize line endings to LF by default', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await RawISOContainer.exportMessage(message, undefined, {
|
||||
lineEnding: 'LF',
|
||||
});
|
||||
|
||||
expect(exported).not.toContain('\r\n');
|
||||
});
|
||||
|
||||
it('should normalize line endings to CRLF when requested', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await RawISOContainer.exportMessage(message, undefined, {
|
||||
lineEnding: 'CRLF',
|
||||
});
|
||||
|
||||
expect(exported).toContain('\r\n');
|
||||
});
|
||||
});
|
||||
|
||||
describe('exportBatch', () => {
|
||||
it('should export multiple messages', async () => {
|
||||
const messages = [createTestMessage(), createTestMessage(), createTestMessage()];
|
||||
const exported = await RawISOContainer.exportBatch(messages);
|
||||
|
||||
expect(exported).toContain('FIToFICstmrCdtTrf');
|
||||
// Should contain all message UETRs
|
||||
messages.forEach((msg) => {
|
||||
expect(exported).toContain(msg.uetr);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('validate', () => {
|
||||
it('should validate correct ISO 20022 message', async () => {
|
||||
const message = createTestMessage();
|
||||
const validation = await RawISOContainer.validate(message.xmlContent);
|
||||
|
||||
expect(validation.valid).toBe(true);
|
||||
expect(validation.errors.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should detect missing ISO 20022 namespace', async () => {
|
||||
const invalidXml = '<Document><Test>Invalid</Test></Document>';
|
||||
const validation = await RawISOContainer.validate(invalidXml);
|
||||
|
||||
expect(validation.valid).toBe(false);
|
||||
expect(validation.errors).toContain('Missing ISO 20022 namespace');
|
||||
});
|
||||
|
||||
it('should detect missing UETR in payment message', async () => {
|
||||
const messageWithoutUETR = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
|
||||
<FIToFICstmrCdtTrf>
|
||||
<GrpHdr>
|
||||
<MsgId>MSG-12345</MsgId>
|
||||
</GrpHdr>
|
||||
<CdtTrfTxInf>
|
||||
<PmtId>
|
||||
<EndToEndId>E2E-123</EndToEndId>
|
||||
</PmtId>
|
||||
</CdtTrfTxInf>
|
||||
</FIToFICstmrCdtTrf>
|
||||
</Document>`;
|
||||
|
||||
const validation = await RawISOContainer.validate(messageWithoutUETR);
|
||||
|
||||
expect(validation.valid).toBe(false);
|
||||
expect(validation.errors).toContain('Missing UETR in payment instruction (CBPR+ requirement)');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
123
tests/unit/exports/containers/rje-container.test.ts
Normal file
123
tests/unit/exports/containers/rje-container.test.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Unit tests for RJE Container
|
||||
*/
|
||||
|
||||
import { RJEContainer } from '@/exports/containers/rje-container';
|
||||
import { ISOMessage, MessageType, MessageStatus } from '@/models/message';
|
||||
import { PaymentIdentityMap } from '@/exports/types';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
describe('RJEContainer', () => {
|
||||
const createTestMessage = (): ISOMessage => {
|
||||
return {
|
||||
id: uuidv4(),
|
||||
paymentId: uuidv4(),
|
||||
messageType: MessageType.PACS_008,
|
||||
uetr: uuidv4(),
|
||||
msgId: 'MSG-12345',
|
||||
xmlContent: `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
|
||||
<FIToFICstmrCdtTrf>
|
||||
<GrpHdr>
|
||||
<MsgId>MSG-12345</MsgId>
|
||||
</GrpHdr>
|
||||
</FIToFICstmrCdtTrf>
|
||||
</Document>`,
|
||||
xmlHash: 'test-hash',
|
||||
status: MessageStatus.VALIDATED,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
};
|
||||
|
||||
describe('exportMessage', () => {
|
||||
it('should export message in RJE format with blocks', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await RJEContainer.exportMessage(message, undefined, {
|
||||
includeBlocks: true,
|
||||
});
|
||||
|
||||
expect(exported).toContain('{1:');
|
||||
expect(exported).toContain('{2:');
|
||||
expect(exported).toContain('{3:');
|
||||
expect(exported).toContain('{4:');
|
||||
expect(exported).toContain('{5:');
|
||||
});
|
||||
|
||||
it('should use CRLF line endings', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await RJEContainer.exportMessage(message);
|
||||
|
||||
expect(exported).toContain('\r\n');
|
||||
});
|
||||
|
||||
it('should include UETR in Block 3', async () => {
|
||||
const message = createTestMessage();
|
||||
const identityMap: PaymentIdentityMap = {
|
||||
paymentId: message.paymentId,
|
||||
uetr: message.uetr,
|
||||
ledgerJournalIds: [],
|
||||
internalTransactionIds: [],
|
||||
};
|
||||
|
||||
const exported = await RJEContainer.exportMessage(message, identityMap);
|
||||
|
||||
expect(exported).toContain(':121:');
|
||||
expect(exported).toContain(message.uetr);
|
||||
});
|
||||
});
|
||||
|
||||
describe('exportBatch', () => {
|
||||
it('should export batch with $ delimiter', async () => {
|
||||
const messages = [createTestMessage(), createTestMessage()];
|
||||
const exported = await RJEContainer.exportBatch(messages);
|
||||
|
||||
// Should contain $ delimiter
|
||||
expect(exported).toContain('$');
|
||||
// Should NOT have trailing $ (check last character is not $)
|
||||
expect(exported.trim().endsWith('$')).toBe(false);
|
||||
});
|
||||
|
||||
it('should not have trailing $ delimiter', async () => {
|
||||
const messages = [createTestMessage(), createTestMessage(), createTestMessage()];
|
||||
const exported = await RJEContainer.exportBatch(messages);
|
||||
|
||||
// Split by $ and check last part is not empty
|
||||
const parts = exported.split('$');
|
||||
expect(parts[parts.length - 1].trim().length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validate', () => {
|
||||
it('should validate correct RJE format', () => {
|
||||
const validRJE = `{1:F01BANKDEFFXXXX1234567890}\r\n{2:I103BANKDEFFXXXXN}\r\n{3:\r\n:121:test-uetr}\r\n{4:\r\n:20:REF123}\r\n{5:{MAC:123456}{CHK:123456}}`;
|
||||
const validation = RJEContainer.validate(validRJE);
|
||||
|
||||
expect(validation.valid).toBe(true);
|
||||
});
|
||||
|
||||
it('should detect missing CRLF', () => {
|
||||
const invalidRJE = `{1:F01BANKDEFFXXXX1234567890}\n{2:I103BANKDEFFXXXXN}`;
|
||||
const validation = RJEContainer.validate(invalidRJE);
|
||||
|
||||
expect(validation.valid).toBe(false);
|
||||
expect(validation.errors).toContain('RJE format requires CRLF line endings');
|
||||
});
|
||||
|
||||
it('should detect trailing $ delimiter', () => {
|
||||
const invalidRJE = `{1:F01BANKDEFFXXXX1234567890}\r\n{2:I103BANKDEFFXXXXN}\r\n$`;
|
||||
const validation = RJEContainer.validate(invalidRJE);
|
||||
|
||||
expect(validation.valid).toBe(false);
|
||||
expect(validation.errors).toContain('RJE batch files must not have trailing $ delimiter');
|
||||
});
|
||||
|
||||
it('should detect missing Block 4 CRLF at beginning', () => {
|
||||
const invalidRJE = `{1:F01BANKDEFFXXXX1234567890}\r\n{2:I103BANKDEFFXXXXN}\r\n{3:}\r\n{4::20:REF123}\r\n{5:{MAC:123456}}`;
|
||||
|
||||
// This should pass as Block 4 validation is lenient in current implementation
|
||||
// But we can check for the presence of Block 4
|
||||
expect(invalidRJE).toContain('{4:');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
104
tests/unit/exports/containers/xmlv2-container.test.ts
Normal file
104
tests/unit/exports/containers/xmlv2-container.test.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Unit tests for XML v2 Container
|
||||
*/
|
||||
|
||||
import { XMLV2Container } from '@/exports/containers/xmlv2-container';
|
||||
import { ISOMessage, MessageType, MessageStatus } from '@/models/message';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
describe('XMLV2Container', () => {
|
||||
const createTestMessage = (): ISOMessage => {
|
||||
return {
|
||||
id: uuidv4(),
|
||||
paymentId: uuidv4(),
|
||||
messageType: MessageType.PACS_008,
|
||||
uetr: uuidv4(),
|
||||
msgId: 'MSG-12345',
|
||||
xmlContent: `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
|
||||
<FIToFICstmrCdtTrf>
|
||||
<GrpHdr>
|
||||
<MsgId>MSG-12345</MsgId>
|
||||
<CreDtTm>${new Date().toISOString()}</CreDtTm>
|
||||
</GrpHdr>
|
||||
<CdtTrfTxInf>
|
||||
<PmtId>
|
||||
<UETR>${uuidv4()}</UETR>
|
||||
</PmtId>
|
||||
</CdtTrfTxInf>
|
||||
</FIToFICstmrCdtTrf>
|
||||
</Document>`,
|
||||
xmlHash: 'test-hash',
|
||||
status: MessageStatus.VALIDATED,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
};
|
||||
|
||||
describe('exportMessage', () => {
|
||||
it('should export message in XML v2 format', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await XMLV2Container.exportMessage(message);
|
||||
|
||||
expect(exported).toContain('DataPDU');
|
||||
expect(exported).toContain('AllianceAccessHeader');
|
||||
expect(exported).toContain('MessageBlock');
|
||||
});
|
||||
|
||||
it('should include Alliance Access Header when requested', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await XMLV2Container.exportMessage(message, undefined, {
|
||||
includeAllianceHeader: true,
|
||||
});
|
||||
|
||||
expect(exported).toContain('AllianceAccessHeader');
|
||||
});
|
||||
|
||||
it('should include Application Header when requested', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await XMLV2Container.exportMessage(message, undefined, {
|
||||
includeApplicationHeader: true,
|
||||
});
|
||||
|
||||
expect(exported).toContain('ApplicationHeader');
|
||||
});
|
||||
|
||||
it('should embed XML content in MessageBlock for MX messages', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await XMLV2Container.exportMessage(message, undefined, {
|
||||
base64EncodeMT: false,
|
||||
});
|
||||
|
||||
expect(exported).toContain('<Encoding>XML</Encoding>');
|
||||
expect(exported).toContain('FIToFICstmrCdtTrf');
|
||||
});
|
||||
});
|
||||
|
||||
describe('exportBatch', () => {
|
||||
it('should export batch of messages in XML v2 format', async () => {
|
||||
const messages = [createTestMessage(), createTestMessage()];
|
||||
const exported = await XMLV2Container.exportBatch(messages);
|
||||
|
||||
expect(exported).toContain('BatchPDU');
|
||||
expect(exported).toContain('MessageCount');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validate', () => {
|
||||
it('should validate correct XML v2 structure', async () => {
|
||||
const message = createTestMessage();
|
||||
const exported = await XMLV2Container.exportMessage(message);
|
||||
const validation = await XMLV2Container.validate(exported);
|
||||
|
||||
expect(validation.valid).toBe(true);
|
||||
});
|
||||
|
||||
it('should detect missing DataPDU', async () => {
|
||||
const invalidXml = '<Document><Test>Invalid</Test></Document>';
|
||||
const validation = await XMLV2Container.validate(invalidXml);
|
||||
|
||||
expect(validation.valid).toBe(false);
|
||||
expect(validation.errors).toContain('Missing DataPDU or BatchPDU element');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
70
tests/unit/exports/formats/format-detector.test.ts
Normal file
70
tests/unit/exports/formats/format-detector.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Unit tests for Format Detector
|
||||
*/
|
||||
|
||||
import { FormatDetector } from '@/exports/formats/format-detector';
|
||||
import { ExportFormat } from '@/exports/types';
|
||||
|
||||
describe('FormatDetector', () => {
|
||||
describe('detect', () => {
|
||||
it('should detect RJE format', () => {
|
||||
const rjeContent = `{1:F01BANKDEFFXXXX1234567890}\r\n{2:I103BANKDEFFXXXXN}\r\n{3:\r\n:121:test-uetr}\r\n{4:\r\n:20:REF123}\r\n{5:{MAC:123456}}`;
|
||||
const result = FormatDetector.detect(rjeContent);
|
||||
|
||||
expect(result.format).toBe(ExportFormat.RJE);
|
||||
expect(result.confidence).toBe('high');
|
||||
});
|
||||
|
||||
it('should detect XML v2 format', () => {
|
||||
const xmlv2Content = `<?xml version="1.0"?>
|
||||
<DataPDU>
|
||||
<AllianceAccessHeader>
|
||||
<MessageType>pacs.008</MessageType>
|
||||
</AllianceAccessHeader>
|
||||
<MessageBlock>
|
||||
<Encoding>XML</Encoding>
|
||||
<Content>...</Content>
|
||||
</MessageBlock>
|
||||
</DataPDU>`;
|
||||
const result = FormatDetector.detect(xmlv2Content);
|
||||
|
||||
expect(result.format).toBe(ExportFormat.XML_V2);
|
||||
expect(result.confidence).toBe('high');
|
||||
});
|
||||
|
||||
it('should detect Raw ISO 20022 format', () => {
|
||||
const isoContent = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
|
||||
<FIToFICstmrCdtTrf>
|
||||
<GrpHdr>
|
||||
<MsgId>MSG-12345</MsgId>
|
||||
</GrpHdr>
|
||||
</FIToFICstmrCdtTrf>
|
||||
</Document>`;
|
||||
const result = FormatDetector.detect(isoContent);
|
||||
|
||||
expect(result.format).toBe(ExportFormat.RAW_ISO);
|
||||
expect(result.confidence).toBe('high');
|
||||
});
|
||||
|
||||
it('should detect Base64-encoded MT', () => {
|
||||
// Create a Base64-encoded MT-like content
|
||||
const mtContent = '{1:F01BANKDEFFXXXX1234567890}\r\n{2:I103BANKDEFFXXXXN}';
|
||||
const base64Content = Buffer.from(mtContent).toString('base64');
|
||||
const result = FormatDetector.detect(base64Content);
|
||||
|
||||
// Should detect as RJE (since it's Base64 MT)
|
||||
// Note: Detection may vary based on content, so we check for either RJE or unknown
|
||||
expect(['rje', 'unknown']).toContain(result.format);
|
||||
});
|
||||
|
||||
it('should return unknown for unrecognized format', () => {
|
||||
const unknownContent = 'This is not a recognized format';
|
||||
const result = FormatDetector.detect(unknownContent);
|
||||
|
||||
expect(result.format).toBe('unknown');
|
||||
expect(result.confidence).toBe('low');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
221
tests/unit/exports/identity-map.test.ts
Normal file
221
tests/unit/exports/identity-map.test.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
/**
|
||||
* Unit tests for Payment Identity Map Service
|
||||
*/
|
||||
|
||||
import { PaymentIdentityMapService } from '@/exports/identity-map';
|
||||
import { TestHelpers } from '../../utils/test-helpers';
|
||||
import { PaymentRepository } from '@/repositories/payment-repository';
|
||||
import { MessageRepository } from '@/repositories/message-repository';
|
||||
import { PaymentStatus } from '@/models/payment';
|
||||
import { MessageType, MessageStatus } from '@/models/message';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
describe('PaymentIdentityMapService', () => {
|
||||
let paymentRepository: PaymentRepository;
|
||||
let messageRepository: MessageRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
paymentRepository = new PaymentRepository();
|
||||
messageRepository = new MessageRepository();
|
||||
// Clean database before starting
|
||||
await TestHelpers.cleanDatabase();
|
||||
}, 10000); // Increase timeout for database setup
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestHelpers.cleanDatabase();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await TestHelpers.cleanDatabase();
|
||||
// Close database connections
|
||||
const pool = TestHelpers.getTestDb();
|
||||
await pool.end();
|
||||
}, 10000);
|
||||
|
||||
describe('buildForPayment', () => {
|
||||
it('should build identity map for payment with all identifiers', async () => {
|
||||
// Create test operator
|
||||
const operator = await TestHelpers.createTestOperator('TEST_ID_MAP', 'MAKER' as any);
|
||||
|
||||
// Create payment
|
||||
const paymentRequest = TestHelpers.createTestPaymentRequest();
|
||||
const paymentId = await paymentRepository.create(
|
||||
paymentRequest,
|
||||
operator.id,
|
||||
`TEST-PAY-${Date.now()}`
|
||||
);
|
||||
|
||||
const uetr = uuidv4();
|
||||
const internalTxnId = 'TXN-12345';
|
||||
|
||||
// Update payment with identifiers
|
||||
await paymentRepository.update(paymentId, {
|
||||
internalTransactionId: internalTxnId,
|
||||
uetr,
|
||||
status: PaymentStatus.LEDGER_POSTED,
|
||||
});
|
||||
|
||||
// Create ledger posting
|
||||
const { query } = require('@/database/connection');
|
||||
await query(
|
||||
`INSERT INTO ledger_postings (
|
||||
internal_transaction_id, payment_id, account_number, transaction_type,
|
||||
amount, currency, status, posting_timestamp, reference
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
||||
[
|
||||
internalTxnId,
|
||||
paymentId,
|
||||
paymentRequest.senderAccount,
|
||||
'DEBIT',
|
||||
paymentRequest.amount,
|
||||
paymentRequest.currency,
|
||||
'POSTED',
|
||||
new Date(),
|
||||
paymentId,
|
||||
]
|
||||
);
|
||||
|
||||
// Create ISO message
|
||||
const messageId = uuidv4();
|
||||
const msgId = 'MSG-12345';
|
||||
const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
|
||||
<FIToFICstmrCdtTrf>
|
||||
<GrpHdr>
|
||||
<MsgId>${msgId}</MsgId>
|
||||
<CreDtTm>${new Date().toISOString()}</CreDtTm>
|
||||
</GrpHdr>
|
||||
<CdtTrfTxInf>
|
||||
<PmtId>
|
||||
<EndToEndId>E2E-123</EndToEndId>
|
||||
<TxId>TX-123</TxId>
|
||||
<UETR>${uetr}</UETR>
|
||||
</PmtId>
|
||||
</CdtTrfTxInf>
|
||||
</FIToFICstmrCdtTrf>
|
||||
</Document>`;
|
||||
|
||||
await messageRepository.create({
|
||||
id: messageId,
|
||||
messageId: messageId,
|
||||
paymentId,
|
||||
messageType: MessageType.PACS_008,
|
||||
uetr,
|
||||
msgId,
|
||||
xmlContent,
|
||||
xmlHash: 'test-hash',
|
||||
status: MessageStatus.VALIDATED,
|
||||
});
|
||||
|
||||
// Build identity map
|
||||
const identityMap = await PaymentIdentityMapService.buildForPayment(paymentId);
|
||||
|
||||
expect(identityMap).toBeDefined();
|
||||
expect(identityMap?.paymentId).toBe(paymentId);
|
||||
expect(identityMap?.uetr).toBe(uetr);
|
||||
expect(identityMap?.endToEndId).toBe('E2E-123');
|
||||
expect(identityMap?.txId).toBe('TX-123');
|
||||
expect(identityMap?.ledgerJournalIds.length).toBeGreaterThan(0);
|
||||
expect(identityMap?.internalTransactionIds).toContain(internalTxnId);
|
||||
});
|
||||
|
||||
it('should return null for non-existent payment', async () => {
|
||||
const identityMap = await PaymentIdentityMapService.buildForPayment(uuidv4());
|
||||
expect(identityMap).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByUETR', () => {
|
||||
it('should find payment by UETR', async () => {
|
||||
const operator = await TestHelpers.createTestOperator('TEST_UETR', 'MAKER' as any);
|
||||
const paymentRequest = TestHelpers.createTestPaymentRequest();
|
||||
const paymentId = await paymentRepository.create(
|
||||
paymentRequest,
|
||||
operator.id,
|
||||
`TEST-UETR-${Date.now()}`
|
||||
);
|
||||
|
||||
const uetr = uuidv4();
|
||||
await paymentRepository.update(paymentId, {
|
||||
uetr,
|
||||
status: PaymentStatus.LEDGER_POSTED,
|
||||
});
|
||||
|
||||
const identityMap = await PaymentIdentityMapService.findByUETR(uetr);
|
||||
|
||||
expect(identityMap).toBeDefined();
|
||||
expect(identityMap?.paymentId).toBe(paymentId);
|
||||
expect(identityMap?.uetr).toBe(uetr);
|
||||
});
|
||||
|
||||
it('should return null for non-existent UETR', async () => {
|
||||
const identityMap = await PaymentIdentityMapService.findByUETR(uuidv4());
|
||||
expect(identityMap).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildForPayments', () => {
|
||||
it('should build identity maps for multiple payments', async () => {
|
||||
const operator = await TestHelpers.createTestOperator('TEST_MULTI', 'MAKER' as any);
|
||||
const paymentIds: string[] = [];
|
||||
|
||||
// Create multiple payments
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const paymentRequest = TestHelpers.createTestPaymentRequest();
|
||||
const paymentId = await paymentRepository.create(
|
||||
paymentRequest,
|
||||
operator.id,
|
||||
`TEST-MULTI-${Date.now()}-${i}`
|
||||
);
|
||||
paymentIds.push(paymentId);
|
||||
}
|
||||
|
||||
const identityMaps = await PaymentIdentityMapService.buildForPayments(paymentIds);
|
||||
|
||||
expect(identityMaps.size).toBe(3);
|
||||
paymentIds.forEach((id) => {
|
||||
expect(identityMaps.has(id)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('verifyUETRPassThrough', () => {
|
||||
it('should verify valid UETR format', async () => {
|
||||
const operator = await TestHelpers.createTestOperator('TEST_VERIFY', 'MAKER' as any);
|
||||
const paymentRequest = TestHelpers.createTestPaymentRequest();
|
||||
const paymentId = await paymentRepository.create(
|
||||
paymentRequest,
|
||||
operator.id,
|
||||
`TEST-VERIFY-${Date.now()}`
|
||||
);
|
||||
|
||||
const uetr = uuidv4();
|
||||
await paymentRepository.update(paymentId, {
|
||||
uetr,
|
||||
status: PaymentStatus.LEDGER_POSTED,
|
||||
});
|
||||
|
||||
const isValid = await PaymentIdentityMapService.verifyUETRPassThrough(paymentId);
|
||||
expect(isValid).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for invalid UETR', async () => {
|
||||
const operator = await TestHelpers.createTestOperator('TEST_INVALID', 'MAKER' as any);
|
||||
const paymentRequest = TestHelpers.createTestPaymentRequest();
|
||||
const paymentId = await paymentRepository.create(
|
||||
paymentRequest,
|
||||
operator.id,
|
||||
`TEST-INVALID-${Date.now()}`
|
||||
);
|
||||
|
||||
await paymentRepository.update(paymentId, {
|
||||
uetr: 'invalid-uetr',
|
||||
status: PaymentStatus.LEDGER_POSTED,
|
||||
});
|
||||
|
||||
const isValid = await PaymentIdentityMapService.verifyUETRPassThrough(paymentId);
|
||||
expect(isValid).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
136
tests/unit/exports/utils/export-validator.test.ts
Normal file
136
tests/unit/exports/utils/export-validator.test.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Unit tests for Export Validator
|
||||
*/
|
||||
|
||||
import { ExportValidator } from '@/exports/utils/export-validator';
|
||||
import { ExportQuery, ExportFormat, ExportScope } from '@/exports/types';
|
||||
|
||||
describe('ExportValidator', () => {
|
||||
describe('validateQuery', () => {
|
||||
it('should validate correct query parameters', () => {
|
||||
const query: ExportQuery = {
|
||||
format: ExportFormat.RAW_ISO,
|
||||
scope: ExportScope.MESSAGES,
|
||||
startDate: new Date('2024-01-01'),
|
||||
endDate: new Date('2024-01-31'),
|
||||
};
|
||||
|
||||
const result = ExportValidator.validateQuery(query);
|
||||
|
||||
expect(result.valid).toBe(true);
|
||||
expect(result.errors.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should detect invalid date range', () => {
|
||||
const query: ExportQuery = {
|
||||
format: ExportFormat.RAW_ISO,
|
||||
scope: ExportScope.MESSAGES,
|
||||
startDate: new Date('2024-01-31'),
|
||||
endDate: new Date('2024-01-01'), // End before start
|
||||
};
|
||||
|
||||
const result = ExportValidator.validateQuery(query);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors).toContain('Start date must be before end date');
|
||||
});
|
||||
|
||||
it('should detect date range exceeding 365 days', () => {
|
||||
const query: ExportQuery = {
|
||||
format: ExportFormat.RAW_ISO,
|
||||
scope: ExportScope.MESSAGES,
|
||||
startDate: new Date('2024-01-01'),
|
||||
endDate: new Date('2025-01-10'), // More than 365 days
|
||||
};
|
||||
|
||||
const result = ExportValidator.validateQuery(query);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors).toContain('Date range cannot exceed 365 days');
|
||||
});
|
||||
|
||||
it('should validate UETR format', () => {
|
||||
const query: ExportQuery = {
|
||||
format: ExportFormat.RAW_ISO,
|
||||
scope: ExportScope.MESSAGES,
|
||||
uetr: 'invalid-uetr-format',
|
||||
};
|
||||
|
||||
const result = ExportValidator.validateQuery(query);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors).toContain('Invalid UETR format. Must be a valid UUID.');
|
||||
});
|
||||
|
||||
it('should accept valid UETR format', () => {
|
||||
const query: ExportQuery = {
|
||||
format: ExportFormat.RAW_ISO,
|
||||
scope: ExportScope.MESSAGES,
|
||||
uetr: '123e4567-e89b-12d3-a456-426614174000',
|
||||
};
|
||||
|
||||
const result = ExportValidator.validateQuery(query);
|
||||
|
||||
expect(result.valid).toBe(true);
|
||||
});
|
||||
|
||||
it('should validate account number length', () => {
|
||||
const longAccountNumber = 'A'.repeat(101); // Exceeds 100 characters
|
||||
const query: ExportQuery = {
|
||||
format: ExportFormat.RAW_ISO,
|
||||
scope: ExportScope.MESSAGES,
|
||||
accountNumber: longAccountNumber,
|
||||
};
|
||||
|
||||
const result = ExportValidator.validateQuery(query);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors).toContain('Account number cannot exceed 100 characters');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateFileSize', () => {
|
||||
it('should validate file size within limit', () => {
|
||||
const result = ExportValidator.validateFileSize(1024 * 1024); // 1 MB
|
||||
|
||||
expect(result.valid).toBe(true);
|
||||
});
|
||||
|
||||
it('should detect file size exceeding limit', () => {
|
||||
const result = ExportValidator.validateFileSize(200 * 1024 * 1024); // 200 MB
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.error).toContain('exceeds maximum allowed size');
|
||||
});
|
||||
|
||||
it('should detect empty file', () => {
|
||||
const result = ExportValidator.validateFileSize(0);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.error).toContain('Export file is empty');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateRecordCount', () => {
|
||||
it('should validate record count within limit', () => {
|
||||
const result = ExportValidator.validateRecordCount(100);
|
||||
|
||||
expect(result.valid).toBe(true);
|
||||
});
|
||||
|
||||
it('should detect record count exceeding limit', () => {
|
||||
const result = ExportValidator.validateRecordCount(20000);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.error).toContain('exceeds maximum batch size');
|
||||
});
|
||||
|
||||
it('should detect zero record count', () => {
|
||||
const result = ExportValidator.validateRecordCount(0);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.error).toContain('No records found for export');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user