test(server): seed the atlas and notes tools, day removal, doc sync config and plugin runtime specs through the factories

The atlas and notes MCP tool suites, day removal, the doc sync config
service and the plugin runtime conversion suite build their fixtures and
read back what a call left through the ORM factories. Helpers that used
to answer synchronously off the handle (dayRows, range, the reservation
and boundary readers) are async now and every caller awaits them; a
reader that answered undefined for a gone row still does, so the
toBeUndefined/toBeDefined checks keep their meaning.

The one statement left raw rewinds sqlite_sequence, the engine's own
autoincrement table, which no entity maps; it is marked.
This commit is contained in:
Maurice
2026-10-08 16:16:02 +02:00
parent 2b41a55022
commit 2cf853e504
5 changed files with 401 additions and 302 deletions
@@ -27,6 +27,17 @@ import { createUser, createBucketListItem, createVisitedCountry, createTrip, cre
import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness';
import { setAddonEnabled } from '../../helpers/test-db';
import { ADDON_IDS } from '../../../src/addons';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import { countRows, findRow, insertRow, upsertRow } from '../../helpers/factories/rows';
import { makePlace } from '../../helpers/factories/places';
import { makeTrip } from '../../helpers/factories/trips';
import { markCountryVisited } from '../../helpers/factories/atlas';
import { BucketList } from '../../../src/db/entities/BucketList.entity';
import { HiddenRegions } from '../../../src/db/entities/HiddenRegions.entity';
import { PlaceRegions } from '../../../src/db/entities/PlaceRegions.entity';
import { Places } from '../../../src/db/entities/Places.entity';
import { ReservationEndpoints } from '../../../src/db/entities/ReservationEndpoints.entity';
import { VisitedRegions } from '../../../src/db/entities/VisitedRegions.entity';
beforeEach(() => {
setAddonEnabled(testDb, ADDON_IDS.ATLAS, true);
@@ -35,7 +46,14 @@ beforeEach(() => {
delete process.env.DEMO_MODE;
});
afterAll(() => {
let orm: TestOrm;
beforeAll(async () => {
orm = await createTestOrm(testDb);
});
afterAll(async () => {
await orm.close();
testDb.close();
});
@@ -53,25 +71,32 @@ async function withResourceHarness(userId: number, fn: (h: McpHarness) => Promis
}
// A place carrying an address and coordinates, which createPlace does not write.
function insertPlace(tripId: number, name: string, address: string | null, lat: number, lng: number): number {
const r = testDb
.prepare('INSERT INTO places (trip_id, name, address, lat, lng) VALUES (?, ?, ?, ?, ?)')
.run(tripId, name, address, lat, lng);
return r.lastInsertRowid as number;
async function insertPlace(tripId: number, name: string, address: string | null, lat: number, lng: number): Promise<number> {
return (await makePlace(orm, tripId, { name, address, lat, lng, category: null })).id;
}
// The geocoder's answer for a place, pre-seeded so visitedRegions() reads the cache
// and never reaches for Nominatim (same trick as ATLAS-UNIT-020).
function cacheRegion(placeId: number, countryCode: string, regionCode: string, regionName: string): void {
testDb
.prepare('INSERT OR REPLACE INTO place_regions (place_id, country_code, region_code, region_name) VALUES (?, ?, ?, ?)')
.run(placeId, countryCode, regionCode, regionName);
async function cacheRegion(placeId: number, countryCode: string, regionCode: string, regionName: string): Promise<void> {
await upsertRow(orm, PlaceRegions, { place: placeId, country_code: countryCode, region_code: regionCode, region_name: regionName });
}
function insertEndpoint(reservationId: number, role: 'from' | 'to', sequence: number, lat: number, lng: number): void {
testDb
.prepare('INSERT INTO reservation_endpoints (reservation_id, role, sequence, name, lat, lng) VALUES (?, ?, ?, ?, ?, ?)')
.run(reservationId, role, sequence, `Endpoint ${sequence}`, lat, lng);
async function insertEndpoint(reservationId: number, role: 'from' | 'to', sequence: number, lat: number, lng: number): Promise<void> {
await insertRow(orm, ReservationEndpoints, { reservation: reservationId, role, sequence, name: `Endpoint ${sequence}`, lat, lng });
}
/** Marks a region by hand, the way the region route stores it. */
async function markRegion(userId: number, regionCode: string, regionName: string, countryCode: string): Promise<void> {
await insertRow(orm, VisitedRegions, { user: userId, region_code: regionCode, region_name: regionName, country_code: countryCode });
}
/** A wish with no coordinates; returns its id. */
async function insertWish(userId: number, name: string, targetDate?: string): Promise<number> {
return insertRow(orm, BucketList, {
user: userId,
name,
...(targetDate !== undefined ? { target_date: targetDate } : { lat: null, lng: null }),
});
}
// Trips have to sit in the past for their countries and regions to count as
@@ -105,10 +130,10 @@ describe('Tool: get_atlas_stats', () => {
it('carries the passport figures REST answers with: the cities by name and the distance flown', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END });
insertPlace(trip.id, 'Colosseum', 'Colosseum, Rome, Italy', 41.8902, 12.4922);
await insertPlace(trip.id, 'Colosseum', 'Colosseum, Rome, Italy', 41.8902, 12.4922);
const flight = createReservation(testDb, trip.id, { type: 'flight', title: 'FCO-JFK' });
insertEndpoint(flight.id, 'from', 0, 41.8003, 12.2389);
insertEndpoint(flight.id, 'to', 1, 40.6413, -73.7781);
await insertEndpoint(flight.id, 'from', 0, 41.8003, 12.2389);
await insertEndpoint(flight.id, 'to', 1, 40.6413, -73.7781);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'get_atlas_stats', arguments: {} });
@@ -120,8 +145,7 @@ describe('Tool: get_atlas_stats', () => {
expect(data.travel.countries).toContain('US');
// Rome to New York, so a four-figure number rather than a rounding artefact.
expect(data.travel.totalDistanceKm).toBeGreaterThan(6000);
const places = testDb.prepare('SELECT COUNT(*) AS c FROM places WHERE trip_id = ?').get(trip.id) as { c: number };
expect(data.travel.totalPlaces).toBe(places.c);
expect(data.travel.totalPlaces).toBe(await countRows(orm, Places, { trip: trip.id }));
// Rendering data stays out unless asked for.
expect(data.travel.coords).toBeUndefined();
});
@@ -130,16 +154,14 @@ describe('Tool: get_atlas_stats', () => {
it('include_coords adds the per-place coordinates the dashboard map plots', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END });
insertPlace(trip.id, 'Colosseum', 'Colosseum, Rome, Italy', 41.8902, 12.4922);
insertPlace(trip.id, 'Pantheon', 'Pantheon, Rome, Italy', 41.8986, 12.4769);
await insertPlace(trip.id, 'Colosseum', 'Colosseum, Rome, Italy', 41.8902, 12.4922);
await insertPlace(trip.id, 'Pantheon', 'Pantheon, Rome, Italy', 41.8986, 12.4769);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'get_atlas_stats', arguments: { include_coords: true } });
const data = parseToolResult(result) as any;
const withCoords = testDb
.prepare('SELECT COUNT(*) AS c FROM places WHERE trip_id = ? AND lat IS NOT NULL AND lng IS NOT NULL')
.get(trip.id) as { c: number };
expect(data.travel.coords).toHaveLength(withCoords.c);
const withCoords = await countRows(orm, Places, { trip: trip.id, lat: { $ne: null }, lng: { $ne: null } });
expect(data.travel.coords).toHaveLength(withCoords);
expect(data.travel.coords[0]).toEqual({ lat: 41.8902, lng: 12.4922 });
});
});
@@ -170,9 +192,7 @@ describe('Tool: list_visited_regions', () => {
it('groups a manual mark under its country, in the shape the map reads', async () => {
const { user } = createUser(testDb);
testDb.prepare(
'INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES (?, ?, ?, ?)'
).run(user.id, 'FR-75', 'Paris', 'FR');
await markRegion(user.id, 'FR-75', 'Paris', 'FR');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'list_visited_regions', arguments: {} });
const data = parseToolResult(result) as any;
@@ -185,8 +205,8 @@ describe('Tool: list_visited_regions', () => {
it('reports regions derived from trip places, which the manual table never held', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END });
cacheRegion(insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio');
cacheRegion(insertPlace(trip.id, 'Pantheon', null, 41.8986, 12.4769), 'IT', 'IT-62', 'Lazio');
await cacheRegion(await insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio');
await cacheRegion(await insertPlace(trip.id, 'Pantheon', null, 41.8986, 12.4769), 'IT', 'IT-62', 'Lazio');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'list_visited_regions', arguments: {} });
@@ -194,22 +214,19 @@ describe('Tool: list_visited_regions', () => {
expect(data.regions.IT).toEqual([{ code: 'IT-62', name: 'Lazio', placeCount: 2, status: 'visited' }]);
// Nothing was ever marked by hand, so the old manual-only read had no row
// to answer with at all.
const marked = testDb.prepare('SELECT COUNT(*) AS c FROM visited_regions WHERE user_id = ?').get(user.id) as { c: number };
expect(marked.c).toBe(0);
expect(await countRows(orm, VisitedRegions, { user: user.id })).toBe(0);
});
});
it('drops a region the user dismissed, matching the map', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END });
cacheRegion(insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio');
await cacheRegion(await insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio');
await withHarness(user.id, async (h) => {
const unmark = await h.client.callTool({ name: 'unmark_region_visited', arguments: { regionCode: 'IT-62' } });
expect(parseToolResult(unmark)).toEqual({ success: true });
const tombstone = testDb
.prepare('SELECT region_code FROM hidden_regions WHERE user_id = ? AND region_code = ?')
.get(user.id, 'IT-62');
const tombstone = await findRow(orm, HiddenRegions, { user: user.id, region_code: 'IT-62' });
expect(tombstone).toBeTruthy();
const result = await h.client.callTool({ name: 'list_visited_regions', arguments: {} });
@@ -257,10 +274,11 @@ describe('Tool: locate_atlas_region', () => {
countryCode: located.country_code,
},
});
const row = testDb
.prepare('SELECT region_code, country_code FROM visited_regions WHERE user_id = ?')
.get(user.id) as { region_code: string; country_code: string };
expect(row).toEqual({ region_code: located.region_code, country_code: 'IT' });
const row = await findRow(orm, VisitedRegions, { user: user.id });
expect({ region_code: row?.region_code, country_code: row?.country_code }).toEqual({
region_code: located.region_code,
country_code: 'IT',
});
});
});
@@ -309,8 +327,7 @@ describe('Tool: mark_region_visited', () => {
arguments: { regionCode: 'jp-13' },
});
expect(parseToolResult(unmark)).toEqual({ success: true });
const row = testDb.prepare('SELECT 1 FROM visited_regions WHERE user_id = ?').get(user.id);
expect(row).toBeUndefined();
expect(await findRow(orm, VisitedRegions, { user: user.id })).toBeNull();
});
});
@@ -328,7 +345,7 @@ describe('Tool: mark_region_visited', () => {
expect(data.region.name).toBe('California');
expect(data.region.country_code).toBe('US');
expect(data.region.manuallyMarked).toBe(true);
const row = testDb.prepare('SELECT * FROM visited_regions WHERE user_id = ? AND region_code = ?').get(user.id, 'US-CA');
const row = await findRow(orm, VisitedRegions, { user: user.id, region_code: 'US-CA' });
expect(row).toBeTruthy();
});
});
@@ -353,9 +370,7 @@ describe('Tool: mark_region_visited', () => {
describe('Tool: unmark_region_visited', () => {
it('removes region and returns success', async () => {
const { user } = createUser(testDb);
testDb.prepare(
'INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES (?, ?, ?, ?)'
).run(user.id, 'IT-LO', 'Lombardy', 'IT');
await markRegion(user.id, 'IT-LO', 'Lombardy', 'IT');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'unmark_region_visited',
@@ -363,8 +378,7 @@ describe('Tool: unmark_region_visited', () => {
});
const data = parseToolResult(result) as any;
expect(data.success).toBe(true);
const row = testDb.prepare('SELECT * FROM visited_regions WHERE user_id = ? AND region_code = ?').get(user.id, 'IT-LO');
expect(row).toBeUndefined();
expect(await findRow(orm, VisitedRegions, { user: user.id, region_code: 'IT-LO' })).toBeNull();
});
});
@@ -401,9 +415,9 @@ describe('Tool: get_country_atlas_places', () => {
it('uppercases the country code like REST does (post-fold quirk fix)', async () => {
const { user } = createUser(testDb);
testDb.prepare('INSERT INTO visited_countries (user_id, country_code) VALUES (?, ?)').run(user.id, 'JP');
await markCountryVisited(orm, user.id, 'JP');
// A trip so countryPlaces reaches the visited_countries lookup pre-quirk-fix too.
testDb.prepare('INSERT INTO trips (user_id, title) VALUES (?, ?)').run(user.id, 'Japan');
await makeTrip(orm, user.id, { title: 'Japan' });
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'get_country_atlas_places',
@@ -423,10 +437,7 @@ describe('Tool: get_country_atlas_places', () => {
describe('Tool: update_bucket_list_item', () => {
it('updates notes and returns item', async () => {
const { user } = createUser(testDb);
const r = testDb.prepare(
'INSERT INTO bucket_list (user_id, name, lat, lng) VALUES (?, ?, NULL, NULL)'
).run(user.id, 'Visit Tokyo');
const itemId = r.lastInsertRowid as number;
const itemId = await insertWish(user.id, 'Visit Tokyo');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'update_bucket_list_item',
@@ -440,10 +451,7 @@ describe('Tool: update_bucket_list_item', () => {
it('updates name of existing item', async () => {
const { user } = createUser(testDb);
const r = testDb.prepare(
'INSERT INTO bucket_list (user_id, name, lat, lng) VALUES (?, ?, NULL, NULL)'
).run(user.id, 'Old Name');
const itemId = r.lastInsertRowid as number;
const itemId = await insertWish(user.id, 'Old Name');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'update_bucket_list_item',
@@ -467,27 +475,23 @@ describe('Tool: update_bucket_list_item', () => {
it('returns isError when the edit would land on another wish (#1898)', async () => {
const { user } = createUser(testDb);
const insert = testDb.prepare('INSERT INTO bucket_list (user_id, name, target_date) VALUES (?, ?, ?)');
insert.run(user.id, 'Japan', '2027-05');
const itemId = insert.run(user.id, 'Japan', '2028-09').lastInsertRowid as number;
await insertWish(user.id, 'Japan', '2027-05');
const itemId = await insertWish(user.id, 'Japan', '2028-09');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'update_bucket_list_item',
arguments: { itemId, target_date: '2027-05' },
});
expect(result.isError).toBe(true);
const row = testDb.prepare('SELECT target_date FROM bucket_list WHERE id = ?').get(itemId) as { target_date: string };
expect(row.target_date).toBe('2028-09');
const row = await findRow(orm, BucketList, { id: itemId });
expect(row?.target_date).toBe('2028-09');
});
});
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const r = testDb.prepare(
'INSERT INTO bucket_list (user_id, name, lat, lng) VALUES (?, ?, NULL, NULL)'
).run(user.id, 'Bucket Item');
const itemId = r.lastInsertRowid as number;
const itemId = await insertWish(user.id, 'Bucket Item');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'update_bucket_list_item',
@@ -529,11 +533,9 @@ describe('Resource: trek://atlas/regions', () => {
it('returns marked and derived regions together, as the tool does', async () => {
const { user } = createUser(testDb);
testDb.prepare(
'INSERT INTO visited_regions (user_id, region_code, region_name, country_code) VALUES (?, ?, ?, ?)'
).run(user.id, 'ES-CT', 'Catalonia', 'ES');
await markRegion(user.id, 'ES-CT', 'Catalonia', 'ES');
const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END });
cacheRegion(insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio');
await cacheRegion(await insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio');
await withResourceHarness(user.id, async (h) => {
const result = await h.client.readResource({ uri: 'trek://atlas/regions' });
+56 -25
View File
@@ -32,6 +32,12 @@ vi.mock('fs', async (importOriginal) => {
import { resetTestDb } from '../../helpers/test-db';
import { createUser, createTrip, createDay, createDayNote, createCollabNote } from '../../helpers/factories';
import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import { countRows, findRow, updateRows } from '../../helpers/factories/rows';
import { makeTripFile } from '../../helpers/factories/files';
import { CollabNotes } from '../../../src/db/entities/CollabNotes.entity';
import { DayNotes } from '../../../src/db/entities/DayNotes.entity';
import { TripFiles } from '../../../src/db/entities/TripFiles.entity';
beforeEach(() => {
resetTestDb(testDb);
@@ -40,10 +46,31 @@ beforeEach(() => {
delete process.env.DEMO_MODE;
});
afterAll(() => {
let orm: TestOrm;
beforeAll(async () => {
orm = await createTestOrm(testDb);
});
afterAll(async () => {
await orm.close();
testDb.close();
});
/** The stored day note; fails the case when it is gone. */
async function dayNote(id: number) {
const row = await findRow(orm, DayNotes, { id });
if (!row) throw new Error(`no day note ${id}`);
return row;
}
/** The stored collab note; fails the case when it is gone. */
async function collabNote(id: number) {
const row = await findRow(orm, CollabNotes, { id });
if (!row) throw new Error(`no collab note ${id}`);
return row;
}
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
const h = await createMcpHarness({ userId, withResources: false });
try { await fn(h); } finally { await h.cleanup(); }
@@ -104,7 +131,7 @@ describe('Tool: create_day_note', () => {
arguments: { tripId: trip.id, dayId: day.id, text: 'Ferry leaves early', color: '#dc2626', sort_order: 2 },
});
const data = parseToolResult(result) as any;
const row = testDb.prepare('SELECT color, sort_order FROM day_notes WHERE id = ?').get(data.note.id) as any;
const row = await dayNote(data.note.id);
expect(row.color).toBe('#dc2626');
expect(row.sort_order).toBe(2);
});
@@ -120,7 +147,7 @@ describe('Tool: create_day_note', () => {
arguments: { tripId: trip.id, dayId: day.id, text: 'A note' },
});
const data = parseToolResult(result) as any;
const row = testDb.prepare('SELECT color, sort_order FROM day_notes WHERE id = ?').get(data.note.id) as any;
const row = await dayNote(data.note.id);
expect(row.color).toBeNull();
expect(row.sort_order).toBe(9999);
});
@@ -136,7 +163,7 @@ describe('Tool: create_day_note', () => {
arguments: { tripId: trip.id, dayId: day.id, text: 'A note', color: '#ff0000' },
});
expect(result.isError).toBe(true);
expect(testDb.prepare('SELECT COUNT(*) AS n FROM day_notes WHERE day_id = ?').get(day.id)).toEqual({ n: 0 });
expect(await countRows(orm, DayNotes, { day: day.id })).toBe(0);
});
});
@@ -210,7 +237,7 @@ describe('Tool: update_day_note', () => {
name: 'update_day_note',
arguments: { tripId: trip.id, dayId: day.id, noteId: note.id, color: '#16a34a', sort_order: 1.5 },
});
const row = testDb.prepare('SELECT color, sort_order FROM day_notes WHERE id = ?').get(note.id) as any;
const row = await dayNote(note.id);
expect(row.color).toBe('#16a34a');
expect(row.sort_order).toBe(1.5);
});
@@ -221,13 +248,13 @@ describe('Tool: update_day_note', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const note = createDayNote(testDb, day.id, trip.id);
testDb.prepare('UPDATE day_notes SET color = ? WHERE id = ?').run('#2563eb', note.id);
await updateRows(orm, DayNotes, { id: note.id }, { color: '#2563eb' });
await withHarness(user.id, async (h) => {
await h.client.callTool({
name: 'update_day_note',
arguments: { tripId: trip.id, dayId: day.id, noteId: note.id, color: null },
});
expect((testDb.prepare('SELECT color FROM day_notes WHERE id = ?').get(note.id) as any).color).toBeNull();
expect((await dayNote(note.id)).color).toBeNull();
});
});
@@ -236,13 +263,13 @@ describe('Tool: update_day_note', () => {
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const note = createDayNote(testDb, day.id, trip.id, { sort_order: 3 });
testDb.prepare('UPDATE day_notes SET color = ? WHERE id = ?').run('#9333ea', note.id);
await updateRows(orm, DayNotes, { id: note.id }, { color: '#9333ea' });
await withHarness(user.id, async (h) => {
await h.client.callTool({
name: 'update_day_note',
arguments: { tripId: trip.id, dayId: day.id, noteId: note.id, text: 'Only the text changed' },
});
const row = testDb.prepare('SELECT color, sort_order FROM day_notes WHERE id = ?').get(note.id) as any;
const row = await dayNote(note.id);
expect(row.color).toBe('#9333ea');
expect(row.sort_order).toBe(3);
});
@@ -259,7 +286,7 @@ describe('Tool: update_day_note', () => {
arguments: { tripId: trip.id, dayId: day.id, noteId: note.id, color: 'red' },
});
expect(result.isError).toBe(true);
expect((testDb.prepare('SELECT color FROM day_notes WHERE id = ?').get(note.id) as any).color).toBeNull();
expect((await dayNote(note.id)).color).toBeNull();
});
});
@@ -311,7 +338,7 @@ describe('Tool: delete_day_note', () => {
const result = await h.client.callTool({ name: 'delete_day_note', arguments: { tripId: trip.id, dayId: day.id, noteId: note.id } });
const data = parseToolResult(result) as any;
expect(data.success).toBe(true);
expect(testDb.prepare('SELECT id FROM day_notes WHERE id = ?').get(note.id)).toBeUndefined();
expect(await findRow(orm, DayNotes, { id: note.id })).toBeNull();
});
});
@@ -469,7 +496,7 @@ describe('Tool: create_collab_note', () => {
});
const data = parseToolResult(result) as any;
expect(data.note.website).toBe('https://example.com/tour');
expect((testDb.prepare('SELECT website FROM collab_notes WHERE id = ?').get(data.note.id) as any).website)
expect((await collabNote(data.note.id)).website)
.toBe('https://example.com/tour');
});
});
@@ -480,7 +507,7 @@ describe('Tool: create_collab_note', () => {
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'create_collab_note', arguments: { tripId: trip.id, title: 'Plain note' } });
const data = parseToolResult(result) as any;
expect((testDb.prepare('SELECT website FROM collab_notes WHERE id = ?').get(data.note.id) as any).website).toBeNull();
expect((await collabNote(data.note.id)).website).toBeNull();
});
});
@@ -534,7 +561,7 @@ describe('Tool: update_collab_note', () => {
name: 'update_collab_note',
arguments: { tripId: trip.id, noteId: note.id, website: 'https://example.com/museum' },
});
expect((testDb.prepare('SELECT website FROM collab_notes WHERE id = ?').get(note.id) as any).website)
expect((await collabNote(note.id)).website)
.toBe('https://example.com/museum');
});
});
@@ -543,10 +570,10 @@ describe('Tool: update_collab_note', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const note = createCollabNote(testDb, trip.id, user.id);
testDb.prepare('UPDATE collab_notes SET website = ? WHERE id = ?').run('https://example.com/old', note.id);
await updateRows(orm, CollabNotes, { id: note.id }, { website: 'https://example.com/old' });
await withHarness(user.id, async (h) => {
await h.client.callTool({ name: 'update_collab_note', arguments: { tripId: trip.id, noteId: note.id, website: null } });
expect((testDb.prepare('SELECT website FROM collab_notes WHERE id = ?').get(note.id) as any).website).toBeNull();
expect((await collabNote(note.id)).website).toBeNull();
});
});
@@ -554,10 +581,10 @@ describe('Tool: update_collab_note', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const note = createCollabNote(testDb, trip.id, user.id);
testDb.prepare('UPDATE collab_notes SET website = ? WHERE id = ?').run('https://example.com/keep', note.id);
await updateRows(orm, CollabNotes, { id: note.id }, { website: 'https://example.com/keep' });
await withHarness(user.id, async (h) => {
await h.client.callTool({ name: 'update_collab_note', arguments: { tripId: trip.id, noteId: note.id, title: 'Renamed' } });
expect((testDb.prepare('SELECT website FROM collab_notes WHERE id = ?').get(note.id) as any).website)
expect((await collabNote(note.id)).website)
.toBe('https://example.com/keep');
});
});
@@ -572,7 +599,7 @@ describe('Tool: update_collab_note', () => {
arguments: { tripId: trip.id, noteId: note.id, website: `https://example.com/${'x'.repeat(600)}` },
});
expect(result.isError).toBe(true);
expect((testDb.prepare('SELECT website FROM collab_notes WHERE id = ?').get(note.id) as any).website).toBeNull();
expect((await collabNote(note.id)).website).toBeNull();
});
});
@@ -620,7 +647,7 @@ describe('Tool: delete_collab_note', () => {
const result = await h.client.callTool({ name: 'delete_collab_note', arguments: { tripId: trip.id, noteId: note.id } });
const data = parseToolResult(result) as any;
expect(data.success).toBe(true);
expect(testDb.prepare('SELECT id FROM collab_notes WHERE id = ?').get(note.id)).toBeUndefined();
expect(await findRow(orm, CollabNotes, { id: note.id })).toBeNull();
});
});
@@ -629,9 +656,13 @@ describe('Tool: delete_collab_note', () => {
const trip = createTrip(testDb, user.id);
const note = createCollabNote(testDb, trip.id, user.id);
// Insert a trip_file linked to this note
testDb.prepare(
`INSERT INTO trip_files (trip_id, note_id, filename, original_name, mime_type, file_size) VALUES (?, ?, ?, ?, ?, ?)`
).run(trip.id, note.id, 'test-file.pdf', 'document.pdf', 'application/pdf', 1024);
await makeTripFile(orm, trip.id, {
note: note.id,
filename: 'test-file.pdf',
original_name: 'document.pdf',
mime_type: 'application/pdf',
file_size: 1024,
});
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'delete_collab_note', arguments: { tripId: trip.id, noteId: note.id } });
@@ -639,9 +670,9 @@ describe('Tool: delete_collab_note', () => {
});
// trip_files rows are deleted as part of the transaction
expect(testDb.prepare('SELECT id FROM trip_files WHERE note_id = ?').all(note.id)).toHaveLength(0);
expect(await countRows(orm, TripFiles, { note: note.id })).toBe(0);
// note itself is deleted
expect(testDb.prepare('SELECT id FROM collab_notes WHERE id = ?').get(note.id)).toBeUndefined();
expect(await findRow(orm, CollabNotes, { id: note.id })).toBeNull();
});
it('broadcasts collab:note:deleted event', async () => {