mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
test(server): plan 4 — atlas read-model parity remainder (3f L6 carry)
Task 8b-2 item 3 (atlas half): full-key toEqual(<legacy raw>) coverage for the atlas read models the 3f Task 7 review's L6 finding listed as missing (minus AT1/TRAVELER_OWNS-consumer, already landed) — AT2 (Places. listForTripIds), AT3/AT28 (PlaceRegions.listCountryCodesForPlaceIds/ listForPlaceIds), AT5/AT10/AT43 (VisitedCountries.listCodesForUser/ listForUser), AT21/AT24/AT25 (VisitedRegions.listForUser/ listRegionCodesForCountry/findCountryCode), AT22 (HiddenRegions.listForUser), AT23/AT40/AT44 (PlaceRegions.listDistinctRegionCodesForCountryAndPlaces/ countPlacesByCountryForTrip/listVisitedCountryCodesForUser), AT30/33/34 (BucketList.listForUser/findById/findForUser), AT41 (Places. listAddressesForUser), AT42 (Trips.countTripsAndDaysForUser). New standalone files for HiddenRegions/VisitedCountries/VisitedRegions/PlaceRegions, which had none. AT6/45/46 (the TRAVELER_OWNS endpoint reads) are proven on ids only by the existing consumer test — full-row parity for those three is NOT done here (carried forward, same as the review's own open item).
This commit is contained in:
@@ -54,3 +54,70 @@ describe('BucketListRepository.listForPublicApi (Plan 4 Task 1, public-api.servi
|
||||
expect(await bucketList.listForPublicApi(user.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Plan 4 Task 8b-2, item 3 (3f L6 carry): AT30/AT33/AT34 had no
|
||||
// repository-level toEqual(<legacy raw>) parity test. `SELECT *` — every
|
||||
// nullable column (target_date, visited_at, visited_source, notes, lat/lng,
|
||||
// country_code) exercised both NULL and SET.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('BucketListRepository.listForUser (AT30) — SELECT * ordered by created_at DESC', () => {
|
||||
it('BUCKETREPO-004: matches the legacy row exactly, every nullable column both NULL and SET', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { user: other } = createUser(testDb, { username: 'other' });
|
||||
const sparse = testDb.prepare(
|
||||
'INSERT INTO bucket_list (user_id, name, created_at) VALUES (?, ?, ?)',
|
||||
).run(user.id, 'Sparse', '2026-01-01T00:00:00.000Z');
|
||||
const full = testDb.prepare(
|
||||
'INSERT INTO bucket_list (user_id, name, lat, lng, country_code, notes, target_date, visited_at, visited_source, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
).run(user.id, 'Full', 10.5, 20.5, 'JP', 'note', '2027-01-01', '2026-06-01', 'manual', '2026-02-01T00:00:00.000Z');
|
||||
testDb.prepare('INSERT INTO bucket_list (user_id, name) VALUES (?, ?)').run(other.id, 'Not mine');
|
||||
|
||||
const legacy = testDb.prepare('SELECT * FROM bucket_list WHERE user_id = ? ORDER BY created_at DESC').all(user.id);
|
||||
const rows = await bucketList.listForUser(user.id);
|
||||
|
||||
expect(rows).toEqual(legacy);
|
||||
expect(rows.map((r) => r.id)).toEqual([Number(full.lastInsertRowid), Number(sparse.lastInsertRowid)]);
|
||||
});
|
||||
|
||||
it('BUCKETREPO-005: empty array for a user with nothing on their list', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await bucketList.listForUser(user.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BucketListRepository.findById (AT33) — SELECT * WHERE id = ?, NOT user-scoped', () => {
|
||||
it('BUCKETREPO-006: matches the legacy row, regardless of owner', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const inserted = testDb.prepare('INSERT INTO bucket_list (user_id, name) VALUES (?, ?)').run(user.id, 'Item');
|
||||
const id = Number(inserted.lastInsertRowid);
|
||||
|
||||
const legacy = testDb.prepare('SELECT * FROM bucket_list WHERE id = ?').get(id);
|
||||
expect(await bucketList.findById(id)).toEqual(legacy);
|
||||
});
|
||||
|
||||
it('BUCKETREPO-007: undefined for a missing id', async () => {
|
||||
expect(await bucketList.findById(999999)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('BucketListRepository.findForUser (AT34/AT36/AT37) — SELECT * WHERE id = ? AND user_id = ?', () => {
|
||||
it('BUCKETREPO-008: matches the legacy row for the owning user', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const inserted = testDb.prepare('INSERT INTO bucket_list (user_id, name) VALUES (?, ?)').run(user.id, 'Item');
|
||||
const id = Number(inserted.lastInsertRowid);
|
||||
|
||||
const legacy = testDb.prepare('SELECT * FROM bucket_list WHERE id = ? AND user_id = ?').get(id, user.id);
|
||||
expect(await bucketList.findForUser(id, user.id)).toEqual(legacy);
|
||||
});
|
||||
|
||||
it('BUCKETREPO-009: undefined for a foreign owner — the guard never leaks another user\'s item', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { user: other } = createUser(testDb, { username: 'other' });
|
||||
const inserted = testDb.prepare('INSERT INTO bucket_list (user_id, name) VALUES (?, ?)').run(user.id, 'Item');
|
||||
const id = Number(inserted.lastInsertRowid);
|
||||
|
||||
expect(await bucketList.findForUser(id, other.id)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* `HiddenRegionsRepository.listForUser` — Plan 4 Task 8b-2 item 3 (3f L6
|
||||
* carry): AT22 (`getHiddenRegions`) had no repository-level
|
||||
* `toEqual(<legacy raw>)` parity test.
|
||||
*/
|
||||
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 } from '../../../helpers/factories';
|
||||
import { HiddenRegions } from '../../../../src/db/entities/HiddenRegions.entity';
|
||||
import type { HiddenRegionsRepository } from '../../../../src/db/repositories/HiddenRegions.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let repo: HiddenRegionsRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
repo = t.repo(HiddenRegions);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
describe('HiddenRegionsRepository.listForUser (AT22)', () => {
|
||||
it('HIDDENREGREPO-001: matches SELECT region_code FROM hidden_regions WHERE user_id = ? run raw, scoped to the user', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { user: other } = createUser(testDb, { username: 'other' });
|
||||
testDb.prepare('INSERT INTO hidden_regions (user_id, region_code, country_code) VALUES (?, ?, ?)').run(user.id, 'DE-BY', 'DE');
|
||||
testDb.prepare('INSERT INTO hidden_regions (user_id, region_code, country_code) VALUES (?, ?, ?)').run(user.id, 'DE-BE', 'DE');
|
||||
testDb.prepare('INSERT INTO hidden_regions (user_id, region_code, country_code) VALUES (?, ?, ?)').run(other.id, 'FR-IDF', 'FR');
|
||||
|
||||
const legacy = testDb.prepare('SELECT region_code FROM hidden_regions WHERE user_id = ?').all(user.id);
|
||||
const codes = await repo.listForUser(user.id);
|
||||
|
||||
expect(codes).toEqual(legacy.map((r) => (r as { region_code: string }).region_code));
|
||||
expect(codes.sort()).toEqual(['DE-BE', 'DE-BY']);
|
||||
});
|
||||
|
||||
it('HIDDENREGREPO-002: empty array for a user with nothing hidden', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await repo.listForUser(user.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* PlaceRegionsRepository — full-key parity (Plan 4 Task 8b-2, item 3: 3f
|
||||
* Task 7 review's L6 carry, "Atlas and vacay read models without a full-key
|
||||
* legacy-raw `toEqual`"). AT3, AT23, AT28, AT40, AT44 had no repository-level
|
||||
* test at all (the review's side-by-side HTTP run showed the rows are
|
||||
* identical to the legacy today, but nothing ratchets them). One seeded
|
||||
* world, `toEqual(<legacy raw>)` per method.
|
||||
*/
|
||||
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 { createPlace, createTrip, createUser, addTripMember } from '../../../helpers/factories';
|
||||
import { PlaceRegions } from '../../../../src/db/entities/PlaceRegions.entity';
|
||||
import type { PlaceRegionsRepository } from '../../../../src/db/repositories/PlaceRegions.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let repo: PlaceRegionsRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
repo = t.repo(PlaceRegions);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
function insertPlaceRegion(placeId: number, countryCode: string, regionCode: string, regionName: string): void {
|
||||
testDb.prepare('INSERT INTO place_regions (place_id, country_code, region_code, region_name) VALUES (?, ?, ?, ?)')
|
||||
.run(placeId, countryCode, regionCode, regionName);
|
||||
}
|
||||
|
||||
describe('PlaceRegionsRepository.listCountryCodesForPlaceIds (AT3)', () => {
|
||||
it('PLACEREGREPO-001: matches SELECT place_id, country_code FROM place_regions WHERE place_id IN (...) run raw', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const p1 = createPlace(testDb, trip.id, { name: 'Eiffel Tower' });
|
||||
const p2 = createPlace(testDb, trip.id, { name: 'Louvre' });
|
||||
const p3 = createPlace(testDb, trip.id, { name: 'Not geocoded' });
|
||||
insertPlaceRegion(p1.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
insertPlaceRegion(p2.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
|
||||
const legacy = testDb.prepare(`SELECT place_id, country_code FROM place_regions WHERE place_id IN (${[p1.id, p2.id, p3.id].join(',')})`).all();
|
||||
const typed = await repo.listCountryCodesForPlaceIds([p1.id, p2.id, p3.id]);
|
||||
expect(typed).toEqual(legacy);
|
||||
expect(typed.map((r) => r.place_id).sort((a, b) => a - b)).toEqual([p1.id, p2.id].sort((a, b) => a - b));
|
||||
});
|
||||
|
||||
it('PLACEREGREPO-002: an empty id array short-circuits to [] without a query', async () => {
|
||||
expect(await repo.listCountryCodesForPlaceIds([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PlaceRegionsRepository.listForPlaceIds (AT28)', () => {
|
||||
it('PLACEREGREPO-003: matches SELECT * FROM place_regions WHERE place_id IN (...) run raw, every column', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const p1 = createPlace(testDb, trip.id);
|
||||
const p2 = createPlace(testDb, trip.id);
|
||||
insertPlaceRegion(p1.id, 'US', 'US-NY', 'New York');
|
||||
insertPlaceRegion(p2.id, 'US', 'US-CA', 'California');
|
||||
|
||||
const legacy = testDb.prepare(`SELECT * FROM place_regions WHERE place_id IN (${[p1.id, p2.id].join(',')})`).all();
|
||||
const typed = await repo.listForPlaceIds([p1.id, p2.id]);
|
||||
expect(typed).toEqual(legacy);
|
||||
});
|
||||
|
||||
it('PLACEREGREPO-004: an empty id array short-circuits to []', async () => {
|
||||
expect(await repo.listForPlaceIds([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PlaceRegionsRepository.listDistinctRegionCodesForCountryAndPlaces (AT23)', () => {
|
||||
it('PLACEREGREPO-005: matches SELECT DISTINCT region_code ... run raw, deduped across places sharing the region', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const p1 = createPlace(testDb, trip.id);
|
||||
const p2 = createPlace(testDb, trip.id);
|
||||
const p3 = createPlace(testDb, trip.id);
|
||||
insertPlaceRegion(p1.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
insertPlaceRegion(p2.id, 'FR', 'FR-IDF', 'Île-de-France'); // same region, dedup target
|
||||
insertPlaceRegion(p3.id, 'DE', 'DE-BY', 'Bavaria'); // different country, excluded
|
||||
|
||||
const legacy = testDb.prepare(
|
||||
`SELECT DISTINCT region_code FROM place_regions WHERE country_code = ? AND place_id IN (${[p1.id, p2.id, p3.id].join(',')})`,
|
||||
).all('FR');
|
||||
const typed = await repo.listDistinctRegionCodesForCountryAndPlaces('FR', [p1.id, p2.id, p3.id]);
|
||||
expect(typed).toEqual(legacy.map((r: unknown) => (r as { region_code: string }).region_code));
|
||||
expect(typed).toEqual(['FR-IDF']);
|
||||
});
|
||||
|
||||
it('PLACEREGREPO-006: an empty id array short-circuits to []', async () => {
|
||||
expect(await repo.listDistinctRegionCodesForCountryAndPlaces('FR', [])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PlaceRegionsRepository.countPlacesByCountryForTrip (AT40)', () => {
|
||||
it('PLACEREGREPO-007: matches the legacy GROUP BY statement, ordered by count DESC then country ASC, NULL country excluded', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const other = createTrip(testDb, user.id);
|
||||
const fr1 = createPlace(testDb, trip.id);
|
||||
const fr2 = createPlace(testDb, trip.id);
|
||||
const de1 = createPlace(testDb, trip.id);
|
||||
const noRegion = createPlace(testDb, trip.id);
|
||||
insertPlaceRegion(fr1.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
insertPlaceRegion(fr2.id, 'FR', 'FR-PAC', 'Provence');
|
||||
insertPlaceRegion(de1.id, 'DE', 'DE-BY', 'Bavaria');
|
||||
// A different trip's place must not leak in.
|
||||
const foreign = createPlace(testDb, other.id);
|
||||
insertPlaceRegion(foreign.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
void noRegion;
|
||||
|
||||
const legacy = testDb.prepare(`
|
||||
SELECT pr.country_code, COUNT(DISTINCT p.id) AS places
|
||||
FROM place_regions pr JOIN places p ON p.id = pr.place_id
|
||||
WHERE p.trip_id = ? AND pr.country_code IS NOT NULL
|
||||
GROUP BY pr.country_code ORDER BY places DESC, pr.country_code ASC`).all(trip.id);
|
||||
const typed = await repo.countPlacesByCountryForTrip(trip.id);
|
||||
expect(typed).toEqual(legacy);
|
||||
expect(typed).toEqual([{ country_code: 'FR', places: 2 }, { country_code: 'DE', places: 1 }]);
|
||||
});
|
||||
|
||||
it('PLACEREGREPO-008: empty array for a trip with no geocoded places', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
expect(await repo.countPlacesByCountryForTrip(trip.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PlaceRegionsRepository.listVisitedCountryCodesForUser (AT44)', () => {
|
||||
it('PLACEREGREPO-009: matches the legacy statement — owner and member trips count, only started trips, deduped, NULL country excluded', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: member } = createUser(testDb);
|
||||
const { user: stranger } = createUser(testDb);
|
||||
const started = createTrip(testDb, owner.id, { start_date: '2020-01-01', end_date: '2020-01-10' });
|
||||
const notStarted = createTrip(testDb, owner.id, { start_date: '2099-01-01', end_date: '2099-01-10' });
|
||||
addTripMember(testDb, started.id, member.id);
|
||||
|
||||
const startedPlace = createPlace(testDb, started.id);
|
||||
insertPlaceRegion(startedPlace.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
const startedPlace2 = createPlace(testDb, started.id);
|
||||
insertPlaceRegion(startedPlace2.id, 'FR', 'FR-PAC', 'Provence'); // same country, dedup target
|
||||
const notStartedPlace = createPlace(testDb, notStarted.id);
|
||||
insertPlaceRegion(notStartedPlace.id, 'DE', 'DE-BY', 'Bavaria'); // not started, excluded
|
||||
|
||||
const today = '2026-01-01';
|
||||
const legacy = testDb.prepare(`
|
||||
SELECT DISTINCT pr.country_code
|
||||
FROM place_regions pr
|
||||
JOIN places p ON p.id = pr.place_id
|
||||
JOIN trips t ON p.trip_id = t.id
|
||||
LEFT JOIN trip_members tm ON t.id = tm.trip_id
|
||||
WHERE (t.user_id = ? OR tm.user_id = ?)
|
||||
AND pr.country_code IS NOT NULL
|
||||
AND COALESCE(t.start_date, t.end_date) IS NOT NULL
|
||||
AND COALESCE(t.start_date, t.end_date) <= ?`).all(owner.id, owner.id, today);
|
||||
|
||||
const typedOwner = await repo.listVisitedCountryCodesForUser(owner.id, today);
|
||||
expect(typedOwner).toEqual(legacy.map((r: unknown) => (r as { country_code: string }).country_code));
|
||||
expect(typedOwner).toEqual(['FR']);
|
||||
|
||||
const legacyMember = testDb.prepare(`
|
||||
SELECT DISTINCT pr.country_code
|
||||
FROM place_regions pr
|
||||
JOIN places p ON p.id = pr.place_id
|
||||
JOIN trips t ON p.trip_id = t.id
|
||||
LEFT JOIN trip_members tm ON t.id = tm.trip_id
|
||||
WHERE (t.user_id = ? OR tm.user_id = ?)
|
||||
AND pr.country_code IS NOT NULL
|
||||
AND COALESCE(t.start_date, t.end_date) IS NOT NULL
|
||||
AND COALESCE(t.start_date, t.end_date) <= ?`).all(member.id, member.id, today);
|
||||
const typedMember = await repo.listVisitedCountryCodesForUser(member.id, today);
|
||||
expect(typedMember).toEqual(legacyMember.map((r: unknown) => (r as { country_code: string }).country_code));
|
||||
expect(typedMember).toEqual(['FR']);
|
||||
|
||||
expect(await repo.listVisitedCountryCodesForUser(stranger.id, today)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,7 @@ import { TRACK_COLORS } from '@trek/shared';
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { createCategory, createDay, createDayAssignment, createPlace, createTag, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { addTripMember, createCategory, createDay, createDayAssignment, createPlace, createTag, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { Places } from '../../../../src/db/entities/Places.entity';
|
||||
import type { PlacesRepository } from '../../../../src/db/repositories/Places.repository';
|
||||
|
||||
@@ -1312,3 +1312,65 @@ describe('PlacesRepository.isTrackInTrip (RT13, RoadtripService.trackExists)', (
|
||||
expect(await places.isTrackInTrip(place.id, trip.id)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Plan 4 Task 8b-2, item 3 (3f L6 carry): AtlasService's own reads on
|
||||
// PlacesRepository — AT2 (listForTripIds) and AT41 (listAddressesForUser) —
|
||||
// had no repository-level toEqual(<legacy raw>) parity test.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('PlacesRepository.listForTripIds (AT2, AtlasService#getPlacesForTrips)', () => {
|
||||
it('PLACEREPO-036: matches SELECT * FROM places WHERE trip_id IN (...) run raw', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const tripA = createTrip(testDb, user.id);
|
||||
const tripB = createTrip(testDb, user.id);
|
||||
const other = createTrip(testDb, user.id);
|
||||
const pa = createPlace(testDb, tripA.id, { name: 'A' });
|
||||
const pb = createPlace(testDb, tripB.id, { name: 'B' });
|
||||
createPlace(testDb, other.id, { name: 'Not in the batch' });
|
||||
|
||||
const legacy = testDb.prepare(`SELECT * FROM places WHERE trip_id IN (${[tripA.id, tripB.id].join(',')})`).all();
|
||||
const typed = await places.listForTripIds([tripA.id, tripB.id]);
|
||||
expect(typed).toEqual(legacy);
|
||||
expect(typed.map((p) => p.id).sort((a, b) => a - b)).toEqual([pa.id, pb.id].sort((a, b) => a - b));
|
||||
});
|
||||
|
||||
it('PLACEREPO-037: an empty trip-id array short-circuits to [] without a query', async () => {
|
||||
expect(await places.listForTripIds([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PlacesRepository.listAddressesForUser (AT41, AtlasService#getTravelStats)', () => {
|
||||
it('PLACEREPO-038: matches the legacy DISTINCT statement, owner and member trips, region_name joined from place_regions', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: member } = createUser(testDb);
|
||||
const { user: stranger } = createUser(testDb);
|
||||
const trip = createTrip(testDb, owner.id);
|
||||
addTripMember(testDb, trip.id, member.id);
|
||||
const place = createPlace(testDb, trip.id, { name: 'Tower', lat: 48.85, lng: 2.35 });
|
||||
testDb.prepare('UPDATE places SET address = ? WHERE id = ?').run('5 Avenue Anatole France', place.id);
|
||||
testDb.prepare('INSERT INTO place_regions (place_id, country_code, region_code, region_name) VALUES (?, ?, ?, ?)')
|
||||
.run(place.id, 'FR', 'FR-IDF', 'Île-de-France');
|
||||
const noAddress = createPlace(testDb, trip.id, { name: 'No address' });
|
||||
testDb.prepare('UPDATE places SET address = NULL, lat = NULL, lng = NULL WHERE id = ?').run(noAddress.id);
|
||||
|
||||
const legacy = testDb.prepare(`
|
||||
SELECT DISTINCT p.address, p.lat, p.lng, pr.region_name
|
||||
FROM places p JOIN trips t ON p.trip_id = t.id
|
||||
LEFT JOIN trip_members tm ON t.id = tm.trip_id
|
||||
LEFT JOIN place_regions pr ON pr.place_id = p.id
|
||||
WHERE t.user_id = ? OR tm.user_id = ?`).all(owner.id, owner.id);
|
||||
|
||||
const typedOwner = await places.listAddressesForUser(owner.id);
|
||||
expect(typedOwner).toEqual(legacy);
|
||||
expect(typedOwner).toEqual([
|
||||
{ address: '5 Avenue Anatole France', lat: 48.85, lng: 2.35, region_name: 'Île-de-France' },
|
||||
{ address: null, lat: null, lng: null, region_name: null },
|
||||
]);
|
||||
|
||||
const typedMember = await places.listAddressesForUser(member.id);
|
||||
expect(typedMember).toEqual(typedOwner);
|
||||
|
||||
expect(await places.listAddressesForUser(stranger.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -970,3 +970,37 @@ describe('TripsRepository.listIdTitleOrderedByTitle (Plan 4 Task 1, RI2)', () =>
|
||||
expect(await trips.listIdTitleOrderedByTitle()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Plan 4 Task 8b-2, item 3 (3f L6 carry): AT42 (AtlasService#getTravelStats)
|
||||
// had no repository-level toEqual(<legacy raw>) parity test.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('TripsRepository.countTripsAndDaysForUser (AT42, AtlasService#getTravelStats)', () => {
|
||||
it('TRIPREPO-066: matches the legacy COUNT(DISTINCT) statement, owner and member trips, archived trips still counted', async () => {
|
||||
const { user: owner } = createUser(testDb);
|
||||
const { user: member } = createUser(testDb);
|
||||
const { user: stranger } = createUser(testDb);
|
||||
const tripA = createTrip(testDb, owner.id);
|
||||
createDay(testDb, tripA.id);
|
||||
createDay(testDb, tripA.id);
|
||||
const tripB = createTrip(testDb, owner.id); // archived, still counts
|
||||
testDb.prepare('UPDATE trips SET is_archived = 1 WHERE id = ?').run(tripB.id);
|
||||
createDay(testDb, tripB.id);
|
||||
addTripMember(testDb, tripA.id, member.id);
|
||||
|
||||
const legacy = testDb.prepare(`
|
||||
SELECT COUNT(DISTINCT t.id) as trips, COUNT(DISTINCT d.id) as days
|
||||
FROM trips t LEFT JOIN days d ON d.trip_id = t.id LEFT JOIN trip_members tm ON t.id = tm.trip_id
|
||||
WHERE (t.user_id = ? OR tm.user_id = ?)`).get(owner.id, owner.id) as { trips: number; days: number };
|
||||
|
||||
const typedOwner = await trips.countTripsAndDaysForUser(owner.id);
|
||||
expect(typedOwner).toEqual(legacy);
|
||||
expect(typedOwner).toEqual({ trips: 2, days: 3 });
|
||||
|
||||
const typedMember = await trips.countTripsAndDaysForUser(member.id);
|
||||
expect(typedMember).toEqual({ trips: 1, days: 2 });
|
||||
|
||||
expect(await trips.countTripsAndDaysForUser(stranger.id)).toEqual({ trips: 0, days: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* `VisitedCountriesRepository` — Plan 4 Task 8b-2 item 3 (3f L6 carry):
|
||||
* AT10 (`listForUser`) and AT5/AT7/AT43 (`listCodesForUser`) had no
|
||||
* repository-level `toEqual(<legacy raw>)` parity test. Every nullable
|
||||
* column (`created_at`, via the schema's `DEFAULT CURRENT_TIMESTAMP`) is
|
||||
* exercised through the ordinary insert path, matching
|
||||
* `ShareTokens.repository.test.ts`'s shape.
|
||||
*/
|
||||
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 } from '../../../helpers/factories';
|
||||
import { VisitedCountries } from '../../../../src/db/entities/VisitedCountries.entity';
|
||||
import type { VisitedCountriesRepository } from '../../../../src/db/repositories/VisitedCountries.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let repo: VisitedCountriesRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
repo = t.repo(VisitedCountries);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
describe('VisitedCountriesRepository.listForUser (AT10)', () => {
|
||||
it('VISCOUNTRYREPO-001: matches SELECT country_code, created_at, source FROM visited_countries WHERE user_id = ? ORDER BY created_at DESC run raw, scoped to the user', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { user: other } = createUser(testDb, { username: 'other' });
|
||||
testDb.prepare('INSERT INTO visited_countries (user_id, country_code, source, created_at) VALUES (?, ?, ?, ?)')
|
||||
.run(user.id, 'DE', 'manual', '2026-01-01T00:00:00.000Z');
|
||||
testDb.prepare('INSERT INTO visited_countries (user_id, country_code, source, created_at) VALUES (?, ?, ?, ?)')
|
||||
.run(user.id, 'FR', 'dawarich', '2026-02-01T00:00:00.000Z');
|
||||
testDb.prepare('INSERT INTO visited_countries (user_id, country_code, source, created_at) VALUES (?, ?, ?, ?)')
|
||||
.run(other.id, 'JP', 'manual', '2026-03-01T00:00:00.000Z');
|
||||
|
||||
const legacy = testDb.prepare('SELECT country_code, created_at, source FROM visited_countries WHERE user_id = ? ORDER BY created_at DESC').all(user.id);
|
||||
const rows = await repo.listForUser(user.id);
|
||||
|
||||
expect(rows).toEqual(legacy);
|
||||
expect(rows.map((r) => r.country_code)).toEqual(['FR', 'DE']);
|
||||
});
|
||||
|
||||
it('VISCOUNTRYREPO-002: empty array for a user with nothing visited', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await repo.listForUser(user.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('VisitedCountriesRepository.listCodesForUser (AT5/AT7/AT43)', () => {
|
||||
it('VISCOUNTRYREPO-003: matches SELECT country_code FROM visited_countries WHERE user_id = ? run raw', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('INSERT INTO visited_countries (user_id, country_code, source) VALUES (?, ?, ?)').run(user.id, 'DE', 'manual');
|
||||
testDb.prepare('INSERT INTO visited_countries (user_id, country_code, source) VALUES (?, ?, ?)').run(user.id, 'FR', 'manual');
|
||||
|
||||
const legacy = testDb.prepare('SELECT country_code FROM visited_countries WHERE user_id = ?').all(user.id);
|
||||
const codes = await repo.listCodesForUser(user.id);
|
||||
|
||||
expect(codes).toEqual(legacy.map((r) => (r as { country_code: string }).country_code));
|
||||
expect(codes.sort()).toEqual(['DE', 'FR']);
|
||||
});
|
||||
|
||||
it('VISCOUNTRYREPO-004: empty array for a user with nothing visited', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await repo.listCodesForUser(user.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* `VisitedRegionsRepository` — Plan 4 Task 8b-2 item 3 (3f L6 carry): AT21
|
||||
* (`listForUser`), AT24 (`listRegionCodesForCountry`) and AT25
|
||||
* (`findCountryCode`) had no repository-level `toEqual(<legacy raw>)`
|
||||
* parity test. AT25's JS fallback (`countryCodeFromRegionCode`, exercised
|
||||
* when no row matches) is a service concern, not this repository's — this
|
||||
* file pins only the raw statement's own `undefined` shape on a miss.
|
||||
*/
|
||||
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 } from '../../../helpers/factories';
|
||||
import { VisitedRegions } from '../../../../src/db/entities/VisitedRegions.entity';
|
||||
import type { VisitedRegionsRepository } from '../../../../src/db/repositories/VisitedRegions.repository';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
let repo: VisitedRegionsRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb);
|
||||
repo = t.repo(VisitedRegions);
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
describe('VisitedRegionsRepository.listForUser (AT21)', () => {
|
||||
it('VISREGREPO-001: matches SELECT region_code, region_name, country_code FROM visited_regions WHERE user_id = ? ORDER BY created_at DESC run raw', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { user: other } = createUser(testDb, { username: 'other' });
|
||||
testDb.prepare('INSERT INTO visited_regions (user_id, region_code, region_name, country_code, created_at) VALUES (?, ?, ?, ?, ?)')
|
||||
.run(user.id, 'DE-BY', 'Bavaria', 'DE', '2026-01-01T00:00:00.000Z');
|
||||
testDb.prepare('INSERT INTO visited_regions (user_id, region_code, region_name, country_code, created_at) VALUES (?, ?, ?, ?, ?)')
|
||||
.run(user.id, 'DE-BE', 'Berlin', 'DE', '2026-02-01T00:00:00.000Z');
|
||||
testDb.prepare('INSERT INTO visited_regions (user_id, region_code, region_name, country_code, created_at) VALUES (?, ?, ?, ?, ?)')
|
||||
.run(other.id, 'FR-IDF', 'Ile-de-France', 'FR', '2026-03-01T00:00:00.000Z');
|
||||
|
||||
const legacy = testDb.prepare('SELECT region_code, region_name, country_code FROM visited_regions WHERE user_id = ? ORDER BY created_at DESC').all(user.id);
|
||||
const rows = await repo.listForUser(user.id);
|
||||
|
||||
expect(rows).toEqual(legacy);
|
||||
expect(rows.map((r) => r.region_code)).toEqual(['DE-BE', 'DE-BY']);
|
||||
});
|
||||
|
||||
it('VISREGREPO-002: empty array for a user with nothing visited', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await repo.listForUser(user.id)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('VisitedRegionsRepository.listRegionCodesForCountry (AT24)', () => {
|
||||
it('VISREGREPO-003: matches SELECT region_code FROM visited_regions WHERE user_id = ? AND country_code = ? run raw', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES (?, ?, ?, ?)').run(user.id, 'DE-BY', 'Bavaria', 'DE');
|
||||
testDb.prepare('INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES (?, ?, ?, ?)').run(user.id, 'FR-IDF', 'Ile-de-France', 'FR');
|
||||
|
||||
const legacy = testDb.prepare('SELECT region_code FROM visited_regions WHERE user_id = ? AND country_code = ?').all(user.id, 'DE');
|
||||
const codes = await repo.listRegionCodesForCountry(user.id, 'DE');
|
||||
|
||||
expect(codes).toEqual(legacy.map((r) => (r as { region_code: string }).region_code));
|
||||
expect(codes).toEqual(['DE-BY']);
|
||||
});
|
||||
|
||||
it('VISREGREPO-004: empty array for a country with nothing visited', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await repo.listRegionCodesForCountry(user.id, 'DE')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('VisitedRegionsRepository.findCountryCode (AT25)', () => {
|
||||
it('VISREGREPO-005: matches SELECT country_code FROM visited_regions WHERE user_id = ? AND region_code = ? run raw', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
testDb.prepare('INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES (?, ?, ?, ?)').run(user.id, 'DE-BY', 'Bavaria', 'DE');
|
||||
|
||||
const legacy = testDb.prepare('SELECT country_code FROM visited_regions WHERE user_id = ? AND region_code = ?').get(user.id, 'DE-BY') as { country_code: string };
|
||||
expect(await repo.findCountryCode(user.id, 'DE-BY')).toEqual(legacy.country_code);
|
||||
});
|
||||
|
||||
it('VISREGREPO-006: undefined for an unmarked region — the JS fallback is the service\'s concern, not this repository\'s', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
expect(await repo.findCountryCode(user.id, 'DE-BY')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user