File size: 13,383 Bytes
c09f67c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | import { describe, expect, test } from "bun:test";
/**
* Transaction categorization logic tests
*
* These tests document the expected behavior of transaction categorization
* in the ExportTransactionsProcessor.
*
* Categorization rules:
* 1. No sync record OR failed status -> toExport (needs full export)
* 2. Synced with new attachments -> toSyncAttachments (only sync new attachments)
* 3. Synced with removed attachments -> toSyncAttachments (sync removals)
* 4. Synced with "partial" status -> toSyncAttachments (retry failed)
* 5. Synced with no changes -> alreadyComplete (skip)
*/
// Helper function that mirrors the categorization logic
function categorizeTransactions(
transactions: Array<{
id: string;
attachments: Array<{ id: string; name: string | null }>;
}>,
syncRecordMap: Map<
string,
{
status: "pending" | "synced" | "failed" | "partial";
providerTransactionId: string | null;
syncedAttachmentMapping: Record<string, string | null> | null;
}
>,
) {
const result = {
toExport: [] as string[],
toSyncAttachments: [] as Array<{
transactionId: string;
providerTransactionId: string;
newAttachmentIds: string[];
removedAttachments: Array<{
middayId: string;
providerId: string | null;
}>;
}>,
alreadyComplete: [] as string[],
};
for (const tx of transactions) {
const syncRecord = syncRecordMap.get(tx.id);
// No sync record or failed status = needs export
if (!syncRecord || syncRecord.status === "failed") {
result.toExport.push(tx.id);
continue;
}
// Already synced - check for attachment changes
const currentAttachmentIds = new Set(
tx.attachments?.filter((a) => a.name !== null).map((a) => a.id) ?? [],
);
const syncedMapping = syncRecord.syncedAttachmentMapping ?? {};
const syncedIds = new Set(Object.keys(syncedMapping));
// Find new attachments (in current, not in synced)
const newAttachmentIds = [...currentAttachmentIds].filter(
(id) => !syncedIds.has(id),
);
// Find removed attachments (in synced, not in current)
const removedAttachments = [...syncedIds]
.filter((id) => !currentAttachmentIds.has(id))
.map((middayId) => ({
middayId,
providerId: syncedMapping[middayId] ?? null,
}));
// Has attachment changes OR status is "partial" (needs retry)?
const needsAttachmentSync =
newAttachmentIds.length > 0 ||
removedAttachments.length > 0 ||
syncRecord.status === "partial";
if (needsAttachmentSync) {
if (syncRecord.providerTransactionId) {
result.toSyncAttachments.push({
transactionId: tx.id,
providerTransactionId: syncRecord.providerTransactionId,
newAttachmentIds,
removedAttachments,
});
} else {
// Has changes but no provider transaction ID - needs re-export
result.toExport.push(tx.id);
}
} else {
// No changes - skip
result.alreadyComplete.push(tx.id);
}
}
return result;
}
describe("categorizeTransactions", () => {
describe("toExport categorization", () => {
test("puts new transactions (no sync record) in toExport", () => {
const transactions = [
{ id: "tx-new", attachments: [{ id: "att-1", name: "receipt.pdf" }] },
];
const syncRecordMap = new Map();
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toExport).toContain("tx-new");
expect(result.toSyncAttachments.length).toBe(0);
expect(result.alreadyComplete.length).toBe(0);
});
test("puts failed transactions in toExport for retry", () => {
const transactions = [
{
id: "tx-failed",
attachments: [{ id: "att-1", name: "receipt.pdf" }],
},
];
const syncRecordMap = new Map([
[
"tx-failed",
{
status: "failed" as const,
providerTransactionId: null,
syncedAttachmentMapping: null,
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toExport).toContain("tx-failed");
});
test("puts pending transactions with no provider ID in toExport", () => {
const transactions = [
{
id: "tx-pending",
attachments: [{ id: "att-1", name: "receipt.pdf" }],
},
];
const syncRecordMap = new Map([
[
"tx-pending",
{
status: "synced" as const,
providerTransactionId: null, // No provider ID
syncedAttachmentMapping: null,
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
// Has new attachment but no provider ID = needs export
expect(result.toExport).toContain("tx-pending");
});
});
describe("toSyncAttachments categorization", () => {
test("puts synced transactions with new attachments in toSyncAttachments", () => {
const transactions = [
{
id: "tx-synced",
attachments: [
{ id: "att-existing", name: "existing.pdf" },
{ id: "att-new", name: "new.pdf" },
],
},
];
const syncRecordMap = new Map([
[
"tx-synced",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: { "att-existing": "provider-att-1" },
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toSyncAttachments.length).toBe(1);
expect(result.toSyncAttachments[0]?.transactionId).toBe("tx-synced");
expect(result.toSyncAttachments[0]?.newAttachmentIds).toContain(
"att-new",
);
});
test("puts synced transactions with removed attachments in toSyncAttachments", () => {
const transactions = [
{
id: "tx-removed",
attachments: [], // Attachment removed
},
];
const syncRecordMap = new Map([
[
"tx-removed",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: { "att-deleted": "provider-att-1" },
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toSyncAttachments.length).toBe(1);
expect(result.toSyncAttachments[0]?.removedAttachments).toContainEqual({
middayId: "att-deleted",
providerId: "provider-att-1",
});
});
test("puts partial status transactions in toSyncAttachments for retry", () => {
const transactions = [
{
id: "tx-partial",
attachments: [{ id: "att-1", name: "receipt.pdf" }],
},
];
const syncRecordMap = new Map([
[
"tx-partial",
{
status: "partial" as const, // Previous upload failed
providerTransactionId: "provider-123",
syncedAttachmentMapping: {},
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toSyncAttachments.length).toBe(1);
expect(result.toSyncAttachments[0]?.transactionId).toBe("tx-partial");
});
});
describe("alreadyComplete categorization", () => {
test("puts fully synced transactions in alreadyComplete", () => {
const transactions = [
{
id: "tx-complete",
attachments: [{ id: "att-1", name: "receipt.pdf" }],
},
];
const syncRecordMap = new Map([
[
"tx-complete",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: { "att-1": "provider-att-1" },
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.alreadyComplete).toContain("tx-complete");
expect(result.toExport.length).toBe(0);
expect(result.toSyncAttachments.length).toBe(0);
});
test("puts synced transactions with no attachments in alreadyComplete", () => {
const transactions = [{ id: "tx-no-att", attachments: [] }];
const syncRecordMap = new Map([
[
"tx-no-att",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: {},
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.alreadyComplete).toContain("tx-no-att");
});
});
describe("edge cases", () => {
test("ignores attachments with null names", () => {
const transactions = [
{
id: "tx-null-name",
attachments: [
{ id: "att-1", name: "valid.pdf" },
{ id: "att-2", name: null }, // Should be ignored
],
},
];
const syncRecordMap = new Map([
[
"tx-null-name",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: { "att-1": "provider-att-1" },
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
// att-2 has null name, should be ignored, so no changes
expect(result.alreadyComplete).toContain("tx-null-name");
});
test("handles empty syncedAttachmentMapping", () => {
const transactions = [
{
id: "tx-empty-map",
attachments: [{ id: "att-new", name: "new.pdf" }],
},
];
const syncRecordMap = new Map([
[
"tx-empty-map",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: {}, // Empty mapping
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toSyncAttachments.length).toBe(1);
expect(result.toSyncAttachments[0]?.newAttachmentIds).toContain(
"att-new",
);
});
test("handles null syncedAttachmentMapping", () => {
const transactions = [
{
id: "tx-null-map",
attachments: [{ id: "att-new", name: "new.pdf" }],
},
];
const syncRecordMap = new Map([
[
"tx-null-map",
{
status: "synced" as const,
providerTransactionId: "provider-123",
syncedAttachmentMapping: null, // Null mapping
},
],
]);
const result = categorizeTransactions(transactions, syncRecordMap);
expect(result.toSyncAttachments.length).toBe(1);
expect(result.toSyncAttachments[0]?.newAttachmentIds).toContain(
"att-new",
);
});
});
});
describe("status update logic", () => {
// These tests document the expected behavior of status updates
// in the SyncAttachmentsProcessor
function determineStatus(_uploadedCount: number, failedCount: number) {
if (failedCount > 0) {
return "partial";
}
return "synced";
}
function determineErrorFields(
failedCount: number,
errorCodes: (string | null)[],
errorMessages: string[],
) {
if (failedCount > 0) {
return {
errorCode: errorCodes[0] ?? null,
errorMessage:
errorMessages[0] ?? `${failedCount} attachment(s) failed to upload`,
};
}
// Explicitly clear on success
return {
errorCode: null,
errorMessage: null,
};
}
test("sets status to partial when failures occur", () => {
const status = determineStatus(2, 1); // 2 succeeded, 1 failed
expect(status).toBe("partial");
});
test("sets status to synced when all succeed", () => {
const status = determineStatus(3, 0); // All succeeded
expect(status).toBe("synced");
});
test("clears errorCode on successful retry", () => {
const { errorCode } = determineErrorFields(0, [], []);
expect(errorCode).toBe(null); // Explicitly null to clear DB field
});
test("clears errorMessage on successful retry", () => {
const { errorMessage } = determineErrorFields(0, [], []);
expect(errorMessage).toBe(null); // Explicitly null to clear DB field
});
test("preserves first error code when multiple failures", () => {
const { errorCode } = determineErrorFields(
2,
["RATE_LIMIT", "VALIDATION"],
["Rate limit exceeded", "Invalid file type"],
);
expect(errorCode).toBe("RATE_LIMIT"); // First error
});
test("preserves first error message when multiple failures", () => {
const { errorMessage } = determineErrorFields(
2,
["RATE_LIMIT", "VALIDATION"],
["Rate limit exceeded", "Invalid file type"],
);
expect(errorMessage).toBe("Rate limit exceeded"); // First error
});
test("uses fallback message when no specific error message", () => {
const { errorMessage } = determineErrorFields(
3,
[null],
[], // No specific messages
);
expect(errorMessage).toBe("3 attachment(s) failed to upload");
});
});
|