Files
FlockPal/backend/src/repositories/birdRepository.test.ts
T
2026-04-15 22:57:34 -04:00

104 lines
2.7 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createBird, getBirdById, listWeightsForBird, transferBirdToWorkspace } from './birdRepository.js';
import { mockDb } from '../test/mockDb.js';
test('getBirdById returns null when the bird does not exist in the workspace', async () => {
const { calls } = mockDb({ rowCount: 0, rows: [] });
const bird = await getBirdById('bird-1', 10);
assert.equal(bird, null);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0].params, ['bird-1', 10]);
});
test('createBird returns the inserted bird row', async () => {
mockDb({
rowCount: 1,
rows: [
{
id: 'bird-1',
workspace_id: 10,
name: 'Kiwi',
tag_id: 'A-1',
species: 'Cockatiel',
gender: 'female',
date_of_birth: null,
gotcha_day: null,
chart_color: '#cb3a35',
photo_data_url: null,
notify_on_dob: false,
notify_on_gotcha_day: false,
created_at: '2026-04-14T00:00:00.000Z',
latest_weight_grams: null,
latest_recorded_on: null,
},
],
});
const bird = await createBird({
workspaceId: 10,
name: 'Kiwi',
tagId: 'A-1',
species: 'Cockatiel',
gender: 'female',
dateOfBirth: null,
gotchaDay: null,
chartColor: '#cb3a35',
photoDataUrl: null,
notifyOnDob: false,
notifyOnGotchaDay: false,
});
assert.equal(bird?.name, 'Kiwi');
assert.equal(bird?.workspace_id, 10);
assert.equal(bird?.gender, 'female');
});
test('listWeightsForBird scopes by bird, workspace, and day window', async () => {
const { calls } = mockDb({
rowCount: 0,
rows: [],
});
const weights = await listWeightsForBird('bird-1', 10, 30);
assert.deepEqual(weights, []);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0].params, ['bird-1', 30, 10]);
assert.match(calls[0].text, /FROM weight_records/);
});
test('transferBirdToWorkspace moves the bird to the target workspace', async () => {
const { calls } = mockDb({
rowCount: 1,
rows: [
{
id: 'bird-1',
workspace_id: 22,
name: 'Kiwi',
tag_id: 'A-1',
species: 'Cockatiel',
gender: 'female',
date_of_birth: null,
gotcha_day: null,
chart_color: '#cb3a35',
photo_data_url: null,
notify_on_dob: false,
notify_on_gotcha_day: false,
created_at: '2026-04-14T00:00:00.000Z',
latest_weight_grams: '92',
latest_recorded_on: '2026-04-14',
},
],
});
const bird = await transferBirdToWorkspace('bird-1', 10, 22);
assert.equal(bird?.workspace_id, 22);
assert.deepEqual(calls[0].params, ['bird-1', 10, 22]);
assert.match(calls[0].text, /UPDATE birds/);
});