const MAILSINK = process.env.MAILSINK_URL || 'http://localhost:8025' export async function waitForLatestOtc(toAddress, { attempts = 20, delayMs = 500 } = {}) { for (let i = 0; i < attempts; i++) { const res = await fetch(`${MAILSINK}/api/v1/messages`) if (res.ok) { const data = await res.json() const msg = (data.messages || []).find( (m) => (m.To || []).some((t) => t.Address === toAddress), ) if (msg) { const full = await fetch(`${MAILSINK}/api/v1/message/${msg.ID}`) const body = await full.json() const text = `${body.Text || ''} ${body.HTML || ''}` const code = text.match(/\b(\d{6})\b/) if (code) return code[1] } } await new Promise((r) => setTimeout(r, delayMs)) } throw new Error(`no OTC email for ${toAddress} arrived in Mailpit`) } export async function clearMailpit() { await fetch(`${MAILSINK}/api/v1/messages`, { method: 'DELETE' }) }