mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
382ba79584
A visit is a place planned on a day of one of the reader's trips; nothing is kept or confirmed by hand. A place keeps a log of changes to its details, links the tours and bookings it is about, and carries tags private to the reader, which a copy into a trip takes along only when picked. Contracts, routes, MCP tools and migrations for both engines.
146 lines
5.6 KiB
TypeScript
146 lines
5.6 KiB
TypeScript
/**
|
|
* PlaceChangesService (PLC-14) in its module with a scripted repository: the
|
|
* details before and after a write, what it records and drops, who it names,
|
|
* and that a write that throws records nothing. The SQL behind it runs in
|
|
* tests/unit/db/repositories/PlaceChangeLog.repository.test.ts and the whole
|
|
* path in tests/e2e/place-history.e2e.test.ts.
|
|
*/
|
|
import { PlaceChangeLog } from '../../../../src/db/entities/PlaceChangeLog.entity';
|
|
import type {
|
|
SavedPlaceDetailsRow,
|
|
TripPlaceDetailsRow,
|
|
} from '../../../../src/db/repositories/PlaceChangeLog.repository';
|
|
import { PlaceChangesModule } from '../../../../src/nest/place-changes/place-changes.module';
|
|
import { PlaceChangesService } from '../../../../src/nest/place-changes/place-changes.service';
|
|
import {
|
|
AUTOMATIC_CHANGE,
|
|
savedPlaceTarget,
|
|
tripPlaceTarget,
|
|
} from '../../../../src/nest/place-changes/place-changes.types';
|
|
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
|
import { createTestModule, type TestModule } from '../../../helpers/test-module';
|
|
import { getRepositoryToken } from '@mikro-orm/nestjs';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const trip = (overrides: Partial<TripPlaceDetailsRow> = {}): TripPlaceDetailsRow => ({
|
|
id: 1,
|
|
name: 'Café',
|
|
address: 'Herrengasse 14',
|
|
lat: 48.2,
|
|
lng: 16.3,
|
|
category_name: 'Food',
|
|
description: null,
|
|
notes: 'Book a table',
|
|
website: null,
|
|
phone: null,
|
|
image_url: null,
|
|
opening_hours: null,
|
|
...overrides,
|
|
});
|
|
|
|
const saved = (overrides: Partial<SavedPlaceDetailsRow> = {}): SavedPlaceDetailsRow => ({
|
|
...trip(),
|
|
id: 2,
|
|
status: 'idea',
|
|
...overrides,
|
|
});
|
|
|
|
const repo = {
|
|
tripPlaceDetails: vi.fn(),
|
|
savedPlaceDetails: vi.fn(),
|
|
tripPlaceIdsWithoutImage: vi.fn(),
|
|
insertChanges: vi.fn(),
|
|
deleteCleared: vi.fn(),
|
|
page: vi.fn(),
|
|
};
|
|
|
|
const db = createSnapshotTestDb();
|
|
let t: TestModule;
|
|
let service: PlaceChangesService;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({
|
|
db,
|
|
imports: [PlaceChangesModule],
|
|
overrides: [{ provide: getRepositoryToken(PlaceChangeLog), useValue: repo }],
|
|
});
|
|
service = t.get(PlaceChangesService);
|
|
});
|
|
beforeEach(() => {
|
|
for (const fn of Object.values(repo)) fn.mockReset();
|
|
repo.tripPlaceDetails.mockResolvedValue([]);
|
|
repo.savedPlaceDetails.mockResolvedValue([]);
|
|
});
|
|
afterAll(async () => {
|
|
await t.close();
|
|
db.close();
|
|
});
|
|
|
|
describe('PlaceChangesService', () => {
|
|
it('PCHG-S-001: records each field a write changed, by who wrote it, and drops the cleared ones', async () => {
|
|
repo.tripPlaceDetails
|
|
.mockResolvedValueOnce([trip()])
|
|
.mockResolvedValueOnce([trip({ name: 'Café Central', notes: '', phone: '+43' })]);
|
|
const write = vi.fn().mockResolvedValue('written');
|
|
|
|
expect(await service.track(tripPlaceTarget(1), 7, write)).toBe('written');
|
|
expect(write).toHaveBeenCalledTimes(1);
|
|
expect(repo.insertChanges).toHaveBeenCalledWith([
|
|
expect.objectContaining({ place_id: 1, field: 'name', old_value: 'Café', new_value: 'Café Central', user_id: 7 }),
|
|
expect.objectContaining({ place_id: 1, field: 'phone', old_value: null, new_value: '+43', user_id: 7 }),
|
|
]);
|
|
expect(repo.deleteCleared).toHaveBeenCalledWith([{ place_id: 1, collection_place_id: null, fields: ['notes'] }]);
|
|
expect(repo.savedPlaceDetails).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('PCHG-S-002: a write that changes nothing records nothing', async () => {
|
|
repo.savedPlaceDetails.mockResolvedValue([saved()]);
|
|
await service.track([savedPlaceTarget(2), savedPlaceTarget(2)], 7, () => Promise.resolve(undefined));
|
|
expect(repo.insertChanges).toHaveBeenCalledWith([]);
|
|
expect(repo.deleteCleared).toHaveBeenCalledWith([{ place_id: null, collection_place_id: 2, fields: [] }]);
|
|
});
|
|
|
|
it('PCHG-S-003: the places can be found inside the transaction, and TREK fills are marked automatic', async () => {
|
|
repo.tripPlaceIdsWithoutImage.mockResolvedValue([1]);
|
|
repo.tripPlaceDetails
|
|
.mockResolvedValueOnce([trip()])
|
|
.mockResolvedValueOnce([trip({ image_url: '/api/maps/place-photo/g/bytes' })]);
|
|
const ids = await service.tripPlacesWithoutImage('g');
|
|
await service.track(
|
|
() => Promise.resolve(ids.map(tripPlaceTarget)),
|
|
AUTOMATIC_CHANGE,
|
|
() => Promise.resolve(1),
|
|
);
|
|
expect(repo.tripPlaceIdsWithoutImage).toHaveBeenCalledWith('g');
|
|
expect(repo.insertChanges).toHaveBeenCalledWith([
|
|
expect.objectContaining({ field: 'image', user_id: null, automatic: 1 }),
|
|
]);
|
|
});
|
|
|
|
it('PCHG-S-004: no places, or a place the write deleted, record nothing; a write that throws neither', async () => {
|
|
const write = vi.fn().mockResolvedValue(0);
|
|
await service.track(() => Promise.resolve([]), null, write);
|
|
expect(write).toHaveBeenCalledTimes(1);
|
|
expect(repo.tripPlaceDetails).not.toHaveBeenCalled();
|
|
|
|
repo.tripPlaceDetails.mockResolvedValueOnce([trip()]).mockResolvedValueOnce([]);
|
|
await service.track(tripPlaceTarget(1), 7, () => Promise.resolve(undefined));
|
|
expect(repo.insertChanges).toHaveBeenCalledWith([]);
|
|
|
|
repo.insertChanges.mockClear();
|
|
repo.tripPlaceDetails.mockResolvedValue([trip()]);
|
|
await expect(service.track(tripPlaceTarget(1), 7, () => Promise.reject(new Error('refused')))).rejects.toThrow(
|
|
'refused',
|
|
);
|
|
expect(repo.insertChanges).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('PCHG-S-005: reads a page of changes as the repository answers it', async () => {
|
|
const rows = [{ id: 9 }];
|
|
repo.page.mockResolvedValue(rows);
|
|
expect(await service.page([1], [2], 10, 5)).toBe(rows);
|
|
expect(repo.page).toHaveBeenCalledWith([1], [2], 10, 5);
|
|
});
|
|
});
|