Files
TREK/server/tests/unit/nest/claude-connector.controller.test.ts
T
Maurice 214984f398 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-21 22:29:56 +02:00

68 lines
3.2 KiB
TypeScript

import { ADDON_IDS } from '../../../src/addons';
import { AddonGuard } from '../../../src/nest/addons/addon.guard';
import { REQUIRE_ADDON } from '../../../src/nest/addons/require-addon.decorator';
import { JwtAuthGuard } from '../../../src/nest/auth-core/jwt-auth.guard';
import { RESPONSE_CONTRACT_KEY } from '../../../src/nest/common/response-contract';
import { ClaudeConnectorController } from '../../../src/nest/oauth/claude-connector.controller';
import { ClaudeConnectorService } from '../../../src/nest/oauth/claude-connector.service';
import { OauthService } from '../../../src/nest/oauth/oauth.service';
import { GUARDS_METADATA } from '@nestjs/common/constants';
import { Test } from '@nestjs/testing';
import { aiClaudeConnectorResponseSchema } from '@trek/shared';
import { describe, expect, it, vi } from 'vitest';
async function service(mcpSafeUrl: string): Promise<ClaudeConnectorService> {
const moduleRef = await Test.createTestingModule({
providers: [ClaudeConnectorService, { provide: OauthService, useValue: { mcpSafeUrl: () => mcpSafeUrl } }],
}).compile();
return moduleRef.get(ClaudeConnectorService);
}
describe('ClaudeConnectorService', () => {
it('CLAUDE-001: links claude.ai to the MCP endpoint of this instance and encodes it once', async () => {
const res = await (await service('https://trek.example.test')).describe();
expect(res.mcpUrl).toBe('https://trek.example.test/mcp');
expect(res.deepLink).toBe(
'https://claude.ai/customize/connectors?modal=add-custom-connector&connectorName=TREK' +
'&connectorUrl=https%3A%2F%2Ftrek.example.test%2Fmcp',
);
expect(aiClaudeConnectorResponseSchema.parse(res)).toEqual(res);
});
it('CLAUDE-002: trailing slashes on the configured URL do not double the path', async () => {
expect((await (await service('https://trek.example.test/sub///')).describe()).mcpUrl).toBe(
'https://trek.example.test/sub/mcp',
);
});
it('CLAUDE-003: the QR code comes as an SVG document', async () => {
const { qrSvg } = await (await service('https://trek.example.test')).describe();
expect(qrSvg.startsWith('<svg')).toBe(true);
expect(qrSvg.trimEnd().endsWith('</svg>')).toBe(true);
});
});
describe('ClaudeConnectorController', () => {
it('CLAUDE-004: answers what the service describes', async () => {
const answer = { mcpUrl: 'https://t.example/mcp', deepLink: 'https://claude.ai/x', qrSvg: '<svg/>' };
const describeConnector = vi.fn().mockResolvedValue(answer);
const controller = new ClaudeConnectorController({
describe: describeConnector,
} as unknown as ClaudeConnectorService);
expect(await controller.get()).toBe(answer);
expect(describeConnector).toHaveBeenCalledOnce();
});
it('CLAUDE-005: follows the MCP addon, checks it before the session, and declares its contract', () => {
expect(Reflect.getMetadata(GUARDS_METADATA, ClaudeConnectorController)).toEqual([AddonGuard, JwtAuthGuard]);
expect(Reflect.getMetadata(REQUIRE_ADDON, ClaudeConnectorController)).toEqual({
addonId: ADDON_IDS.MCP,
label: 'MCP',
});
expect(Reflect.getMetadata(RESPONSE_CONTRACT_KEY, ClaudeConnectorController.prototype.get)).toBe(
aiClaudeConnectorResponseSchema,
);
});
});