mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
refactor(roadtrip): roadtrip tables through repositories
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* `RoadtripDayBoundariesRepository` (Plan 3d Task 1). RB1 (`listForTrip`),
|
||||
* RB3 (`upsertBoundary`, `ON CONFLICT (trip_id, day_number) DO UPDATE SET
|
||||
* from_assignment_id = excluded.from_assignment_id, to_assignment_id =
|
||||
* excluded.to_assignment_id, fraction = excluded.fraction`) and RB4
|
||||
* (`deleteForDay`).
|
||||
*/
|
||||
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 { createDay, createDayAssignment, createPlace, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { RoadtripDayBoundaries } from '../../../../src/db/entities/RoadtripDayBoundaries.entity';
|
||||
import type { RoadtripDayBoundariesRepository } from '../../../../src/db/repositories/RoadtripDayBoundaries.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let repo: RoadtripDayBoundariesRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
repo = t.repo(RoadtripDayBoundaries);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
function fixture() {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id);
|
||||
const from = createDayAssignment(testDb, day.id, place.id);
|
||||
const to = createDayAssignment(testDb, day.id, place.id);
|
||||
return { trip, day, from, to };
|
||||
}
|
||||
|
||||
describe('RoadtripDayBoundariesRepository', () => {
|
||||
it('RB1REPO-001: listForTrip is empty for a trip with no boundaries', async () => {
|
||||
const { trip } = fixture();
|
||||
expect(await repo.listForTrip(trip.id)).toEqual([]);
|
||||
});
|
||||
|
||||
it('UPSERTBOUNDARYREPO-001: upsertBoundary inserts a fresh (trip, day_number) row with every column', async () => {
|
||||
const { trip, from, to } = fixture();
|
||||
await repo.upsertBoundary(trip.id, { day_number: 1, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.4 });
|
||||
expect(await repo.listForTrip(trip.id)).toEqual([{ day_number: 1, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.4 }]);
|
||||
});
|
||||
|
||||
it('UPSERTBOUNDARYREPO-002: re-upserting the SAME (trip, day_number) replaces in place — one row, never touching the key', async () => {
|
||||
const { trip, from, to } = fixture();
|
||||
await repo.upsertBoundary(trip.id, { day_number: 1, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.4 });
|
||||
await repo.upsertBoundary(trip.id, { day_number: 1, from_assignment_id: to.id, to_assignment_id: null, fraction: 1 });
|
||||
|
||||
const rows = await repo.listForTrip(trip.id);
|
||||
expect(rows).toEqual([{ day_number: 1, from_assignment_id: to.id, to_assignment_id: null, fraction: 1 }]);
|
||||
});
|
||||
|
||||
it('RB1REPO-002: listForTrip orders by day_number ascending', async () => {
|
||||
const { trip, from, to } = fixture();
|
||||
await repo.upsertBoundary(trip.id, { day_number: 3, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.5 });
|
||||
await repo.upsertBoundary(trip.id, { day_number: 1, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.5 });
|
||||
await repo.upsertBoundary(trip.id, { day_number: 2, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.5 });
|
||||
|
||||
expect((await repo.listForTrip(trip.id)).map((b) => b.day_number)).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('RB4REPO-001: deleteForDay removes only the named day_number, silent on a miss', async () => {
|
||||
const { trip, from, to } = fixture();
|
||||
await repo.upsertBoundary(trip.id, { day_number: 1, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.4 });
|
||||
await repo.upsertBoundary(trip.id, { day_number: 2, from_assignment_id: from.id, to_assignment_id: to.id, fraction: 0.4 });
|
||||
|
||||
await expect(repo.deleteForDay(trip.id, 99)).resolves.toBeUndefined();
|
||||
expect(await repo.listForTrip(trip.id)).toHaveLength(2);
|
||||
|
||||
await repo.deleteForDay(trip.id, 1);
|
||||
expect((await repo.listForTrip(trip.id)).map((b) => b.day_number)).toEqual([2]);
|
||||
});
|
||||
|
||||
it('boundaries of one trip are never boundaries of another', async () => {
|
||||
const { trip: tripA, from: fromA, to: toA } = fixture();
|
||||
const { trip: tripB, from: fromB, to: toB } = fixture();
|
||||
await repo.upsertBoundary(tripA.id, { day_number: 1, from_assignment_id: fromA.id, to_assignment_id: toA.id, fraction: 0.4 });
|
||||
await repo.upsertBoundary(tripB.id, { day_number: 1, from_assignment_id: fromB.id, to_assignment_id: toB.id, fraction: 0.9 });
|
||||
|
||||
expect(await repo.listForTrip(tripA.id)).toEqual([{ day_number: 1, from_assignment_id: fromA.id, to_assignment_id: toA.id, fraction: 0.4 }]);
|
||||
expect(await repo.listForTrip(tripB.id)).toEqual([{ day_number: 1, from_assignment_id: fromB.id, to_assignment_id: toB.id, fraction: 0.9 }]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* `RoadtripDayTracksRepository` (Plan 3d Task 1). RT7 (`deleteForDay`), RT8
|
||||
* (`upsertTrack`) and RT12 (`listForTrip`).
|
||||
*
|
||||
* RT8's `onConflictFields: ['day']` targets `RoadtripDayTracks.day` — this
|
||||
* table's PRIMARY KEY, a NULLABLE one-to-one relation (Task 0's RULE12/
|
||||
* generator finding, ruled NOT a defect). Task 0's review carried a live
|
||||
* finding forward to this task: SQLite accepts `INSERT … VALUES (NULL, …)`
|
||||
* on an INTEGER PRIMARY KEY column and silently assigns the next free
|
||||
* rowid instead of refusing — so an unguarded `day_id` reaching this
|
||||
* upsert could silently write to the WRONG day (another trip's). Two
|
||||
* things prove this is safe here: (1) `upsertTrack`'s own signature is
|
||||
* `day_id: number`, never nullable — TypeScript refuses a `null`/
|
||||
* `undefined` call at compile time; (2) `RoadtripService.requireDayId`
|
||||
* (the caller) parses every `dayId` with `toRowId` BEFORE it reaches this
|
||||
* repository (RT8-GUARD-001 below, at the service level — a repository
|
||||
* unit test cannot express a `null` argument a typed signature rejects).
|
||||
*/
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { createDay, createPlace, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { RoadtripDayTracks } from '../../../../src/db/entities/RoadtripDayTracks.entity';
|
||||
import type { RoadtripDayTracksRepository } from '../../../../src/db/repositories/RoadtripDayTracks.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let tracks: RoadtripDayTracksRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
tracks = t.repo(RoadtripDayTracks);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
function fixture() {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const dayA = createDay(testDb, trip.id);
|
||||
const placeA = createPlace(testDb, trip.id, { name: 'Track A' });
|
||||
const placeB = createPlace(testDb, trip.id, { name: 'Track B' });
|
||||
return { trip, dayA, placeA, placeB };
|
||||
}
|
||||
|
||||
function rawRows(): { day_id: number; place_id: number; stray_km: number | null; created_at: string | null }[] {
|
||||
return testDb.prepare('SELECT day_id, place_id, stray_km, created_at FROM roadtrip_day_tracks ORDER BY day_id').all() as never[];
|
||||
}
|
||||
|
||||
describe('RoadtripDayTracksRepository', () => {
|
||||
it('RT7REPO-001: deleteForDay removes the one row for that day', async () => {
|
||||
const { dayA, placeA } = fixture();
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, null);
|
||||
expect(rawRows()).toHaveLength(1);
|
||||
await tracks.deleteForDay(dayA.id);
|
||||
expect(rawRows()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('RT7REPO-002: deleteForDay on a day with no track is a silent no-op', async () => {
|
||||
const { dayA } = fixture();
|
||||
await expect(tracks.deleteForDay(dayA.id)).resolves.toBeUndefined();
|
||||
expect(rawRows()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('UPSERTTRACKREPO-001: a fresh day_id inserts one row', async () => {
|
||||
const { dayA, placeA } = fixture();
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, 1.5);
|
||||
const rows = rawRows();
|
||||
expect(rows).toEqual([{ day_id: dayA.id, place_id: placeA.id, stray_km: 1.5, created_at: expect.any(String) }]);
|
||||
});
|
||||
|
||||
it('UPSERTTRACKREPO-002: re-upserting the SAME day_id updates in place — one row, the second call\'s place_id/stray_km, created_at untouched', async () => {
|
||||
const { dayA, placeA, placeB } = fixture();
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, 1.5);
|
||||
const before = rawRows()[0]!;
|
||||
|
||||
await tracks.upsertTrack(dayA.id, placeB.id, 9.25);
|
||||
const after = rawRows();
|
||||
|
||||
// Exactly one row — if `onConflictFields: ['day']` resolved to anything
|
||||
// other than the physical `day_id` PK, the second call would either
|
||||
// throw ("ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE
|
||||
// constraint") or raise a UNIQUE-constraint violation trying to insert
|
||||
// a second row with the same PK. Neither happens here — proof, not
|
||||
// assumption, per the Task 0 review carry.
|
||||
expect(after).toHaveLength(1);
|
||||
expect(after[0]).toEqual({ day_id: dayA.id, place_id: placeB.id, stray_km: 9.25, created_at: before.created_at });
|
||||
});
|
||||
|
||||
it('UPSERTTRACKREPO-003: a track for a different day never touches this one\'s row', async () => {
|
||||
const { trip, dayA, placeA } = fixture();
|
||||
const dayB = createDay(testDb, trip.id);
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, null);
|
||||
await tracks.upsertTrack(dayB.id, placeA.id, null);
|
||||
expect(rawRows()).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('UPSERTTRACKREPO-004: the rendered upsert SQL names the physical day_id column as its conflict target', async () => {
|
||||
const { dayA, placeA } = fixture();
|
||||
const connection = t.orm.em.getConnection();
|
||||
const spy = vi.spyOn(connection, 'execute');
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, null);
|
||||
// Read `spy.mock.calls` BEFORE `mockRestore()` — vitest's `mockRestore`
|
||||
// does everything `mockReset` does (clearing recorded calls) in
|
||||
// addition to restoring the original implementation, so a `finally {
|
||||
// spy.mockRestore() }` ahead of this read empties `.calls` first.
|
||||
const calls = [...spy.mock.calls];
|
||||
spy.mockRestore();
|
||||
const upsertCall = calls.find((call) => typeof call[0] === 'string' && call[0].includes('roadtrip_day_tracks') && /insert/i.test(call[0]));
|
||||
expect(upsertCall).toBeDefined();
|
||||
const sql = String(upsertCall![0]);
|
||||
expect(sql).toMatch(/on conflict\s*\(\s*`?day_id`?\s*\)/i);
|
||||
});
|
||||
|
||||
it('RT12REPO-001: listForTrip reads every day-track of the trip, ordered by day, day_id/place_id as bare scalars', async () => {
|
||||
const { trip, dayA, placeA } = fixture();
|
||||
const dayB = createDay(testDb, trip.id);
|
||||
const placeB = createPlace(testDb, trip.id);
|
||||
await tracks.upsertTrack(dayB.id, placeB.id, 2);
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, null);
|
||||
|
||||
const rows = await tracks.listForTrip(trip.id);
|
||||
expect(rows).toEqual([
|
||||
{ day_id: dayA.id, place_id: placeA.id, stray_km: null },
|
||||
{ day_id: dayB.id, place_id: placeB.id, stray_km: 2 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('RT12REPO-002: listForTrip never returns another trip\'s tracks', async () => {
|
||||
const { dayA, placeA } = fixture();
|
||||
const { user: otherUser } = createUser(testDb);
|
||||
const otherTrip = createTrip(testDb, otherUser.id);
|
||||
const otherDay = createDay(testDb, otherTrip.id);
|
||||
const otherPlace = createPlace(testDb, otherTrip.id);
|
||||
await tracks.upsertTrack(dayA.id, placeA.id, null);
|
||||
await tracks.upsertTrack(otherDay.id, otherPlace.id, null);
|
||||
|
||||
const rows = await tracks.listForTrip(otherTrip.id);
|
||||
expect(rows).toEqual([{ day_id: otherDay.id, place_id: otherPlace.id, stray_km: null }]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* `RoadtripPreferencesRepository` (Plan 3d Task 1). RPF1 (`listForTrip`) and
|
||||
* RPF3 (`upsertValue`, `ON CONFLICT(trip_id, key) DO UPDATE SET value =
|
||||
* excluded.value`).
|
||||
*/
|
||||
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 { createTrip, createUser } from '../../../helpers/factories';
|
||||
import { RoadtripPreferences } from '../../../../src/db/entities/RoadtripPreferences.entity';
|
||||
import type { RoadtripPreferencesRepository } from '../../../../src/db/repositories/RoadtripPreferences.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let repo: RoadtripPreferencesRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
repo = t.repo(RoadtripPreferences);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
describe('RoadtripPreferencesRepository', () => {
|
||||
it('RPF1REPO-001: listForTrip is empty for a trip with no saved preferences', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
expect(await repo.listForTrip(trip.id)).toEqual([]);
|
||||
});
|
||||
|
||||
it('UPSERTPREFREPO-001: upsertValue inserts a fresh (trip, key) pair', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
await repo.upsertValue(trip.id, 'roadtrip_range_km', '100');
|
||||
expect(await repo.listForTrip(trip.id)).toEqual([{ key: 'roadtrip_range_km', value: '100' }]);
|
||||
});
|
||||
|
||||
it('UPSERTPREFREPO-002: re-upserting the SAME (trip, key) replaces the value in place — one row', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
await repo.upsertValue(trip.id, 'roadtrip_range_km', '100');
|
||||
await repo.upsertValue(trip.id, 'roadtrip_range_km', '250');
|
||||
const rows = await repo.listForTrip(trip.id);
|
||||
expect(rows).toEqual([{ key: 'roadtrip_range_km', value: '250' }]);
|
||||
});
|
||||
|
||||
it('UPSERTPREFREPO-003: two different keys on the same trip coexist as two rows', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
await repo.upsertValue(trip.id, 'roadtrip_range_km', '100');
|
||||
await repo.upsertValue(trip.id, 'roadtrip_vehicle', '"electric"');
|
||||
const rows = await repo.listForTrip(trip.id);
|
||||
expect(rows.map((r) => r.key).sort()).toEqual(['roadtrip_range_km', 'roadtrip_vehicle']);
|
||||
});
|
||||
|
||||
it('UPSERTPREFREPO-004: the same key on two different trips is two independent rows', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const tripA = createTrip(testDb, user.id);
|
||||
const tripB = createTrip(testDb, user.id);
|
||||
await repo.upsertValue(tripA.id, 'roadtrip_range_km', '100');
|
||||
await repo.upsertValue(tripB.id, 'roadtrip_range_km', '999');
|
||||
expect(await repo.listForTrip(tripA.id)).toEqual([{ key: 'roadtrip_range_km', value: '100' }]);
|
||||
expect(await repo.listForTrip(tripB.id)).toEqual([{ key: 'roadtrip_range_km', value: '999' }]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* `RoadtripViasRepository` (Plan 3d Task 1). RT2 (`listForDay`, the ONE copy
|
||||
* — `AccommodationsService`/`AssignmentsService` both consume it now), RT3
|
||||
* (`listForTrip`), RT4/RT10 (`nextSequence`), RT5/RT11 (`insertVia`), RT9
|
||||
* (`deleteLeg`), RT14 (`existsInDay`), RT15/RT16 (`moveCoordinates`/
|
||||
* `moveCoordinatesAndAnchor`), RT18 (`listAnchors`), RT19/RT23
|
||||
* (`deleteInDay`/`deleteInDayCounted`), RT20 (`setAnchor`), RT21
|
||||
* (`listLegs`), RT22 (`setSequence`), RT24 (`findById`), AS20
|
||||
* (`listForReanchor`).
|
||||
*/
|
||||
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 { createDay, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { RoadtripVias } from '../../../../src/db/entities/RoadtripVias.entity';
|
||||
import type { RoadtripViasRepository } from '../../../../src/db/repositories/RoadtripVias.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let vias: RoadtripViasRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
vias = t.repo(RoadtripVias);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
function fixture() {
|
||||
const { user } = createUser(testDb);
|
||||
const tripA = createTrip(testDb, user.id);
|
||||
const tripB = createTrip(testDb, user.id);
|
||||
const dayA1 = createDay(testDb, tripA.id);
|
||||
const dayA2 = createDay(testDb, tripA.id);
|
||||
const dayB1 = createDay(testDb, tripB.id);
|
||||
return { tripA, tripB, dayA1, dayA2, dayB1 };
|
||||
}
|
||||
|
||||
describe('RoadtripViasRepository', () => {
|
||||
it('RT5REPO-001: insertVia writes every column and returns the new id', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 53.1, lng: 10.2 });
|
||||
const row = await vias.findById(id);
|
||||
expect(row).toEqual({ id, day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 53.1, lng: 10.2, created_at: expect.any(String) });
|
||||
});
|
||||
|
||||
it('RT24REPO-001: findById on a missing id is null', async () => {
|
||||
expect(await vias.findById(999999)).toBeNull();
|
||||
});
|
||||
|
||||
it('RT2REPO-001: listForDay orders by after_order_index, sequence, id and is scoped to the day', async () => {
|
||||
const { dayA1, dayA2 } = fixture();
|
||||
const b = await vias.insertVia({ day_id: dayA1.id, after_order_index: 1, sequence: 0, lat: 1, lng: 1 });
|
||||
const a1 = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 1, lat: 1, lng: 1 });
|
||||
const a0 = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.insertVia({ day_id: dayA2.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
|
||||
const rows = await vias.listForDay(dayA1.id);
|
||||
expect(rows.map((v) => v.id)).toEqual([a0, a1, b]);
|
||||
});
|
||||
|
||||
it('RT3REPO-001: listForTrip spans every day of the trip, ordered by day/after_order_index/sequence/id, and never crosses trips', async () => {
|
||||
const { tripA, tripB, dayA1, dayA2, dayB1 } = fixture();
|
||||
const v2 = await vias.insertVia({ day_id: dayA2.id, after_order_index: 0, sequence: 0, lat: 2, lng: 2 });
|
||||
const v1 = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.insertVia({ day_id: dayB1.id, after_order_index: 0, sequence: 0, lat: 3, lng: 3 });
|
||||
|
||||
const rows = await vias.listForTrip(tripA.id);
|
||||
expect(rows).toEqual([
|
||||
{ id: v1, day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1, created_at: expect.any(String) },
|
||||
{ id: v2, day_id: dayA2.id, after_order_index: 0, sequence: 0, lat: 2, lng: 2, created_at: expect.any(String) },
|
||||
]);
|
||||
expect((await vias.listForTrip(tripB.id)).length).toBe(1);
|
||||
});
|
||||
|
||||
it('RT4REPO-001: nextSequence is 0 for an empty leg, and MAX+1 for a populated one', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
expect(await vias.nextSequence(dayA1.id, 0)).toBe(0);
|
||||
await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 3, lat: 1, lng: 1 });
|
||||
expect(await vias.nextSequence(dayA1.id, 0)).toBe(4);
|
||||
// A different leg on the same day starts fresh.
|
||||
expect(await vias.nextSequence(dayA1.id, 1)).toBe(0);
|
||||
});
|
||||
|
||||
it('RT9REPO-001: deleteLeg removes only the named leg’s vias', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
const keep = await vias.insertVia({ day_id: dayA1.id, after_order_index: 1, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
|
||||
await vias.deleteLeg(dayA1.id, 0);
|
||||
|
||||
const rows = await vias.listForDay(dayA1.id);
|
||||
expect(rows.map((v) => v.id)).toEqual([keep]);
|
||||
});
|
||||
|
||||
it('RT14REPO-001: existsInDay is true only for the exact (id, day) pair', async () => {
|
||||
const { dayA1, dayA2 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
expect(await vias.existsInDay(id, dayA1.id)).toBe(true);
|
||||
expect(await vias.existsInDay(id, dayA2.id)).toBe(false);
|
||||
expect(await vias.existsInDay(999999, dayA1.id)).toBe(false);
|
||||
});
|
||||
|
||||
it('RT15REPO-001: moveCoordinates writes lat/lng only, unscoped by day (matches the legacy statement exactly)', async () => {
|
||||
const { dayA1, dayA2 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 3, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.moveCoordinates(id, 9, 9);
|
||||
const row = await vias.findById(id);
|
||||
expect(row).toMatchObject({ lat: 9, lng: 9, after_order_index: 3, day_id: dayA1.id });
|
||||
expect(dayA2.id).not.toBe(dayA1.id);
|
||||
});
|
||||
|
||||
it('RT16REPO-001: moveCoordinatesAndAnchor writes lat/lng/after_order_index, unscoped by day', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.moveCoordinatesAndAnchor(id, 9, 9, 5);
|
||||
expect(await vias.findById(id)).toMatchObject({ lat: 9, lng: 9, after_order_index: 5 });
|
||||
});
|
||||
|
||||
it('RT18REPO-001: listAnchors returns id/after_order_index/sequence for the day only', async () => {
|
||||
const { dayA1, dayA2 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 2, sequence: 1, lat: 1, lng: 1 });
|
||||
await vias.insertVia({ day_id: dayA2.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
|
||||
const rows = await vias.listAnchors(dayA1.id);
|
||||
expect(rows).toEqual([{ id, after_order_index: 2, sequence: 1 }]);
|
||||
});
|
||||
|
||||
it('RT21REPO-001: listLegs returns id/after_order_index only, for the day', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 2, sequence: 1, lat: 1, lng: 1 });
|
||||
expect(await vias.listLegs(dayA1.id)).toEqual([{ id, after_order_index: 2 }]);
|
||||
});
|
||||
|
||||
it('RT19REPO-001/RT23REPO-001: deleteInDay is silent, deleteInDayCounted reports the row count (0 on a miss)', async () => {
|
||||
const { dayA1, dayA2 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
|
||||
// A foreign day scope never deletes.
|
||||
expect(await vias.deleteInDayCounted(id, dayA2.id)).toBe(0);
|
||||
expect(await vias.findById(id)).not.toBeNull();
|
||||
|
||||
await expect(vias.deleteInDay(999999, dayA1.id)).resolves.toBeUndefined();
|
||||
|
||||
expect(await vias.deleteInDayCounted(id, dayA1.id)).toBe(1);
|
||||
expect(await vias.findById(id)).toBeNull();
|
||||
expect(await vias.deleteInDayCounted(id, dayA1.id)).toBe(0);
|
||||
});
|
||||
|
||||
it('RT20REPO-001: setAnchor writes after_order_index only, scoped to (id, day)', async () => {
|
||||
const { dayA1, dayA2 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.setAnchor(id, dayA2.id, 9);
|
||||
expect((await vias.findById(id))?.after_order_index).toBe(0);
|
||||
await vias.setAnchor(id, dayA1.id, 9);
|
||||
expect((await vias.findById(id))?.after_order_index).toBe(9);
|
||||
});
|
||||
|
||||
it('RT22REPO-001: setSequence writes sequence only, scoped to (id, day)', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 0, sequence: 0, lat: 1, lng: 1 });
|
||||
await vias.setSequence(id, dayA1.id, 7);
|
||||
expect((await vias.findById(id))?.sequence).toBe(7);
|
||||
});
|
||||
|
||||
it('AS20REPO-001: listForReanchor returns id/after_order_index/lat/lng, the AnchoredVia shape', async () => {
|
||||
const { dayA1 } = fixture();
|
||||
const id = await vias.insertVia({ day_id: dayA1.id, after_order_index: 1, sequence: 0, lat: 12.5, lng: -3.2 });
|
||||
expect(await vias.listForReanchor(dayA1.id)).toEqual([{ id, after_order_index: 1, lat: 12.5, lng: -3.2 }]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user