Merge dev into fix/prettier

Brings in the Tours addon and the MikroORM follow-ups. Conflicts were
resolved to dev's content, then every file dev brought in was run through
prettier and eslint --fix so the branch stays uniformly formatted.
This commit is contained in:
Maurice
2026-10-07 15:43:13 +02:00
242 changed files with 22952 additions and 731 deletions
@@ -19,6 +19,7 @@ import {
} from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createTour } from '../../../helpers/tours-repos';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -52,9 +53,11 @@ function legacyProjectionRow(id: number): unknown {
COALESCE(da.assignment_end_time, p.end_time) as end_time,
p.duration_minutes, p.notes as place_notes,
p.image_url, p.transport_mode, p.google_place_id, p.google_ftid, p.osm_id, p.amap_poi_id, p.website, p.phone, p.stop_type, p.fill_percent,
t.place_id AS tour_place_id, CASE WHEN t.place_id IS NOT NULL THEN p.route_geometry END AS tour_route_geometry,
c.name as category_name, c.color as category_color, c.icon as category_icon
FROM day_assignments da
JOIN places p ON da.place_id = p.id
LEFT JOIN tours t ON t.place_id = p.id
LEFT JOIN categories c ON p.category_id = c.id
WHERE da.id = ?
`,
@@ -116,6 +119,36 @@ describe('DayAssignmentsRepository — the DY1/DY3/AS1/AS3 projection', () => {
expect(await assignments.findWithPlaceAndCategory(999999)).toBeNull();
});
it('ASSIGNPLACEREPO-009: the Tour facet columns are set for a Tour and null for a plain place, even one with a route', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const tourPlace = createPlace(testDb, trip.id, { name: 'Ridge walk' });
const trackPlace = createPlace(testDb, trip.id, { name: 'Imported track' });
testDb.prepare('UPDATE places SET route_geometry = ? WHERE id = ?').run('[[47,11],[47.1,11.1]]', tourPlace.id);
testDb.prepare('UPDATE places SET route_geometry = ? WHERE id = ?').run('[[1,2],[3,4]]', trackPlace.id);
createTour(testDb, tourPlace.id);
const tour = createDayAssignment(testDb, day.id, tourPlace.id, { order_index: 0 });
const track = createDayAssignment(testDb, day.id, trackPlace.id, { order_index: 1 });
const one = await assignments.findWithPlaceAndCategory(tour.id);
expect(one).toStrictEqual(legacyProjectionRow(tour.id));
expect(one).toMatchObject({ tour_place_id: tourPlace.id, tour_route_geometry: '[[47,11],[47.1,11.1]]' });
expect(await assignments.findWithPlaceAndCategory(track.id)).toMatchObject({
tour_place_id: null,
tour_route_geometry: null,
});
const facets = (rows: { tour_place_id: number | null; tour_route_geometry: string | null }[]) =>
rows.map((r) => [r.tour_place_id, r.tour_route_geometry]);
const expected = [
[tourPlace.id, '[[47,11],[47.1,11.1]]'],
[null, null],
];
expect(facets(await assignments.listForDay(day.id))).toEqual(expected);
expect(facets(await assignments.listWithPlaceAndCategory([day.id]))).toEqual(expected);
});
it("ASSIGNPLACEREPO-004: place_time/end_time COALESCE — the assignment's own time wins, the place's time is the fallback", async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -426,6 +459,16 @@ describe('DayAssignmentsRepository — AS10/AS11/AS13/AS14/AS19 (delete / order
expect(await assignments.getDayId(999999)).toBeUndefined();
});
it('ASSIGNREPO-042 (getPlaceId): the place_id of the assignment, undefined for a missing id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id);
const a = createDayAssignment(testDb, day.id, place.id);
expect(await assignments.getPlaceId(a.id)).toBe(place.id);
expect(await assignments.getPlaceId(999999)).toBeUndefined();
});
it('ASSIGNREPO-011 (AS14, moveToDay): writes day_id and order_index together', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -20,6 +20,7 @@ import {
} from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createTour } from '../../../helpers/tours-repos';
import { TRACK_COLORS } from '@trek/shared';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
@@ -835,9 +836,10 @@ describe('PlacesRepository.listForTrip (PL3) — toEqual(legacy) over all 32 fil
},
): unknown[] {
let query = `
SELECT DISTINCT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon,
SELECT DISTINCT p.*, t.place_id AS tour_place_id, c.name as category_name, c.color as category_color, c.icon as category_icon,
pr.country_code as country_code, pr.region_name as region_name
FROM places p
LEFT JOIN tours t ON t.place_id = p.id
LEFT JOIN categories c ON p.category_id = c.id
LEFT JOIN place_regions pr ON pr.place_id = p.id
WHERE p.trip_id = ?
@@ -887,8 +889,10 @@ describe('PlacesRepository.listForTrip (PL3) — toEqual(legacy) over all 32 fil
const p3 = createPlace(testDb, trip.id, { name: 'Louvre' });
testDb.prepare('INSERT INTO place_tags (place_id, tag_id) VALUES (?, ?)').run(p3.id, tag.id);
createDayAssignment(testDb, day.id, p3.id);
// p4: no category, no tag, unassigned, does NOT match the search term.
createPlace(testDb, trip.id, { name: 'Eiffel Tower' });
// p4: no category, no tag, unassigned, does NOT match the search term,
// and a Tour, so tour_place_id is non-null on exactly one row.
const p4 = createPlace(testDb, trip.id, { name: 'Eiffel Tower' });
createTour(testDb, p4.id);
// Another trip's place, named to collide with the search term — must
// never appear in either statement's output (both scope by trip_id).
createPlace(testDb, otherTrip.id, { name: 'Central Perk' });
@@ -1860,3 +1864,77 @@ describe('PlacesRepository — share.service.ts SH12 read', () => {
expect(await places.listPublicForShare(trip.id)).toEqual([]);
});
});
describe('PlacesRepository.insertTourPlace (TO10) / updateTourRoute (TO11)', () => {
it('PLACEREPO-TOUR-001: insertTourPlace writes the route place as a walking place and leaves the rest at its defaults', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const id = await places.insertTourPlace({
trip_id: trip.id,
name: 'Ridge walk',
lat: 47,
lng: 11,
route_geometry: '[[47,11],[47.1,11.1]]',
});
expect(await places.findInTrip(id, trip.id)).toMatchObject({
trip_id: trip.id,
name: 'Ridge walk',
lat: 47,
lng: 11,
transport_mode: 'walking',
route_geometry: '[[47,11],[47.1,11.1]]',
duration_minutes: 60,
reservation_status: 'none',
category_id: null,
});
});
it('PLACEREPO-TOUR-002: updateTourRoute rewrites the route of a place of that trip, stamps updated_at, and refuses another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Old', lat: 1, lng: 1 });
testDb.prepare("UPDATE places SET transport_mode = 'driving', updated_at = NULL WHERE id = ?").run(place.id);
const route = { name: 'New', lat: 47, lng: 11, route_geometry: '[[47,11],[47.2,11.2]]' };
expect(await places.updateTourRoute(place.id, other.id, route)).toBe(false);
expect(await places.findInTrip(place.id, trip.id)).toMatchObject({
name: 'Old',
transport_mode: 'driving',
updated_at: null,
});
expect(await places.updateTourRoute(place.id, trip.id, route)).toBe(true);
const row = await places.findInTrip(place.id, trip.id);
expect(row).toMatchObject({ ...route, transport_mode: 'walking' });
expect(row?.updated_at).not.toBeNull();
});
});
describe('PlacesRepository.isTour / listForTrip tour_place_id', () => {
it('PLACEREPO-TOUR-003: isTour is true only for a place with a tours row', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const tour = createPlace(testDb, trip.id, { name: 'Ridge walk' });
const plain = createPlace(testDb, trip.id, { name: 'Cafe' });
createTour(testDb, tour.id);
expect(await places.isTour(tour.id)).toBe(true);
expect(await places.isTour(plain.id)).toBe(false);
expect(await places.isTour(999_999)).toBe(false);
});
it('PLACEREPO-TOUR-004: listForTrip carries the place id as tour_place_id for Tours and null otherwise', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const tour = createPlace(testDb, trip.id, { name: 'Ridge walk' });
const plain = createPlace(testDb, trip.id, { name: 'Cafe' });
createTour(testDb, tour.id);
const rows = await places.listForTrip(String(trip.id), {});
const byId = new Map(rows.map((r) => [r.id, r.tour_place_id]));
expect(byId.get(tour.id)).toBe(tour.id);
expect(byId.get(plain.id)).toBeNull();
});
});
@@ -0,0 +1,92 @@
/**
* `TourWaypointsRepository`: TW1 (`listForPlace`), TW2 (`listForTrip`), TW3
* (`insertForPlace`) and TW4 (`deleteForPlace`).
*/
import { TourWaypoints } from '../../../../src/db/entities/TourWaypoints.entity';
import type {
TourWaypointRow,
TourWaypointsRepository,
} from '../../../../src/db/repositories/TourWaypoints.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createPlace, createTrip, createUser } from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createTour } from '../../../helpers/tours-repos';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: TourWaypointsRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(TourWaypoints);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
const ROUTE: TourWaypointRow[] = [
{ lat: 47.1, lng: 11.1, role: 'end', sequence: 2 },
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
{ lat: 47.05, lng: 11.05, role: 'via', sequence: 1 },
];
function tourInTrip() {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id);
createTour(testDb, place.id);
return { trip, place };
}
describe('TourWaypointsRepository', () => {
it('TWREPO-001: insertForPlace writes every point and listForPlace reads them back in route order', async () => {
const { place } = tourInTrip();
await repo.insertForPlace(place.id, ROUTE);
expect(await repo.listForPlace(place.id)).toEqual([...ROUTE].sort((a, b) => a.sequence - b.sequence));
});
it('TWREPO-002: insertForPlace with no points writes nothing', async () => {
const { place } = tourInTrip();
await repo.insertForPlace(place.id, []);
expect(await repo.listForPlace(place.id)).toEqual([]);
});
it('TWREPO-003: deleteForPlace clears one tour and leaves another alone', async () => {
const first = tourInTrip();
const second = tourInTrip();
await repo.insertForPlace(first.place.id, ROUTE);
await repo.insertForPlace(second.place.id, ROUTE);
await repo.deleteForPlace(first.place.id);
expect(await repo.listForPlace(first.place.id)).toEqual([]);
expect(await repo.listForPlace(second.place.id)).toHaveLength(3);
});
it('TWREPO-004: listForTrip returns every waypoint of the trip with its place, ordered by place and sequence', async () => {
const { trip, place } = tourInTrip();
const other = tourInTrip();
const second = createPlace(testDb, trip.id);
createTour(testDb, second.id);
await repo.insertForPlace(second.id, ROUTE.slice(0, 2));
await repo.insertForPlace(place.id, ROUTE);
await repo.insertForPlace(other.place.id, ROUTE);
const rows = await repo.listForTrip(trip.id);
expect(rows.map((r) => [r.place_id, r.sequence])).toEqual([
[place.id, 0],
[place.id, 1],
[place.id, 2],
[second.id, 0],
[second.id, 2],
]);
expect(rows[0]).toEqual({ place_id: place.id, lat: 47, lng: 11, role: 'start', sequence: 0 });
});
});
@@ -0,0 +1,222 @@
/**
* `ToursRepository`: the list/detail projection (TO1/TO2), the facet probes
* places and assignments ask (TO3/TO4/TO5), the trip copy read and write
* (TO6/TO8), and the create/edit writes (TO7/TO9).
*/
import { Tours } from '../../../../src/db/entities/Tours.entity';
import type { ToursRepository } from '../../../../src/db/repositories/Tours.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createDay, createDayAssignment, createPlace, createTrip, createUser } from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { createTour } from '../../../helpers/tours-repos';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: ToursRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(Tours);
});
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 other = createTrip(testDb, user.id);
return { trip, other };
}
function addWaypoint(placeId: number, sequence: number): void {
testDb
.prepare("INSERT INTO tour_waypoints (place_id, lat, lng, role, sequence) VALUES (?, 47, 11, 'via', ?)")
.run(placeId, sequence);
}
const METRICS = {
tour_type: 'hike',
distance: 8.25,
elevation_gain: 420,
elevation_loss: 380,
duration: 150,
match_confidence: 1,
max_hiking_difficulty: 3,
};
describe('ToursRepository reads', () => {
it('TOURREPO-001: listForTrip projects the facet with its place name and both flags, newest first, never another trip', async () => {
const { trip, other } = fixture();
const older = createPlace(testDb, trip.id, { name: 'Older' });
const newer = createPlace(testDb, trip.id, { name: 'Newer' });
const plain = createPlace(testDb, trip.id, { name: 'Not a tour' });
const foreign = createPlace(testDb, other.id, { name: 'Foreign' });
createTour(testDb, older.id, { created_at: '2026-01-01 10:00:00', distance: 5, match_confidence: 0.3 });
createTour(testDb, newer.id, { created_at: '2026-01-02 10:00:00', max_hiking_difficulty: 5 });
createTour(testDb, foreign.id);
createDayAssignment(testDb, createDay(testDb, trip.id).id, older.id);
addWaypoint(newer.id, 0);
expect(await repo.listForTrip(trip.id)).toEqual([
{
place_id: newer.id,
name: 'Newer',
tour_type: 'hike',
distance: null,
elevation_gain: null,
elevation_loss: null,
duration: null,
difficulty: null,
wanderer_ref: null,
match_confidence: null,
tour_group_id: null,
max_hiking_difficulty: 5,
planned: 0,
has_waypoints: 1,
},
{
place_id: older.id,
name: 'Older',
tour_type: 'hike',
distance: 5,
elevation_gain: null,
elevation_loss: null,
duration: null,
difficulty: null,
wanderer_ref: null,
match_confidence: 0.3,
tour_group_id: null,
max_hiking_difficulty: 2,
planned: 1,
has_waypoints: 0,
},
]);
expect((await repo.listForTrip(trip.id)).map((r) => r.place_id)).not.toContain(plain.id);
});
it('TOURREPO-002: listForTrip breaks a created_at tie by the newer place', async () => {
const { trip } = fixture();
const a = createPlace(testDb, trip.id);
const b = createPlace(testDb, trip.id);
createTour(testDb, a.id, { created_at: '2026-01-01 10:00:00' });
createTour(testDb, b.id, { created_at: '2026-01-01 10:00:00' });
expect((await repo.listForTrip(trip.id)).map((r) => r.place_id)).toEqual([b.id, a.id]);
});
it('TOURREPO-003: findInTrip returns the row for its own trip and undefined across trips or for a plain place', async () => {
const { trip, other } = fixture();
const tour = createPlace(testDb, trip.id, { name: 'Ridge' });
const plain = createPlace(testDb, trip.id);
createTour(testDb, tour.id);
expect(await repo.findInTrip(trip.id, tour.id)).toMatchObject({
place_id: tour.id,
name: 'Ridge',
planned: 0,
has_waypoints: 0,
});
expect(await repo.findInTrip(other.id, tour.id)).toBeUndefined();
expect(await repo.findInTrip(trip.id, plain.id)).toBeUndefined();
});
it('TOURREPO-004: existsForPlace and listTourPlaceIds tell tours from plain places', async () => {
const { trip } = fixture();
const tour = createPlace(testDb, trip.id);
const plain = createPlace(testDb, trip.id);
createTour(testDb, tour.id);
expect(await repo.existsForPlace(tour.id)).toBe(true);
expect(await repo.existsForPlace(plain.id)).toBe(false);
expect(await repo.listTourPlaceIds([plain.id, tour.id])).toEqual([tour.id]);
expect(await repo.listTourPlaceIds([])).toEqual([]);
});
it('TOURREPO-005: isOnDay is true only for a tour already on that day, and ignores the assignment being moved', async () => {
const { trip } = fixture();
const day1 = createDay(testDb, trip.id);
const day2 = createDay(testDb, trip.id);
const tour = createPlace(testDb, trip.id);
const plain = createPlace(testDb, trip.id);
createTour(testDb, tour.id);
const onDay1 = createDayAssignment(testDb, day1.id, tour.id);
createDayAssignment(testDb, day1.id, plain.id);
expect(await repo.isOnDay(day1.id, tour.id)).toBe(true);
expect(await repo.isOnDay(day2.id, tour.id)).toBe(false);
expect(await repo.isOnDay(day1.id, plain.id)).toBe(false);
expect(await repo.isOnDay(day1.id, tour.id, onDay1.id)).toBe(false);
});
});
describe('ToursRepository writes', () => {
it('TOURREPO-006: insertTour writes the metrics and leaves the unset metadata NULL', async () => {
const { trip } = fixture();
const place = createPlace(testDb, trip.id, { name: 'Drawn' });
await repo.insertTour({ place_id: place.id, ...METRICS });
expect(await repo.findInTrip(trip.id, place.id)).toEqual({
place_id: place.id,
name: 'Drawn',
...METRICS,
difficulty: null,
wanderer_ref: null,
tour_group_id: null,
planned: 0,
has_waypoints: 0,
});
});
it('TOURREPO-007: updateInTrip replaces the metrics of a tour of that trip and refuses one of another trip', async () => {
const { trip, other } = fixture();
const place = createPlace(testDb, trip.id);
createTour(testDb, place.id);
expect(await repo.updateInTrip(other.id, place.id, METRICS)).toBe(false);
expect(await repo.findInTrip(trip.id, place.id)).toMatchObject({ distance: null, max_hiking_difficulty: 2 });
expect(await repo.updateInTrip(trip.id, place.id, METRICS)).toBe(true);
expect(await repo.findInTrip(trip.id, place.id)).toMatchObject(METRICS);
});
it('TOURREPO-008: listRowsForTrip and insertCopy carry every column, created_at included, onto the copied place', async () => {
const { trip, other } = fixture();
const source = createPlace(testDb, trip.id);
createTour(testDb, source.id, { created_at: '2026-03-04 05:06:07', distance: 3.5, max_hiking_difficulty: 4 });
testDb
.prepare("UPDATE tours SET difficulty = 'T3', wanderer_ref = 'w-1', tour_group_id = 9 WHERE place_id = ?")
.run(source.id);
const rows = await repo.listRowsForTrip(trip.id);
expect(rows).toEqual([
{
place_id: source.id,
tour_type: 'hike',
distance: 3.5,
elevation_gain: null,
elevation_loss: null,
duration: null,
difficulty: 'T3',
wanderer_ref: 'w-1',
match_confidence: null,
tour_group_id: 9,
created_at: '2026-03-04 05:06:07',
max_hiking_difficulty: 4,
},
]);
expect(await repo.listRowsForTrip(other.id)).toEqual([]);
const copy = createPlace(testDb, other.id);
const { place_id: _sourceId, ...columns } = rows[0];
await repo.insertCopy(copy.id, columns);
expect(await repo.listRowsForTrip(other.id)).toEqual([{ ...rows[0], place_id: copy.id }]);
});
});