mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
feat(assistant): expense, payment and settle-up cards, and expense links into Costs
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import type { TripCard } from '../../assistant-context';
|
||||
import type { AnswerInput } from '../card-types';
|
||||
import { finite, recordOf, type Row } from '../card-values';
|
||||
import { rowsOf } from '../undo-snapshot';
|
||||
import {
|
||||
amountOf,
|
||||
codeOf,
|
||||
dateOf,
|
||||
expenseFromRow,
|
||||
fitted,
|
||||
paymentFromRow,
|
||||
personFrom,
|
||||
positiveId,
|
||||
type PeopleCaps,
|
||||
} from './budget-values';
|
||||
import {
|
||||
COST_PER_PERSON_ROWS_MAX,
|
||||
EXPENSE_ROWS_MAX,
|
||||
PAYMENT_ROWS_MAX,
|
||||
SETTLEMENT_BALANCES_MAX,
|
||||
SETTLEMENT_FLOWS_MAX,
|
||||
type CardPerson,
|
||||
type CostPerPersonCard,
|
||||
type ExpensesCard,
|
||||
type PaymentsCard,
|
||||
type SettlementCard,
|
||||
} from '@trek/shared';
|
||||
|
||||
/**
|
||||
* The cards the budget's reads show beside the answer, built from each
|
||||
* read's raw answer: the expense list, settle up, the payments recorded and
|
||||
* what the trip costs each traveller. A read that found nothing shows no card.
|
||||
*/
|
||||
|
||||
/** People an expense row of the list names: the list stays short, a change row names everyone. */
|
||||
const LIST_PEOPLE: PeopleCaps = { shares: 6, payers: 3 };
|
||||
|
||||
/** Counts a card states, kept inside their bounds. */
|
||||
function count(value: number, max: number): number {
|
||||
return Math.min(max, Math.max(0, Math.trunc(value)));
|
||||
}
|
||||
|
||||
/** A person an answer names as `{ user_id, username, avatar_url }`. */
|
||||
function personIn(card: TripCard, raw: unknown): CardPerson | null {
|
||||
const row = recordOf(raw);
|
||||
const id = positiveId(row.user_id);
|
||||
return id === null ? null : personFrom(card, id, row.username, row.avatar_url);
|
||||
}
|
||||
|
||||
/** `list_budget_items`: the page's expenses in the list's order, where they start, the rest counted, the total. */
|
||||
export function expensesAnswer({ value, card }: AnswerInput): ExpensesCard | null {
|
||||
const root = recordOf(value);
|
||||
const tripCurrency = codeOf(root.currency, card.currency);
|
||||
const names = new Map(
|
||||
rowsOf(root.people).flatMap((person): Array<[number, string]> => {
|
||||
const id = positiveId(person.user_id);
|
||||
return id !== null && typeof person.username === 'string' ? [[id, person.username]] : [];
|
||||
}),
|
||||
);
|
||||
const items = rowsOf(root.items);
|
||||
const rows = items.slice(0, EXPENSE_ROWS_MAX).flatMap((item) => {
|
||||
const row = expenseFromRow(card, item, { tripCurrency, names, caps: LIST_PEOPLE, change: 'none' });
|
||||
return row ? [row] : [];
|
||||
});
|
||||
if (rows.length === 0) return null;
|
||||
const total = amountOf(root.total);
|
||||
const all = finite(root.item_count) ?? items.length;
|
||||
return fitted<ExpensesCard>({
|
||||
kind: 'expenses',
|
||||
rows,
|
||||
offset: count(finite(root.offset) ?? 0, 100_000),
|
||||
more: count(all - rows.length, 100_000),
|
||||
total: total === null ? null : { amount: total, currency: tripCurrency },
|
||||
unconverted: count(Array.isArray(root.unconverted_item_ids) ? root.unconverted_item_ids.length : 0, 100_000),
|
||||
});
|
||||
}
|
||||
|
||||
/** The balances worth naming: everyone not square, or all of them when everyone is; largest credit first. */
|
||||
function balancesOf(card: TripCard, rows: Row[]) {
|
||||
const all = rows.flatMap((row) => {
|
||||
const person = personIn(card, row);
|
||||
const balance = amountOf(row.balance);
|
||||
return person && balance !== null ? [{ person, balance }] : [];
|
||||
});
|
||||
const open = all.filter((row) => Math.abs(row.balance) >= 0.005);
|
||||
return (open.length > 0 ? open : all).sort((a, b) => b.balance - a.balance);
|
||||
}
|
||||
|
||||
/** `get_settlement_summary`: who pays whom, the balances, the payments recorded, what no rate converts. */
|
||||
export function settlementAnswer({ value, card }: AnswerInput): SettlementCard | null {
|
||||
const summary = recordOf(recordOf(value).summary);
|
||||
if (!Array.isArray(summary.flows) && !Array.isArray(summary.balances)) return null;
|
||||
const flows = rowsOf(summary.flows).flatMap((flow) => {
|
||||
const from = personIn(card, flow.from);
|
||||
const to = personIn(card, flow.to);
|
||||
const amount = amountOf(flow.amount);
|
||||
return from && to && amount !== null && amount > 0 ? [{ from, to, amount }] : [];
|
||||
});
|
||||
const balances = balancesOf(card, rowsOf(summary.balances));
|
||||
const unconverted = recordOf(summary.unconverted);
|
||||
const listed = (key: string) => (Array.isArray(unconverted[key]) ? (unconverted[key] as unknown[]).length : 0);
|
||||
const currencies = Array.isArray(unconverted.currencies)
|
||||
? unconverted.currencies.filter((code): code is string => typeof code === 'string' && /^[A-Z]{3}$/.test(code))
|
||||
: [];
|
||||
return {
|
||||
kind: 'settlement',
|
||||
currency: codeOf(summary.currency, card.currency),
|
||||
flows: flows.slice(0, SETTLEMENT_FLOWS_MAX),
|
||||
moreFlows: count(flows.length - SETTLEMENT_FLOWS_MAX, 1000),
|
||||
balances: balances.slice(0, SETTLEMENT_BALANCES_MAX),
|
||||
moreBalances: count(balances.length - SETTLEMENT_BALANCES_MAX, 1000),
|
||||
recorded: count(Array.isArray(summary.settlements) ? summary.settlements.length : 0, 100_000),
|
||||
unconverted: {
|
||||
entries: count(listed('item_ids') + listed('settlement_ids'), 100_000),
|
||||
currencies: currencies.slice(0, 6),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** `get_budget_per_person`: each traveller's share, largest first, with what of it is marked paid. */
|
||||
export function costPerPersonAnswer({ value, card }: AnswerInput): CostPerPersonCard | null {
|
||||
const summary = rowsOf(recordOf(value).summary);
|
||||
const rows = summary
|
||||
.flatMap((row) => {
|
||||
const person = personIn(card, row);
|
||||
const share = amountOf(row.total_assigned);
|
||||
if (!person || share === null) return [];
|
||||
return [
|
||||
{ person, share, paid: amountOf(row.total_paid) ?? 0, count: count(finite(row.items_count) ?? 0, 100_000) },
|
||||
];
|
||||
})
|
||||
.sort((a, b) => b.share - a.share);
|
||||
if (rows.length === 0) return null;
|
||||
return {
|
||||
kind: 'costPerPerson',
|
||||
currency: codeOf(summary[0]?.currency, card.currency),
|
||||
rows: rows.slice(0, COST_PER_PERSON_ROWS_MAX),
|
||||
more: count(rows.length - COST_PER_PERSON_ROWS_MAX, 1000),
|
||||
};
|
||||
}
|
||||
|
||||
/** The day a payment is filed under, the newest first: the day it was paid, else the day it was recorded. */
|
||||
function newestFirst(a: Row, b: Row): number {
|
||||
const day = (row: Row) => dateOf(row.settled_at) ?? dateOf(row.created_at) ?? '';
|
||||
const byDay = day(b).localeCompare(day(a));
|
||||
return byDay !== 0 ? byDay : (finite(b.id) ?? 0) - (finite(a.id) ?? 0);
|
||||
}
|
||||
|
||||
/** `list_settlements`: the payments recorded, newest first, the rest counted. */
|
||||
export function paymentsAnswer({ value, card }: AnswerInput): PaymentsCard | null {
|
||||
const all = rowsOf(recordOf(value).settlements).sort(newestFirst);
|
||||
const rows = all.slice(0, PAYMENT_ROWS_MAX).flatMap((row) => {
|
||||
const payment = paymentFromRow(card, row, 'none');
|
||||
return payment ? [payment] : [];
|
||||
});
|
||||
if (rows.length === 0) return null;
|
||||
return fitted<PaymentsCard>({ kind: 'payments', rows, more: count(all.length - rows.length, 100_000) });
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
import type { TripCard } from '../../assistant-context';
|
||||
import type { PreviewContext, ToolArgs } from '../card-types';
|
||||
import { argId, clipped, recordOf, type Row } from '../card-values';
|
||||
import { rowsOf } from '../undo-snapshot';
|
||||
import {
|
||||
ALL_PEOPLE,
|
||||
amountOf,
|
||||
argLine,
|
||||
budgetRows,
|
||||
codeOf,
|
||||
dateOf,
|
||||
expenseFromRow,
|
||||
payersOf,
|
||||
paymentFromRow,
|
||||
personFrom,
|
||||
placeOf,
|
||||
positiveId,
|
||||
settlementRow,
|
||||
splitOf,
|
||||
type SplitMember,
|
||||
} from './budget-values';
|
||||
import {
|
||||
sumMinor,
|
||||
type CardExpense,
|
||||
type CardExpenseBefore,
|
||||
type CardPayment,
|
||||
type ExpensesCard,
|
||||
type PaymentsCard,
|
||||
} from '@trek/shared';
|
||||
|
||||
/**
|
||||
* The cards a held budget write shows inside its change card: the expense
|
||||
* as it will be (the full split, who paid, the amounts) or the payment, with
|
||||
* what an update replaces and the people a change is about. A new expense is
|
||||
* read from the call's arguments the way the write path reads them; a change
|
||||
* to one starts from the expense in the turn's trip summary, a change to a
|
||||
* payment from the payments its lookup read. Without the row a change is
|
||||
* about there is no card, and the change card built from the words stands.
|
||||
*/
|
||||
|
||||
/** One expense as a change card holds it. */
|
||||
function expenseCard(row: CardExpense): ExpensesCard {
|
||||
return { kind: 'expenses', rows: [row], offset: 0, more: 0, total: null, unconverted: 0 };
|
||||
}
|
||||
|
||||
function paymentCard(row: CardPayment): PaymentsCard {
|
||||
return { kind: 'payments', rows: [row], more: 0 };
|
||||
}
|
||||
|
||||
function idsOf(value: unknown): number[] {
|
||||
return Array.isArray(value) ? value.filter((id): id is number => positiveId(id) !== null) : [];
|
||||
}
|
||||
|
||||
/** Members splitting evenly, nobody marked paid yet. */
|
||||
function evenly(ids: readonly number[]): SplitMember[] {
|
||||
return ids.map((id) => ({ id, amount: null, paid: false }));
|
||||
}
|
||||
|
||||
/** The uneven split of `members` (`[{ user_id, amount }]`). */
|
||||
function unevenly(value: unknown): SplitMember[] {
|
||||
return rowsOf(value).flatMap((member) => {
|
||||
const id = positiveId(member.user_id);
|
||||
const amount = amountOf(member.amount);
|
||||
return id === null || amount === null ? [] : [{ id, amount, paid: false }];
|
||||
});
|
||||
}
|
||||
|
||||
/** Everyone on the trip, as an expense split across all members reads them. */
|
||||
function everyone(card: TripCard): number[] {
|
||||
return [...card.members.keys()];
|
||||
}
|
||||
|
||||
/** What a new expense comes to: the payers' sum when they are named, as the write path derives it. */
|
||||
function newTotal(card: TripCard, args: ToolArgs): number | null {
|
||||
const payers = payersOf(card, args.payers, Number.MAX_SAFE_INTEGER);
|
||||
return payers.length > 0 ? sumMinor(payers.map((payer) => payer.amount)) / 100 : amountOf(args.total_price);
|
||||
}
|
||||
|
||||
/** A new expense from its arguments: who shares it is `members`, else `split`, else everyone on the trip. */
|
||||
function newExpense(ctx: PreviewContext, args: ToolArgs, split: SplitMember[]): ExpensesCard | null {
|
||||
const name = argLine(args.name, 120);
|
||||
const total = newTotal(ctx.card, args);
|
||||
if (name === null || total === null) return null;
|
||||
return expenseCard({
|
||||
id: null,
|
||||
name,
|
||||
category: argLine(args.category, 60),
|
||||
amount: { amount: total, currency: codeOf(args.currency, ctx.card.currency) },
|
||||
tripAmount: null,
|
||||
date: dateOf(args.expense_date),
|
||||
place: placeOf(ctx.card, args.place_id),
|
||||
payers: payersOf(ctx.card, args.payers, ALL_PEOPLE.payers),
|
||||
...splitOf(ctx.card, split, total, 0, ALL_PEOPLE.shares),
|
||||
note: argLine(args.note, 160),
|
||||
receipts: 0,
|
||||
before: null,
|
||||
highlight: [],
|
||||
change: 'new',
|
||||
});
|
||||
}
|
||||
|
||||
/** `create_budget_item`: an uneven split, an even one among `member_ids` (none for `[]`), else everyone. */
|
||||
export function createExpenseChange(ctx: PreviewContext, args: ToolArgs): ExpensesCard | null {
|
||||
const split = Array.isArray(args.members)
|
||||
? unevenly(args.members)
|
||||
: evenly(Array.isArray(args.member_ids) ? idsOf(args.member_ids) : everyone(ctx.card));
|
||||
return newExpense(ctx, args, split);
|
||||
}
|
||||
|
||||
/**
|
||||
* `create_budget_item_with_members`: shared evenly by `userIds`, or by
|
||||
* everyone on the trip when it lists nobody, an empty list included, as the
|
||||
* tool does and says.
|
||||
*/
|
||||
export function createWithMembersChange(ctx: PreviewContext, args: ToolArgs): ExpensesCard | null {
|
||||
const ids = idsOf(args.userIds);
|
||||
// The tool takes no currency, date or payers: the expense is in the trip currency and nobody paid yet.
|
||||
const own = { name: args.name, category: args.category, total_price: args.total_price, note: args.note };
|
||||
return newExpense(ctx, { ...own, place_id: args.place_id }, evenly(ids.length > 0 ? ids : everyone(ctx.card)));
|
||||
}
|
||||
|
||||
/** The stored expense a change is about, as a row and as the card reads it. */
|
||||
function storedExpense(ctx: PreviewContext, args: ToolArgs): { raw: Row; was: CardExpense } | null {
|
||||
const itemId = argId(args, 'itemId');
|
||||
const raw = itemId === null ? undefined : budgetRows(ctx.card).get(itemId);
|
||||
const was = raw ? expenseOf(ctx, raw) : null;
|
||||
return raw && was ? { raw, was } : null;
|
||||
}
|
||||
|
||||
function expenseOf(ctx: PreviewContext, raw: unknown): CardExpense | null {
|
||||
return expenseFromRow(ctx.card, raw, {
|
||||
tripCurrency: codeOf(ctx.card.currency, null),
|
||||
caps: ALL_PEOPLE,
|
||||
change: 'changed',
|
||||
});
|
||||
}
|
||||
|
||||
/** Whether a member was marked paid on the stored expense. */
|
||||
function paidBefore(raw: Row, id: number): boolean {
|
||||
return rowsOf(raw.members).some((member) => member.user_id === id && (member.paid === 1 || member.paid === true));
|
||||
}
|
||||
|
||||
/** The people a change brings into the split. */
|
||||
function joined(was: CardExpense, now: CardExpense): number[] {
|
||||
const before = new Set(was.shares.map((share) => share.person.id));
|
||||
return now.shares.map((share) => share.person.id).filter((id) => !before.has(id));
|
||||
}
|
||||
|
||||
/** What a change replaces, each value only where it differs; null when nothing it shows changes. */
|
||||
function beforeOf(was: CardExpense, now: CardExpense): CardExpenseBefore | null {
|
||||
const moved = was.amount.amount !== now.amount.amount || was.amount.currency !== now.amount.currency;
|
||||
const before: CardExpenseBefore = {
|
||||
name: was.name !== now.name ? was.name : null,
|
||||
amount: moved ? was.amount : null,
|
||||
category: was.category !== now.category ? was.category : null,
|
||||
date: was.date !== now.date ? was.date : null,
|
||||
split: was.split !== now.split ? was.split : null,
|
||||
};
|
||||
return Object.values(before).some((value) => value !== null) ? before : null;
|
||||
}
|
||||
|
||||
/** The stored row with an update's arguments laid over it, the way the write path stores them. */
|
||||
function updatedRow(ctx: PreviewContext, raw: Row, args: ToolArgs): Row {
|
||||
const next: Row = { ...raw };
|
||||
const payers = payersOf(ctx.card, args.payers, Number.MAX_SAFE_INTEGER);
|
||||
const total = amountOf(args.total_price);
|
||||
if (Array.isArray(args.payers)) {
|
||||
next.payers = payers.map((payer) => ({ user_id: payer.person.id, amount: payer.amount }));
|
||||
// The payers' sum is the total; clearing them keeps a total the call names, as the write path re-applies it.
|
||||
next.total_price =
|
||||
payers.length === 0 && total !== null ? total : sumMinor(payers.map((payer) => payer.amount)) / 100;
|
||||
} else if (total !== null) next.total_price = total;
|
||||
if (args.currency !== undefined) next.currency = args.currency;
|
||||
if (args.expense_date !== undefined) next.expense_date = args.expense_date;
|
||||
if (args.place_id !== undefined) next.place_id = args.place_id;
|
||||
const keep = (id: number) => ({ user_id: id, paid: paidBefore(raw, id) });
|
||||
if (Array.isArray(args.members)) {
|
||||
next.members = unevenly(args.members).map((member) => ({ ...keep(member.id), amount: member.amount }));
|
||||
} else if (Array.isArray(args.member_ids)) {
|
||||
next.members = idsOf(args.member_ids).map((id) => ({ ...keep(id), amount: null }));
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
/** `update_budget_item`: the expense as it will be, with what it replaces and who joins its split. */
|
||||
export function updateExpenseChange(ctx: PreviewContext, args: ToolArgs): ExpensesCard | null {
|
||||
const stored = storedExpense(ctx, args);
|
||||
const laid = stored ? expenseOf(ctx, updatedRow(ctx, stored.raw, args)) : null;
|
||||
if (!stored || !laid) return null;
|
||||
const now: CardExpense = {
|
||||
...laid,
|
||||
name: argLine(args.name, 120) ?? laid.name,
|
||||
category: typeof args.category === 'string' ? argLine(args.category, 60) : laid.category,
|
||||
note: args.note === undefined ? null : argLine(args.note, 160),
|
||||
};
|
||||
return expenseCard({ ...now, before: beforeOf(stored.was, now), highlight: joined(stored.was, now) });
|
||||
}
|
||||
|
||||
/** `set_budget_item_members`: an even split among `userIds`, the paid ticks kept, own amounts dropped. */
|
||||
export function setMembersChange(ctx: PreviewContext, args: ToolArgs): ExpensesCard | null {
|
||||
const stored = storedExpense(ctx, args);
|
||||
if (!stored || !Array.isArray(args.userIds)) return null;
|
||||
const members = idsOf(args.userIds).map((id) => ({ user_id: id, paid: paidBefore(stored.raw, id), amount: null }));
|
||||
const now = expenseOf(ctx, { ...stored.raw, members });
|
||||
if (!now) return null;
|
||||
return expenseCard({ ...now, before: beforeOf(stored.was, now), highlight: joined(stored.was, now) });
|
||||
}
|
||||
|
||||
/** `toggle_budget_member_paid`: one member's paid tick set or taken off. */
|
||||
export function togglePaidChange(ctx: PreviewContext, args: ToolArgs): ExpensesCard | null {
|
||||
const stored = storedExpense(ctx, args);
|
||||
const memberId = argId(args, 'memberId');
|
||||
if (!stored || memberId === null || typeof args.paid !== 'boolean') return null;
|
||||
const paid = args.paid;
|
||||
const shares = stored.was.shares.map((share) => (share.person.id === memberId ? { ...share, paid } : share));
|
||||
return expenseCard({ ...stored.was, shares, highlight: [memberId] });
|
||||
}
|
||||
|
||||
/** `delete_budget_item`: the expense that goes. */
|
||||
export function deleteExpenseChange(ctx: PreviewContext, args: ToolArgs): ExpensesCard | null {
|
||||
const stored = storedExpense(ctx, args);
|
||||
return stored ? expenseCard({ ...stored.was, change: 'willDelete' }) : null;
|
||||
}
|
||||
|
||||
/** A payment from its arguments; the stored one's values stand in for those the call leaves out. */
|
||||
function paymentOf(card: TripCard, args: ToolArgs, base: Row | undefined): CardPayment | null {
|
||||
const stored = recordOf(base);
|
||||
const from = argId(args, 'from_user_id') ?? positiveId(stored.from_user_id);
|
||||
const to = argId(args, 'to_user_id') ?? positiveId(stored.to_user_id);
|
||||
const amount = amountOf(args.amount ?? stored.amount);
|
||||
if (from === null || to === null || amount === null || amount <= 0) return null;
|
||||
const currency = args.currency === undefined ? stored.currency : args.currency;
|
||||
const settled = args.settled_at === undefined ? stored.settled_at : args.settled_at;
|
||||
// Someone who left the trip keeps the name the stored payment gives them.
|
||||
const side = (id: number, key: 'from' | 'to') =>
|
||||
stored[`${key}_user_id`] === id
|
||||
? personFrom(card, id, stored[`${key}_username`], stored[`${key}_avatar_url`])
|
||||
: personFrom(card, id);
|
||||
return {
|
||||
id: positiveId(stored.id),
|
||||
from: side(from, 'from'),
|
||||
to: side(to, 'to'),
|
||||
amount: { amount, currency: codeOf(currency, card.currency) },
|
||||
date: dateOf(settled) ?? dateOf(stored.created_at),
|
||||
note: args.note === undefined ? clipped(stored.note, 120) : argLine(args.note, 120),
|
||||
before: null,
|
||||
change: base ? 'changed' : 'new',
|
||||
};
|
||||
}
|
||||
|
||||
/** `create_settlement`: who pays whom how much, and on which day. */
|
||||
export function createPaymentChange(ctx: PreviewContext, args: ToolArgs): PaymentsCard | null {
|
||||
const payment = paymentOf(ctx.card, args, undefined);
|
||||
return payment ? paymentCard(payment) : null;
|
||||
}
|
||||
|
||||
/** `update_settlement`: the payment as it will be, with the people and the amount it replaces. */
|
||||
export function updatePaymentChange(ctx: PreviewContext, args: ToolArgs, loaded: unknown): PaymentsCard | null {
|
||||
const base = settlementRow(loaded, argId(args, 'settlementId'));
|
||||
const was = base ? paymentFromRow(ctx.card, base, 'none') : null;
|
||||
const now = paymentOf(ctx.card, args, base);
|
||||
if (!now) return null;
|
||||
if (!was) return paymentCard({ ...now, id: argId(args, 'settlementId'), change: 'changed' });
|
||||
const moved = was.amount.amount !== now.amount.amount || was.amount.currency !== now.amount.currency;
|
||||
const before = {
|
||||
from: was.from.id !== now.from.id ? was.from : null,
|
||||
to: was.to.id !== now.to.id ? was.to : null,
|
||||
amount: moved ? was.amount : null,
|
||||
};
|
||||
const changed = before.from !== null || before.to !== null || before.amount !== null;
|
||||
return paymentCard({ ...now, before: changed ? before : null });
|
||||
}
|
||||
|
||||
/** `delete_settlement`: the payment that goes, when the lookup found it. */
|
||||
export function deletePaymentChange(ctx: PreviewContext, args: ToolArgs, loaded: unknown): PaymentsCard | null {
|
||||
const base = settlementRow(loaded, argId(args, 'settlementId'));
|
||||
const payment = base ? paymentFromRow(ctx.card, base, 'willDelete') : null;
|
||||
return payment ? paymentCard(payment) : null;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import type { Described, PreviewContext, ToolArgs } from '../card-types';
|
||||
import { argId, recordOf, type Row } from '../card-values';
|
||||
import { formatDate, id, memberNames, money, placeName, Rows, text } from '../preview-format';
|
||||
import { rowsOf } from '../undo-snapshot';
|
||||
import { budgetRows, codeOf, settlementRow } from './budget-values';
|
||||
|
||||
/**
|
||||
* The change cards, in words, of the budget writes that had none of their
|
||||
* own: an expense added with its split, who shares one, who has paid their
|
||||
* part, an expense removed, and the settle-up payments recorded, changed and
|
||||
* removed. Each names the tool it runs, so the card reads the tool's own
|
||||
* action ("Record a payment") rather than a plain add or change. The
|
||||
* expense or payment it is about is named from the turn's trip summary or the
|
||||
* payments its lookup read; one the card cannot find reads as its id.
|
||||
*/
|
||||
|
||||
/** The stored expense a call names with `itemId`. */
|
||||
function expense(ctx: PreviewContext, args: ToolArgs): Row | undefined {
|
||||
const itemId = argId(args, 'itemId');
|
||||
return itemId === null ? undefined : budgetRows(ctx.card).get(itemId);
|
||||
}
|
||||
|
||||
function expenseName(ctx: PreviewContext, args: ToolArgs): string {
|
||||
const found = expense(ctx, args);
|
||||
const itemId = argId(args, 'itemId');
|
||||
return text(found?.name) || (itemId === null ? '' : `#${itemId}`);
|
||||
}
|
||||
|
||||
/** Who shares a stored expense, by name. */
|
||||
function sharedBy(ctx: PreviewContext, row: Row | undefined): string {
|
||||
return memberNames(ctx, rowsOf(row?.members).map((member) => member.user_id));
|
||||
}
|
||||
|
||||
/** "Ana → Ben", from the two people of a payment. */
|
||||
function between(ctx: PreviewContext, from: unknown, to: unknown): string {
|
||||
const named = [from, to].map((person) => (typeof person === 'number' ? memberNames(ctx, [person]) : ''));
|
||||
return named.every((name) => name !== '') ? text(`${named[0]} → ${named[1]}`) : '';
|
||||
}
|
||||
|
||||
/** An amount in its currency, empty for one that is no number. */
|
||||
function amount(ctx: PreviewContext, value: unknown, currency: unknown): string {
|
||||
if (typeof value !== 'number' || !Number.isFinite(value)) return '';
|
||||
return money(ctx, value, codeOf(currency, ctx.card.currency));
|
||||
}
|
||||
|
||||
function day(ctx: PreviewContext, value: unknown): string {
|
||||
return typeof value === 'string' && value !== '' ? formatDate(ctx, value.slice(0, 10)) : '';
|
||||
}
|
||||
|
||||
/** The stored payment `settlementId` names, from the payments the lookup read. */
|
||||
function payment(args: ToolArgs, loaded: unknown): Row {
|
||||
return recordOf(settlementRow(loaded, argId(args, 'settlementId')));
|
||||
}
|
||||
|
||||
/** "Ana → Ben" for a payment, its id where the card cannot name both people. */
|
||||
function paymentSubject(ctx: PreviewContext, row: Row, args: ToolArgs): string {
|
||||
const named = between(ctx, args.from_user_id ?? row.from_user_id, args.to_user_id ?? row.to_user_id);
|
||||
const settlementId = argId(args, 'settlementId');
|
||||
return named || (settlementId === null ? '' : `#${settlementId}`);
|
||||
}
|
||||
|
||||
export const BUDGET_DESCRIBERS = {
|
||||
create_budget_item_with_members: (ctx, args) => {
|
||||
const placeId = id(args, 'place_id');
|
||||
const ids = Array.isArray(args.userIds) && args.userIds.length > 0 ? args.userIds : [...ctx.card.members.keys()];
|
||||
const rows = new Rows()
|
||||
.add('price', amount(ctx, args.total_price, ctx.card.currency))
|
||||
.add('category', text(args.category))
|
||||
.add('members', memberNames(ctx, ids))
|
||||
.add('location', placeName(ctx, placeId))
|
||||
.add('notes', text(args.note));
|
||||
return {
|
||||
subject: text(args.name),
|
||||
rows,
|
||||
dayId: null,
|
||||
placeIds: placeId ? [placeId] : [],
|
||||
tool: 'create_budget_item_with_members',
|
||||
};
|
||||
},
|
||||
set_budget_item_members: (ctx, args) => {
|
||||
const rows = new Rows();
|
||||
if (Array.isArray(args.userIds)) {
|
||||
rows.change('members', sharedBy(ctx, expense(ctx, args)), memberNames(ctx, args.userIds));
|
||||
}
|
||||
return { subject: expenseName(ctx, args), rows, dayId: null, placeIds: [], tool: 'set_budget_item_members' };
|
||||
},
|
||||
toggle_budget_member_paid: (ctx, args) => {
|
||||
const memberId = argId(args, 'memberId');
|
||||
const who = memberId === null ? '' : memberNames(ctx, [memberId]);
|
||||
const rows = new Rows().add('members', who && typeof args.paid === 'boolean' ? `${who} ${text(args.paid)}` : who);
|
||||
return { subject: expenseName(ctx, args), rows, dayId: null, placeIds: [], tool: 'toggle_budget_member_paid' };
|
||||
},
|
||||
delete_budget_item: (ctx, args) => {
|
||||
const row = expense(ctx, args);
|
||||
const rows = new Rows().add('price', amount(ctx, row?.total_price, row?.currency));
|
||||
return { subject: expenseName(ctx, args), rows, dayId: null, placeIds: [], tool: 'delete_budget_item' };
|
||||
},
|
||||
create_settlement: (ctx, args) => {
|
||||
const rows = new Rows()
|
||||
.add('price', amount(ctx, args.amount, args.currency))
|
||||
.add('day', day(ctx, args.settled_at))
|
||||
.add('notes', text(args.note));
|
||||
return { subject: paymentSubject(ctx, {}, args), rows, dayId: null, placeIds: [], tool: 'create_settlement' };
|
||||
},
|
||||
update_settlement: (ctx, args, loaded) => {
|
||||
const row = payment(args, loaded);
|
||||
const rows = new Rows();
|
||||
const after = amount(ctx, args.amount, args.currency === undefined ? row.currency : args.currency);
|
||||
const before = amount(ctx, row.amount, row.currency);
|
||||
if (after !== '') rows.change('price', before === after ? null : before, after);
|
||||
if (args.settled_at !== undefined) rows.change('day', day(ctx, row.settled_at) || null, day(ctx, args.settled_at));
|
||||
if (args.note !== undefined) rows.change('notes', text(row.note) || null, text(args.note));
|
||||
return { subject: paymentSubject(ctx, row, args), rows, dayId: null, placeIds: [], tool: 'update_settlement' };
|
||||
},
|
||||
delete_settlement: (ctx, args, loaded) => {
|
||||
const row = payment(args, loaded);
|
||||
const rows = new Rows().add('price', amount(ctx, row.amount, row.currency));
|
||||
return { subject: paymentSubject(ctx, row, args), rows, dayId: null, placeIds: [], tool: 'delete_settlement' };
|
||||
},
|
||||
} satisfies Readonly<Record<string, (ctx: PreviewContext, args: ToolArgs, loaded: unknown) => Described>>;
|
||||
@@ -0,0 +1,83 @@
|
||||
import type { ToolArgs, ToolCards, UndoSnapshot } from '../card-types';
|
||||
import { argId, idIn, readKey, recordOf } from '../card-values';
|
||||
import { byId, rowsOf, same, STABLE_FIELDS } from '../undo-snapshot';
|
||||
import type { AssistantChangePreview, TrekCard } from '@trek/shared';
|
||||
|
||||
/**
|
||||
* What happens to a budget write once it is applied: the ids its receipt
|
||||
* names, the card it leaves (the waiting one with the new row's id, so the
|
||||
* receipt opens that very expense), and the way back from what it created,
|
||||
* offered only while the created row is as the change left it.
|
||||
*/
|
||||
|
||||
type Undo = NonNullable<ToolCards['undo']>;
|
||||
|
||||
/** The fields that say a recorded payment is still the one that was created. */
|
||||
const PAYMENT_FIELDS = ['from_user_id', 'to_user_id', 'amount', 'currency', 'settled_at', 'note'] as const;
|
||||
|
||||
/** The read the payment undo checks against. */
|
||||
const SETTLEMENTS_READ = { tool: 'list_settlements', args: {} } as const;
|
||||
|
||||
/** The waiting card of a create, now with the id the answer names under `key`. */
|
||||
function withCreatedId(preview: AssistantChangePreview | null, result: unknown, key: 'item' | 'settlement') {
|
||||
const card = preview?.card;
|
||||
const createdId = idIn(result, key);
|
||||
if (!card || createdId === null) return null;
|
||||
if (key === 'item' && card.kind === 'expenses' && card.rows.length === 1) {
|
||||
return { ...card, rows: card.rows.map((row) => ({ ...row, id: createdId })) };
|
||||
}
|
||||
if (key === 'settlement' && card.kind === 'payments' && card.rows.length === 1) {
|
||||
return { ...card, rows: card.rows.map((row) => ({ ...row, id: createdId })) };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The card of an applied expense create. */
|
||||
export function appliedExpense(input: { result: unknown; preview: AssistantChangePreview | null }): TrekCard | null {
|
||||
return withCreatedId(input.preview, input.result, 'item');
|
||||
}
|
||||
|
||||
/** The card of an applied payment. */
|
||||
export function appliedPayment(input: { result: unknown; preview: AssistantChangePreview | null }): TrekCard | null {
|
||||
return withCreatedId(input.preview, input.result, 'settlement');
|
||||
}
|
||||
|
||||
/** A payment's receipt names the payment: the one created, or the one the call changed or removed. */
|
||||
export function paymentIds(args: ToolArgs, result: unknown): number[] {
|
||||
const paymentId = idIn(result, 'settlement') ?? argId(args, 'settlementId');
|
||||
return paymentId === null ? [] : [paymentId];
|
||||
}
|
||||
|
||||
/** An expense created with its split, taken back while it is as it was created. */
|
||||
export const DELETE_CREATED_EXPENSE: Undo = {
|
||||
tool: 'delete_budget_item',
|
||||
args: (_args, result) => {
|
||||
const itemId = idIn(result, 'item');
|
||||
return itemId ? { itemId } : null;
|
||||
},
|
||||
unchanged: (result: unknown, snapshot: UndoSnapshot) => {
|
||||
const created = recordOf(recordOf(result).item);
|
||||
const items = rowsOf(recordOf(recordOf(snapshot.summary).budget).items);
|
||||
return same(created, byId(items, created.id), STABLE_FIELDS.budget);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* A recorded payment, taken back while nobody changed it since: the tool
|
||||
* calls itself the undo of create_settlement, and removing the payment puts
|
||||
* the balances back as they were.
|
||||
*/
|
||||
export const DELETE_CREATED_PAYMENT: Undo = {
|
||||
tool: 'delete_settlement',
|
||||
args: (_args, result) => {
|
||||
const settlementId = idIn(result, 'settlement');
|
||||
return settlementId ? { settlementId } : null;
|
||||
},
|
||||
reads: () => [SETTLEMENTS_READ],
|
||||
unchanged: (result: unknown, snapshot: UndoSnapshot) => {
|
||||
const created = recordOf(recordOf(result).settlement);
|
||||
const read = snapshot.reads.get(readKey(SETTLEMENTS_READ.tool, SETTLEMENTS_READ.args));
|
||||
const payments = rowsOf(recordOf(read).settlements);
|
||||
return same(created, byId(payments, created.id), PAYMENT_FIELDS);
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,237 @@
|
||||
import type { TripCard } from '../../assistant-context';
|
||||
import { clipped, fileKeyOf, finite, personOf, recordOf, type Row } from '../card-values';
|
||||
import { rowsOf } from '../undo-snapshot';
|
||||
import {
|
||||
EXPENSE_PEOPLE_MAX,
|
||||
MAX_CARD_CHARS,
|
||||
splitEqualShares,
|
||||
toMinor,
|
||||
type CardExpense,
|
||||
type CardPayment,
|
||||
type CardPerson,
|
||||
type ExpenseChange,
|
||||
} from '@trek/shared';
|
||||
|
||||
/**
|
||||
* The readers the budget's cards are built with. Expenses and payments come
|
||||
* from three places: the expense list a read answered, the trip summary the
|
||||
* turn holds (for the expense a change is about) and the payments
|
||||
* `list_settlements` answered. Each reader takes the row as it comes, trusts
|
||||
* no shape and leaves out what is not there. People are named from the trip
|
||||
* card, and someone who left the trip from the name the row still carries;
|
||||
* their picture is the stored file, never an address.
|
||||
*/
|
||||
|
||||
export function positiveId(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isInteger(value) && value > 0 ? value : null;
|
||||
}
|
||||
|
||||
/** An amount a card may show, to the cent; null for anything else. */
|
||||
export function amountOf(value: unknown): number | null {
|
||||
const number = finite(value);
|
||||
return number !== null && Math.abs(number) <= 1e9 ? toMinor(number) / 100 : null;
|
||||
}
|
||||
|
||||
/** A currency code, upper case; the fallback's when the value is none, euros when neither is. */
|
||||
export function codeOf(value: unknown, fallback: string | null): string {
|
||||
for (const candidate of [value, fallback]) {
|
||||
const code = typeof candidate === 'string' ? candidate.trim().toUpperCase() : '';
|
||||
if (/^[A-Z]{3}$/.test(code)) return code;
|
||||
}
|
||||
return 'EUR';
|
||||
}
|
||||
|
||||
/** The day of a stored date or date and time, `YYYY-MM-DD`. */
|
||||
export function dateOf(value: unknown): string | null {
|
||||
if (typeof value !== 'string') return null;
|
||||
const day = value.trim().slice(0, 10);
|
||||
return /^\d{4}-\d{2}-\d{2}$/.test(day) ? day : null;
|
||||
}
|
||||
|
||||
/** A text from a call's arguments, on one line and cut to `max`: what will be stored, so it is not masked. */
|
||||
export function argLine(value: unknown, max: number): string | null {
|
||||
if (typeof value !== 'string') return null;
|
||||
const line = value.replace(/\s+/g, ' ').trim();
|
||||
if (line === '') return null;
|
||||
return line.length > max ? `${line.slice(0, max - 1)}…` : line;
|
||||
}
|
||||
|
||||
/** A traveller as a card shows them: from the trip card, else from what the row names. */
|
||||
export function personFrom(card: TripCard, id: number, name?: unknown, avatarUrl?: unknown): CardPerson {
|
||||
return personOf(card, id) ?? { id, name: clipped(name, 80) ?? `#${id}`, avatarKey: fileKeyOf(avatarUrl) };
|
||||
}
|
||||
|
||||
/** A trip place an expense belongs to, by its name on the trip card. */
|
||||
export function placeOf(card: TripCard, placeId: unknown): CardExpense['place'] {
|
||||
const id = positiveId(placeId);
|
||||
const name = id === null ? null : clipped(card.places.get(id)?.name, 120);
|
||||
return id !== null && name !== null ? { id, name } : null;
|
||||
}
|
||||
|
||||
/** One member of a split as the rows store it: their own amount on an uneven split, null on an even one. */
|
||||
export interface SplitMember {
|
||||
id: number;
|
||||
amount: number | null;
|
||||
paid: boolean;
|
||||
name?: unknown;
|
||||
avatarUrl?: unknown;
|
||||
}
|
||||
|
||||
/** How many people one row names. */
|
||||
export interface PeopleCaps {
|
||||
shares: number;
|
||||
payers: number;
|
||||
}
|
||||
|
||||
/** Every person a change row can name. */
|
||||
export const ALL_PEOPLE: PeopleCaps = { shares: EXPENSE_PEOPLE_MAX, payers: EXPENSE_PEOPLE_MAX };
|
||||
|
||||
/** The members of a split as a row lists them, either the expense list's or the trip summary's. */
|
||||
function membersOf(value: unknown, names?: ReadonlyMap<number, string>): SplitMember[] {
|
||||
return rowsOf(value).flatMap((member) => {
|
||||
const id = positiveId(member.user_id);
|
||||
if (id === null) return [];
|
||||
return [
|
||||
{
|
||||
id,
|
||||
amount: member.amount === null || member.amount === undefined ? null : amountOf(member.amount),
|
||||
paid: member.paid === true || member.paid === 1,
|
||||
name: member.username ?? names?.get(id),
|
||||
avatarUrl: member.avatar_url,
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Who shares an expense and what each part comes to. A member with an
|
||||
* amount of their own owes exactly that and the split is uneven; the others
|
||||
* share the total evenly, the left-over cents handed out from `itemId` on as
|
||||
* the server does, so the parts add up to the total.
|
||||
*/
|
||||
export function splitOf(
|
||||
card: TripCard,
|
||||
members: readonly SplitMember[],
|
||||
total: number,
|
||||
itemId: number,
|
||||
max: number,
|
||||
): Pick<CardExpense, 'split' | 'shares' | 'moreShares'> {
|
||||
if (members.length === 0) return { split: 'none', shares: [], moreShares: 0 };
|
||||
const equal = splitEqualShares(toMinor(total), members.map((member) => ({ user_id: member.id })), itemId);
|
||||
const shares = members.map((member) => ({
|
||||
person: personFrom(card, member.id, member.name, member.avatarUrl),
|
||||
amount: member.amount ?? (equal[member.id] ?? 0) / 100,
|
||||
paid: member.paid,
|
||||
}));
|
||||
return {
|
||||
split: members.some((member) => member.amount !== null) ? 'custom' : 'equal',
|
||||
shares: shares.slice(0, max),
|
||||
moreShares: Math.min(1000, Math.max(0, shares.length - max)),
|
||||
};
|
||||
}
|
||||
|
||||
/** Who paid how much, as the rows store it. */
|
||||
export function payersOf(card: TripCard, value: unknown, max: number, names?: ReadonlyMap<number, string>) {
|
||||
return rowsOf(value)
|
||||
.flatMap((payer) => {
|
||||
const id = positiveId(payer.user_id);
|
||||
const amount = amountOf(payer.amount);
|
||||
if (id === null || amount === null) return [];
|
||||
return [{ person: personFrom(card, id, payer.username ?? names?.get(id), payer.avatar_url), amount }];
|
||||
})
|
||||
.slice(0, max);
|
||||
}
|
||||
|
||||
/** What an expense row needs beside the row: the trip currency, the names a list answered with and the caps. */
|
||||
interface ExpenseRowContext {
|
||||
tripCurrency: string;
|
||||
names?: ReadonlyMap<number, string>;
|
||||
caps: PeopleCaps;
|
||||
change: ExpenseChange;
|
||||
}
|
||||
|
||||
/**
|
||||
* One expense from a stored row: the expense list's slim one or the trip
|
||||
* summary's whole one. Null for a row without an id, a name or an amount.
|
||||
*/
|
||||
export function expenseFromRow(card: TripCard, raw: unknown, at: ExpenseRowContext): CardExpense | null {
|
||||
const row = recordOf(raw);
|
||||
const id = positiveId(row.id);
|
||||
const name = clipped(row.name, 120);
|
||||
const total = amountOf(row.total_price);
|
||||
if (id === null || name === null || total === null) return null;
|
||||
const currency = codeOf(row.currency, at.tripCurrency);
|
||||
const inTrip = amountOf(row.trip_amount);
|
||||
const receipts = finite(row.receipt_count) ?? (Array.isArray(row.receipts) ? row.receipts.length : 0);
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
category: clipped(row.category, 60),
|
||||
amount: { amount: total, currency },
|
||||
tripAmount: inTrip !== null && currency !== at.tripCurrency ? { amount: inTrip, currency: at.tripCurrency } : null,
|
||||
date: dateOf(row.expense_date),
|
||||
place: placeOf(card, row.place_id),
|
||||
payers: payersOf(card, row.payers, at.caps.payers, at.names),
|
||||
...splitOf(card, membersOf(row.members, at.names), total, id, at.caps.shares),
|
||||
note: null,
|
||||
receipts: Math.min(999, Math.max(0, Math.trunc(receipts))),
|
||||
before: null,
|
||||
highlight: [],
|
||||
change: at.change,
|
||||
};
|
||||
}
|
||||
|
||||
/** One recorded payment from a row of `list_settlements`; null without an id, both people or an amount. */
|
||||
export function paymentFromRow(card: TripCard, raw: unknown, change: ExpenseChange): CardPayment | null {
|
||||
const row = recordOf(raw);
|
||||
const id = positiveId(row.id);
|
||||
const from = positiveId(row.from_user_id);
|
||||
const to = positiveId(row.to_user_id);
|
||||
const amount = amountOf(row.amount);
|
||||
if (id === null || from === null || to === null || amount === null || amount <= 0) return null;
|
||||
return {
|
||||
id,
|
||||
from: personFrom(card, from, row.from_username, row.from_avatar_url),
|
||||
to: personFrom(card, to, row.to_username, row.to_avatar_url),
|
||||
amount: { amount, currency: codeOf(row.currency, card.currency) },
|
||||
date: dateOf(row.settled_at) ?? dateOf(row.created_at),
|
||||
note: clipped(row.note, 120),
|
||||
before: null,
|
||||
change,
|
||||
};
|
||||
}
|
||||
|
||||
const EXPENSES = new WeakMap<TripCard, ReadonlyMap<number, Row>>();
|
||||
|
||||
/** The trip's expenses as the turn's trip summary holds them, by id; read once per trip card. */
|
||||
export function budgetRows(card: TripCard): ReadonlyMap<number, Row> {
|
||||
let rows = EXPENSES.get(card);
|
||||
if (!rows) {
|
||||
const items = rowsOf(recordOf(recordOf(card.sources.summary).budget).items);
|
||||
rows = new Map(
|
||||
items.flatMap((item): Array<[number, Row]> => {
|
||||
const id = positiveId(item.id);
|
||||
return id === null ? [] : [[id, item]];
|
||||
}),
|
||||
);
|
||||
EXPENSES.set(card, rows);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** The recorded payment `list_settlements` answered under `id`. */
|
||||
export function settlementRow(loaded: unknown, id: number | null): Row | undefined {
|
||||
return id === null ? undefined : rowsOf(recordOf(loaded).settlements).find((row) => row.id === id);
|
||||
}
|
||||
|
||||
/**
|
||||
* A list card cut to what one card may weigh: rows come off its end, and
|
||||
* into `more`, until its JSON fits, so a long list still shows its start.
|
||||
*/
|
||||
export function fitted<C extends { rows: readonly unknown[]; more: number }>(card: C): C {
|
||||
let fit = card;
|
||||
while (fit.rows.length > 1 && JSON.stringify(fit).length > MAX_CARD_CHARS) {
|
||||
fit = { ...fit, rows: fit.rows.slice(0, -1), more: fit.more + 1 };
|
||||
}
|
||||
return fit;
|
||||
}
|
||||
@@ -1,4 +1,103 @@
|
||||
import type { DomainCards } from '../card-types';
|
||||
import type { TripCard } from '../../assistant-context';
|
||||
import type { CardReader, DomainCards, ToolArgs } from '../card-types';
|
||||
import { clipped } from '../card-values';
|
||||
import { costPerPersonAnswer, expensesAnswer, paymentsAnswer, settlementAnswer } from './budget-answers';
|
||||
import {
|
||||
createExpenseChange,
|
||||
createPaymentChange,
|
||||
createWithMembersChange,
|
||||
deleteExpenseChange,
|
||||
deletePaymentChange,
|
||||
setMembersChange,
|
||||
togglePaidChange,
|
||||
updateExpenseChange,
|
||||
updatePaymentChange,
|
||||
} from './budget-changes';
|
||||
import { BUDGET_DESCRIBERS } from './budget-describe';
|
||||
import {
|
||||
appliedExpense,
|
||||
appliedPayment,
|
||||
DELETE_CREATED_EXPENSE,
|
||||
DELETE_CREATED_PAYMENT,
|
||||
paymentIds,
|
||||
} from './budget-receipts';
|
||||
import { budgetRows } from './budget-values';
|
||||
|
||||
/** The cards of the budget: expenses, payments and who settles with whom. None yet: this package fills them in. */
|
||||
export const BUDGET_CARDS: DomainCards = { id: 'budget', tools: {} };
|
||||
/**
|
||||
* The costs in the assistant chat. The four reads (the expense list, settle
|
||||
* up, the payments recorded, the share per traveller) show their answer as a
|
||||
* card the way the Costs page draws it, and every write to an expense or a
|
||||
* payment shows what it will be: the full split, who paid, the amounts, with
|
||||
* what an update replaces. `create_budget_item` and `update_budget_item` keep
|
||||
* the core's words, verb and undo and only add their card.
|
||||
*/
|
||||
|
||||
/** The payments a change to a recorded one starts from. */
|
||||
const readPayments = (_args: ToolArgs, read: CardReader) => read('list_settlements');
|
||||
|
||||
/** The name of the expense an `itemId` names, for the card built from the arguments. */
|
||||
function expenseName(key: string, value: number, card: TripCard): string | null {
|
||||
return key === 'itemId' ? clipped(budgetRows(card).get(value)?.name, 160) : null;
|
||||
}
|
||||
|
||||
export const BUDGET_CARDS: DomainCards = {
|
||||
id: 'budget',
|
||||
prompt: [
|
||||
'The chat draws the answers of list_budget_items, get_settlement_summary, list_settlements and get_budget_per_person as cards with every row; do not repeat those rows as text, sum up or answer the question instead.',
|
||||
],
|
||||
tools: {
|
||||
list_budget_items: { answer: expensesAnswer },
|
||||
get_settlement_summary: { answer: settlementAnswer },
|
||||
list_settlements: { answer: paymentsAnswer },
|
||||
get_budget_per_person: { answer: costPerPersonAnswer },
|
||||
create_budget_item: { change: createExpenseChange, applied: appliedExpense },
|
||||
update_budget_item: { change: updateExpenseChange },
|
||||
create_budget_item_with_members: {
|
||||
write: { verbKey: 'addBudgetItem', entity: 'budget' },
|
||||
describe: BUDGET_DESCRIBERS.create_budget_item_with_members,
|
||||
change: createWithMembersChange,
|
||||
applied: appliedExpense,
|
||||
undo: DELETE_CREATED_EXPENSE,
|
||||
},
|
||||
set_budget_item_members: {
|
||||
write: { verbKey: 'updateBudgetItem', entity: 'budget' },
|
||||
describe: BUDGET_DESCRIBERS.set_budget_item_members,
|
||||
change: setMembersChange,
|
||||
nameArg: expenseName,
|
||||
},
|
||||
toggle_budget_member_paid: {
|
||||
write: { verbKey: 'updateBudgetItem', entity: 'budget' },
|
||||
describe: BUDGET_DESCRIBERS.toggle_budget_member_paid,
|
||||
change: togglePaidChange,
|
||||
nameArg: expenseName,
|
||||
},
|
||||
delete_budget_item: {
|
||||
write: { verbKey: 'genericRemove', entity: 'budget' },
|
||||
describe: BUDGET_DESCRIBERS.delete_budget_item,
|
||||
change: deleteExpenseChange,
|
||||
nameArg: expenseName,
|
||||
},
|
||||
create_settlement: {
|
||||
write: { verbKey: 'addPayment', entity: 'settlement' },
|
||||
describe: BUDGET_DESCRIBERS.create_settlement,
|
||||
change: createPaymentChange,
|
||||
applied: appliedPayment,
|
||||
appliedIds: paymentIds,
|
||||
undo: DELETE_CREATED_PAYMENT,
|
||||
},
|
||||
update_settlement: {
|
||||
write: { verbKey: 'updatePayment', entity: 'settlement' },
|
||||
load: readPayments,
|
||||
describe: BUDGET_DESCRIBERS.update_settlement,
|
||||
change: updatePaymentChange,
|
||||
appliedIds: paymentIds,
|
||||
},
|
||||
delete_settlement: {
|
||||
write: { verbKey: 'genericRemove', entity: 'settlement' },
|
||||
load: readPayments,
|
||||
describe: BUDGET_DESCRIBERS.delete_settlement,
|
||||
change: deletePaymentChange,
|
||||
appliedIds: paymentIds,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -4,8 +4,15 @@ import { McpController, Tool, TOOL_ANNOTATIONS_READONLY, ok, type McpContext } f
|
||||
import type { BudgetItem } from '../../types';
|
||||
import { addonGate } from '../addons/addon-gate';
|
||||
import { AddonsService } from '../addons/addons.service';
|
||||
import { tripConverter } from './budget-money.helpers';
|
||||
import { BudgetService } from './budget.service';
|
||||
import { BUDGET_ITEMS_PAGE_DEFAULT, budgetItemsPageSchema, idSchema, type BudgetItemsPage } from '@trek/shared';
|
||||
import {
|
||||
BUDGET_ITEMS_PAGE_DEFAULT,
|
||||
budgetItemsPageSchema,
|
||||
idSchema,
|
||||
toMinor,
|
||||
type BudgetItemsPage,
|
||||
} from '@trek/shared';
|
||||
|
||||
/** The whole budget surface rides the budget addon, as in BudgetMcp. */
|
||||
const budgetAddonOn = addonGate(ADDON_IDS.BUDGET);
|
||||
@@ -33,25 +40,42 @@ function shortNote(note: string | null | undefined): string | null {
|
||||
return line.length > NOTE_CHARS ? `${line.slice(0, NOTE_CHARS - 1)}…` : line;
|
||||
}
|
||||
|
||||
/** Today's rates for the expenses that froze none, as the totals use them; null when none were needed. */
|
||||
type Rates = Record<string, number> | null;
|
||||
|
||||
/**
|
||||
* A foreign expense in the trip currency, at the rate it was booked at (else
|
||||
* today's), to the cent; null for one in the trip currency or one no rate
|
||||
* converts.
|
||||
*/
|
||||
function tripAmount(item: BudgetItem, tripCurrency: string, rates: Rates): number | null {
|
||||
if ((item.currency || tripCurrency).toUpperCase() === tripCurrency) return null;
|
||||
const convert = tripConverter(item.currency, item.exchange_rate, tripCurrency, rates);
|
||||
return convert ? toMinor(convert(item.total_price || 0)) / 100 : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* One expense as the tool answers it: the fields a model plans with and a
|
||||
* card shows, without the avatars, receipts and itemised ticket the REST row
|
||||
* carries, and with the start of its note. Amounts stay in the expense's own
|
||||
* currency, which is the trip's when the row names none.
|
||||
* card shows, without the avatars, receipt files and itemised ticket the REST
|
||||
* row carries, and with the start of its note and the number of its receipts.
|
||||
* Amounts stay in the expense's own currency, which is the trip's when the row
|
||||
* names none; a foreign one also says what it comes to in the trip currency.
|
||||
*/
|
||||
function slimItem(item: BudgetItem, tripCurrency: string) {
|
||||
function slimItem(item: BudgetItem, tripCurrency: string, rates: Rates) {
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
category: item.category,
|
||||
total_price: item.total_price,
|
||||
currency: item.currency || tripCurrency,
|
||||
trip_amount: tripAmount(item, tripCurrency, rates),
|
||||
expense_date: item.expense_date ?? null,
|
||||
note: shortNote(item.note),
|
||||
place_id: item.place_id ?? null,
|
||||
reservation_id: item.reservation_id ?? null,
|
||||
payers: rows(item.payers).map((p) => ({ user_id: p.user_id, amount: p.amount })),
|
||||
members: rows(item.members).map((m) => ({ user_id: m.user_id, amount: m.amount ?? null, paid: m.paid === 1 })),
|
||||
receipt_count: rows(item.receipts).length,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -60,11 +84,11 @@ function slimItem(item: BudgetItem, tripCurrency: string) {
|
||||
* would pass PAGE_CHARS. Always at least one, so a page is never empty while
|
||||
* expenses are left.
|
||||
*/
|
||||
function fillPage(items: BudgetItem[], tripCurrency: string) {
|
||||
function fillPage(items: BudgetItem[], tripCurrency: string, rates: Rates) {
|
||||
const page: ReturnType<typeof slimItem>[] = [];
|
||||
let chars = 0;
|
||||
for (const item of items) {
|
||||
const slim = slimItem(item, tripCurrency);
|
||||
const slim = slimItem(item, tripCurrency, rates);
|
||||
chars += JSON.stringify(slim).length + 1;
|
||||
if (page.length > 0 && chars > PAGE_CHARS) break;
|
||||
page.push(slim);
|
||||
@@ -100,7 +124,7 @@ export class BudgetItemsMcp {
|
||||
|
||||
@Tool({
|
||||
name: 'list_budget_items',
|
||||
description: `List the trip's expenses with their ids, up to ${BUDGET_ITEMS_PAGE_DEFAULT} at a time from offset. A page of long expenses ends earlier, so always go on at next_offset (null on the last page). Each expense: name, category, total_price in its currency, expense_date, the start of its note, the place or booking it belongs to (place_id, reservation_id), who paid how much (payers) and who shares it (members: amount is the member's share on an uneven split, null on an equal one; paid says whether they paid their share). people names the user ids. Next to the page, over all expenses: the trip currency, the total and the totals per category in it, and unconverted_item_ids, foreign expenses no exchange rate converts, which count in no total. Use the ids with update_budget_item and delete_budget_item.`,
|
||||
description: `List the trip's expenses with their ids, up to ${BUDGET_ITEMS_PAGE_DEFAULT} at a time from offset. A page of long expenses ends earlier, so always go on at next_offset (null on the last page). Each expense: name, category, total_price in its currency, expense_date, the start of its note, the place or booking it belongs to (place_id, reservation_id), who paid how much (payers), who shares it (members: amount is the member's share on an uneven split, null on an equal one; paid says whether they paid their share), trip_amount (a foreign expense in the trip currency, null otherwise or without a rate) and receipt_count. people names the user ids. Next to the page, over all expenses: the trip currency, the total and the totals per category in it, and unconverted_item_ids, foreign expenses no exchange rate converts, which count in no total. Use the ids with update_budget_item and delete_budget_item.`,
|
||||
inputSchema: { tripId: idSchema, ...budgetItemsPageSchema.shape },
|
||||
annotations: TOOL_ANNOTATIONS_READONLY,
|
||||
when: budgetAddonOn,
|
||||
@@ -112,13 +136,10 @@ export class BudgetItemsMcp {
|
||||
const currency = (trip.currency || 'EUR').toUpperCase();
|
||||
const items = await this.budget.listBudgetItems(tripId);
|
||||
// The figures get_trip_summary shows: each row at the rate it was booked at (#2525).
|
||||
const totals = await this.budget.tripTotals(
|
||||
tripId,
|
||||
currency,
|
||||
await this.budget.ratesForTripTotals(tripId, currency),
|
||||
);
|
||||
const rates = await this.budget.ratesForTripTotals(tripId, currency);
|
||||
const totals = await this.budget.tripTotals(tripId, currency, rates);
|
||||
const start = offset ?? 0;
|
||||
const page = fillPage(items.slice(start, start + (limit ?? BUDGET_ITEMS_PAGE_DEFAULT)), currency);
|
||||
const page = fillPage(items.slice(start, start + (limit ?? BUDGET_ITEMS_PAGE_DEFAULT)), currency, rates);
|
||||
const end = start + page.length;
|
||||
return ok({
|
||||
currency,
|
||||
|
||||
@@ -470,7 +470,7 @@ export class BudgetMcp {
|
||||
@Tool({
|
||||
name: 'create_budget_item_with_members',
|
||||
description:
|
||||
'Create a budget/expense item and set the trip members splitting it equally in one atomic operation. If userIds is omitted, the cost is split across all trip members; pass an explicit list to split among a subset, or an empty array for a planning-only entry with no split. Ask the user which members share this expense rather than guessing; resolve user IDs with list_trip_members. Only use when the item does not yet exist; if it already exists, use set_budget_item_members directly. For an uneven split, a foreign currency or a date, use create_budget_item instead.',
|
||||
'Create a budget/expense item and set the trip members splitting it equally in one atomic operation. If userIds is omitted or empty, the cost is split across all trip members; pass an explicit list to split among a subset. For a planning-only entry with no split, use create_budget_item with member_ids []. Ask the user which members share this expense rather than guessing; resolve user IDs with list_trip_members. Only use when the item does not yet exist; if it already exists, use set_budget_item_members directly. For an uneven split, a foreign currency or a date, use create_budget_item instead.',
|
||||
inputSchema: {
|
||||
tripId: idSchema,
|
||||
name: z.string().min(1).max(200),
|
||||
@@ -480,9 +480,7 @@ export class BudgetMcp {
|
||||
userIds: z
|
||||
.array(idSchema)
|
||||
.optional()
|
||||
.describe(
|
||||
'User IDs splitting this item; omit to split across all trip members, or pass an empty array for no split',
|
||||
),
|
||||
.describe('User IDs splitting this item; omit or pass an empty array to split across all trip members'),
|
||||
place_id: idSchema
|
||||
.optional()
|
||||
.describe(
|
||||
|
||||
@@ -0,0 +1,658 @@
|
||||
/**
|
||||
* ASTCARD-BUD-001 to ASTCARD-BUD-027
|
||||
*
|
||||
* The costs in the assistant chat: the cards the budget's reads show beside
|
||||
* their answer (the expense list, settle up, the payments recorded, the share
|
||||
* per traveller), the cards its held writes show inside the change card, the
|
||||
* words of the writes that had none, the receipts and the way back. Every
|
||||
* card goes through the registry's guarded calls, so each one asserted here
|
||||
* also fits the shared card union and the size cap.
|
||||
*/
|
||||
import { buildTripCard } from '../../../../../../src/nest/assistant/assistant-context';
|
||||
import { unchangedSinceApplied, undoTargetsOf } from '../../../../../../src/nest/assistant/assistant-undo.helpers';
|
||||
import { loadFor } from '../../../../../../src/nest/assistant/cards/card-context';
|
||||
import type { AnswerInput, PreviewContext } from '../../../../../../src/nest/assistant/cards/card-types';
|
||||
import { readKey } from '../../../../../../src/nest/assistant/cards/card-values';
|
||||
import {
|
||||
answerCardOf,
|
||||
appliedCardOf,
|
||||
ASSISTANT_UNDO_MAP,
|
||||
ASSISTANT_WRITE_TOOLS,
|
||||
CARD_PROMPT_LINES,
|
||||
nameArgOf,
|
||||
} from '../../../../../../src/nest/assistant/cards/registry';
|
||||
import { appliedChange, describeToolCall } from '../../../../../../src/nest/assistant/tool-call-preview.helpers';
|
||||
import {
|
||||
assistantChangePreviewSchema,
|
||||
MAX_CARD_CHARS,
|
||||
type CostPerPersonCard,
|
||||
type ExpensesCard,
|
||||
type PaymentsCard,
|
||||
type SettlementCard,
|
||||
type TrekCard,
|
||||
} from '@trek/shared';
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const logMock = vi.hoisted(() => ({ logInfo: vi.fn(), logError: vi.fn(), logWarn: vi.fn(), logDebug: vi.fn() }));
|
||||
vi.mock('../../../../../../src/nest/audit/audit-log.logger', () => logMock);
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
/** Mia (an uploaded picture), Jon (a picture from a sign-in provider) and Ada (none) on a euro trip in Lisbon. */
|
||||
const SUMMARY = {
|
||||
trip: { title: 'Lisbon', currency: 'EUR' },
|
||||
members: {
|
||||
owner: { id: 7, username: 'mia', avatar_url: '/uploads/avatars/mia.png' },
|
||||
collaborators: [
|
||||
{ id: 8, username: 'jon', avatar_url: 'https://sso.example/jon.png' },
|
||||
{ id: 9, username: 'ada', avatar_url: null },
|
||||
],
|
||||
},
|
||||
days: [],
|
||||
budget: {
|
||||
items: [
|
||||
{
|
||||
id: 31,
|
||||
name: 'Dinner',
|
||||
category: 'food',
|
||||
total_price: 90,
|
||||
currency: null,
|
||||
expense_date: '2026-06-03',
|
||||
place_id: 12,
|
||||
members: [
|
||||
{ user_id: 7, paid: 1, username: 'mia', avatar_url: '/uploads/avatars/mia.png', amount: null },
|
||||
{ user_id: 8, paid: 0, username: 'jon', avatar_url: null, amount: null },
|
||||
],
|
||||
payers: [{ user_id: 7, amount: 90, username: 'mia' }],
|
||||
receipts: [{ id: 1 }],
|
||||
},
|
||||
{
|
||||
id: 32,
|
||||
name: 'Museum',
|
||||
category: 'activities',
|
||||
total_price: 50,
|
||||
currency: 'USD',
|
||||
members: [
|
||||
{ user_id: 7, paid: 0, username: 'mia', amount: 30 },
|
||||
{ user_id: 8, paid: 1, username: 'jon', amount: 20 },
|
||||
],
|
||||
payers: [{ user_id: 8, amount: 50, username: 'jon' }],
|
||||
receipts: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const card = buildTripCard(1, 7, SUMMARY, { places: [{ id: 12, name: 'Time Out Market' }] }, null);
|
||||
const ctx: PreviewContext = { card, categories: new Map(), locale: 'en-GB', today: '2026-06-01', airport: () => null };
|
||||
|
||||
const MIA = { id: 7, name: 'mia', avatarKey: 'mia.png' };
|
||||
const JON = { id: 8, name: 'jon', avatarKey: null };
|
||||
const ADA = { id: 9, name: 'ada', avatarKey: null };
|
||||
|
||||
function answer<C extends TrekCard>(tool: string, value: unknown): C | null {
|
||||
const input: AnswerInput = { toolCallId: 'r1', args: {}, value, card, today: '2026-06-01' };
|
||||
return answerCardOf(tool, input) as C | null;
|
||||
}
|
||||
|
||||
const describe_ = (tool: string, args: Record<string, unknown>, loaded: unknown = null) =>
|
||||
describeToolCall(tool, 'tc-1', args, ctx, loaded);
|
||||
|
||||
function expensesOf(tool: string, args: Record<string, unknown>): ExpensesCard['rows'][number] | undefined {
|
||||
const preview = describe_(tool, args);
|
||||
return preview.card?.kind === 'expenses' ? preview.card.rows[0] : undefined;
|
||||
}
|
||||
|
||||
function paymentOf(tool: string, args: Record<string, unknown>, loaded: unknown = null) {
|
||||
const preview = describe_(tool, args, loaded);
|
||||
return preview.card?.kind === 'payments' ? preview.card.rows[0] : undefined;
|
||||
}
|
||||
|
||||
/** A page of `list_budget_items` as the tool answers it. */
|
||||
const LIST = {
|
||||
currency: 'EUR',
|
||||
total: 140,
|
||||
unconverted_item_ids: [34],
|
||||
item_count: 3,
|
||||
items: [
|
||||
{
|
||||
id: 31,
|
||||
name: 'Dinner',
|
||||
category: 'food',
|
||||
total_price: 100,
|
||||
currency: 'EUR',
|
||||
trip_amount: null,
|
||||
expense_date: '2026-06-03',
|
||||
note: 'tip',
|
||||
place_id: 12,
|
||||
reservation_id: null,
|
||||
payers: [{ user_id: 7, amount: 100 }],
|
||||
members: [
|
||||
{ user_id: 7, amount: null, paid: true },
|
||||
{ user_id: 8, amount: null, paid: false },
|
||||
{ user_id: 9, amount: null, paid: false },
|
||||
],
|
||||
receipt_count: 2,
|
||||
},
|
||||
{
|
||||
id: 32,
|
||||
name: 'Museum',
|
||||
category: 'activities',
|
||||
total_price: 50,
|
||||
currency: 'USD',
|
||||
trip_amount: 40,
|
||||
expense_date: null,
|
||||
place_id: null,
|
||||
payers: [{ user_id: 44, amount: 50 }],
|
||||
members: [
|
||||
{ user_id: 7, amount: 30, paid: false },
|
||||
{ user_id: 44, amount: 20, paid: true },
|
||||
],
|
||||
receipt_count: 0,
|
||||
},
|
||||
],
|
||||
people: [
|
||||
{ user_id: 7, username: 'mia' },
|
||||
{ user_id: 44, username: 'gone@example.com' },
|
||||
],
|
||||
};
|
||||
|
||||
describe('answer cards', () => {
|
||||
it('ASTCARD-BUD-001: the expense list shows each expense with its amounts, day, place, payers and split', () => {
|
||||
const list = answer<ExpensesCard>('list_budget_items', LIST);
|
||||
expect(list).toMatchObject({
|
||||
kind: 'expenses',
|
||||
offset: 0,
|
||||
more: 1,
|
||||
total: { amount: 140, currency: 'EUR' },
|
||||
unconverted: 1,
|
||||
});
|
||||
const [dinner, museum] = list?.rows ?? [];
|
||||
expect(dinner).toMatchObject({
|
||||
id: 31,
|
||||
name: 'Dinner',
|
||||
category: 'food',
|
||||
amount: { amount: 100, currency: 'EUR' },
|
||||
tripAmount: null,
|
||||
date: '2026-06-03',
|
||||
place: { id: 12, name: 'Time Out Market' },
|
||||
payers: [{ person: MIA, amount: 100 }],
|
||||
split: 'equal',
|
||||
receipts: 2,
|
||||
note: null,
|
||||
before: null,
|
||||
highlight: [],
|
||||
change: 'none',
|
||||
});
|
||||
// An even split hands the left-over cent out from the expense's id on, as the server does.
|
||||
expect(dinner?.shares).toEqual([
|
||||
{ person: MIA, amount: 33.33, paid: true },
|
||||
{ person: JON, amount: 33.34, paid: false },
|
||||
{ person: ADA, amount: 33.33, paid: false },
|
||||
]);
|
||||
// A foreign expense says what it comes to in the trip currency; an uneven split keeps its own amounts.
|
||||
expect(museum).toMatchObject({
|
||||
amount: { amount: 50, currency: 'USD' },
|
||||
tripAmount: { amount: 40, currency: 'EUR' },
|
||||
});
|
||||
expect(museum?.split).toBe('custom');
|
||||
expect(museum?.shares.map((share) => share.amount)).toEqual([30, 20]);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-002: someone who left the trip keeps the name the list gives them, masked like any answer', () => {
|
||||
const museum = answer<ExpensesCard>('list_budget_items', LIST)?.rows[1];
|
||||
const gone = museum?.payers[0]?.person;
|
||||
expect(gone).toMatchObject({ id: 44, avatarKey: null });
|
||||
expect(gone?.name).not.toContain('@');
|
||||
expect(museum?.shares[1]?.person.id).toBe(44);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-003: the list names twelve expenses and six people of a split, and counts the rest', () => {
|
||||
const members = [7, 8, 9, 10, 11, 12, 13].map((id) => ({ user_id: id, amount: null, paid: false }));
|
||||
const items = Array.from({ length: 14 }, (_, i) => ({ id: 100 + i, name: `Taxi ${i}`, total_price: 7, members }));
|
||||
const list = answer<ExpensesCard>('list_budget_items', { currency: 'EUR', total: 98, item_count: 14, items });
|
||||
expect(list?.rows).toHaveLength(12);
|
||||
expect(list?.more).toBe(2);
|
||||
expect(list?.rows[0]?.shares).toHaveLength(6);
|
||||
expect(list?.rows[0]?.moreShares).toBe(1);
|
||||
// Every share of the seven, named or not, adds up to the total.
|
||||
expect(list?.total).toEqual({ amount: 98, currency: 'EUR' });
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-004: an empty list, a broken answer and rows without a name or an amount show no card', () => {
|
||||
expect(answer('list_budget_items', { currency: 'EUR', total: 0, items: [] })).toBeNull();
|
||||
expect(answer('list_budget_items', 'not a list')).toBeNull();
|
||||
expect(answer('list_budget_items', { items: [{ id: 1, total_price: 5 }, { id: 2, name: 'Tip' }] })).toBeNull();
|
||||
expect(answer('get_settlement_summary', { error: 'nope' })).toBeNull();
|
||||
expect(answer('get_budget_per_person', { summary: [] })).toBeNull();
|
||||
expect(answer('list_settlements', { settlements: [] })).toBeNull();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-005: settle up names who pays whom, the balances of everyone not square and what no rate converts', () => {
|
||||
const settle = answer<SettlementCard>('get_settlement_summary', {
|
||||
summary: {
|
||||
currency: 'EUR',
|
||||
flows: [{ from: { user_id: 8, username: 'jon' }, to: { user_id: 7, username: 'mia' }, amount: 42.5 }],
|
||||
balances: [
|
||||
{ user_id: 8, balance: -42.5 },
|
||||
{ user_id: 9, balance: 0 },
|
||||
{ user_id: 7, balance: 42.5 },
|
||||
],
|
||||
settlements: [{ id: 1 }, { id: 2 }],
|
||||
unconverted: { item_ids: [3, 4], settlement_ids: [5], currencies: ['USD', 'bad', 'VND'] },
|
||||
},
|
||||
});
|
||||
expect(settle).toEqual({
|
||||
kind: 'settlement',
|
||||
currency: 'EUR',
|
||||
flows: [{ from: JON, to: MIA, amount: 42.5 }],
|
||||
moreFlows: 0,
|
||||
balances: [
|
||||
{ person: MIA, balance: 42.5 },
|
||||
{ person: JON, balance: -42.5 },
|
||||
],
|
||||
moreBalances: 0,
|
||||
recorded: 2,
|
||||
unconverted: { entries: 3, currencies: ['USD', 'VND'] },
|
||||
});
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-006: a trip everyone is square on keeps its balances and names no payment; long lists are counted', () => {
|
||||
const square = answer<SettlementCard>('get_settlement_summary', {
|
||||
summary: { currency: 'eur', flows: [], balances: [{ user_id: 7, balance: 0 }], settlements: [] },
|
||||
});
|
||||
expect(square).toMatchObject({ currency: 'EUR', flows: [], balances: [{ person: MIA, balance: 0 }], recorded: 0 });
|
||||
const flows = Array.from({ length: 13 }, () => ({ from: { user_id: 8 }, to: { user_id: 7 }, amount: 1 }));
|
||||
const long = answer<SettlementCard>('get_settlement_summary', { summary: { currency: 'EUR', flows } });
|
||||
expect(long?.flows).toHaveLength(12);
|
||||
expect(long?.moreFlows).toBe(1);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-007: the share per traveller lists the largest share first, with what of it is marked paid', () => {
|
||||
const shares = answer<CostPerPersonCard>('get_budget_per_person', {
|
||||
summary: [
|
||||
{ user_id: 8, username: 'jon', total_assigned: 61, total_paid: 61, items_count: 2, currency: 'EUR' },
|
||||
{ user_id: 7, username: 'mia', total_assigned: 69, total_paid: 45, items_count: 2, currency: 'EUR' },
|
||||
],
|
||||
});
|
||||
expect(shares).toEqual({
|
||||
kind: 'costPerPerson',
|
||||
currency: 'EUR',
|
||||
rows: [
|
||||
{ person: MIA, share: 69, paid: 45, count: 2 },
|
||||
{ person: JON, share: 61, paid: 61, count: 2 },
|
||||
],
|
||||
more: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-008: the payments read newest first, each on the day it was paid or else recorded', () => {
|
||||
const payments = answer<PaymentsCard>('list_settlements', {
|
||||
settlements: [
|
||||
{ id: 1, from_user_id: 8, to_user_id: 7, amount: 20, currency: null, created_at: '2026-06-01 10:00:00' },
|
||||
{
|
||||
id: 2,
|
||||
from_user_id: 9,
|
||||
to_user_id: 7,
|
||||
amount: 5,
|
||||
currency: 'usd',
|
||||
created_at: '2026-06-02 10:00:00',
|
||||
settled_at: '2026-06-05',
|
||||
note: 'Cash to mia@example.com',
|
||||
},
|
||||
{ id: 3, from_user_id: 9, to_user_id: 8, amount: 7, created_at: '2026-06-05 08:00:00', settled_at: null },
|
||||
],
|
||||
});
|
||||
expect(payments?.rows.map((row) => row.id)).toEqual([3, 2, 1]);
|
||||
expect(payments?.rows[1]).toMatchObject({
|
||||
from: ADA,
|
||||
to: MIA,
|
||||
amount: { amount: 5, currency: 'USD' },
|
||||
date: '2026-06-05',
|
||||
change: 'none',
|
||||
});
|
||||
expect(payments?.rows[1]?.note).toContain('Cash to');
|
||||
expect(payments?.rows[1]?.note).not.toContain('@');
|
||||
expect(payments?.rows[2]).toMatchObject({ amount: { amount: 20, currency: 'EUR' }, date: '2026-06-01' });
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-009: a list of long names and pictures stays under the size cap by counting the expenses it leaves out', () => {
|
||||
const roster = Array.from({ length: 20 }, (_, i) => ({
|
||||
id: 200 + i,
|
||||
username: `${'Traveller with a very long name '.repeat(3)}${i}`.slice(0, 80),
|
||||
avatar_url: `/uploads/avatars/${'a'.repeat(180)}${i}.png`,
|
||||
}));
|
||||
const team = { owner: roster[0], collaborators: roster.slice(1) };
|
||||
const crowded = buildTripCard(2, 200, { members: team }, null, null);
|
||||
const members = roster.map((person) => ({ user_id: person.id, amount: null, paid: false }));
|
||||
const payers = roster.slice(0, 3).map((person) => ({ user_id: person.id, amount: 1 }));
|
||||
const items = Array.from({ length: 12 }, (_, i) => ({
|
||||
id: 300 + i,
|
||||
name: `${'Dinner at the harbour '.repeat(6)}${i}`,
|
||||
total_price: 3,
|
||||
members,
|
||||
payers,
|
||||
}));
|
||||
const input: AnswerInput = { toolCallId: 'r1', args: {}, value: { items }, card: crowded, today: '2026-06-01' };
|
||||
const list = answerCardOf('list_budget_items', input) as ExpensesCard | null;
|
||||
expect(list).not.toBeNull();
|
||||
expect(JSON.stringify(list).length).toBeLessThanOrEqual(MAX_CARD_CHARS);
|
||||
expect(list?.rows.length).toBeLessThan(12);
|
||||
expect((list?.rows.length ?? 0) + (list?.more ?? 0)).toBe(12);
|
||||
});
|
||||
});
|
||||
|
||||
describe('change cards', () => {
|
||||
it('ASTCARD-BUD-010: a new expense is split evenly across everyone when the call names nobody', () => {
|
||||
const taxi = expensesOf('create_budget_item', { name: 'Taxi', total_price: 10, category: 'transport' });
|
||||
expect(taxi).toMatchObject({ id: null, name: 'Taxi', amount: { amount: 10, currency: 'EUR' }, change: 'new' });
|
||||
expect(taxi?.shares).toEqual([
|
||||
{ person: MIA, amount: 3.34, paid: false },
|
||||
{ person: JON, amount: 3.33, paid: false },
|
||||
{ person: ADA, amount: 3.33, paid: false },
|
||||
]);
|
||||
expect(describe_('create_budget_item', { name: 'Taxi', total_price: 10 }).verbKey).toBe('addBudgetItem');
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-011: named payers set the amount, an empty member list plans only, members split unevenly', () => {
|
||||
const paid = expensesOf('create_budget_item', {
|
||||
name: 'Groceries',
|
||||
total_price: 99,
|
||||
currency: 'usd',
|
||||
payers: [
|
||||
{ user_id: 8, amount: 12.5 },
|
||||
{ user_id: 9, amount: 7.5 },
|
||||
],
|
||||
member_ids: [8, 9],
|
||||
expense_date: '2026-06-04',
|
||||
place_id: 12,
|
||||
note: 'Market',
|
||||
});
|
||||
expect(paid).toMatchObject({
|
||||
amount: { amount: 20, currency: 'USD' },
|
||||
payers: [
|
||||
{ person: JON, amount: 12.5 },
|
||||
{ person: ADA, amount: 7.5 },
|
||||
],
|
||||
date: '2026-06-04',
|
||||
place: { id: 12, name: 'Time Out Market' },
|
||||
note: 'Market',
|
||||
split: 'equal',
|
||||
});
|
||||
const planned = expensesOf('create_budget_item', { name: 'Hotel', total_price: 300, member_ids: [] });
|
||||
expect(planned).toMatchObject({ split: 'none', shares: [], moreShares: 0 });
|
||||
const uneven = expensesOf('create_budget_item', {
|
||||
name: 'Boat',
|
||||
total_price: 10,
|
||||
members: [
|
||||
{ user_id: 7, amount: 6 },
|
||||
{ user_id: 8, amount: 4 },
|
||||
],
|
||||
});
|
||||
expect(uneven).toMatchObject({ split: 'custom' });
|
||||
expect(uneven?.shares.map((share) => [share.person.id, share.amount])).toEqual([
|
||||
[7, 6],
|
||||
[8, 4],
|
||||
]);
|
||||
expect(describe_('create_budget_item', {}).card).toBeUndefined();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-012: an expense with members is split by everyone when the list is empty, as the tool does', () => {
|
||||
const preview = describe_('create_budget_item_with_members', { name: 'Fuel', total_price: 30, userIds: [] });
|
||||
expect(preview).toMatchObject({
|
||||
verbKey: 'addBudgetItem',
|
||||
tool: 'create_budget_item_with_members',
|
||||
undoable: true,
|
||||
});
|
||||
expect(preview.rows).toContainEqual({ op: 'add', labelKey: 'members', before: null, after: 'mia, jon, ada' });
|
||||
const row = preview.card?.kind === 'expenses' ? preview.card.rows[0] : undefined;
|
||||
expect(row?.shares.map((share) => share.person.id)).toEqual([7, 8, 9]);
|
||||
expect(row?.amount).toEqual({ amount: 30, currency: 'EUR' });
|
||||
const two = expensesOf('create_budget_item_with_members', { name: 'Fuel', total_price: 30, userIds: [8, 9] });
|
||||
expect(two?.shares.map((share) => share.amount)).toEqual([15, 15]);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-013: a changed expense shows what it replaces, its new split and who joins it', () => {
|
||||
const preview = describe_('update_budget_item', {
|
||||
itemId: 31,
|
||||
name: 'Dinner for three',
|
||||
total_price: 120,
|
||||
member_ids: [7, 8, 9],
|
||||
});
|
||||
// The core's words stay; the card comes on top.
|
||||
expect(preview).toMatchObject({ verbKey: 'updateBudgetItem', subject: 'Dinner for three' });
|
||||
const row = preview.card?.kind === 'expenses' ? preview.card.rows[0] : undefined;
|
||||
expect(row).toMatchObject({
|
||||
id: 31,
|
||||
name: 'Dinner for three',
|
||||
amount: { amount: 120, currency: 'EUR' },
|
||||
date: '2026-06-03',
|
||||
receipts: 1,
|
||||
change: 'changed',
|
||||
highlight: [9],
|
||||
before: { name: 'Dinner', amount: { amount: 90, currency: 'EUR' }, category: null, date: null, split: null },
|
||||
});
|
||||
// Mia's paid tick stays; the even split is worked out again for the new total.
|
||||
expect(row?.shares).toEqual([
|
||||
{ person: MIA, amount: 40, paid: true },
|
||||
{ person: JON, amount: 40, paid: false },
|
||||
{ person: ADA, amount: 40, paid: false },
|
||||
]);
|
||||
const cleared = expensesOf('update_budget_item', { itemId: 31, expense_date: null, currency: 'USD' });
|
||||
expect(cleared?.before).toMatchObject({ amount: { amount: 90, currency: 'EUR' }, date: '2026-06-03' });
|
||||
expect(cleared).toMatchObject({ date: null, amount: { amount: 90, currency: 'USD' } });
|
||||
expect(describe_('update_budget_item', { itemId: 99, name: 'Ghost' }).card).toBeUndefined();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-014: new members of an expense share it evenly, the paid ticks kept and own amounts dropped', () => {
|
||||
const preview = describe_('set_budget_item_members', { itemId: 32, userIds: [7, 8, 9] });
|
||||
expect(preview).toMatchObject({ verbKey: 'updateBudgetItem', subject: 'Museum', tool: 'set_budget_item_members' });
|
||||
expect(preview.rows).toEqual([{ op: 'change', labelKey: 'members', before: 'mia, jon', after: 'mia, jon, ada' }]);
|
||||
const row = preview.card?.kind === 'expenses' ? preview.card.rows[0] : undefined;
|
||||
expect(row).toMatchObject({ split: 'equal', highlight: [9], before: { split: 'custom' } });
|
||||
expect(row?.shares).toEqual([
|
||||
{ person: MIA, amount: 16.67, paid: false },
|
||||
{ person: JON, amount: 16.66, paid: true },
|
||||
{ person: ADA, amount: 16.67, paid: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-015: a paid tick changes one share and names the person it is about', () => {
|
||||
const preview = describe_('toggle_budget_member_paid', { itemId: 31, memberId: 8, paid: true });
|
||||
expect(preview).toMatchObject({ subject: 'Dinner', tool: 'toggle_budget_member_paid' });
|
||||
expect(preview.rows).toEqual([{ op: 'add', labelKey: 'members', before: null, after: 'jon ✓' }]);
|
||||
const row = preview.card?.kind === 'expenses' ? preview.card.rows[0] : undefined;
|
||||
expect(row?.shares.map((share) => share.paid)).toEqual([true, true]);
|
||||
expect(row?.highlight).toEqual([8]);
|
||||
expect(row?.before).toBeNull();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-016: a removed expense shows as it is, marked to go; one the trip does not hold reads as its id', () => {
|
||||
const preview = describe_('delete_budget_item', { itemId: 32 });
|
||||
expect(preview).toMatchObject({ verbKey: 'genericRemove', subject: 'Museum', tool: 'delete_budget_item' });
|
||||
expect(preview.rows).toEqual([{ op: 'add', labelKey: 'price', before: null, after: 'US$50.00' }]);
|
||||
const row = preview.card?.kind === 'expenses' ? preview.card.rows[0] : undefined;
|
||||
expect(row).toMatchObject({ id: 32, change: 'willDelete', amount: { amount: 50, currency: 'USD' } });
|
||||
const ghost = describe_('delete_budget_item', { itemId: 99 });
|
||||
expect(ghost.subject).toBe('#99');
|
||||
expect(ghost.card).toBeUndefined();
|
||||
expect(nameArgOf('delete_budget_item', 'itemId', 31, card)).toBe('Dinner');
|
||||
expect(nameArgOf('delete_budget_item', 'itemId', 99, card)).toBeNull();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-017: a recorded payment names who pays whom, how much and on which day', () => {
|
||||
const args = { from_user_id: 8, to_user_id: 7, amount: 42.5, settled_at: '2026-06-07', note: 'Bank' };
|
||||
const preview = describe_('create_settlement', args);
|
||||
expect(preview).toMatchObject({
|
||||
verbKey: 'addPayment',
|
||||
subject: 'jon → mia',
|
||||
tool: 'create_settlement',
|
||||
undoable: true,
|
||||
});
|
||||
expect(preview.rows.map((row) => row.labelKey)).toEqual(['price', 'day', 'notes']);
|
||||
expect(paymentOf('create_settlement', args)).toEqual({
|
||||
id: null,
|
||||
from: JON,
|
||||
to: MIA,
|
||||
amount: { amount: 42.5, currency: 'EUR' },
|
||||
date: '2026-06-07',
|
||||
note: 'Bank',
|
||||
before: null,
|
||||
change: 'new',
|
||||
});
|
||||
expect(describe_('create_settlement', { from_user_id: 8, to_user_id: 7, amount: 0 }).card).toBeUndefined();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-018: a changed payment shows the people and amount it replaces, a removed one as it is', () => {
|
||||
const stored = {
|
||||
id: 5,
|
||||
from_user_id: 8,
|
||||
to_user_id: 7,
|
||||
amount: 20,
|
||||
currency: 'EUR',
|
||||
created_at: '2026-06-02 09:00:00',
|
||||
settled_at: null,
|
||||
note: 'cash',
|
||||
};
|
||||
const loaded = { settlements: [stored] };
|
||||
const args = { settlementId: 5, from_user_id: 9, to_user_id: 7, amount: 25 };
|
||||
const changed = describe_('update_settlement', args, loaded);
|
||||
expect(changed).toMatchObject({ verbKey: 'updatePayment', subject: 'ada → mia', tool: 'update_settlement' });
|
||||
expect(changed.rows).toEqual([{ op: 'change', labelKey: 'price', before: '€20.00', after: '€25.00' }]);
|
||||
const row = changed.card?.kind === 'payments' ? changed.card.rows[0] : undefined;
|
||||
expect(row).toMatchObject({
|
||||
id: 5,
|
||||
from: ADA,
|
||||
amount: { amount: 25, currency: 'EUR' },
|
||||
date: '2026-06-02',
|
||||
note: 'cash',
|
||||
change: 'changed',
|
||||
before: { from: JON, to: null, amount: { amount: 20, currency: 'EUR' } },
|
||||
});
|
||||
expect(paymentOf('delete_settlement', { settlementId: 5 }, loaded)).toMatchObject({ id: 5, change: 'willDelete' });
|
||||
const unknown = describe_('delete_settlement', { settlementId: 6 }, loaded);
|
||||
expect(unknown).toMatchObject({ verbKey: 'genericRemove', subject: '#6' });
|
||||
expect(unknown.card).toBeUndefined();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-019: a change to a payment looks the payments up once, through the turn reader', async () => {
|
||||
const read = vi.fn(async (_tool: string, _args?: Record<string, unknown>) => ({ settlements: [] }));
|
||||
expect(await loadFor('update_settlement', { settlementId: 5 }, read)).toEqual({ settlements: [] });
|
||||
expect(await loadFor('delete_settlement', { settlementId: 5 }, read)).toEqual({ settlements: [] });
|
||||
expect(read.mock.calls.map((call) => call[0])).toEqual(['list_settlements', 'list_settlements']);
|
||||
expect(await loadFor('create_settlement', {}, read)).toBeNull();
|
||||
expect(read).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-020: every budget write reads without arguments, and its card names the verb it declares', () => {
|
||||
const tools = [
|
||||
'create_budget_item_with_members',
|
||||
'set_budget_item_members',
|
||||
'toggle_budget_member_paid',
|
||||
'delete_budget_item',
|
||||
'create_settlement',
|
||||
'update_settlement',
|
||||
'delete_settlement',
|
||||
];
|
||||
for (const tool of tools) {
|
||||
const preview = describe_(tool, {});
|
||||
expect(assistantChangePreviewSchema.safeParse(preview).success, tool).toBe(true);
|
||||
expect(preview.verbKey, tool).toBe(ASSISTANT_WRITE_TOOLS[tool]?.verbKey);
|
||||
expect(preview.card, tool).toBeUndefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('receipts and the way back', () => {
|
||||
it('ASTCARD-BUD-021: a payment receipt names the payment, an expense receipt the expense', () => {
|
||||
const receipt = (tool: string, args: Record<string, unknown>, result: unknown) =>
|
||||
appliedChange(tool, 'tc', args, result, null, null);
|
||||
expect(receipt('create_settlement', {}, { settlement: { id: 77 } })).toMatchObject({
|
||||
entity: 'settlement',
|
||||
ids: [77],
|
||||
});
|
||||
expect(receipt('update_settlement', { settlementId: 5 }, { settlement: {} })).toMatchObject({ ids: [5] });
|
||||
expect(receipt('delete_settlement', { settlementId: 5 }, { success: true })).toMatchObject({ ids: [5] });
|
||||
expect(receipt('toggle_budget_member_paid', { itemId: 31 }, { member: {} })).toMatchObject({
|
||||
entity: 'budget',
|
||||
ids: [31],
|
||||
});
|
||||
expect(receipt('set_budget_item_members', { itemId: 32 }, { item: { id: 32 } })?.ids).toEqual([32]);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-022: an applied create keeps its card with the id the answer names', () => {
|
||||
const preview = describe_('create_budget_item', { name: 'Taxi', total_price: 10 });
|
||||
const expense = appliedCardOf('create_budget_item', { args: {}, result: { item: { id: 40 } }, preview });
|
||||
expect(expense?.kind === 'expenses' && expense.rows[0]?.id).toBe(40);
|
||||
const payment = describe_('create_settlement', { from_user_id: 8, to_user_id: 7, amount: 5 });
|
||||
const recorded = { settlement: { id: 9 } };
|
||||
const applied = appliedCardOf('create_settlement', { args: {}, result: recorded, preview: payment });
|
||||
expect(applied?.kind === 'payments' && applied.rows[0]?.id).toBe(9);
|
||||
// A card of another kind, or an answer that names no id, leaves the waiting card as it was.
|
||||
expect(appliedCardOf('create_settlement', { args: {}, result: { settlement: { id: 9 } }, preview })).toBeNull();
|
||||
expect(appliedCardOf('create_budget_item', { args: {}, result: { success: true }, preview })).toBeNull();
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-023: a recorded payment is taken back while nobody changed it since', () => {
|
||||
const created = {
|
||||
id: 77,
|
||||
from_user_id: 8,
|
||||
to_user_id: 7,
|
||||
amount: 5,
|
||||
currency: 'EUR',
|
||||
settled_at: null,
|
||||
note: null,
|
||||
};
|
||||
const result = { settlement: created };
|
||||
expect(ASSISTANT_UNDO_MAP.create_settlement?.tool).toBe('delete_settlement');
|
||||
expect(ASSISTANT_UNDO_MAP.create_settlement?.args({}, result, null)).toEqual({ settlementId: 77 });
|
||||
expect(ASSISTANT_UNDO_MAP.create_settlement?.args({}, { success: true }, null)).toBeNull();
|
||||
expect([...undoTargetsOf('delete_settlement', 'create_settlement', {}, result)]).toContain('list_settlements');
|
||||
const snapshot = (settlements: unknown[]) => ({
|
||||
summary: {},
|
||||
places: {},
|
||||
reads: new Map([[readKey('list_settlements', {}), { settlements }]]),
|
||||
});
|
||||
expect(unchangedSinceApplied('create_settlement', result, snapshot([created]))).toBe(true);
|
||||
const edited = { ...created, note: 'paid back' };
|
||||
expect(unchangedSinceApplied('create_settlement', result, snapshot([edited]))).toBe(false);
|
||||
expect(unchangedSinceApplied('create_settlement', result, snapshot([]))).toBe(false);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-024: an expense created with its split is taken back while it is as it was created', () => {
|
||||
const created = { id: 31, name: 'Dinner', total_price: 90, category: 'food' };
|
||||
const undo = ASSISTANT_UNDO_MAP.create_budget_item_with_members;
|
||||
expect(undo?.tool).toBe('delete_budget_item');
|
||||
expect(undo?.args({}, { item: created }, null)).toEqual({ itemId: 31 });
|
||||
const snapshot = { summary: SUMMARY, places: {} };
|
||||
expect(unchangedSinceApplied('create_budget_item_with_members', { item: created }, snapshot)).toBe(true);
|
||||
const renamed = { ...created, name: 'Lunch' };
|
||||
expect(unchangedSinceApplied('create_budget_item_with_members', { item: renamed }, snapshot)).toBe(false);
|
||||
expect(CARD_PROMPT_LINES.some((line) => line.includes('list_budget_items'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('edges', () => {
|
||||
it('ASTCARD-BUD-025: a later page of the list says where its expenses start, so pages join only where they meet', () => {
|
||||
const items = [{ id: 120, name: 'Taxi', total_price: 7 }];
|
||||
const page = answer<ExpensesCard>('list_budget_items', { currency: 'EUR', item_count: 21, offset: 20, items });
|
||||
expect(page).toMatchObject({ offset: 20, more: 20 });
|
||||
expect(answer<ExpensesCard>('list_budget_items', { items, offset: 'twenty' })?.offset).toBe(0);
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-026: an update that clears the payers but names a total keeps that total, as the write path does', () => {
|
||||
const row = expensesOf('update_budget_item', { itemId: 31, payers: [], total_price: 75 });
|
||||
expect(row).toMatchObject({ amount: { amount: 75, currency: 'EUR' }, payers: [] });
|
||||
expect(row?.before?.amount).toEqual({ amount: 90, currency: 'EUR' });
|
||||
const unpaid = expensesOf('update_budget_item', { itemId: 31, payers: [] });
|
||||
expect(unpaid?.amount).toEqual({ amount: 0, currency: 'EUR' });
|
||||
});
|
||||
|
||||
it('ASTCARD-BUD-027: a split of more people than a card may count says the most it may', () => {
|
||||
const members = Array.from({ length: 1100 }, (_, i) => ({ user_id: 1000 + i, amount: null, paid: false }));
|
||||
const items = [{ id: 5, name: 'Festival', total_price: 1100, members }];
|
||||
const list = answer<ExpensesCard>('list_budget_items', { items });
|
||||
expect(list?.rows[0]?.shares).toHaveLength(6);
|
||||
expect(list?.rows[0]?.moreShares).toBe(1000);
|
||||
});
|
||||
});
|
||||
@@ -155,6 +155,7 @@ describe('list_budget_items', () => {
|
||||
category: 'food',
|
||||
total_price: 90,
|
||||
currency: 'EUR',
|
||||
trip_amount: null,
|
||||
expense_date: '2026-05-02',
|
||||
note: 'Window table, tip included',
|
||||
place_id: f.place.id,
|
||||
@@ -164,6 +165,7 @@ describe('list_budget_items', () => {
|
||||
{ user_id: f.owner.id, amount: null, paid: true },
|
||||
{ user_id: f.guest.id, amount: null, paid: true },
|
||||
],
|
||||
receipt_count: 0,
|
||||
},
|
||||
{
|
||||
id: f.museum.id,
|
||||
@@ -171,6 +173,8 @@ describe('list_budget_items', () => {
|
||||
category: 'activities',
|
||||
total_price: 50,
|
||||
currency: 'USD',
|
||||
// 50 USD at the 1.25 frozen when it was booked.
|
||||
trip_amount: 40,
|
||||
expense_date: null,
|
||||
note: null,
|
||||
place_id: null,
|
||||
@@ -180,6 +184,7 @@ describe('list_budget_items', () => {
|
||||
{ user_id: f.owner.id, amount: 30, paid: false },
|
||||
{ user_id: f.guest.id, amount: 20, paid: true },
|
||||
],
|
||||
receipt_count: 0,
|
||||
},
|
||||
],
|
||||
people: [
|
||||
@@ -248,6 +253,8 @@ describe('list_budget_items', () => {
|
||||
const answer = read(await mcp.listBudgetItems({ tripId: trip.id }, ctx(user.id)));
|
||||
expect(answer).toMatchObject({ total: 200, by_category: { accommodation: 200 }, unconverted_item_ids: [pho.id] });
|
||||
expect(getRates).toHaveBeenCalledWith('EUR');
|
||||
// Nothing converts it, so it names no amount in the trip currency either.
|
||||
expect(answer.items.find((item: { id: number }) => item.id === pho.id)).toMatchObject({ trip_amount: null });
|
||||
});
|
||||
|
||||
it('BGI-MCP-014: someone who cannot open the trip gets the trip refusal and no row', async () => {
|
||||
@@ -274,6 +281,40 @@ describe('list_budget_items', () => {
|
||||
expect(answer.people).toEqual([]);
|
||||
});
|
||||
|
||||
it('BGI-MCP-019: a foreign expense says what it comes to in the trip currency, and every expense how many receipts it has', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
const trip = await makeTrip(orm, user.id, { currency: 'EUR' });
|
||||
const receipt = { filename: 'r.pdf', original_name: 'r.pdf', file_size: 1, mime_type: 'application/pdf' };
|
||||
const items: BudgetItem[] = [
|
||||
{
|
||||
id: 8,
|
||||
trip_id: trip.id,
|
||||
category: 'food',
|
||||
name: 'Diner',
|
||||
total_price: 30,
|
||||
currency: 'USD',
|
||||
exchange_rate: 1.2,
|
||||
sort_order: 0,
|
||||
receipts: [
|
||||
{ id: 1, url: '/a', ...receipt },
|
||||
{ id: 2, url: '/b', ...receipt },
|
||||
],
|
||||
},
|
||||
{ id: 9, trip_id: trip.id, category: 'food', name: 'Bakery', total_price: 4, currency: 'eur', sort_order: 1 },
|
||||
];
|
||||
vi.spyOn(t.get(BudgetService), 'listBudgetItems').mockResolvedValue(items);
|
||||
|
||||
const answer = read(await mcp.listBudgetItems({ tripId: trip.id }, ctx(user.id)));
|
||||
|
||||
// 30 USD at the 1.2 frozen when it was booked; a lower-case trip currency is the trip's own.
|
||||
expect(answer.items).toEqual([
|
||||
expect.objectContaining({ id: 8, currency: 'USD', trip_amount: 25, receipt_count: 2 }),
|
||||
expect.objectContaining({ id: 9, trip_amount: null, receipt_count: 0 }),
|
||||
]);
|
||||
// A count, never the files: no receipt name or link reaches the model.
|
||||
expect(JSON.stringify(answer)).not.toMatch(/r\.pdf|receipts/);
|
||||
});
|
||||
|
||||
it('BGI-MCP-016: a page of long expenses ends before the assistant cuts the answer, and next_offset starts at the first one it left out', async () => {
|
||||
const { user } = await makeUser(orm, { username: 'organiser' });
|
||||
const trip = await makeTrip(orm, user.id, { currency: 'EUR' });
|
||||
|
||||
Reference in New Issue
Block a user