fix: receipts, cached places, chat previews and the Google-route tool

Saving an expense twice while its receipt is also attached to a place and a
booking hit the new unique index on (file, expense): the row kept from the
first save was skipped, the next spare link row was adopted into a duplicate
pair, and the constraint threw inside the transaction. The whole edit rolled
back with a 500, every time, so that expense could never be changed again. The
adopt now skips a link the expense already holds, and a receipt named twice in
one save is linked once.

The offline place cache was keyed on the place id alone while it is owned and
evicted per trip, so two trips over the same city took each other's rows: the
second write claimed them, switching the second trip off deleted them for both,
and the first trip's fingerprint still matched so nothing ever refilled them.
Offline search then answered nothing for a trip whose switch read "on". Each
trip keeps its own copies now, and the upgrade clears the fingerprints so the
next sync refills what the rebuild dropped.

The chat composer minted its object URLs inside a state updater, which React
runs twice in development, so one picked image drew two thumbnails and removing
one left a preview for a picture that would not be sent. File and URL are one
record now and every mutator commits a finished list.

import_google_maps_route wrote up to thirty places and their day assignments
under trips:write, which does not imply the places:write that create_place is
gated on, and it was the one non-admin write tool with no demo gate and the
only tool in the repo with no annotations. All three corrected.
This commit is contained in:
Maurice
2026-09-13 22:55:15 +02:00
parent f0e4c9a33d
commit 9deedb80ea
9 changed files with 292 additions and 54 deletions
@@ -1046,6 +1046,39 @@ describe('an expense whose split leaves a remainder', () => {
expect(testDb.prepare('SELECT COUNT(*) c FROM file_links WHERE file_id = ?').get(f4)).toEqual({ c: 1 });
});
it('re-saving an expense whose receipt is also linked elsewhere twice does not 500', () => {
// A file may carry one link row per place and one per booking, and the receipt
// link is written onto a spare one of those. On the second save the row kept
// from last time is skipped, so the next spare used to be adopted into a
// second (file, item) pair — refused by the unique index, thrown inside the
// transaction, and the whole expense edit rolled back. Every time, for good.
const { user: alice } = createUser(testDb);
const trip = createTrip(testDb, alice.id);
const file = Number(testDb.prepare('INSERT INTO trip_files (trip_id, filename, original_name) VALUES (?, ?, ?)').run(trip.id, 'r.jpg', 'r.jpg').lastInsertRowid);
const place = testDb.prepare('INSERT INTO places (trip_id, name) VALUES (?, ?)').run(trip.id, 'Osteria').lastInsertRowid;
const reservation = testDb.prepare("INSERT INTO reservations (trip_id, title, type) VALUES (?, 'Table', 'restaurant')").run(trip.id).lastInsertRowid;
testDb.prepare('INSERT INTO file_links (file_id, place_id) VALUES (?, ?)').run(file, place);
testDb.prepare('INSERT INTO file_links (file_id, reservation_id) VALUES (?, ?)').run(file, reservation);
const item = budget.createBudgetItem(trip.id, { name: 'Dinner', total_price: 40, receipt_file_ids: [file] });
const again = budget.updateBudgetItem(item.id, trip.id, { total_price: 42, receipt_file_ids: [file] });
expect(again?.total_price).toBe(42);
expect(again?.receipts?.map(r => r.id)).toEqual([file]);
expect(testDb.prepare('SELECT COUNT(*) c FROM file_links WHERE file_id = ? AND budget_item_id = ?').get(file, item.id)).toEqual({ c: 1 });
});
it('a receipt named twice in one save is linked once', () => {
const { user: alice } = createUser(testDb);
const trip = createTrip(testDb, alice.id);
const file = Number(testDb.prepare('INSERT INTO trip_files (trip_id, filename, original_name) VALUES (?, ?, ?)').run(trip.id, 'dup.jpg', 'dup.jpg').lastInsertRowid);
const item = budget.createBudgetItem(trip.id, { name: 'Taxi' });
const updated = budget.updateBudgetItem(item.id, trip.id, { receipt_file_ids: [file, file] });
expect(updated?.receipts?.map(r => r.id)).toEqual([file]);
});
it('unlinks receipts on deleteBudgetItem and leaves every file in place', () => {
const { user: alice } = createUser(testDb);
const trip = createTrip(testDb, alice.id);
@@ -22,13 +22,27 @@ describe('roadtrip read tools', () => {
});
it('passes reviewed Google stops and the authenticated user to the importer', async () => {
const routes = { preview: vi.fn(async () => ({ stops: [] })), import: vi.fn(() => ({ imported: 2 })) };
const tool = new GoogleRouteMcp(routes as never, {} as never);
const auth = { isDemoUser: vi.fn(() => false) };
const tool = new GoogleRouteMcp(routes as never, auth as never, {} as never);
await tool.preview({ url: 'https://www.google.com/maps/dir/A/B' });
expect(routes.preview).toHaveBeenCalledWith('https://www.google.com/maps/dir/A/B');
const input = { tripId: 10, dayId: 1, stops: [{ name: 'A', lat: 1, lng: 2 }] };
expect(JSON.stringify(tool.import(input, ctx))).toContain('2');
expect(routes.import).toHaveBeenCalledWith(10, 5, input);
});
it('refuses the Google import for the demo account, like every other write tool', () => {
// The one non-admin write tool that had no gate: a demo session could write
// thirty places and their assignments onto the shared demo trip.
const routes = { preview: vi.fn(), import: vi.fn() };
const auth = { isDemoUser: vi.fn(() => true) };
const tool = new GoogleRouteMcp(routes as never, auth as never, {} as never);
const res = tool.import({ tripId: 10, dayId: 1, stops: [{ name: 'A', lat: 1, lng: 2 }] } as never, ctx);
expect(res.isError).toBe(true);
expect(routes.import).not.toHaveBeenCalled();
});
it('does not expose manual boundaries to nonmembers', async () => {
const boundaries = { list: vi.fn(() => [{ day_number: 1 }]) };
const db = { canAccessTrip: vi.fn(() => false) };