Files
Genarrative/scripts/repair-auth-public-user-codes.test.ts
T
kdletters 1fab3e79fa 修复陶泥号重复生成
认证快照恢复 next_user_id 改为跟随最大陶泥号

新增 user_account 迁移 JSON 陶泥号补救脚本

补充 UUID 用户编号恢复回归测试

(cherry picked from commit 114773c5876d421e9be4d3bc7372c189d70eae86)
2026-06-30 17:12:13 +08:00

113 lines
2.9 KiB
TypeScript

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',
},
]);
});
});