fix(assistant): tighten the journal cards' types and keep their helpers private

A Dawarich stay's row read fields one side of a union did not have, the
photo cards narrowed an entry's day through an optional chain, and the
settings service cast a journey row straight to a record. Helpers only
their own file reads are no longer exported, and the photo tests seed
their rows with insertRow like the other journey suites.
This commit is contained in:
Maurice
2026-10-24 22:12:58 +02:00
parent 0050072b76
commit 8a2cb10f7b
10 changed files with 30 additions and 21 deletions
@@ -6,7 +6,7 @@
* the change links or unlinks marked; what a new journal brings and what a
* deletion or an unlink takes; the receipt that opens the journal.
*/
import type { AssistantApplied, AssistantChangePreview, JourneyHeaderCard } from '@trek/shared';
import type { AssistantApplied, AssistantChangePreview, JourneyHeaderCard, JourneyHeaderTrip } from '@trek/shared';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '../../../../../tests/helpers/render';
@@ -22,13 +22,21 @@ afterEach(() => {
useAssistantStore.setState(ASSISTANT, true);
});
const TOKYO: JourneyHeaderTrip = {
id: 5,
title: 'Tokyo days',
startDate: '2026-11-02',
endDate: '2026-11-08',
change: 'kept',
};
const JAPAN: JourneyHeaderCard = {
kind: 'journeyHeader',
journeyId: 12,
title: 'Japan 2026',
subtitle: null,
change: 'changed',
trips: [{ id: 5, title: 'Tokyo days', startDate: '2026-11-02', endDate: '2026-11-08', change: 'kept' }],
trips: [TOKYO],
entries: 3,
photos: 40,
suggestions: null,
@@ -86,7 +94,7 @@ describe('a change of a journal itself', () => {
journeyId: null,
change: 'new',
subtitle: 'Autumn',
trips: [{ ...JAPAN.trips[0], change: 'link' }],
trips: [{ ...TOKYO, change: 'link' }],
entries: null,
photos: null,
suggestions: 9,
@@ -105,7 +113,7 @@ describe('a change of a journal itself', () => {
first.unmount();
const unlink: JourneyHeaderCard = {
...JAPAN,
trips: [{ ...JAPAN.trips[0], change: 'unlink' }],
trips: [{ ...TOKYO, change: 'unlink' }],
entries: 1,
suggestions: 4,
};
@@ -26,7 +26,7 @@ export interface JournalCardEntry {
}
/** A trip the journey holds. */
export interface JournalCardTrip {
interface JournalCardTrip {
id: number;
title: string;
startDate: string | null;
@@ -73,7 +73,7 @@ export const JOURNAL_DENIED_TOOLS: Readonly<Record<string, string>> = {
* taken into a journal names it `journalId`. The session pins and hides them
* like `journeyId`; renaming the argument would break every MCP client.
*/
export const JOURNEY_PIN_ALIASES: readonly string[] = ['journalId'];
const JOURNEY_PIN_ALIASES: readonly string[] = ['journalId'];
/** Every argument a journey's session pins to its journey. */
export const JOURNEY_PIN_ARGS: readonly string[] = [JOURNEY_PINNED_ARG, ...JOURNEY_PIN_ALIASES];
@@ -30,7 +30,7 @@ export interface JournalHeaderLoaded {
}
/** The tools these cards are for. */
export const JOURNAL_HEADER_TOOLS = [
const JOURNAL_HEADER_TOOLS = [
'create_journey',
'update_journey',
'delete_journey',
@@ -131,7 +131,7 @@ function stayEntry(args: ToolArgs, stay: Row): Row {
/** The row of a stay taken in: what the entry is about, its day, time, place and text. */
export function describeStay(ctx: PreviewContext, args: ToolArgs, loaded: StayLoaded | null): Described {
const entry = loaded?.stay
const entry: Row = loaded?.stay
? stayEntry(args, loaded.stay)
: { title: args.name, story: args.notes, entry_date: args.date };
const day = typeof entry.entry_date === 'string' ? formatDate(ctx, entry.entry_date) : '';
@@ -88,8 +88,8 @@ function photoLook(row: Row | undefined): JourneyPhotoLook | null {
function entryRef(entry: Row | undefined, entryId: number | null): JourneyPhotosEntry | null {
if (entryId === null) return null;
const date =
typeof entry?.entry_date === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(entry.entry_date) ? entry.entry_date : null;
const raw = entry?.entry_date;
const date = typeof raw === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(raw) ? raw : null;
const title =
clipped(entry?.title, JOURNEY_PHOTOS_LIMITS.title) ?? clipped(entry?.location_name, JOURNEY_PHOTOS_LIMITS.title);
return { entryId, title, date };
@@ -156,7 +156,8 @@ export function journalPhotosCard(
/** What an entry is called on a row: its title, else its place, else its day. */
function entryName(ctx: PreviewContext, entry: Row | undefined): string {
const day = typeof entry?.entry_date === 'string' ? formatDate(ctx, entry.entry_date) : '';
const raw = entry?.entry_date;
const day = typeof raw === 'string' ? formatDate(ctx, raw) : '';
return text(entry?.title) || text(entry?.location_name) || day;
}
@@ -30,7 +30,7 @@ export type JourneyEntryUpdateInput = Partial<{
}>;
/** The columns an update writes, as they are stored. */
export interface JourneyEntryColumns {
interface JourneyEntryColumns {
type: string;
title: string | null;
story: string | null;
@@ -25,7 +25,7 @@ export type JourneySettingsInput = Partial<{
}>;
/** The columns an update writes, as they are stored. */
export interface JourneySettingsColumns {
interface JourneySettingsColumns {
title: string;
subtitle: string | null;
cover_gradient: string | null;
@@ -45,7 +45,7 @@ const ALLOWED_STATUSES: readonly string[] = ['draft', 'active', 'completed', 'ar
const ALLOWED_OVERRIDES: readonly (string | null)[] = [null, 'draft', 'live', 'completed'];
/** The flag columns, stored 0/1 and spoken as booleans. */
export const JOURNEY_SETTING_FLAGS = [
const JOURNEY_SETTING_FLAGS = [
'show_trip_tracks',
'photo_location',
'show_verdict',
@@ -13,7 +13,7 @@ import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
/** A settings update as the MCP tool answers it: the journey as stored now, and what the update replaced. */
export interface JourneySettingsUpdate {
interface JourneySettingsUpdate {
journey: Journey;
previous: JourneySettingsPrevious;
}
@@ -47,7 +47,7 @@ export class JourneySettingsService {
const previous = await this.uow.transactional(async () => {
const before = await this.journeysRepo.findById(journeyId);
if (written) await this.journeysRepo.updateFields(journeyId, { ...patch, updated_at: Date.now() });
return previousSettings(before as Readonly<Record<string, unknown>> | undefined, patch);
return previousSettings(before as unknown as Readonly<Record<string, unknown>> | undefined, patch);
});
if (written) await this.journeys.broadcastJourneyEvent(journeyId, 'journey:updated', {}, sid);
const journey = (await this.journeysRepo.findById(journeyId)) as Journey | undefined;
+5 -5
View File
@@ -802,10 +802,10 @@ let assetSeq = 0;
/** A photo in the journey's gallery, and on the entry when one is named; answers its journey photo id. */
async function seedGalleryPhoto(journeyId: number, entryId?: number): Promise<number> {
assetSeq += 1;
const trek = await createRow(orm, TrekPhotos, { provider: 'immich', asset_id: `asset-${assetSeq}` });
const gallery = await createRow(orm, JourneyPhotos, {
const trekId = await insertRow(orm, TrekPhotos, { provider: 'immich', asset_id: `asset-${assetSeq}` });
const galleryId = await insertRow(orm, JourneyPhotos, {
journey: journeyId,
photo: trek.id,
photo: trekId,
caption: null,
shared: 0,
sort_order: 0,
@@ -814,12 +814,12 @@ async function seedGalleryPhoto(journeyId: number, entryId?: number): Promise<nu
if (entryId !== undefined) {
await insertRow(orm, JourneyEntryPhotos, {
entry: entryId,
journeyPhoto: gallery.id,
journeyPhoto: galleryId,
sort_order: 0,
created_at: Date.now(),
});
}
return gallery.id;
return galleryId;
}
describe('journey photo tools', () => {