// Optional browser integration check; use the disposable setup in docs/BIRD_MEMBERSHIP.md. import assert from 'node:assert/strict'; import { randomUUID, createHash } from 'node:crypto'; import { spawn } from 'node:child_process'; import { once } from 'node:events'; import { fileURLToPath } from 'node:url'; import { chromium } from 'playwright'; Object.assign(process.env, { POSTGRES_HOST: '127.0.0.1', POSTGRES_PORT: '55439', POSTGRES_DB: 'behavior_test', POSTGRES_USER: 'postgres', POSTGRES_PASSWORD: 'behavior_test_only', PORT: '55440', FRONTEND_URL: 'http://127.0.0.1:55441', VITE_API_BASE_URL: 'http://127.0.0.1:55440/api', MILESTONE_REMINDERS_ENABLED: 'false', MEDICATION_REMINDERS_ENABLED: 'false', WEIGHT_REMINDERS_ENABLED: 'false', }); const { db } = await import('../../backend/dist/db/client.js'); const children = []; let browser; async function start(path, args, marker) { let output = ''; const child = spawn(process.execPath, [fileURLToPath(path), ...args], { cwd: fileURLToPath(new URL('../', import.meta.url)), env: process.env, stdio: ['ignore', 'pipe', 'pipe'] }); children.push(child); child.stdout.on('data', (data) => { output += data; }); child.stderr.on('data', (data) => { output += data; }); for (let i = 0; i < 100 && !output.includes(marker); i++) await new Promise((resolve) => setTimeout(resolve, 100)); assert.ok(output.includes(marker), output); } try { const accounts = []; const baseId = 100000 + Math.floor(Math.random() * 1000000000); for (let i = 0; i < 2; i++) { const workspace = baseId + i, user = randomUUID(), token = randomUUID(), email = `${user}@example.test`; await db.query(`INSERT INTO workspaces (id, name, workspace_type, rescue_verification_status) VALUES ($1, $2, 'rescue', 'approved')`, [workspace, `Browser membership ${i}`]); await db.query(`INSERT INTO users (id, email, name) VALUES ($1, $2, 'Browser tester')`, [user, email]); await db.query(`INSERT INTO workspace_members (workspace_id, user_id, invite_email, name, role, accepted_at) VALUES ($1, $2, $3, 'Browser tester', 'owner', NOW())`, [workspace, user, email]); await db.query(`INSERT INTO auth_sessions (user_id, active_workspace_id, token_hash, expires_at) VALUES ($1, $2, $3, NOW() + INTERVAL '1 hour')`, [user, workspace, createHash('sha256').update(token).digest('hex')]); accounts.push({ workspace, token }); } const [source, destination] = accounts; const bird = randomUUID(), tagId = `FP-${randomUUID()}`; await db.query(`INSERT INTO birds (id, workspace_id, name, tag_id, species) VALUES ($1, $2, 'Piper', $3, 'Parrot')`, [bird, source.workspace, tagId]); await db.query(`INSERT INTO weight_records (bird_id, weight_grams, recorded_on) VALUES ($1, 120, CURRENT_DATE)`, [bird]); await db.query(`INSERT INTO flock_notes (workspace_id, bird_id, title, body) VALUES ($1, $2, 'Existing note', 'Enjoys morning play')`, [source.workspace, bird]); await start(new URL('../../backend/dist/app.js', import.meta.url), [], 'listening'); await start(new URL('../node_modules/vite/bin/vite.js', import.meta.url), ['--host', '127.0.0.1', '--port', '55441'], 'Local:'); browser = await chromium.launch({ headless: true }); const sourcePage = await browser.newPage({ viewport: { width: 1280, height: 1000 } }); await sourcePage.addInitScript((value) => localStorage.setItem('flockpal_auth_token', value), source.token); await sourcePage.goto('http://127.0.0.1:55441', { waitUntil: 'domcontentloaded' }); await sourcePage.getByRole('button', { name: 'Flock', exact: true }).click(); await sourcePage.locator('.bird-card-select').filter({ hasText: 'Piper' }).click(); await sourcePage.getByRole('button', { name: 'Edit details for Piper' }).click(); sourcePage.once('dialog', async (dialog) => { assert.match(dialog.message(), /history will be retained/); await dialog.accept(); }); await sourcePage.getByRole('button', { name: 'Remove from flock', exact: true }).click(); await sourcePage.locator('.bird-card-select').filter({ hasText: 'Piper' }).waitFor({ state: 'detached' }); assert.equal((await db.query(`SELECT workspace_id FROM birds WHERE id = $1`, [bird])).rows[0].workspace_id, null); const page = await browser.newPage({ viewport: { width: 390, height: 844 } }); const pageErrors = []; page.on('pageerror', (error) => pageErrors.push(error.message)); await page.addInitScript((value) => localStorage.setItem('flockpal_auth_token', value), destination.token); await page.goto('http://127.0.0.1:55441', { waitUntil: 'domcontentloaded' }); await page.getByRole('button', { name: 'Flock', exact: true }).click(); await page.getByRole('button', { name: 'Add bird', exact: true }).click(); const bandInput = page.getByLabel('Band ID, if known', { exact: true }); await bandInput.fill(tagId.slice(0, -1)); await page.getByText('Checking band ID for existing history…').waitFor({ state: 'hidden' }); assert.equal(await page.getByRole('button', { name: 'Add bird with existing history' }).count(), 0); await bandInput.fill(` ${tagId.toLowerCase()} `); const claimButton = page.getByRole('button', { name: 'Add bird with existing history' }); await claimButton.waitFor(); assert.equal(await page.getByRole('button', { name: 'Save bird profile', exact: true }).isDisabled(), true); const notice = page.getByRole('status').filter({ hasText: 'This band ID matches' }); assert.equal(await notice.evaluate((element) => element.scrollWidth <= element.clientWidth), true); await notice.screenshot({ path: '/tmp/flockpal-existing-history-notice.png' }); page.once('dialog', async (dialog) => { assert.match(dialog.message(), /history will be brought in/); await dialog.dismiss(); }); await claimButton.click(); assert.equal((await db.query(`SELECT workspace_id FROM birds WHERE id = $1`, [bird])).rows[0].workspace_id, null); // Only a band ID is needed: no replacement name or species should be required to claim history. assert.equal(await page.getByLabel('Bird name', { exact: true }).inputValue(), ''); page.on('dialog', (dialog) => dialog.accept()); await claimButton.click(); await page.getByRole('tab', { name: 'Weight', exact: true }).waitFor(); assert.equal((await db.query(`SELECT workspace_id FROM birds WHERE id = $1`, [bird])).rows[0].workspace_id, destination.workspace); assert.equal((await db.query(`SELECT COUNT(*)::int AS count FROM birds WHERE LOWER(tag_id) = LOWER($1)`, [tagId])).rows[0].count, 1); await page.getByRole('tab', { name: 'Notes', exact: true }).click(); await page.getByText('Enjoys morning play', { exact: true }).waitFor(); await page.getByRole('tab', { name: 'Timeline', exact: true }).click(); await page.getByText('Joined flock with history', { exact: true }).first().waitFor(); assert.deepEqual(pageErrors, []); console.log('PASS: desktop removal retains history; mobile exact band match notice, cancellation, confirmation, claim without replacement fields, and restored notes/timeline.'); } finally { if (browser) await browser.close(); for (const child of children.reverse()) if (child.exitCode === null) { const done = once(child, 'exit'); child.kill('SIGTERM'); await done; } await db.close(); }