fix(dawarich): name a stay from its geocoded points when Dawarich sends a placeholder

Since 1.22 Dawarich sends a visit's display name, which is "Unknown Location"
for a visit without a place. TREK showed that as the name, although the points
recorded during the stay are reverse geocoded. A stay without a real name now
takes its place's name or address, and otherwise the city most of its points
fall in. A visit without a place also takes the middle of its points as its
coordinate.

Closes #3105
This commit is contained in:
Maurice
2026-11-16 16:34:47 +01:00
parent eb56e77600
commit 04e0dc26e1
7 changed files with 334 additions and 9 deletions
@@ -75,7 +75,8 @@ const CAPABILITIES: DawarichCapabilities = {
};
const listVisits = vi.fn();
const client = { listVisits } as unknown as DawarichClient;
const listGeocodedPoints = vi.fn();
const client = { listVisits, listGeocodedPoints } as unknown as DawarichClient;
/** Reads the same two columns the real service reads, so a row without a key is null. */
const getCredentials = vi.fn(async (userId: number): Promise<DawarichCreds | null> => {
@@ -282,6 +283,7 @@ beforeEach(async () => {
t.clear();
vi.clearAllMocks();
probeCapabilities.mockResolvedValue(CAPABILITIES);
listGeocodedPoints.mockReset().mockResolvedValue([]);
setAddonEnabled(testDb, 'dawarich', true);
USER = createUser(testDb).user.id;
@@ -296,6 +298,78 @@ afterAll(async () => {
// ── Creating suggestions ─────────────────────────────────────────────────────
describe('DawarichSyncService, naming a stay Dawarich sends without a name (#3105)', () => {
const nameless = (id: number) => visit({ id, name: 'Unknown Location', place: null });
it('DAWARICH-SYNC-090: names it after the reverse-geocoded points recorded during it', async () => {
withVisits(nameless(901));
listGeocodedPoints.mockResolvedValue([{ city: 'Cinisi', country_name: 'Italy' }]);
await svc.syncUser(USER);
expect((await only()).name).toBe('Cinisi, Italy');
const [, from, to] = listGeocodedPoints.mock.calls[0] as [DawarichCreds, Date, Date];
expect(from.toISOString()).toBe(`${VISIT_DAY}T09:00:00.000Z`);
expect(to.toISOString()).toBe(`${VISIT_DAY}T12:00:00.000Z`);
});
it('DAWARICH-SYNC-091: a name the visit carries itself needs no points read', async () => {
withVisits(visit({ id: 902, name: 'Hotel Adlon' }));
await svc.syncUser(USER);
expect((await only()).name).toBe('Hotel Adlon');
expect(listGeocodedPoints).not.toHaveBeenCalled();
});
it('DAWARICH-SYNC-092: a found name is kept on the next run without reading the points again', async () => {
withVisits(nameless(903));
listGeocodedPoints.mockResolvedValue([{ city: 'Cinisi', country_name: 'Italy' }]);
await svc.syncUser(USER);
listGeocodedPoints.mockClear();
await svc.syncUser(USER);
expect((await only()).name).toBe('Cinisi, Italy');
expect(listGeocodedPoints).not.toHaveBeenCalled();
});
it('DAWARICH-SYNC-093: points without a geocode settle on "Unnamed stay" until the visit changes', async () => {
withVisits(nameless(904));
await svc.syncUser(USER);
expect((await only()).name).toBe('Unnamed stay');
listGeocodedPoints.mockClear();
await svc.syncUser(USER);
expect(listGeocodedPoints).not.toHaveBeenCalled();
});
it('DAWARICH-SYNC-094: points that could not be read keep the placeholder and are read on the next run', async () => {
withVisits(nameless(905));
listGeocodedPoints.mockRejectedValue(new DawarichError('unreachable', 'down'));
const result = await svc.syncUser(USER);
expect(result.state).toBe('ok');
expect((await only()).name).toBe('Unknown Location');
listGeocodedPoints.mockResolvedValue([{ city: 'Cinisi', country_name: 'Italy' }]);
await svc.syncUser(USER);
expect((await only()).name).toBe('Cinisi, Italy');
});
it('DAWARICH-SYNC-095: after one failed read a run stops asking for the rest', async () => {
withVisits(nameless(906), nameless(907), nameless(908));
listGeocodedPoints.mockRejectedValue(new DawarichError('unreachable', 'down'));
await svc.syncUser(USER);
// The first stay of each window is asked about; the rest wait for the next run.
expect(listGeocodedPoints).toHaveBeenCalledTimes(listVisits.mock.calls.length);
expect((await rows()).map((r) => r.name)).toEqual(['Unknown Location', 'Unknown Location', 'Unknown Location']);
});
});
describe('DawarichSyncService — new visits', () => {
it('DAWARICH-SYNC-001: stores an unseen visit as a suggestion in state "new"', async () => {
withVisits(visit({ id: 501, name: 'Hotel Adlon', confidence: 0.82, confidence_band: 'high' }));
@@ -673,3 +673,26 @@ describe('DawarichClient — findVisitsNear', () => {
await expect(client.findVisitsNear(CREDS, 0, 0, 100, 5)).resolves.toEqual([]);
});
});
describe('DawarichClient, listGeocodedPoints', () => {
it('DAWARICH-CLIENT-100: reads one small page of full points, oldest first, to name a stay (#3105)', async () => {
const points = [{ city: 'Cinisi', country_name: 'Italy' }];
answerWith(reply({ body: points }));
const out = await client.listGeocodedPoints(CREDS, FROM, TO);
const url = requestedUrl(0);
expect(url.pathname).toBe('/api/v1/points');
expect(url.searchParams.get('start_at')).toBe(unixSeconds(FROM));
expect(url.searchParams.get('end_at')).toBe(unixSeconds(TO));
expect(url.searchParams.get('order')).toBe('asc');
expect(url.searchParams.get('per_page')).toBe('50');
expect(url.searchParams.get('slim')).toBeNull();
expect(out).toEqual(points);
});
it('DAWARICH-CLIENT-101: an answer that is not a list names nothing', async () => {
answerWith(reply({ body: { error: 'nope' } }));
await expect(client.listGeocodedPoints(CREDS, FROM, TO)).resolves.toEqual([]);
});
});
@@ -25,6 +25,7 @@ import {
localDateOf,
mergeVisitedCountries,
minutesBetween,
nameFromGeocodedPoints,
normalizeVisit,
offsetMinutesOf,
parseTimeWindow,
@@ -273,6 +274,7 @@ describe('normalizeVisit', () => {
expect(normalizeVisit(BASE_VISIT)).toEqual({
sourceVisitId: '42',
name: 'Cafe Kranzler',
named: true,
lat: 52.5,
lng: 13.4,
startedAt: '2024-06-15T18:30:00+02:00',
@@ -301,6 +303,7 @@ describe('normalizeVisit', () => {
expect(normalizeVisit(sparse)).toEqual({
sourceVisitId: '9',
name: 'Unnamed stay',
named: false,
lat: null,
lng: null,
startedAt: '2024-06-15T18:30:00Z',
@@ -423,6 +426,52 @@ describe('normalizeVisit', () => {
expect(result?.durationMinutes).toBe(0);
}
});
it("DAWARICH-VISIT-018: Dawarich's placeholder names count as no name (#3105)", () => {
for (const name of ['Unknown Location', 'Suggested place', ' unknown location ']) {
const result = normalizeVisit(visit({ name, place: { latitude: 38.18, longitude: 13.14, id: null } }));
expect(result).toMatchObject({ name: 'Unnamed stay', named: false });
}
});
it("DAWARICH-VISIT-019: a nameless visit takes its place's name, then the place's address", () => {
const place = { latitude: 52.5, longitude: 13.4, id: 7 };
expect(normalizeVisit(visit({ name: 'Unknown Location', place: { ...place, name: 'Lido' } }))).toMatchObject({
name: 'Lido',
named: true,
});
expect(
normalizeVisit(
visit({ name: 'Suggested place', place: { ...place, name: 'Suggested place', address: 'Via Roma 1, Cinisi' } }),
),
).toMatchObject({ name: 'Via Roma 1, Cinisi', named: true });
});
it('DAWARICH-VISIT-020: a visit without a place is placed at the middle of its points', () => {
const result = normalizeVisit(visit({ place: null, center: { latitude: '38.178824', longitude: 13.140629 } }));
expect(result?.lat).toBeCloseTo(38.178824, 6);
expect(result?.lng).toBeCloseTo(13.140629, 6);
});
});
describe('nameFromGeocodedPoints', () => {
it('DAWARICH-VISIT-021: names a stay after the city most of its points fall in, with the country', () => {
expect(
nameFromGeocodedPoints([
{ city: null, country_name: null },
{ city: 'Terrasini', country_name: 'Italy' },
{ city: 'Cinisi', country_name: 'Italy' },
{ city: 'Cinisi', country_name: 'Italy' },
]),
).toBe('Cinisi, Italy');
});
it('DAWARICH-VISIT-022: falls back to the country, and to nothing when no point was geocoded', () => {
expect(nameFromGeocodedPoints([{ city: '', country_name: 'Italy' }, { country_name: null }])).toBe('Italy');
expect(nameFromGeocodedPoints([{ city: 'Cinisi', country_name: null }])).toBe('Cinisi');
expect(nameFromGeocodedPoints([{ city: null, country_name: null }])).toBeNull();
expect(nameFromGeocodedPoints([])).toBeNull();
});
});
// ── distanceMeters ───────────────────────────────────────────────────────────