Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(database): pin createdAt as the dedup migrations' keep order

Yudhi Armyndharis4ba2ba0c9d03
diff --git a/src/database/migrations/__tests__/1781100000000-AddTemplateNameUnique.spec.ts b/src/database/migrations/__tests__/1781100000000-AddTemplateNameUnique.spec.ts
index 9e0b74395..a1f94eae4 100644
--- a/src/database/migrations/__tests__/1781100000000-AddTemplateNameUnique.spec.ts
+++ b/src/database/migrations/__tests__/1781100000000-AddTemplateNameUnique.spec.ts
@@ -42,8 +42,9 @@ describe('AddTemplateNameUnique migration', () => {
});
it('deduplicates pre-existing (sessionId, name) collisions losslessly, keeping the earliest', async () => {
- await insert('id-early', 'sess-1', 'welcome', '2026-01-01T00:00:00.000Z');
- await insert('id-late', 'sess-1', 'welcome', '2026-02-01T00:00:00.000Z');
+ // The earlier row has the id that sorts last and is inserted second, so only createdAt can pick it.
+ await insert('id-a-late', 'sess-1', 'welcome', '2026-02-01T00:00:00.000Z');
+ await insert('id-z-early', 'sess-1', 'welcome', '2026-01-01T00:00:00.000Z');
await insert('id-other', 'sess-1', 'promo', '2026-01-01T00:00:00.000Z');
const runner = ds.createQueryRunner();
@@ -54,8 +55,8 @@ describe('AddTemplateNameUnique migration', () => {
expect(all).toHaveLength(3);
const byId = Object.fromEntries(all.map(r => [r.id, r.name]));
- expect(byId['id-early']).toBe('welcome'); // earliest keeps the clean name
- expect(byId['id-late']).toBe('welcome-dup-id-late'); // later duplicate renamed losslessly
+ expect(byId['id-z-early']).toBe('welcome'); // earliest keeps the clean name
+ expect(byId['id-a-late']).toBe('welcome-dup-id-a-late'); // later duplicate renamed losslessly
expect(byId['id-other']).toBe('promo'); // unrelated row untouched
// The unique index now rejects a fresh duplicate.
diff --git a/src/database/migrations/__tests__/1781300000000-AddMessagesWaMessageIdUnique.spec.ts b/src/database/migrations/__tests__/1781300000000-AddMessagesWaMessageIdUnique.spec.ts
index 5eeda89d2..91603c0cc 100644
--- a/src/database/migrations/__tests__/1781300000000-AddMessagesWaMessageIdUnique.spec.ts
+++ b/src/database/migrations/__tests__/1781300000000-AddMessagesWaMessageIdUnique.spec.ts
@@ -29,15 +29,16 @@ describe('AddMessagesWaMessageIdUnique migration', () => {
);
it('deletes duplicate (sessionId, waMessageId) rows keeping the earliest, then enforces uniqueness', async () => {
- await insert('id-early', 'sess-1', 'wa-1', '2026-01-01T00:00:00.000Z');
- await insert('id-late', 'sess-1', 'wa-1', '2026-02-01T00:00:00.000Z');
+ // The earlier row has the id that sorts last and is inserted second, so only createdAt can pick it.
+ await insert('id-a-late', 'sess-1', 'wa-1', '2026-02-01T00:00:00.000Z');
+ await insert('id-z-early', 'sess-1', 'wa-1', '2026-01-01T00:00:00.000Z');
await insert('id-other', 'sess-1', 'wa-2', '2026-01-01T00:00:00.000Z');
const runner = ds.createQueryRunner();
await new AddMessagesWaMessageIdUnique1781300000000().up(runner);
const rows = (await runner.query(`SELECT id FROM "messages" ORDER BY id`)) as { id: string }[];
- expect(rows.map(r => r.id).sort()).toEqual(['id-early', 'id-other']); // later dup deleted, earliest kept
+ expect(rows.map(r => r.id).sort()).toEqual(['id-other', 'id-z-early']); // later dup deleted, earliest kept
await expect(insert('id-new', 'sess-1', 'wa-1', '2026-03-01T00:00:00.000Z')).rejects.toThrow();
await runner.release();