mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
feat(assistant): packing list and to-do cards
This commit is contained in:
@@ -1,8 +1,121 @@
|
||||
import { idSchema } from '../../common/primitives.schema';
|
||||
import { cardHexColorSchema, cardPersonSchema, cardText } from './card-parts';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* A packing list: the trip's, a tour's or a template's. Only its kind so far; the packing and to-do
|
||||
* cards give it its fields.
|
||||
* A packing list in the assistant chat: the trip's, the part a tour points
|
||||
* at, or a template about to be added. It is grouped the way the packing
|
||||
* list shows it, by category or by bag; a bag overview is the same card with
|
||||
* the bags as groups and no item rows. On a change card the rows carry what
|
||||
* the change does to them (`change`, and the old value of every field it
|
||||
* touches), on an answer `change` is `none`.
|
||||
*/
|
||||
export const packingListCardSchema = z.object({ kind: z.literal('packingList') });
|
||||
|
||||
const gramsSchema = z.number().int().min(0).max(1_000_000);
|
||||
const checklistCountSchema = z.number().int().min(0).max(5_000);
|
||||
|
||||
/**
|
||||
* The index of a category in the order its list first shows it; the client
|
||||
* maps it onto the list's own colours, so the chat's dot matches the panel's.
|
||||
* Null where the card does not know the category yet (the hash colour then).
|
||||
*/
|
||||
export const checklistColorSlotSchema = z.number().int().min(0).max(999);
|
||||
|
||||
/** The value a field held before a change, as stored (an ISO date, a colour, grams, a name). */
|
||||
export const checklistBeforeSchema = z.string().max(120).nullable();
|
||||
|
||||
export const PACKING_ROW_CHANGES = ['none', 'add', 'update', 'check', 'uncheck', 'remove', 'share'] as const;
|
||||
export const PACKING_ITEM_FIELDS = ['name', 'category', 'quantity', 'packed', 'weight', 'bag', 'tier'] as const;
|
||||
export const PACKING_BAG_FIELDS = ['name', 'color', 'limit', 'owner'] as const;
|
||||
export const PACKING_TIERS = ['common', 'personal', 'shared'] as const;
|
||||
|
||||
/** A bag on the list (`id:12`) or one a pending import creates (`new:0`). */
|
||||
export const packingBagKeySchema = z.string().regex(/^(id:\d{1,12}|new:\d{1,2})$/);
|
||||
|
||||
export const packingCardBagSchema = z.object({
|
||||
key: packingBagKeySchema,
|
||||
id: idSchema.nullable(),
|
||||
name: cardText(100),
|
||||
/** Null while a create waits for the server to pick it. */
|
||||
color: cardHexColorSchema.nullable(),
|
||||
/** What the bag weighs with every member's items in it (#2191). */
|
||||
weightGrams: gramsSchema.nullable(),
|
||||
limitGrams: gramsSchema.nullable(),
|
||||
owner: cardPersonSchema.nullable(),
|
||||
members: z.array(cardPersonSchema).max(6),
|
||||
membersRemoved: z.array(cardPersonSchema).max(6),
|
||||
change: z.enum(['none', 'add', 'update', 'remove', 'members']),
|
||||
changed: z.array(z.object({ field: z.enum(PACKING_BAG_FIELDS), before: checklistBeforeSchema })).max(4),
|
||||
/** For a bag about to be deleted: the items that land in "Unassigned". */
|
||||
movesOut: checklistCountSchema,
|
||||
});
|
||||
export type PackingCardBag = z.infer<typeof packingCardBagSchema>;
|
||||
|
||||
export const packingCardItemSchema = z.object({
|
||||
/** Null while a create waits. */
|
||||
id: idSchema.nullable(),
|
||||
name: cardText(200),
|
||||
quantity: z.number().int().min(1).max(999),
|
||||
/** All of it once ticked, the partial count before that (#2296). */
|
||||
packed: z.number().int().min(0).max(999),
|
||||
checked: z.boolean(),
|
||||
/** Unit weight times quantity; null where none is recorded. */
|
||||
weightGrams: gramsSchema.nullable(),
|
||||
/** One of the card's `bags`; null for an item in no bag. */
|
||||
bagKey: packingBagKeySchema.nullable(),
|
||||
tier: z.enum(PACKING_TIERS),
|
||||
sharedWith: z.array(cardPersonSchema).max(4),
|
||||
change: z.enum(PACKING_ROW_CHANGES),
|
||||
changed: z.array(z.object({ field: z.enum(PACKING_ITEM_FIELDS), before: checklistBeforeSchema })).max(7),
|
||||
});
|
||||
export type PackingCardItem = z.infer<typeof packingCardItemSchema>;
|
||||
|
||||
export const packingCardGroupSchema = z.object({
|
||||
key: z.discriminatedUnion('by', [
|
||||
z.object({
|
||||
by: z.literal('category'),
|
||||
/** Null: the items without a category, which the list files under its default one. */
|
||||
name: cardText(100).nullable(),
|
||||
slot: checklistColorSlotSchema.nullable(),
|
||||
}),
|
||||
/** Null: the pile in no bag. */
|
||||
z.object({ by: z.literal('bag'), bagKey: packingBagKeySchema.nullable() }),
|
||||
]),
|
||||
/** Who looks after the category. */
|
||||
assignees: z.array(cardPersonSchema).max(6),
|
||||
/** Who a change of the category's people takes off it. */
|
||||
assigneesRemoved: z.array(cardPersonSchema).max(6),
|
||||
items: z.array(packingCardItemSchema).max(40),
|
||||
/** The group's items on the list, shown or not. */
|
||||
count: checklistCountSchema,
|
||||
packed: checklistCountSchema,
|
||||
});
|
||||
export type PackingCardGroup = z.infer<typeof packingCardGroupSchema>;
|
||||
|
||||
export const packingListCardSchema = z.object({
|
||||
kind: z.literal('packingList'),
|
||||
source: z.discriminatedUnion('type', [
|
||||
z.object({ type: z.literal('trip') }),
|
||||
z.object({
|
||||
type: z.literal('tour'),
|
||||
placeId: idSchema,
|
||||
name: cardText(200),
|
||||
category: cardText(200).nullable(),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('template'),
|
||||
templateId: idSchema,
|
||||
name: cardText(100),
|
||||
itemCount: checklistCountSchema,
|
||||
}),
|
||||
]),
|
||||
groupBy: z.enum(['category', 'bag']),
|
||||
/** How much of the list is packed; answers only. */
|
||||
progress: z.object({ packed: checklistCountSchema, total: checklistCountSchema }).nullable(),
|
||||
bags: z.array(packingCardBagSchema).max(20),
|
||||
groups: z.array(packingCardGroupSchema).max(12),
|
||||
/** Groups past the twelve shown. */
|
||||
moreGroups: checklistCountSchema,
|
||||
});
|
||||
export type PackingListCard = z.infer<typeof packingListCardSchema>;
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
import { packingListItemsRequestSchema } from '../../packing/packing.schema';
|
||||
import { storedTrekCardSchema, trekCardSchema } from './index';
|
||||
import { packingListCardSchema } from './packing-list.card';
|
||||
import { todoListCardSchema } from './todo-list.card';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
/**
|
||||
* PKCARD-001 to PKCARD-008: the packing and to-do cards of the assistant
|
||||
* chat. Both kinds join the card union; every field is bounded; a bag is
|
||||
* named by a key, a colour only as `#rrggbb`, a person never with an address,
|
||||
* a date only as `YYYY-MM-DD`; and the list read they come from takes its
|
||||
* filters in a closed shape.
|
||||
*/
|
||||
|
||||
const ANA = { id: 2, name: 'Ana', avatarKey: 'ana.png' };
|
||||
|
||||
const item = (over: Record<string, unknown> = {}) => ({
|
||||
id: 7,
|
||||
name: 'Rain jacket',
|
||||
quantity: 1,
|
||||
packed: 0,
|
||||
checked: false,
|
||||
weightGrams: 480,
|
||||
bagKey: 'id:3',
|
||||
tier: 'common',
|
||||
sharedWith: [],
|
||||
change: 'none',
|
||||
changed: [],
|
||||
...over,
|
||||
});
|
||||
|
||||
const bag = (over: Record<string, unknown> = {}) => ({
|
||||
key: 'id:3',
|
||||
id: 3,
|
||||
name: 'Carry-on',
|
||||
color: '#6366f1',
|
||||
weightGrams: 6200,
|
||||
limitGrams: 8000,
|
||||
owner: ANA,
|
||||
members: [ANA],
|
||||
membersRemoved: [],
|
||||
change: 'none',
|
||||
changed: [],
|
||||
movesOut: 0,
|
||||
...over,
|
||||
});
|
||||
|
||||
const packing = (over: Record<string, unknown> = {}) => ({
|
||||
kind: 'packingList',
|
||||
source: { type: 'trip' },
|
||||
groupBy: 'category',
|
||||
progress: { packed: 1, total: 3 },
|
||||
bags: [bag()],
|
||||
groups: [
|
||||
{
|
||||
key: { by: 'category', name: 'Clothes', slot: 0 },
|
||||
assignees: [ANA],
|
||||
assigneesRemoved: [],
|
||||
items: [item()],
|
||||
count: 3,
|
||||
packed: 1,
|
||||
},
|
||||
],
|
||||
moreGroups: 0,
|
||||
...over,
|
||||
});
|
||||
|
||||
/** The packing card with one row in its one group. */
|
||||
const withRow = (row: unknown) => packing({ groups: [{ ...packing().groups[0], items: [row] }] });
|
||||
|
||||
const todo = (over: Record<string, unknown> = {}) => ({
|
||||
id: 9,
|
||||
name: 'Book the ferry',
|
||||
note: 'Check the early sailing first',
|
||||
checked: false,
|
||||
dueDate: '2026-06-12',
|
||||
dueDayId: 31,
|
||||
priority: 1,
|
||||
assignee: ANA,
|
||||
change: 'none',
|
||||
changed: [],
|
||||
...over,
|
||||
});
|
||||
|
||||
const todos = (items: unknown[] = [todo()]) => ({
|
||||
kind: 'todoList',
|
||||
progress: { open: 2, done: 1, overdue: 1 },
|
||||
groups: [{ category: 'Bookings', slot: 0, assignees: [], assigneesRemoved: [], items, count: 2, open: 2 }],
|
||||
moreGroups: 0,
|
||||
});
|
||||
|
||||
describe('packing and to-do cards', () => {
|
||||
it('PKCARD-001: a packing list, a bag overview, a tour and a template source all read as cards of the union', () => {
|
||||
expect(trekCardSchema.parse(packing()).kind).toBe('packingList');
|
||||
const bags = packing({ groupBy: 'bag', progress: null, groups: [] });
|
||||
expect(packingListCardSchema.safeParse(bags).success).toBe(true);
|
||||
const tour = { type: 'tour', placeId: 11, name: 'Ridge', category: 'Hiking gear' };
|
||||
expect(packingListCardSchema.safeParse(packing({ source: tour })).success).toBe(true);
|
||||
const template = { type: 'template', templateId: 4, name: 'Beach week', itemCount: 24 };
|
||||
expect(packingListCardSchema.safeParse(packing({ source: template, groups: [] })).success).toBe(true);
|
||||
const pile = { key: { by: 'bag', bagKey: null }, assignees: [], assigneesRemoved: [], items: [], count: 2 };
|
||||
expect(packingListCardSchema.safeParse(packing({ groups: [{ ...pile, packed: 0 }] })).success).toBe(true);
|
||||
});
|
||||
|
||||
it('PKCARD-002: bags are named by key, a waiting one as new:N; any other key is refused', () => {
|
||||
const waiting = bag({ key: 'new:0', id: null, color: null });
|
||||
expect(packingListCardSchema.safeParse(packing({ bags: [waiting] })).success).toBe(true);
|
||||
for (const key of ['3', 'id:', 'new:123', 'bag:3']) {
|
||||
expect(packingListCardSchema.safeParse(packing({ bags: [bag({ key })] })).success, key).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it('PKCARD-003: a colour is only #rrggbb and a person never carries an address', () => {
|
||||
expect(packingListCardSchema.safeParse(packing({ bags: [bag({ color: '#abc' })] })).success).toBe(false);
|
||||
const url = { ...ANA, avatarKey: 'https://example.com/a.png' };
|
||||
expect(packingListCardSchema.safeParse(packing({ bags: [bag({ owner: url })] })).success).toBe(false);
|
||||
expect(todoListCardSchema.safeParse(todos([todo({ assignee: url })])).success).toBe(false);
|
||||
});
|
||||
|
||||
it('PKCARD-004: counts stay in bounds: no more than 999 packed, no item without a name', () => {
|
||||
for (const row of [item({ packed: 1000 }), item({ name: '' }), item({ quantity: 0 })]) {
|
||||
expect(packingListCardSchema.safeParse(withRow(row)).success).toBe(false);
|
||||
}
|
||||
const many = packing({ groups: Array(13).fill(packing().groups[0]) });
|
||||
expect(packingListCardSchema.safeParse(many).success).toBe(false);
|
||||
});
|
||||
|
||||
it('PKCARD-005: a change row says what it does and what each touched field held before', () => {
|
||||
const changed = item({ change: 'update', changed: [{ field: 'bag', before: 'Daypack' }] });
|
||||
expect(packingListCardSchema.safeParse(withRow(changed)).success).toBe(true);
|
||||
const odd = item({ change: 'update', changed: [{ field: 'colour', before: null }] });
|
||||
expect(packingListCardSchema.safeParse(withRow(odd)).success).toBe(false);
|
||||
});
|
||||
|
||||
it('PKCARD-006: a to-do list reads as a card, a tick keeping whether it was done; a local date is refused', () => {
|
||||
expect(trekCardSchema.parse(todos()).kind).toBe('todoList');
|
||||
expect(todoListCardSchema.safeParse(todos([todo({ dueDate: '12.06.2026' })])).success).toBe(false);
|
||||
expect(todoListCardSchema.safeParse(todos([todo({ priority: 4 })])).success).toBe(false);
|
||||
const plain = todo({ dueDate: null, dueDayId: null, note: null });
|
||||
expect(todoListCardSchema.safeParse(todos([plain])).success).toBe(true);
|
||||
const ticked = todo({ change: 'check', checked: true, changed: [{ field: 'done', before: '0' }] });
|
||||
expect(todoListCardSchema.safeParse(todos([ticked])).success).toBe(true);
|
||||
});
|
||||
|
||||
it('PKCARD-007: a broken stored card reads as null instead of breaking the message', () => {
|
||||
expect(storedTrekCardSchema.parse(packing({ groupBy: 'weight' }))).toBeNull();
|
||||
expect(storedTrekCardSchema.parse(todos([todo({ change: 'archive' })]))).toBeNull();
|
||||
});
|
||||
|
||||
it('PKCARD-008: the list read takes a category, a bag or none, a packed state and a grouping, nothing else', () => {
|
||||
expect(packingListItemsRequestSchema.parse({ bag_id: null, status: 'open', group_by: 'bag' })).toEqual({
|
||||
bag_id: null,
|
||||
status: 'open',
|
||||
group_by: 'bag',
|
||||
});
|
||||
expect(packingListItemsRequestSchema.safeParse({ status: 'done' }).success).toBe(false);
|
||||
expect(packingListItemsRequestSchema.safeParse({ bag_id: 0 }).success).toBe(false);
|
||||
expect(packingListItemsRequestSchema.safeParse({ category: '' }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -9,8 +9,10 @@ export * from './todo-list.card';
|
||||
* the verbs of their change cards and what an applied change of theirs
|
||||
* touches, beyond the core ones. cards/index.ts and assistant.schema.ts
|
||||
* spread these into the card union and the shared enums, so a new kind, verb
|
||||
* or entity is added here and nowhere else.
|
||||
* or entity is added here and nowhere else. Their changes name the generic
|
||||
* verbs with the tool's own label; a bag is the one thing they touch that the
|
||||
* core has no name for.
|
||||
*/
|
||||
export const PACKING_CARD_SCHEMAS = [packingListCardSchema, todoListCardSchema] as const;
|
||||
export const PACKING_VERB_KEYS = [] as const;
|
||||
export const PACKING_APPLIED_ENTITIES = [] as const;
|
||||
export const PACKING_APPLIED_ENTITIES = ['packing_bag'] as const;
|
||||
|
||||
@@ -1,5 +1,59 @@
|
||||
import { idSchema } from '../../common/primitives.schema';
|
||||
import { cardDateSchema, cardPersonSchema, cardText } from './card-parts';
|
||||
import { checklistBeforeSchema, checklistColorSlotSchema } from './packing-list.card';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/** To-dos of the trip. Only its kind so far; the packing and to-do cards give it its fields. */
|
||||
export const todoListCardSchema = z.object({ kind: z.literal('todoList') });
|
||||
/**
|
||||
* The trip's to-dos in the assistant chat, grouped by their list (category)
|
||||
* the way the to-do panel shows them. On a change card the rows carry what
|
||||
* the change does to them and the old value of every field it touches; on an
|
||||
* answer `change` is `none`.
|
||||
*/
|
||||
|
||||
const todoCountSchema = z.number().int().min(0).max(5_000);
|
||||
|
||||
export const TODO_ROW_CHANGES = ['none', 'add', 'update', 'check', 'uncheck', 'remove'] as const;
|
||||
/** `done` is kept by a tick only (`'1'` or `'0'`), for the way back; it is never drawn as a fact. */
|
||||
export const TODO_FIELDS = ['name', 'note', 'category', 'dueDate', 'priority', 'assignee', 'done'] as const;
|
||||
|
||||
export const todoCardItemSchema = z.object({
|
||||
/** Null while a create waits. */
|
||||
id: idSchema.nullable(),
|
||||
name: cardText(300),
|
||||
/** The first line of the description. */
|
||||
note: cardText(160).nullable(),
|
||||
checked: z.boolean(),
|
||||
dueDate: cardDateSchema.nullable(),
|
||||
/** The trip day on the due date, so the date can show that day in the plan. */
|
||||
dueDayId: idSchema.nullable(),
|
||||
/** 0 for none, 1 the highest. */
|
||||
priority: z.number().int().min(0).max(3),
|
||||
assignee: cardPersonSchema.nullable(),
|
||||
change: z.enum(TODO_ROW_CHANGES),
|
||||
changed: z.array(z.object({ field: z.enum(TODO_FIELDS), before: checklistBeforeSchema })).max(7),
|
||||
});
|
||||
export type TodoCardItem = z.infer<typeof todoCardItemSchema>;
|
||||
|
||||
export const todoCardGroupSchema = z.object({
|
||||
/** Null: the to-dos on no list. */
|
||||
category: cardText(100).nullable(),
|
||||
slot: checklistColorSlotSchema.nullable(),
|
||||
/** Who a list's new to-dos go to by default. */
|
||||
assignees: z.array(cardPersonSchema).max(6),
|
||||
assigneesRemoved: z.array(cardPersonSchema).max(6),
|
||||
items: z.array(todoCardItemSchema).max(40),
|
||||
/** The group's to-dos, shown or not. */
|
||||
count: todoCountSchema,
|
||||
open: todoCountSchema,
|
||||
});
|
||||
export type TodoCardGroup = z.infer<typeof todoCardGroupSchema>;
|
||||
|
||||
export const todoListCardSchema = z.object({
|
||||
kind: z.literal('todoList'),
|
||||
/** Overdue against the day of the read; answers only. */
|
||||
progress: z.object({ open: todoCountSchema, done: todoCountSchema, overdue: todoCountSchema }).nullable(),
|
||||
groups: z.array(todoCardGroupSchema).max(12),
|
||||
moreGroups: todoCountSchema,
|
||||
});
|
||||
export type TodoListCard = z.infer<typeof todoListCardSchema>;
|
||||
|
||||
Reference in New Issue
Block a user