mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
b64da26651
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.
141 lines
6.5 KiB
TypeScript
141 lines
6.5 KiB
TypeScript
/**
|
|
* Sharing an assistant answer in the trip chat, over the real thread and
|
|
* message repositories on a migrated test db: the chat must be on, the thread
|
|
* the user's on this trip, the message an answer in it; then it is posted as
|
|
* the user's own message with the assistant's origin, broadcast and notified
|
|
* like a typed one.
|
|
*
|
|
* Replaced: the addon switches and the collab chat itself.
|
|
*/
|
|
import { AssistantMessages } from '../../../../src/db/entities/AssistantMessages.entity';
|
|
import { AssistantThreads } from '../../../../src/db/entities/AssistantThreads.entity';
|
|
import { AddonsService } from '../../../../src/nest/addons/addons.service';
|
|
import { AssistantShareService, messageRowOf } from '../../../../src/nest/assistant/assistant-share.service';
|
|
import { CollabService } from '../../../../src/nest/collab/collab.service';
|
|
import { DomainError } from '../../../../src/nest/common/domain-error';
|
|
import type { User } from '../../../../src/types';
|
|
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
|
import { createRow } from '../../../helpers/factories/rows';
|
|
import { makeTrip } from '../../../helpers/factories/trips';
|
|
import { makeUser } from '../../../helpers/factories/users';
|
|
import { resetTestDb } from '../../../helpers/test-db';
|
|
import { createTestModule, type TestModule } from '../../../helpers/test-module';
|
|
import { MikroORM } from '@mikro-orm/core';
|
|
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const db = createSnapshotTestDb();
|
|
let t: TestModule;
|
|
|
|
const addons = {
|
|
isAddonEnabled: vi.fn(async () => true),
|
|
getCollabFeatures: vi.fn(async () => ({ chat: true, notes: true, polls: true })),
|
|
};
|
|
const collab = {
|
|
createMessage: vi.fn(async (_trip: number, userId: number, text: string) => ({
|
|
message: { id: 90, user_id: userId, text: text.trim(), origin: 'assistant' },
|
|
})),
|
|
broadcast: vi.fn(),
|
|
notifyCollab: vi.fn(async () => undefined),
|
|
};
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({
|
|
db,
|
|
imports: [MikroOrmModule.forFeature([AssistantThreads, AssistantMessages])],
|
|
providers: [
|
|
AssistantShareService,
|
|
{ provide: AddonsService, useValue: addons },
|
|
{ provide: CollabService, useValue: collab },
|
|
],
|
|
});
|
|
});
|
|
|
|
beforeEach(() => {
|
|
resetTestDb(db);
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await t.close();
|
|
db.close();
|
|
});
|
|
|
|
/** A user with a trip and a thread on it holding a question and its answer. */
|
|
async function conversation() {
|
|
const orm = t.get(MikroORM);
|
|
const { user } = await makeUser(orm);
|
|
const trip = await makeTrip(orm, user.id);
|
|
const thread = await createRow(orm, AssistantThreads, { user: user.id, trip: trip.id, title: 'June' });
|
|
const message = (role: string) =>
|
|
createRow(orm, AssistantMessages, { thread: thread.id, role, parts: '[]', status: 'complete' });
|
|
const question = await message('user');
|
|
const answer = await message('assistant');
|
|
return { user: user as unknown as User, tripId: trip.id, threadId: thread.id, question, answer };
|
|
}
|
|
|
|
/** Shares "Hi" from the message as the user, without a socket. */
|
|
function shareAs(user: User, tripId: number, threadId: number, messageId: string) {
|
|
return t.get(AssistantShareService).share(user, tripId, threadId, messageId, 'Hi', undefined);
|
|
}
|
|
|
|
describe('AssistantShareService', () => {
|
|
it('ASTSHR-001: posts the answer as the user, marked as from the assistant, broadcast and notified', async () => {
|
|
const c = await conversation();
|
|
const service = t.get(AssistantShareService);
|
|
const shared = await service.share(c.user, c.tripId, c.threadId, `msg-${c.answer.id}`, 'x'.repeat(100), 'sock-1');
|
|
expect(collab.createMessage).toHaveBeenCalledWith(c.tripId, c.user.id, 'x'.repeat(100), null, [], 'assistant');
|
|
expect(shared).toEqual({ message: expect.objectContaining({ id: 90, origin: 'assistant' }) });
|
|
expect(collab.broadcast).toHaveBeenCalledWith(
|
|
String(c.tripId),
|
|
'collab:message:created',
|
|
{ message: shared.message },
|
|
'sock-1',
|
|
);
|
|
expect(collab.notifyCollab).toHaveBeenCalledWith(String(c.tripId), c.user, `${'x'.repeat(80)}...`);
|
|
});
|
|
|
|
it('ASTSHR-002: a short answer is previewed whole', async () => {
|
|
const c = await conversation();
|
|
await shareAs(c.user, c.tripId, c.threadId, `msg-${c.answer.id}`);
|
|
expect(collab.notifyCollab).toHaveBeenCalledWith(String(c.tripId), c.user, 'Hi');
|
|
});
|
|
|
|
it('ASTSHR-003: refused while the collab addon or its chat is off', async () => {
|
|
const c = await conversation();
|
|
const share = () => shareAs(c.user, c.tripId, c.threadId, `msg-${c.answer.id}`);
|
|
addons.isAddonEnabled.mockResolvedValueOnce(false);
|
|
await expect(share()).rejects.toMatchObject({ status: 403, message: 'Collab chat is off' });
|
|
addons.getCollabFeatures.mockResolvedValueOnce({ chat: false, notes: true, polls: true });
|
|
await expect(share()).rejects.toMatchObject({ status: 403, code: 'forbidden' });
|
|
expect(collab.createMessage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('ASTSHR-004: a thread of another user or trip, a question, an answer elsewhere or no id is not found', async () => {
|
|
const c = await conversation();
|
|
const other = await conversation();
|
|
const notFound = { status: 404, code: 'not_found' };
|
|
await expect(shareAs(other.user, c.tripId, c.threadId, `msg-${c.answer.id}`)).rejects.toMatchObject(notFound);
|
|
await expect(shareAs(c.user, other.tripId, c.threadId, `msg-${c.answer.id}`)).rejects.toMatchObject(notFound);
|
|
await expect(shareAs(c.user, c.tripId, c.threadId, `msg-${c.question.id}`)).rejects.toMatchObject(notFound);
|
|
await expect(shareAs(c.user, c.tripId, c.threadId, `msg-${other.answer.id}`)).rejects.toMatchObject(notFound);
|
|
await expect(shareAs(c.user, c.tripId, c.threadId, 'msg-x')).rejects.toBeInstanceOf(DomainError);
|
|
expect(collab.createMessage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('ASTSHR-005: a message the chat could not post is a refusal, never a silent success', async () => {
|
|
const c = await conversation();
|
|
collab.createMessage.mockResolvedValueOnce({ message: undefined } as never);
|
|
await expect(shareAs(c.user, c.tripId, c.threadId, `msg-${c.answer.id}`)).rejects.toMatchObject({ status: 400 });
|
|
expect(collab.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('ASTSHR-006: reads a wire message id as its row', () => {
|
|
expect(messageRowOf('msg-12')).toBe(12);
|
|
for (const bad of ['msg-0', 'msg-', '12', 'msg-1.5', 'msg-01', 'msg-12345678901234567']) {
|
|
expect(messageRowOf(bad), bad).toBeNull();
|
|
}
|
|
});
|
|
});
|