Files
TREK/server/tests/unit/nest/assistant/assistant-attach-answer.test.ts
T
Maurice 2f3a0219cd feat(assistant): every attached booking file gets an answer with what to do next
A booking file without a booking ended the answer on the error "No booking
found in this file." with no action, and the user was left to type. Now the
server answers every booking file itself, without a model: one line that
names the file and what TREK found in it, or that it found nothing, why, and
asks what to do with the file. Under it come up to three chips, picked from
what the user may do on the trip and what the file allows: keep it in Files,
make a booking from its text, attach it to a booking or an expense, ask what
is in it, or keep it with the booking just found. A chip sends its words as
an ordinary chat message.

The line and the chips are written in the language of the request from new
assistant.attach.* keys in all 27 locales, through a small helper the sports
proposals now share. The empty answer finishes as stop; no_bookings_found
stays in the contract for the threads older builds wrote. Route files keep
their answer as it was.
2026-10-24 22:32:47 +02:00

98 lines
4.5 KiB
TypeScript

/**
* The server's own answer to an attached booking file: which chips it offers
* for what the file held and what the user may do, the line it writes in the
* user's language, and the words it is written with.
*/
import {
attachChipKeys,
attachLine,
hasLinkTargets,
type AttachFacts,
} from '../../../../src/nest/assistant/assistant-attach-answer';
import type { TripCard } from '../../../../src/nest/assistant/assistant-context';
import { AssistantWords, supportedLocale } from '../../../../src/nest/assistant/assistant-words';
import { describe, expect, it } from 'vitest';
const ALL: AttachFacts = {
found: 0,
readable: true,
canKeep: true,
canLink: true,
hasTargets: true,
canBook: true,
};
const chip = (name: string) => `assistant.attach.chip.${name}`;
describe('attachChipKeys', () => {
it('ASTACH-001: a file without a booking offers to keep it, book from it and attach it, three at most', () => {
expect(attachChipKeys(ALL)).toEqual([chip('keep'), chip('book'), chip('attach')]);
expect(attachChipKeys({ ...ALL, hasTargets: false })).toEqual([chip('keep'), chip('book'), chip('read')]);
});
it('ASTACH-002: what the user may not do, and what an unreadable file cannot give, is never offered', () => {
expect(attachChipKeys({ ...ALL, canKeep: false })).toEqual([chip('book'), chip('read')]);
expect(attachChipKeys({ ...ALL, canBook: false })).toEqual([chip('keep'), chip('attach'), chip('read')]);
expect(attachChipKeys({ ...ALL, canLink: false, canBook: false })).toEqual([chip('keep'), chip('read')]);
expect(attachChipKeys({ ...ALL, readable: false })).toEqual([chip('keep'), chip('attach')]);
expect(attachChipKeys({ ...ALL, readable: false, canKeep: false })).toEqual([]);
});
it('ASTACH-003: a file with bookings asks to keep it with the booking', () => {
expect(attachChipKeys({ ...ALL, found: 2 })).toEqual([chip('keepWithBooking'), chip('read')]);
expect(attachChipKeys({ ...ALL, found: 1, canLink: false, readable: false })).toEqual([]);
});
});
describe('attachLine', () => {
it('ASTACH-004: names the file and what was found, or why nothing was, and asks what to do', async () => {
const words = await AssistantWords.of('en');
expect(attachLine(words, 'trip.pdf', 1, null)).toBe(
'I found 1 booking in trip.pdf. Add it below if it looks right.',
);
expect(attachLine(words, 'trip.pdf', 3, null)).toBe('I found 3 bookings in trip.pdf. Add the ones you want below.');
expect(attachLine(words, 'scan.pdf', 0, 'no_text')).toBe(
'I could not find a booking in scan.pdf. It holds no readable text, so it is most likely a scan. ' +
'What should I do with the file?',
);
expect(attachLine(words, 'a.txt', 0, null)).toBe(
'I could not find a booking in a.txt. What should I do with the file?',
);
});
});
describe('AssistantWords', () => {
it('ASTACH-005: picks the form the language takes for the count, and English where a table has no key', async () => {
const ru = await AssistantWords.of('ru-RU');
expect(ru.count('assistant.attach.found', 21, { name: 'a.pdf' })).toContain('21 бронирование');
expect(ru.count('assistant.attach.found', 5, { name: 'a.pdf' })).toContain('5 бронирований');
expect(ru.text('assistant.attach.chip.keep')).toBe('Сохрани его в Файлы');
expect(ru.text('assistant.no.such.key')).toBe('assistant.no.such.key');
const unknown = await AssistantWords.of('xx');
expect(unknown.text('assistant.attach.none', { name: 'b.pdf' })).toBe('I could not find a booking in b.pdf.');
expect(unknown.count('assistant.no.such.key', 2)).toBe('assistant.no.such.key');
});
it('ASTACH-006: a locale reads as itself, its language or English', () => {
expect(supportedLocale('zh-TW')).toBe('zh-TW');
expect(supportedLocale('de-AT')).toBe('de');
expect(supportedLocale('xx-YY')).toBe('en');
});
});
describe('hasLinkTargets', () => {
const card = (reservations: number, budgetItems: unknown) =>
({
reservations: new Map(Array.from({ length: reservations }, (_, i) => [i + 1, {}])),
sources: { summary: { budget: { items: budgetItems } }, places: null },
}) as unknown as TripCard;
it('ASTACH-007: a booking or an expense is something to attach a file to', () => {
expect(hasLinkTargets(card(1, []))).toBe(true);
expect(hasLinkTargets(card(0, [{ id: 3 }]))).toBe(true);
expect(hasLinkTargets(card(0, []))).toBe(false);
expect(hasLinkTargets(card(0, undefined))).toBe(false);
});
});