import { describe, expect, it } from 'vitest'; import { rebindOrphanWorkOwnersInMigration } from './rebind-orphan-work-owners.mjs'; const placeholderUserId = 'wx-openid-placeholder'; function table(name, rows) { return { name, rows }; } describe('rebindOrphanWorkOwnersInMigration', () => { it('把作品表里认证表不存在的 owner_user_id 回填到占位用户', () => { const migration = { schema_version: 1, exported_at_micros: 1, tables: [ table('user_account', [ { user_id: 'user_alive' }, { user_id: placeholderUserId }, ]), table('puzzle_work_profile', [ { profile_id: 'p1', owner_user_id: 'user_missing' }, { profile_id: 'p2', owner_user_id: 'user_alive' }, { profile_id: 'p3', owner_user_id: placeholderUserId }, ]), table('puzzle_agent_session', [ { session_id: 'draft-1', owner_user_id: '' }, ]), table('tracking_event', [ { event_id: 't1', owner_user_id: 'user_missing' }, ]), ], }; const result = rebindOrphanWorkOwnersInMigration(migration, { placeholderUserId, validUserIds: ['user_alive'], }); expect(result.reboundRows).toEqual([ { table: 'puzzle_work_profile', rowKey: 'p1', from: 'user_missing', to: placeholderUserId, }, { table: 'puzzle_agent_session', rowKey: 'draft-1', from: '', to: placeholderUserId, }, ]); expect(migration.tables[1].rows[0].owner_user_id).toBe(placeholderUserId); expect(migration.tables[1].rows[1].owner_user_id).toBe('user_alive'); expect(migration.tables[1].rows[2].owner_user_id).toBe(placeholderUserId); expect(migration.tables[2].rows[0].owner_user_id).toBe(placeholderUserId); expect(migration.tables[3].rows[0].owner_user_id).toBe('user_missing'); }); });