Files
TREK/shared/src/collab/collab.schema.spec.ts
T
Maurice b64da26651 feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect
their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or
OpenRouter key) or use the one the admin set up, including a custom
OpenAI-compatible server. It is an addon and only shows while a connection
is usable.

The chat runs on the existing MCP tools, pinned to the trip. Every change is
held as a change card the user applies or discards, and applied changes can
be undone. On the desktop it is a floating window that can be moved and
resized; on the phone it is fullscreen. Search results show as place cards
that can go onto a day, weather lookups as a strip, proposed places as
ghost markers on the map with a before/after switch, and an answer can be
shared into the trip chat. Booking files can be dropped in and are read
into change cards.
2026-10-22 21:30:12 +00:00

142 lines
5.4 KiB
TypeScript

import {
collabNoteCreateRequestSchema,
collabNoteUpdateRequestSchema,
collabPollCreateRequestSchema,
collabPollVoteRequestSchema,
collabLinkCreateRequestSchema,
collabLinkUpdateRequestSchema,
collabMessageCreateRequestSchema,
collabReactionRequestSchema,
collabMessageSchema,
COLLAB_MESSAGE_ORIGIN_ASSISTANT,
} from './collab.schema';
import { describe, it, expect } from 'vitest';
describe('collabNoteCreateRequestSchema', () => {
it('requires a non-empty title; the rest is optional', () => {
expect(collabNoteCreateRequestSchema.safeParse({ title: 'Idea' }).success).toBe(true);
expect(collabNoteCreateRequestSchema.safeParse({ title: '' }).success).toBe(false);
expect(collabNoteCreateRequestSchema.safeParse({}).success).toBe(false);
});
it('accepts explicit null for the optional fields (desktop null-clearing)', () => {
expect(
collabNoteCreateRequestSchema.safeParse({
title: 'Idea',
content: 'Body',
category: null,
color: '#6366f1',
website: null,
}).success,
).toBe(true);
});
});
describe('collabNoteUpdateRequestSchema', () => {
it('accepts explicit null for the optional fields (desktop null-clearing)', () => {
expect(
collabNoteUpdateRequestSchema.safeParse({
title: 'Idea',
content: null,
category: null,
color: null,
website: null,
pinned: 1,
}).success,
).toBe(true);
expect(collabNoteUpdateRequestSchema.safeParse({ pinned: true }).success).toBe(true);
expect(collabNoteUpdateRequestSchema.safeParse({ title: null }).success).toBe(false);
});
});
describe('collabPollCreateRequestSchema', () => {
it('requires a question and at least two options', () => {
expect(
collabPollCreateRequestSchema.safeParse({
question: 'Where?',
options: ['A', 'B'],
}).success,
).toBe(true);
expect(
collabPollCreateRequestSchema.safeParse({
question: 'Where?',
options: ['A'],
}).success,
).toBe(false);
expect(collabPollCreateRequestSchema.safeParse({ options: ['A', 'B'] }).success).toBe(false);
});
});
describe('collabPollVoteRequestSchema', () => {
it('requires a numeric option_index', () => {
expect(collabPollVoteRequestSchema.safeParse({ option_index: 0 }).success).toBe(true);
expect(collabPollVoteRequestSchema.safeParse({ option_index: 'a' }).success).toBe(false);
});
});
describe('collabLinkCreateRequestSchema', () => {
it('requires a title and an http(s) URL', () => {
expect(collabLinkCreateRequestSchema.safeParse({ title: 'Docs', url: 'https://example.com' }).success).toBe(true);
expect(collabLinkCreateRequestSchema.safeParse({ title: 'Docs', url: 'ftp://example.com' }).success).toBe(false);
expect(collabLinkCreateRequestSchema.safeParse({ title: ' ', url: 'https://example.com' }).success).toBe(false);
expect(collabLinkCreateRequestSchema.safeParse({ title: 'Docs', url: 'not-a-url' }).success).toBe(false);
});
});
describe('collabLinkUpdateRequestSchema', () => {
it('allows partial updates and still rejects non-http URLs', () => {
expect(collabLinkUpdateRequestSchema.safeParse({ pinned: true }).success).toBe(true);
expect(collabLinkUpdateRequestSchema.safeParse({ url: 'https://example.com/x' }).success).toBe(true);
expect(collabLinkUpdateRequestSchema.safeParse({ url: 'javascript:alert(1)' }).success).toBe(false);
});
});
describe('collabMessageCreateRequestSchema', () => {
it('caps text at 5000, allows empty text for image-only messages, and accepts multipart reply_to strings', () => {
expect(collabMessageCreateRequestSchema.safeParse({ text: 'hi', reply_to: null }).success).toBe(true);
expect(collabMessageCreateRequestSchema.safeParse({ text: 'hi', reply_to: 4 }).success).toBe(true);
expect(collabMessageCreateRequestSchema.safeParse({ text: '', reply_to: '4' }).success).toBe(true);
expect(collabMessageCreateRequestSchema.safeParse({}).success).toBe(true);
expect(collabMessageCreateRequestSchema.safeParse({ text: 'x'.repeat(5001) }).success).toBe(false);
});
});
describe('collabReactionRequestSchema', () => {
it('requires a non-empty emoji', () => {
expect(collabReactionRequestSchema.safeParse({ emoji: '👍' }).success).toBe(true);
expect(collabReactionRequestSchema.safeParse({ emoji: '' }).success).toBe(false);
});
});
describe('collabMessageSchema', () => {
const message = {
id: 1,
trip_id: 2,
user_id: 3,
text: 'See you at the station',
reply_to: null,
created_at: '2026-10-21 08:00:00',
deleted: null,
username: 'ana',
avatar: null,
reply_text: null,
reply_username: null,
user_avatar: null,
avatar_url: null,
reactions: [],
attachments: [],
};
it('reads a message without an origin, with none and with one shared from the assistant', () => {
expect(collabMessageSchema.safeParse(message).success).toBe(true);
expect(collabMessageSchema.safeParse({ ...message, origin: null }).success).toBe(true);
expect(collabMessageSchema.parse({ ...message, origin: COLLAB_MESSAGE_ORIGIN_ASSISTANT }).origin).toBe('assistant');
});
it('takes an origin it does not know yet, but only as text', () => {
expect(collabMessageSchema.safeParse({ ...message, origin: 'plugin' }).success).toBe(true);
expect(collabMessageSchema.safeParse({ ...message, origin: 1 }).success).toBe(false);
});
});