mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
7c1de6ed82
A journey day now has a list button in its header beside the route, with the number of stays still waiting, and the row of the day's Dawarich stays gets a "Take over day" button; on a phone the journey's More options offers it for the day of the card shown. The dialog reads the day again from the caller's own Dawarich and shows the route and the stays on one map, numbered in the order they were lived and pinned in the colour of the day, beside a list with times, durations, the stays Dawarich only guessed or is unsure about, the ones without a position, the entries the journal has on the day and how many people will see the drafts. When Dawarich does not answer, it says why and shows the stays of the last sync. Open stays start chosen, the same place twice in a row starts joined as one entry under a title and a chosen map position, any two neighbours can be joined or kept apart, a name can be corrected and a stay left out. "Day report" turns every chosen stay into one entry that lists them with their times, at a chosen stay or off the map. The day's route can go along when the journey keeps none yet. Nothing is written before "Take over N drafts", and a blank name or title holds it back with the reason. Stays left out are dismissed and can be restored in the stay list. The take-over writes drafts only, never touches what the journal holds, and is one transaction: every stay is checked to be the caller's and of that day, the entries, the stays they hold and the marked suggestions commit together, and the entries are announced after the commit. A stay the journey holds already is reported instead of written twice, so a retry after a lost answer is harmless; a failed attempt reads the day again and keeps the choices, and a route that fails after the drafts can be kept again alone. Server: migration 046170 adds journey_entry_stays (one row per stay of an entry with its own name, place and times, unique per journey, source, person and visit, gone with its entry), owned by the journey domain through JourneyEntryStaysService. The single stay accept records its stay there too, over REST and MCP alike. DawarichSyncService.refreshDay reads one day's visits and upserts them without flagging anything missing; DawarichDayStaysService lists and settles a day's stays. JourneyDayImportService in journey-recordings serves POST /api/journeys/:id/recordings/:date/stays/review and /import with @trek/shared contracts, and the MCP tools review_journey_day_stays and take_over_journey_day_stays run on the same service and checks. createEntry can write without announcing for a caller that announces after its commit. The desktop dialog and the phone sheet of the route (JRN-01) and of the take-over share one frame each. All 27 locales carry the strings; the Journey and Dawarich wiki pages and the MCP tool list describe it.
175 lines
7.0 KiB
TypeScript
175 lines
7.0 KiB
TypeScript
/**
|
|
* JourneyDayImportMcp (JRN-02): a recorded day taken into a journey with its
|
|
* stays over MCP, on the same service, refusals and journey access as REST.
|
|
* The suite takes its subject from the container over a snapshot test db;
|
|
* only the caller's Dawarich connection and the visits it answers are replaced.
|
|
*/
|
|
import { DawarichVisitSuggestions } from '../../../src/db/entities/DawarichVisitSuggestions.entity';
|
|
import { JourneyEntries } from '../../../src/db/entities/JourneyEntries.entity';
|
|
import type { McpContext } from '../../../src/nest-mcp';
|
|
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
|
|
import { AddonsService } from '../../../src/nest/addons/addons.service';
|
|
import { DawarichClient, type DawarichVisitRaw } from '../../../src/nest/integrations/dawarich.client';
|
|
import { DawarichService } from '../../../src/nest/integrations/dawarich.service';
|
|
import { JourneyDayImportMcp } from '../../../src/nest/journey-recordings/journey-day-import.mcp';
|
|
import { JourneyRecordingsModule } from '../../../src/nest/journey-recordings/journey-recordings.module';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { addJourneyContributor, makeJourney } from '../../helpers/factories/journeys';
|
|
import { countRows, findRows } from '../../helpers/factories/rows';
|
|
import { setAddonEnabled } from '../../helpers/factories/settings';
|
|
import { makeUser } from '../../helpers/factories/users';
|
|
import { resetTestDb } from '../../helpers/test-db';
|
|
import { createTestModule, type TestModule } from '../../helpers/test-module';
|
|
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const db = createSnapshotTestDb();
|
|
const listVisits = vi.fn();
|
|
let t: TestModule;
|
|
let orm: TestOrm;
|
|
let mcp: JourneyDayImportMcp;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({
|
|
db,
|
|
imports: [JourneyRecordingsModule],
|
|
overrides: [
|
|
{ provide: DawarichClient, useValue: { listVisits } },
|
|
{
|
|
provide: DawarichService,
|
|
useValue: {
|
|
getCredentials: async () => ({
|
|
baseUrl: 'https://dawarich.example.com',
|
|
apiKey: 'k',
|
|
allowInsecureTls: false,
|
|
}),
|
|
getConnection: async () => ({ lastSyncAt: null }),
|
|
},
|
|
},
|
|
],
|
|
});
|
|
mcp = t.get(JourneyDayImportMcp);
|
|
orm = await createTestOrm(db);
|
|
});
|
|
beforeEach(async () => {
|
|
resetTestDb(db);
|
|
orm.clear();
|
|
vi.restoreAllMocks();
|
|
listVisits.mockReset();
|
|
await setAddonEnabled(orm, 'journey', true);
|
|
await setAddonEnabled(orm, 'dawarich', true);
|
|
});
|
|
afterAll(async () => {
|
|
await orm.close();
|
|
await t.close();
|
|
db.close();
|
|
});
|
|
|
|
const ctx = (userId: number) => ({ userId }) as McpContext;
|
|
const DAY = '2026-05-14';
|
|
|
|
function read(result: { content: Array<{ type: string; text: string }>; isError?: boolean }) {
|
|
const text = result.content[0]?.text ?? '';
|
|
return result.isError ? { error: text } : JSON.parse(text);
|
|
}
|
|
|
|
function visit(id: number, name: string, from: string, to: string): DawarichVisitRaw {
|
|
return {
|
|
id,
|
|
area_id: null,
|
|
started_at: `${DAY}T${from}:00+02:00`,
|
|
ended_at: `${DAY}T${to}:00+02:00`,
|
|
duration: null,
|
|
name,
|
|
status: 'confirmed',
|
|
confidence: null,
|
|
confidence_band: null,
|
|
place: { latitude: 45.434, longitude: 12.3388, id: null },
|
|
};
|
|
}
|
|
|
|
async function fixture() {
|
|
const { user: owner } = await makeUser(orm);
|
|
const { user: viewer } = await makeUser(orm);
|
|
const journey = await makeJourney(orm, owner.id, { title: 'Venice' });
|
|
await addJourneyContributor(orm, journey.id, viewer.id, 'viewer');
|
|
listVisits.mockResolvedValue({
|
|
visits: [visit(101, 'Piazza San Marco', '09:10', '10:05'), visit(102, 'Rialto', '13:00', '13:30')],
|
|
truncated: false,
|
|
});
|
|
return { owner, viewer, journey };
|
|
}
|
|
|
|
describe('JourneyDayImportMcp', () => {
|
|
it('JRNDAY-MCP-001: reviews the day and takes a stay over as a draft, the other one left out', async () => {
|
|
const f = await fixture();
|
|
|
|
const review = read(await mcp.reviewDayStays({ journeyId: f.journey.id, date: DAY }, ctx(f.owner.id)));
|
|
expect(review).toMatchObject({ journey: { id: f.journey.id, title: 'Venice' }, fresh: true, audience: 1 });
|
|
const [markus, rialto] = review.stays as Array<{ suggestionId: number; status: string }>;
|
|
expect([markus?.status, rialto?.status]).toEqual(['open', 'open']);
|
|
|
|
const result = read(
|
|
await mcp.takeOverDayStays(
|
|
{
|
|
journeyId: f.journey.id,
|
|
date: DAY,
|
|
groups: [{ kind: 'single', stays: [{ suggestionId: markus?.suggestionId ?? 0, name: 'San Marco' }] }],
|
|
skipSuggestionIds: [rialto?.suggestionId ?? 0],
|
|
},
|
|
ctx(f.owner.id),
|
|
),
|
|
);
|
|
expect(result).toMatchObject({ entries: [{ title: 'San Marco', stayCount: 1 }], alreadyTaken: [] });
|
|
expect(await findRows(orm, JourneyEntries, { journey: f.journey.id })).toEqual([
|
|
expect.objectContaining({ title: 'San Marco', is_draft: 1 }),
|
|
]);
|
|
expect((await findRows(orm, DawarichVisitSuggestions, { state: 'dismissed' })).map((row) => row.name)).toEqual([
|
|
'Rialto',
|
|
]);
|
|
});
|
|
|
|
it('JRNDAY-MCP-002: refuses what REST refuses: a viewer reviews nothing, a malformed group writes nothing', async () => {
|
|
const f = await fixture();
|
|
await expect(mcp.reviewDayStays({ journeyId: f.journey.id, date: DAY }, ctx(f.viewer.id))).rejects.toThrow(
|
|
'Not allowed',
|
|
);
|
|
await mcp.reviewDayStays({ journeyId: f.journey.id, date: DAY }, ctx(f.owner.id));
|
|
const [first] = await findRows(orm, DawarichVisitSuggestions, { user: f.owner.id });
|
|
|
|
await expect(
|
|
mcp.takeOverDayStays(
|
|
{
|
|
journeyId: f.journey.id,
|
|
date: DAY,
|
|
groups: [{ kind: 'report', stays: [{ suggestionId: first?.id ?? 0 }], title: 'Day' }],
|
|
},
|
|
ctx(f.owner.id),
|
|
),
|
|
).rejects.toThrow('This take-over cannot be written');
|
|
expect(await countRows(orm, JourneyEntries, {})).toBe(0);
|
|
});
|
|
|
|
it('JRNDAY-MCP-003: both tools need the Journey and the Dawarich addon; reviewing reads, taking over writes', async () => {
|
|
const options = (name: string) => {
|
|
const entry = getEntry(JourneyDayImportMcp as unknown as ClassRef, name);
|
|
if (entry?.kind !== 'tool') throw new Error(`${name} is no tool`);
|
|
return entry.options;
|
|
};
|
|
const enabled = vi.spyOn(t.get(AddonsService), 'isAddonEnabled').mockResolvedValue(true);
|
|
const ALL = ['reviewDayStays', 'takeOverDayStays'];
|
|
const gates = () => Promise.all(ALL.map((name) => options(name).when?.(ctx(1), mcp)));
|
|
expect(await gates()).toEqual([true, true]);
|
|
|
|
enabled.mockImplementation(async (id: string) => id !== 'dawarich');
|
|
expect(await gates()).toEqual([false, false]);
|
|
|
|
enabled.mockImplementation(async (id: string) => id !== 'journey');
|
|
expect(await gates()).toEqual([false, false]);
|
|
|
|
expect(options('reviewDayStays').access).toEqual({ group: 'journey', mode: 'read' });
|
|
expect(options('takeOverDayStays').access).toEqual({ group: 'journey', mode: 'write' });
|
|
});
|
|
});
|