Files
TREK/server/tests/unit/db/repositories/TodoItems.repository.test.ts
T
Maurice e3982a8e2a test(server): seed the repository, MCP and e2e specs through the factories
Converts the raw prepare() fixtures and reads in the next set of specs to
the ORM factories and readers: the password reset, webauthn, dawarich,
budget, to-do and vacay repository specs, the journey, settings, addons and
share MCP specs, the plugin POI and operator egress specs, the feeds,
storage stats and query helper services, and the packing, collections,
day reorder, leg mode, upload context and trip access integration suites.

The notifications and google quota e2e suites now seed their users through
makeUser with pinned ids, and the notifications suite moves from hand-rolled
tables to the migrated snapshot. Legacy statements that serve as a parity
oracle and the raw rows the primary-relation spike holds toObject() against
stay raw, each with an allow marker naming why.
2026-10-08 18:46:47 +02:00

97 lines
3.6 KiB
TypeScript

/**
* TodoItemsRepository.listDueForReminder — RJ4 parity (L2, task-7-review.md).
*
* `due_date` is an unconstrained `z.string()` on the wire, not always
* canonical `YYYY-MM-DD` text. Legacy wrapped the column in `date(...)`
* before comparing it to the cutoff, which normalizes a time-bearing value
* down to its date part; the converted repository compared the raw string
* against a bare `YYYY-MM-DD` cutoff instead, which sorts a due date on the
* cutoff day itself (`2026-07-14T09:00`) AFTER the cutoff and drops it a day
* early. One seeded world, mixed `due_date` shapes, id set compared against
* the legacy SQL statement (`sn-cycle`/`rj4` reviewer probe, ported per the
* fix-wave brief rather than imported from the reviewer's scratchpad).
*/
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createUser, createTrip } from '../../../helpers/factories';
import { findRow, insertRow } from '../../../helpers/factories/rows';
import { TodoItems } from '../../../../src/db/entities/TodoItems.entity';
import type { TodoItemsRepository } from '../../../../src/db/repositories/TodoItems.repository';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: TodoItemsRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(TodoItems);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
const iso = (d: Date) => d.toISOString().slice(0, 10);
describe('listDueForReminder', () => {
it('RJ4 parity: mixed due_date shapes on the last day of the window match the legacy date()-wrapped SQL, id for id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const now = new Date();
const t0 = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
const plus = (n: number) => {
const d = new Date(t0);
d.setUTCDate(d.getUTCDate() + n);
return d;
};
const LEAD = 3;
const shapes = [
iso(t0),
iso(plus(LEAD)),
iso(plus(LEAD)) + 'T09:00',
iso(plus(LEAD)) + ' 09:00:00',
iso(t0) + 'T00:00:00Z',
iso(plus(-1)),
iso(plus(LEAD + 1)),
'soon',
'',
];
for (const s of shapes) {
await insertRow(t, TodoItems, { trip: trip.id, name: 'n:' + s, due_date: s, checked: 0 });
}
const legacy = (
testDb
// test-sql-allow: the legacy statement is this parity test's oracle and has to run as written.
.prepare(
`SELECT ti.id FROM todo_items ti JOIN trips t ON t.id = ti.trip_id
WHERE ti.checked = 0 AND ti.due_date IS NOT NULL AND ti.due_date <> ''
AND date(ti.due_date) <= date('now', '+' || ? || ' days')
AND date(ti.due_date) >= date('now')
AND (ti.reminded_at IS NULL OR ti.reminded_at <= datetime('now', '-20 hours'))
ORDER BY ti.id`,
)
.all(LEAD) as { id: number }[]
).map((r) => r.id);
const head = (await repo.listDueForReminder(iso(t0), iso(plus(LEAD)))).map((r) => r.id).sort((a, b) => a - b);
const names = (ids: number[]) =>
Promise.all(
ids.map(async (i) => {
const row = await findRow(t, TodoItems, { id: i });
if (!row) throw new Error(`no todo item ${i}`);
return row.name;
}),
);
expect(await names(head)).toEqual(await names(legacy));
expect(head.length).toBeGreaterThan(0);
});
});