import assert from 'node:assert/strict'; import test from 'node:test'; import { exportFlock } from './flockExportRepository.js'; import { mockDb } from '../test/mockDb.js'; test('export scopes every history to the selected flock without date or memorial filters', async () => { const data = { formatVersion: 1, birds: [], workspace: { id: 42, name: 'Flock' } }; const { calls } = mockDb({ rows: [{ data }] }); assert.deepEqual(await exportFlock(42), data); assert.equal(calls.length, 1); const { text, params } = calls[0]; assert.deepEqual(params, [42, null]); assert.match(text, /SELECT \* FROM birds WHERE workspace_id = \$1/); for (const table of ['weight_records', 'vet_visits', 'medications', 'medication_administrations', 'behavior_occurrences', 'bird_timeline_events']) { assert.ok(text.includes(`FROM ${table} r JOIN flock_birds b ON b.id = r.bird_id`)); } assert.match(text, /FROM flock_notes n WHERE n.workspace_id = \$1/); assert.doesNotMatch(text, /LIMIT|memorialized_at IS NULL|INTERVAL/); assert.doesNotMatch(text, /auth_sessions|integration_tokens|stripe|magic_link/); }); test('single member export restricts profiles, notes, and behavior types to the selected bird', async () => { const birdId = 'd0ab1d12-7859-4a64-a354-dd58bf738b94'; const data = { birds: [{ id: birdId }] }; const { calls } = mockDb({ rows: [{ data }] }); assert.deepEqual(await exportFlock(42, birdId), data); assert.deepEqual(calls[0].params, [42, birdId]); assert.ok(calls[0].text.includes('workspace_id = $1 AND ($2::uuid IS NULL OR id = $2)')); assert.ok(calls[0].text.includes('AND ($2::uuid IS NULL OR n.bird_id IN (SELECT id FROM flock_birds))')); assert.ok(calls[0].text.includes('WHERE ($2::uuid IS NULL AND (t.is_default OR t.workspace_id = $1)) OR EXISTS')); });