139 lines
10 KiB
JavaScript
139 lines
10 KiB
JavaScript
// Optional browser check. Requires Playwright in a disposable frontend install; see docs/BEHAVIOR_JOURNAL.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;
|
|
let page;
|
|
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 workspace = 100000 + Math.floor(Math.random() * 1000000000);
|
|
const user = randomUUID(), bird = randomUUID(), secondBird = randomUUID(), token = randomUUID();
|
|
await db.query(`INSERT INTO workspaces (id, name, workspace_type, rescue_verification_status) VALUES ($1, 'Browser test', 'rescue', 'approved')`, [workspace]);
|
|
await db.query(`INSERT INTO users (id, email, name) VALUES ($1, $2, 'Browser tester')`, [user, `${user}@example.test`]);
|
|
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, `${user}@example.test`]);
|
|
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')]);
|
|
await db.query(`INSERT INTO birds (id, workspace_id, name, species) VALUES ($1, $2, 'Piper', 'Parrot'), ($3, $2, 'Willow', 'Parrot')`, [bird, workspace, secondBird]);
|
|
const type = (await db.query(`SELECT id FROM behavior_types WHERE is_default ORDER BY name LIMIT 1`)).rows[0].id;
|
|
for (const timestamp of ['2026-10-01T03:59:59Z', '2026-10-01T04:00:00Z', '2026-11-01T05:30:00Z', '2026-11-01T06:30:00Z']) {
|
|
await db.query(`INSERT INTO behavior_occurrences (bird_id, behavior_type_id, occurred_at) VALUES ($1, $2, $3)`, [bird, type, timestamp]);
|
|
}
|
|
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 context = await browser.newContext({ timezoneId: 'America/New_York', viewport: { width: 1280, height: 1000 } });
|
|
page = await context.newPage();
|
|
const errors = []; page.on('pageerror', (e) => errors.push(e.message));
|
|
await page.addInitScript((value) => localStorage.setItem('flockpal_auth_token', value), token);
|
|
await page.clock.setFixedTime(new Date('2026-10-05T16:00:00Z'));
|
|
await page.goto('http://127.0.0.1:55441');
|
|
await page.getByRole('button', { name: 'Flock', exact: true }).click();
|
|
await page.locator('.bird-card-select').filter({ hasText: 'Piper' }).click();
|
|
await page.getByRole('tab', { name: 'Behavior Journal' }).click();
|
|
const journal = page.getByRole('tabpanel', { name: 'Behavior Journal' });
|
|
await journal.getByText('1 occurrence this month', { exact: true }).waitFor();
|
|
await journal.getByRole('button', { name: 'Record behavior' }).click();
|
|
assert.match(await journal.getByLabel('Date and time', { exact: true }).inputValue(), /^2026-10-05T12:00(:00)?$/);
|
|
await journal.getByLabel('Behavior', { exact: true }).selectOption('custom');
|
|
await journal.getByLabel('Custom behavior name').fill('Wing stretching');
|
|
await journal.getByLabel('Description (optional)').fill('Wings extended');
|
|
await journal.getByLabel('Date and time', { exact: true }).fill('2026-10-05T23:30:12');
|
|
await journal.getByLabel('Duration in seconds (optional)').fill('90');
|
|
await journal.getByLabel('Intensity (optional)').selectOption('3');
|
|
await journal.getByLabel('Notes (optional)').fill('Observed after play');
|
|
await journal.getByRole('button', { name: 'Save occurrence' }).click();
|
|
await journal.getByText('Occurrence saved.', { exact: true }).waitFor();
|
|
await journal.getByRole('button', { name: 'Record behavior' }).click();
|
|
await journal.getByLabel('Behavior', { exact: true }).selectOption({ label: 'Wing stretching' });
|
|
await journal.getByLabel('Date and time', { exact: true }).fill('2026-10-05T22:00');
|
|
await journal.getByRole('button', { name: 'Save occurrence' }).click();
|
|
await journal.getByText('3 occurrences this month', { exact: true }).waitFor();
|
|
const day = journal.getByRole('button', { name: '10/5/2026: 2 occurrences', exact: true });
|
|
await day.click(); assert.equal(await journal.locator('details').count(), 2);
|
|
const entry = journal.locator('details').filter({ hasText: 'Observed after play' });
|
|
await entry.locator('summary').click();
|
|
await entry.getByRole('button', { name: 'Edit', exact: true }).click();
|
|
await journal.getByLabel('Notes (optional)').fill('Edited observation');
|
|
await journal.getByRole('button', { name: 'Save occurrence' }).click();
|
|
await journal.getByText('Edited observation', { exact: true }).first().waitFor();
|
|
await journal.getByLabel('Show behavior', { exact: true }).selectOption({ label: 'Wing stretching' });
|
|
await journal.getByText('2 occurrences this month', { exact: true }).waitFor();
|
|
await journal.getByRole('button', { name: 'Next month' }).click();
|
|
await journal.getByText('0 occurrences this month', { exact: true }).waitFor();
|
|
await journal.getByLabel('Show behavior', { exact: true }).selectOption('');
|
|
await journal.getByText('2 occurrences this month', { exact: true }).waitFor();
|
|
await journal.getByRole('button', { name: '11/1/2026: 2 occurrences', exact: true }).click();
|
|
assert.equal(await journal.locator('details').count(), 2); // Both sides of fall-back belong to Nov 1.
|
|
await journal.locator('details').first().locator('summary').click();
|
|
await journal.locator('details').first().getByRole('button', { name: 'Edit', exact: true }).click();
|
|
await journal.getByLabel('Notes (optional)').fill('Later repeated hour retained');
|
|
await journal.getByRole('button', { name: 'Save occurrence' }).click();
|
|
await journal.getByText('Occurrence saved.', { exact: true }).waitFor();
|
|
const repeated = (await db.query(`SELECT occurred_at FROM behavior_occurrences WHERE bird_id = $1 AND notes = 'Later repeated hour retained'`, [bird])).rows[0];
|
|
assert.equal(repeated.occurred_at.toISOString(), '2026-11-01T06:30:00.000Z');
|
|
|
|
await journal.getByRole('button', { name: 'Previous month' }).click();
|
|
await journal.getByRole('button', { name: 'Previous month' }).click();
|
|
await journal.getByText('1 occurrence this month', { exact: true }).waitFor(); // UTC Oct 1 before local midnight.
|
|
await journal.getByRole('button', { name: 'Next month' }).click();
|
|
await journal.getByRole('button', { name: '10/5/2026: 2 occurrences', exact: true }).click();
|
|
await page.setViewportSize({ width: 390, height: 844 });
|
|
await journal.scrollIntoViewIfNeeded();
|
|
await page.screenshot({ path: '/tmp/flockpal-behavior-mobile.png', fullPage: true });
|
|
assert.equal(await journal.evaluate((element) => element.scrollWidth <= element.clientWidth + 1), true, 'Journal must fit mobile width');
|
|
const editedEntry = journal.locator('details').filter({ hasText: 'Edited observation' });
|
|
await editedEntry.locator('summary').click();
|
|
page.once('dialog', (dialog) => dialog.accept());
|
|
await editedEntry.getByRole('button', { name: 'Delete', exact: true }).click();
|
|
await journal.getByText('Occurrence deleted.', { exact: true }).waitFor();
|
|
await journal.getByRole('button', { name: '10/5/2026: 1 occurrences', exact: true }).waitFor();
|
|
await journal.getByLabel('Bird', { exact: true }).selectOption(secondBird);
|
|
await journal.getByText('0 occurrences this month', { exact: true }).waitFor();
|
|
await journal.getByLabel('Bird', { exact: true }).selectOption(bird);
|
|
await journal.getByText('2 occurrences this month', { exact: true }).waitFor();
|
|
await page.setViewportSize({ width: 1280, height: 1000 });
|
|
await journal.scrollIntoViewIfNeeded();
|
|
await page.screenshot({ path: '/tmp/flockpal-behavior-desktop.png', fullPage: true });
|
|
const utcContext = await browser.newContext({ timezoneId: 'UTC', viewport: { width: 1280, height: 1000 } });
|
|
const utcPage = await utcContext.newPage();
|
|
await utcPage.addInitScript((value) => localStorage.setItem('flockpal_auth_token', value), token);
|
|
await utcPage.clock.setFixedTime(new Date('2026-10-05T16:00:00Z'));
|
|
await utcPage.goto('http://127.0.0.1:55441');
|
|
await utcPage.getByRole('button', { name: 'Flock', exact: true }).click();
|
|
await utcPage.locator('.bird-card-select').filter({ hasText: 'Piper' }).click();
|
|
await utcPage.getByRole('tab', { name: 'Behavior Journal' }).click();
|
|
const utcJournal = utcPage.getByRole('tabpanel', { name: 'Behavior Journal' });
|
|
await utcJournal.getByText('3 occurrences this month', { exact: true }).waitFor();
|
|
await utcJournal.getByRole('button', { name: '10/1/2026: 2 occurrences', exact: true }).waitFor();
|
|
await utcContext.close();
|
|
assert.deepEqual(errors, []);
|
|
console.log('PASS: browser create/custom type/edit/delete, bird/behavior filters, month navigation, multiple events/day, local midnight and DST, and mobile layout.');
|
|
} catch (error) {
|
|
if (page) {
|
|
console.error(await page.locator('.behavior-journal').innerText().catch(() => 'Journal unavailable'));
|
|
await page.screenshot({ path: '/tmp/flockpal-behavior-failure.png', fullPage: true });
|
|
}
|
|
throw error;
|
|
} 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();
|
|
}
|