import { describe, expect, it } from 'vitest'; import { repairAuthPublicUserCodesInMigration } from './repair-auth-public-user-codes.mjs'; function table(name: string, rows: unknown[]) { return { name, rows }; } describe('repairAuthPublicUserCodesInMigration', () => { it('keeps the first public code and reassigns duplicate codes and usernames', () => { const migration = { schema_version: 7, exported_at_micros: 1, tables: [ table('user_account', [ { user_id: 'user_a', public_user_code: 'SY-00000007', username: 'phone_00000008', login_method: 'phone', }, { user_id: 'user_b', public_user_code: 'SY-00000007', username: 'phone_00000008', login_method: 'phone', }, { user_id: 'user_c', public_user_code: 'SY00000009', username: 'phone_00000010', login_method: 'phone', }, ]), table('auth_identity', [{ identity_id: 'authi_phone_1' }]), ], }; const result = repairAuthPublicUserCodesInMigration(migration); expect(result.migration.tables).toHaveLength(1); expect(result.migration.tables[0].name).toBe('user_account'); expect(result.migration.tables[0].rows).toMatchObject([ { user_id: 'user_a', public_user_code: 'SY-00000007', username: 'phone_00000008', }, { user_id: 'user_b', public_user_code: 'SY-00000010', username: 'phone_00000011', }, { user_id: 'user_c', public_user_code: 'SY-00000009', username: 'phone_00000010', }, ]); expect(result.repairedRows).toEqual([ { userId: 'user_b', field: 'public_user_code', from: 'SY-00000007', to: 'SY-00000010', }, { userId: 'user_b', field: 'username', from: 'phone_00000008', to: 'phone_00000011', }, ]); }); it('assigns missing public codes without colliding with existing codes', () => { const migration = { schema_version: 7, exported_at_micros: 1, tables: [ table('user_account', [ { user_id: 'user_a', public_user_code: '', username: '', login_method: 'password', }, { user_id: 'user_b', public_user_code: 'SY-00000002', username: 'phone_00000003', login_method: 'phone', }, ]), ], }; const result = repairAuthPublicUserCodesInMigration(migration); expect(result.migration.tables[0].rows).toMatchObject([ { user_id: 'user_a', public_user_code: 'SY-00000003', username: 'phone_00000004', }, { user_id: 'user_b', public_user_code: 'SY-00000002', username: 'phone_00000003', }, ]); }); });