import { sql } from 'kysely' import * as TestApp from '../../../test/App.js' import * as Db from '../Db.js' import * as Users from './users.js' describe('getByEmail', () => { test('normalizes email and returns the oldest matching user', async () => { const db = TestApp.database() try { const first = await Users.upsertByAddress(db, { address: `0x${'11'.repeat(20)}` }) const second = await Users.upsertByAddress(db, { address: `0x${'22'.repeat(20)}` }) await db.kysely .updateTable('users') .set({ createdAt: '2026-01-01T00:00:00.000Z' }) .where('id', '=', first.id) .execute() await db.kysely .updateTable('users') .set({ createdAt: '2026-01-02T00:00:00.000Z' }) .where('id', '=', second.id) .execute() // Simulate the previous Worker writing after the normalization migration. await db.kysely .updateTable('users') .set({ email: ' Developer@Example.com ', emailVerified: true }) .where('id', '=', first.id) .execute() await Users.setEmail(db, second.id, 'developer@example.com') await expect(Users.getByEmail(db, ' DEVELOPER@example.com ')).resolves.toMatchObject({ id: first.id, }) } finally { await db.close() } }) }) describe('auth field migration compatibility', () => { test('defaults fields omitted by the previous worker', async () => { const db = TestApp.database() try { const now = new Date().toISOString() await sql`INSERT INTO users (id, address, email, created_at, updated_at) VALUES ('usr_legacy_worker', ${`0x${'33'.repeat(20)}`}, NULL, ${now}, ${now})`.execute( db.kysely, ) await expect(Users.get(db, 'usr_legacy_worker')).resolves.toMatchObject({ emailVerified: false, name: '', }) } finally { await db.close() } }) }) describe('email migration precondition', () => { test('rejects a blank email', async () => { const db = TestApp.database() try { const now = new Date().toISOString() await db.kysely .insertInto('users') .values({ address: null, createdAt: now, email: ' ', emailVerified: false, id: 'usr_blank_email', image: null, name: 'Blank email', updatedAt: now, }) .execute() const migration = Db.migrations.find( (candidate) => candidate.name === '0166_users_email_auth_precondition', ) if (!migration) throw new Error('Expected the user-email precondition migration.') await expect(migration.up(db.kysely).execute()).rejects.toThrow( 'users.email contains blank values', ) } finally { await db.close() } }) test('allows duplicate normalized emails during the compatibility deployment', async () => { const db = TestApp.database() try { const now = new Date().toISOString() await db.kysely .insertInto('users') .values([ { address: null, createdAt: now, email: 'developer@example.com', emailVerified: true, id: 'usr_duplicate_one', image: null, name: 'Duplicate one', updatedAt: now, }, { address: null, createdAt: now, email: ' Developer@Example.com ', emailVerified: true, id: 'usr_duplicate_two', image: null, name: 'Duplicate two', updatedAt: now, }, ]) .execute() const migration = Db.migrations.find( (candidate) => candidate.name === '0166_users_email_auth_precondition', ) if (!migration) throw new Error('Expected the user-email precondition migration.') await expect(migration.up(db.kysely).execute()).resolves.toBeDefined() } finally { await db.close() } }) })