|
| 1 | +// Prisma.sql/Prisma.raw are pure template-string builders — safe to use the |
| 2 | +// real ones from @prisma/client without a DB connection. Mocked here only so |
| 3 | +// importing '@db' (which re-exports the whole @prisma/client package plus |
| 4 | +// the lazily-connecting `db` proxy) doesn't risk touching anything else. |
| 5 | +jest.mock('@db', () => { |
| 6 | + const { Prisma } = require('@prisma/client'); |
| 7 | + return { Prisma }; |
| 8 | +}); |
| 9 | + |
| 10 | +import { |
| 11 | + assertNoDanglingMemberReferences, |
| 12 | + findMemberForeignKeys, |
| 13 | + quoteIdentifier, |
| 14 | + repointGenericForeignKeys, |
| 15 | +} from './merge-duplicate-user-fk-discovery'; |
| 16 | + |
| 17 | +function createFakeTx() { |
| 18 | + return { |
| 19 | + $queryRaw: jest.fn(), |
| 20 | + $executeRaw: jest.fn(), |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe('quoteIdentifier', () => { |
| 25 | + it('quotes a safe identifier', () => { |
| 26 | + expect(quoteIdentifier('Task')).toBe('"Task"'); |
| 27 | + expect(quoteIdentifier('background_check_requests')).toBe( |
| 28 | + '"background_check_requests"', |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('rejects identifiers with characters outside [A-Za-z0-9_]', () => { |
| 33 | + expect(() => quoteIdentifier('Task"; DROP TABLE "Task')).toThrow( |
| 34 | + /Refusing to use unexpected SQL identifier/, |
| 35 | + ); |
| 36 | + expect(() => quoteIdentifier('some table')).toThrow(); |
| 37 | + expect(() => quoteIdentifier('')).toThrow(); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('findMemberForeignKeys', () => { |
| 42 | + it('maps information_schema rows to tableName/columnName pairs', async () => { |
| 43 | + const tx = createFakeTx(); |
| 44 | + tx.$queryRaw.mockResolvedValue([ |
| 45 | + { table_name: 'Task', column_name: 'assigneeId' }, |
| 46 | + { table_name: 'background_check_requests', column_name: 'memberId' }, |
| 47 | + ]); |
| 48 | + |
| 49 | + const result = await findMemberForeignKeys(tx as never); |
| 50 | + |
| 51 | + expect(result).toEqual([ |
| 52 | + { tableName: 'Task', columnName: 'assigneeId' }, |
| 53 | + { tableName: 'background_check_requests', columnName: 'memberId' }, |
| 54 | + ]); |
| 55 | + expect(tx.$queryRaw).toHaveBeenCalledTimes(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns an empty list when no foreign keys are found', async () => { |
| 59 | + const tx = createFakeTx(); |
| 60 | + tx.$queryRaw.mockResolvedValue([]); |
| 61 | + |
| 62 | + expect(await findMemberForeignKeys(tx as never)).toEqual([]); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('repointGenericForeignKeys', () => { |
| 67 | + it('runs an UPDATE for each foreign key not in skipTables', async () => { |
| 68 | + const tx = createFakeTx(); |
| 69 | + tx.$executeRaw.mockResolvedValue(1); |
| 70 | + const foreignKeys = [ |
| 71 | + { tableName: 'Task', columnName: 'assigneeId' }, |
| 72 | + { tableName: 'Risk', columnName: 'assigneeId' }, |
| 73 | + ]; |
| 74 | + |
| 75 | + const repointed = await repointGenericForeignKeys( |
| 76 | + tx as never, |
| 77 | + foreignKeys, |
| 78 | + new Set(), |
| 79 | + 'mem_old', |
| 80 | + 'mem_new', |
| 81 | + ); |
| 82 | + |
| 83 | + expect(tx.$executeRaw).toHaveBeenCalledTimes(2); |
| 84 | + expect(repointed).toEqual(foreignKeys); |
| 85 | + }); |
| 86 | + |
| 87 | + it('skips tables in skipTables entirely', async () => { |
| 88 | + const tx = createFakeTx(); |
| 89 | + tx.$executeRaw.mockResolvedValue(1); |
| 90 | + const foreignKeys = [ |
| 91 | + { tableName: 'Task', columnName: 'assigneeId' }, |
| 92 | + { tableName: 'background_check_requests', columnName: 'memberId' }, |
| 93 | + ]; |
| 94 | + |
| 95 | + const repointed = await repointGenericForeignKeys( |
| 96 | + tx as never, |
| 97 | + foreignKeys, |
| 98 | + new Set(['background_check_requests']), |
| 99 | + 'mem_old', |
| 100 | + 'mem_new', |
| 101 | + ); |
| 102 | + |
| 103 | + expect(tx.$executeRaw).toHaveBeenCalledTimes(1); |
| 104 | + expect(repointed).toEqual([ |
| 105 | + { tableName: 'Task', columnName: 'assigneeId' }, |
| 106 | + ]); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns an empty list and issues no updates when given no foreign keys', async () => { |
| 110 | + const tx = createFakeTx(); |
| 111 | + |
| 112 | + const repointed = await repointGenericForeignKeys( |
| 113 | + tx as never, |
| 114 | + [], |
| 115 | + new Set(), |
| 116 | + 'mem_old', |
| 117 | + 'mem_new', |
| 118 | + ); |
| 119 | + |
| 120 | + expect(tx.$executeRaw).not.toHaveBeenCalled(); |
| 121 | + expect(repointed).toEqual([]); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('assertNoDanglingMemberReferences', () => { |
| 126 | + it('resolves without throwing when nothing still references the old member', async () => { |
| 127 | + const tx = createFakeTx(); |
| 128 | + tx.$queryRaw.mockResolvedValue([{ exists: false }]); |
| 129 | + |
| 130 | + await expect( |
| 131 | + assertNoDanglingMemberReferences( |
| 132 | + tx as never, |
| 133 | + [{ tableName: 'Task', columnName: 'assigneeId' }], |
| 134 | + 'mem_old', |
| 135 | + { |
| 136 | + 'Extra.check': () => Promise.resolve(0), |
| 137 | + }, |
| 138 | + ), |
| 139 | + ).resolves.toBeUndefined(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('throws naming the table.column still pointing at the old member', async () => { |
| 143 | + const tx = createFakeTx(); |
| 144 | + tx.$queryRaw.mockResolvedValue([{ exists: true }]); |
| 145 | + |
| 146 | + await expect( |
| 147 | + assertNoDanglingMemberReferences( |
| 148 | + tx as never, |
| 149 | + [{ tableName: 'Task', columnName: 'assigneeId' }], |
| 150 | + 'mem_old', |
| 151 | + {}, |
| 152 | + ), |
| 153 | + ).rejects.toThrow('Task.assigneeId'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('throws naming an extra (non-FK) check that still has dangling rows', async () => { |
| 157 | + const tx = createFakeTx(); |
| 158 | + tx.$queryRaw.mockResolvedValue([{ exists: false }]); |
| 159 | + |
| 160 | + await expect( |
| 161 | + assertNoDanglingMemberReferences(tx as never, [], 'mem_old', { |
| 162 | + 'Policy.signedBy': () => Promise.resolve(2), |
| 163 | + }), |
| 164 | + ).rejects.toThrow('Policy.signedBy'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('aggregates every dangling reference into a single error', async () => { |
| 168 | + const tx = createFakeTx(); |
| 169 | + // Two foreign keys checked in order — both dangling. |
| 170 | + tx.$queryRaw |
| 171 | + .mockResolvedValueOnce([{ exists: true }]) |
| 172 | + .mockResolvedValueOnce([{ exists: true }]); |
| 173 | + |
| 174 | + await expect( |
| 175 | + assertNoDanglingMemberReferences( |
| 176 | + tx as never, |
| 177 | + [ |
| 178 | + { tableName: 'Task', columnName: 'assigneeId' }, |
| 179 | + { tableName: 'Risk', columnName: 'assigneeId' }, |
| 180 | + ], |
| 181 | + 'mem_old', |
| 182 | + { 'Policy.signedBy': () => Promise.resolve(1) }, |
| 183 | + ), |
| 184 | + ).rejects.toThrow('Task.assigneeId, Risk.assigneeId, Policy.signedBy'); |
| 185 | + }); |
| 186 | +}); |
0 commit comments