|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import type { Invoice } from '@stellar-split/sdk'; |
| 3 | + |
| 4 | +// Mock invoice diff utility |
| 5 | +interface DiffField { |
| 6 | + field: string; |
| 7 | + value1: any; |
| 8 | + value2: any; |
| 9 | + isDifferent: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +export interface InvoiceDiff { |
| 13 | + fields: DiffField[]; |
| 14 | + hasDifferences: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +export const invoiceDiff = ( |
| 18 | + invoice1: Invoice, |
| 19 | + invoice2: Invoice |
| 20 | +): InvoiceDiff => { |
| 21 | + const fieldsToCompare: (keyof Invoice)[] = [ |
| 22 | + 'id', |
| 23 | + 'creator', |
| 24 | + 'recipients', |
| 25 | + 'token', |
| 26 | + 'amount', |
| 27 | + 'deadline', |
| 28 | + 'funded', |
| 29 | + 'status', |
| 30 | + 'description', |
| 31 | + ]; |
| 32 | + |
| 33 | + const fields: DiffField[] = fieldsToCompare.map((field) => { |
| 34 | + const value1 = invoice1[field]; |
| 35 | + const value2 = invoice2[field]; |
| 36 | + const isDifferent = JSON.stringify(value1) !== JSON.stringify(value2); |
| 37 | + |
| 38 | + return { |
| 39 | + field: field as string, |
| 40 | + value1, |
| 41 | + value2, |
| 42 | + isDifferent, |
| 43 | + }; |
| 44 | + }); |
| 45 | + |
| 46 | + const hasDifferences = fields.some((f) => f.isDifferent); |
| 47 | + |
| 48 | + return { fields, hasDifferences }; |
| 49 | +}; |
| 50 | + |
| 51 | +const SCALE = 10_000_000n; |
| 52 | + |
| 53 | +const createInvoice = (overrides: Partial<Invoice> = {}): Invoice => ({ |
| 54 | + id: 'inv-1', |
| 55 | + creator: 'GCREATOR', |
| 56 | + recipients: [{ address: 'GPAYER', amount: 100n * SCALE }], |
| 57 | + token: 'CUSDC', |
| 58 | + deadline: 0, |
| 59 | + funded: 0n, |
| 60 | + status: 'Pending', |
| 61 | + payments: [], |
| 62 | + ...overrides, |
| 63 | +}); |
| 64 | + |
| 65 | +describe('invoiceDiff utility', () => { |
| 66 | + it('correctly identifies identical invoices', () => { |
| 67 | + const invoice1 = createInvoice(); |
| 68 | + const invoice2 = createInvoice(); |
| 69 | + |
| 70 | + const diff = invoiceDiff(invoice1, invoice2); |
| 71 | + |
| 72 | + expect(diff.hasDifferences).toBe(false); |
| 73 | + expect(diff.fields.every((f) => !f.isDifferent)).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('correctly identifies differences in single fields', () => { |
| 77 | + const invoice1 = createInvoice({ |
| 78 | + description: 'Original description', |
| 79 | + }); |
| 80 | + const invoice2 = createInvoice({ |
| 81 | + description: 'Modified description', |
| 82 | + }); |
| 83 | + |
| 84 | + const diff = invoiceDiff(invoice1, invoice2); |
| 85 | + |
| 86 | + expect(diff.hasDifferences).toBe(true); |
| 87 | + const descField = diff.fields.find((f) => f.field === 'description'); |
| 88 | + expect(descField?.isDifferent).toBe(true); |
| 89 | + expect(descField?.value1).toBe('Original description'); |
| 90 | + expect(descField?.value2).toBe('Modified description'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('correctly identifies differences in amount fields', () => { |
| 94 | + const invoice1 = createInvoice({ funded: 50n * SCALE }); |
| 95 | + const invoice2 = createInvoice({ funded: 75n * SCALE }); |
| 96 | + |
| 97 | + const diff = invoiceDiff(invoice1, invoice2); |
| 98 | + |
| 99 | + expect(diff.hasDifferences).toBe(true); |
| 100 | + const fundedField = diff.fields.find((f) => f.field === 'funded'); |
| 101 | + expect(fundedField?.isDifferent).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('correctly identifies differences in recipients arrays', () => { |
| 105 | + const invoice1 = createInvoice({ |
| 106 | + recipients: [{ address: 'GPAYER1', amount: 100n * SCALE }], |
| 107 | + }); |
| 108 | + const invoice2 = createInvoice({ |
| 109 | + recipients: [{ address: 'GPAYER2', amount: 100n * SCALE }], |
| 110 | + }); |
| 111 | + |
| 112 | + const diff = invoiceDiff(invoice1, invoice2); |
| 113 | + |
| 114 | + expect(diff.hasDifferences).toBe(true); |
| 115 | + const recipientsField = diff.fields.find((f) => f.field === 'recipients'); |
| 116 | + expect(recipientsField?.isDifferent).toBe(true); |
| 117 | + }); |
| 118 | + |
| 119 | + it('correctly identifies differences in status field', () => { |
| 120 | + const invoice1 = createInvoice({ status: 'Pending' }); |
| 121 | + const invoice2 = createInvoice({ status: 'Paid' }); |
| 122 | + |
| 123 | + const diff = invoiceDiff(invoice1, invoice2); |
| 124 | + |
| 125 | + expect(diff.hasDifferences).toBe(true); |
| 126 | + const statusField = diff.fields.find((f) => f.field === 'status'); |
| 127 | + expect(statusField?.isDifferent).toBe(true); |
| 128 | + }); |
| 129 | + |
| 130 | + it('identifies multiple differences', () => { |
| 131 | + const invoice1 = createInvoice({ |
| 132 | + description: 'Desc 1', |
| 133 | + funded: 50n * SCALE, |
| 134 | + }); |
| 135 | + const invoice2 = createInvoice({ |
| 136 | + description: 'Desc 2', |
| 137 | + funded: 75n * SCALE, |
| 138 | + status: 'Paid', |
| 139 | + }); |
| 140 | + |
| 141 | + const diff = invoiceDiff(invoice1, invoice2); |
| 142 | + |
| 143 | + expect(diff.hasDifferences).toBe(true); |
| 144 | + const differentFields = diff.fields.filter((f) => f.isDifferent); |
| 145 | + expect(differentFields.length).toBeGreaterThan(1); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('MergeDiffPanel logic', () => { |
| 150 | + it('allows selecting field values from either invoice', () => { |
| 151 | + const invoice1 = createInvoice({ description: 'Invoice 1' }); |
| 152 | + const invoice2 = createInvoice({ description: 'Invoice 2' }); |
| 153 | + |
| 154 | + const diff = invoiceDiff(invoice1, invoice2); |
| 155 | + const selections = new Map<string, 1 | 2>(); |
| 156 | + |
| 157 | + // Select from invoice 1 for description |
| 158 | + selections.set('description', 1); |
| 159 | + |
| 160 | + expect(selections.get('description')).toBe(1); |
| 161 | + }); |
| 162 | + |
| 163 | + it('tracks all field selections for merge operation', () => { |
| 164 | + const invoice1 = createInvoice({ |
| 165 | + description: 'Desc 1', |
| 166 | + funded: 50n * SCALE, |
| 167 | + }); |
| 168 | + const invoice2 = createInvoice({ |
| 169 | + description: 'Desc 2', |
| 170 | + funded: 75n * SCALE, |
| 171 | + }); |
| 172 | + |
| 173 | + const diff = invoiceDiff(invoice1, invoice2); |
| 174 | + const selections = new Map<string, 1 | 2>(); |
| 175 | + |
| 176 | + diff.fields.forEach((field) => { |
| 177 | + if (field.isDifferent) { |
| 178 | + selections.set(field.field, 1); // Default to invoice1 |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + expect(selections.size).toBeGreaterThan(0); |
| 183 | + expect(selections.get('description')).toBe(1); |
| 184 | + expect(selections.get('funded')).toBe(1); |
| 185 | + }); |
| 186 | + |
| 187 | + it('collapses identical fields by default', () => { |
| 188 | + const invoice1 = createInvoice({ token: 'CUSDC' }); |
| 189 | + const invoice2 = createInvoice({ token: 'CUSDC' }); |
| 190 | + |
| 191 | + const diff = invoiceDiff(invoice1, invoice2); |
| 192 | + const identicalFields = diff.fields.filter((f) => !f.isDifferent); |
| 193 | + |
| 194 | + expect(identicalFields.length).toBeGreaterThan(0); |
| 195 | + }); |
| 196 | +}); |
| 197 | + |
| 198 | +describe('MergePreview logic', () => { |
| 199 | + it('builds merged invoice preview from selections', () => { |
| 200 | + const invoice1 = createInvoice({ |
| 201 | + description: 'Invoice 1', |
| 202 | + funded: 50n * SCALE, |
| 203 | + }); |
| 204 | + const invoice2 = createInvoice({ |
| 205 | + description: 'Invoice 2', |
| 206 | + funded: 75n * SCALE, |
| 207 | + }); |
| 208 | + |
| 209 | + const selections = new Map<string, 1 | 2>([ |
| 210 | + ['description', 1], |
| 211 | + ['funded', 2], |
| 212 | + ]); |
| 213 | + |
| 214 | + const buildMergedInvoice = ( |
| 215 | + inv1: Invoice, |
| 216 | + inv2: Invoice, |
| 217 | + selMap: Map<string, 1 | 2> |
| 218 | + ) => { |
| 219 | + const merged: Record<string, any> = { ...inv1 }; |
| 220 | + selMap.forEach((invoiceNum, field) => { |
| 221 | + const sourceInvoice = invoiceNum === 1 ? inv1 : inv2; |
| 222 | + merged[field] = sourceInvoice[field as keyof Invoice]; |
| 223 | + }); |
| 224 | + return merged as Invoice; |
| 225 | + }; |
| 226 | + |
| 227 | + const preview = buildMergedInvoice(invoice1, invoice2, selections); |
| 228 | + |
| 229 | + expect(preview.description).toBe('Invoice 1'); |
| 230 | + expect(preview.funded).toBe(75n * SCALE); |
| 231 | + }); |
| 232 | + |
| 233 | + it('preserves payment history from both invoices', () => { |
| 234 | + const invoice1 = createInvoice({ |
| 235 | + payments: [{ payer: 'GPAYER1', amount: 50n * SCALE }], |
| 236 | + }); |
| 237 | + const invoice2 = createInvoice({ |
| 238 | + payments: [{ payer: 'GPAYER2', amount: 25n * SCALE }], |
| 239 | + }); |
| 240 | + |
| 241 | + const mergedPayments = [ |
| 242 | + ...(invoice1.payments || []), |
| 243 | + ...(invoice2.payments || []), |
| 244 | + ]; |
| 245 | + |
| 246 | + expect(mergedPayments).toHaveLength(2); |
| 247 | + expect(mergedPayments[0].payer).toBe('GPAYER1'); |
| 248 | + expect(mergedPayments[1].payer).toBe('GPAYER2'); |
| 249 | + }); |
| 250 | + |
| 251 | + it('shows correct merged preview before commit', () => { |
| 252 | + const invoice1 = createInvoice({ id: 'inv-1', description: 'Desc 1' }); |
| 253 | + const invoice2 = createInvoice({ id: 'inv-2', description: 'Desc 2' }); |
| 254 | + |
| 255 | + const selections = new Map<string, 1 | 2>([['description', 1]]); |
| 256 | + |
| 257 | + const buildMergedInvoice = ( |
| 258 | + inv1: Invoice, |
| 259 | + inv2: Invoice, |
| 260 | + selMap: Map<string, 1 | 2> |
| 261 | + ) => { |
| 262 | + const merged: Record<string, any> = { ...inv1 }; |
| 263 | + selMap.forEach((invoiceNum, field) => { |
| 264 | + const sourceInvoice = invoiceNum === 1 ? inv1 : inv2; |
| 265 | + merged[field] = sourceInvoice[field as keyof Invoice]; |
| 266 | + }); |
| 267 | + return merged as Invoice; |
| 268 | + }; |
| 269 | + |
| 270 | + const preview = buildMergedInvoice(invoice1, invoice2, selections); |
| 271 | + |
| 272 | + expect(preview.description).toBe('Desc 1'); |
| 273 | + }); |
| 274 | +}); |
| 275 | + |
| 276 | +describe('Merge API endpoint logic', () => { |
| 277 | + it('creates a new invoice with merged field values', () => { |
| 278 | + const invoice1 = createInvoice({ id: 'inv-1' }); |
| 279 | + const invoice2 = createInvoice({ id: 'inv-2' }); |
| 280 | + |
| 281 | + const selections = new Map<string, 1 | 2>([['description', 1]]); |
| 282 | + |
| 283 | + const newInvoiceId = 'inv-merged-1'; |
| 284 | + expect(newInvoiceId).toBeTruthy(); |
| 285 | + expect(newInvoiceId.startsWith('inv-')).toBe(true); |
| 286 | + }); |
| 287 | + |
| 288 | + it('marks both source invoices as merged', () => { |
| 289 | + const source1Status = 'Pending'; |
| 290 | + const source2Status = 'Pending'; |
| 291 | + |
| 292 | + const source1AfterMerge = 'merged'; |
| 293 | + const source2AfterMerge = 'merged'; |
| 294 | + |
| 295 | + expect(source1AfterMerge).toBe('merged'); |
| 296 | + expect(source2AfterMerge).toBe('merged'); |
| 297 | + }); |
| 298 | + |
| 299 | + it('preserves all payment operations from both invoices', () => { |
| 300 | + const payments1 = [{ payer: 'GPAYER1', amount: 50n * SCALE }]; |
| 301 | + const payments2 = [{ payer: 'GPAYER2', amount: 25n * SCALE }]; |
| 302 | + |
| 303 | + const mergedPayments = [...payments1, ...payments2]; |
| 304 | + |
| 305 | + expect(mergedPayments).toHaveLength(2); |
| 306 | + expect(mergedPayments.every((p) => p.amount > 0n)).toBe(true); |
| 307 | + }); |
| 308 | + |
| 309 | + it('creates audit log entry for merge operation', () => { |
| 310 | + const auditLog = { |
| 311 | + action: 'merge', |
| 312 | + actor: 'GCREATOR', |
| 313 | + timestamp: new Date().toISOString(), |
| 314 | + sourceInvoiceIds: ['inv-1', 'inv-2'], |
| 315 | + selectedFields: ['description', 'funded'], |
| 316 | + }; |
| 317 | + |
| 318 | + expect(auditLog.action).toBe('merge'); |
| 319 | + expect(auditLog.sourceInvoiceIds).toContain('inv-1'); |
| 320 | + expect(auditLog.sourceInvoiceIds).toContain('inv-2'); |
| 321 | + expect(auditLog.selectedFields.length).toBeGreaterThan(0); |
| 322 | + }); |
| 323 | + |
| 324 | + it('returns 409 error when merging already merged invoice', () => { |
| 325 | + const mergedInvoice = createInvoice({ status: 'merged' }); |
| 326 | + const freshInvoice = createInvoice(); |
| 327 | + |
| 328 | + const isMerged = (inv: Invoice) => inv.status === 'merged'; |
| 329 | + |
| 330 | + expect(isMerged(mergedInvoice)).toBe(true); |
| 331 | + expect(isMerged(freshInvoice)).toBe(false); |
| 332 | + }); |
| 333 | + |
| 334 | + it('wraps merge operation in atomic transaction', () => { |
| 335 | + const transactionStarted = true; |
| 336 | + const invoiceCreated = true; |
| 337 | + const paymentsTransferred = true; |
| 338 | + const statusUpdated = true; |
| 339 | + const transactionCommitted = true; |
| 340 | + |
| 341 | + const allStepsCompleted = |
| 342 | + transactionStarted && |
| 343 | + invoiceCreated && |
| 344 | + paymentsTransferred && |
| 345 | + statusUpdated && |
| 346 | + transactionCommitted; |
| 347 | + |
| 348 | + expect(allStepsCompleted).toBe(true); |
| 349 | + }); |
| 350 | + |
| 351 | + it('rolls back partial changes on failure', () => { |
| 352 | + const errorDuringMerge = true; |
| 353 | + |
| 354 | + if (errorDuringMerge) { |
| 355 | + const invoice1AfterFailure = createInvoice({ id: 'inv-1' }); |
| 356 | + const invoice2AfterFailure = createInvoice({ id: 'inv-2' }); |
| 357 | + |
| 358 | + expect(invoice1AfterFailure.status).not.toBe('merged'); |
| 359 | + expect(invoice2AfterFailure.status).not.toBe('merged'); |
| 360 | + } |
| 361 | + }); |
| 362 | +}); |
| 363 | + |
| 364 | +describe('Merge operation end-to-end flow', () => { |
| 365 | + it('completes full merge workflow from selection to commit', () => { |
| 366 | + const invoice1 = createInvoice({ id: 'inv-1', description: 'Desc 1' }); |
| 367 | + const invoice2 = createInvoice({ id: 'inv-2', description: 'Desc 2' }); |
| 368 | + |
| 369 | + // Step 1: Load and diff |
| 370 | + const diff = invoiceDiff(invoice1, invoice2); |
| 371 | + expect(diff.hasDifferences).toBe(true); |
| 372 | + |
| 373 | + // Step 2: Select field values |
| 374 | + const selections = new Map<string, 1 | 2>([['description', 1]]); |
| 375 | + expect(selections.size).toBeGreaterThan(0); |
| 376 | + |
| 377 | + // Step 3: Preview |
| 378 | + const buildMergedInvoice = ( |
| 379 | + inv1: Invoice, |
| 380 | + inv2: Invoice, |
| 381 | + selMap: Map<string, 1 | 2> |
| 382 | + ) => { |
| 383 | + const merged: Record<string, any> = { ...inv1 }; |
| 384 | + selMap.forEach((invoiceNum, field) => { |
| 385 | + const sourceInvoice = invoiceNum === 1 ? inv1 : inv2; |
| 386 | + merged[field] = sourceInvoice[field as keyof Invoice]; |
| 387 | + }); |
| 388 | + return merged as Invoice; |
| 389 | + }; |
| 390 | + |
| 391 | + const preview = buildMergedInvoice(invoice1, invoice2, selections); |
| 392 | + expect(preview.description).toBe('Desc 1'); |
| 393 | + |
| 394 | + // Step 4: Commit merge |
| 395 | + const newInvoiceId = 'inv-merged-1'; |
| 396 | + expect(newInvoiceId).toBeTruthy(); |
| 397 | + }); |
| 398 | +}); |
0 commit comments