Files
TREK/server/tests/unit/mcp/tools-atlas-globe.test.ts
T
Maurice 04d134755a feat(atlas): flight routes, travellers and badges on the server
- GET /api/addons/atlas/flights folds the user's flight bookings into
  routes, airports and totals for the globe's arcs (#1369, #2642).
- GET /api/addons/atlas/people answers the Atlas side of other travellers
  on the instance: counts, visited countries and earned badges, never
  trips, places or dates. A user hides themselves with the atlas_hidden
  setting; the admin switch atlas_compare_enabled turns it off for all.
- shared/src/atlas/atlas-badges.ts holds the badge rules, computed from the
  data a user already has, so nothing is stored.
- MCP: list_atlas_flight_routes, list_atlas_badges, list_atlas_travellers.
2026-11-28 22:35:11 +01:00

83 lines
3.2 KiB
TypeScript

/**
* Unit tests for the globe Atlas MCP tools (atlas-globe.mcp.ts):
* list_atlas_flight_routes, list_atlas_badges and list_atlas_travellers, through
* a real MCP session on the booted container. They answer as the REST routes
* do, the admin switch for the travellers included.
*/
import { setAppSetting, setUserSetting } from '../../helpers/factories/settings';
import { makeUser } from '../../helpers/factories/users';
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
import { resetDatabase } from '../../helpers/reset-database';
import { bootTestApp } from '../../helpers/test-app';
import { MikroORM } from '@mikro-orm/core';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
let orm: MikroORM;
beforeAll(async () => {
orm = (await bootTestApp()).get(MikroORM);
});
beforeEach(async () => {
await resetDatabase(orm);
delete process.env.DEMO_MODE;
});
afterAll(async () => {
await (await bootTestApp()).close();
});
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
const h = await createMcpHarness({ userId, withResources: false });
try {
await fn(h);
} finally {
await h.cleanup();
}
}
async function call(h: McpHarness, name: string, args: Record<string, unknown> = {}) {
return h.client.callTool({ name, arguments: args });
}
describe('flights, badges and travellers', () => {
it('ATGLOBE-MCP-010: list_atlas_flight_routes answers the empty flight map of a user without flights', async () => {
const { user } = await makeUser(orm);
await withHarness(user.id, async (h) => {
expect(parseToolResult(await call(h, 'list_atlas_flight_routes'))).toEqual({
routes: [],
airports: [],
totals: { flights: 0, km: 0, airports: 0, longestKm: 0 },
});
});
});
it('ATGLOBE-MCP-011: list_atlas_badges answers every badge with its progress', async () => {
const { user } = await makeUser(orm);
await withHarness(user.id, async (h) => {
const { badges } = parseToolResult(await call(h, 'list_atlas_badges')) as {
badges: Array<{ id: string; earned: boolean; current: number }>;
};
expect(badges.find((b) => b.id === 'countries-5')).toMatchObject({ earned: false, current: 0 });
});
});
it('ATGLOBE-MCP-012: list_atlas_travellers leaves out the hidden, and everybody while the admin switch is off', async () => {
const { user } = await makeUser(orm);
const { user: shown } = await makeUser(orm);
const { user: hiddenUser } = await makeUser(orm);
await setUserSetting(orm, hiddenUser.id, 'atlas_hidden', 'true');
await withHarness(user.id, async (h) => {
const on = parseToolResult(await call(h, 'list_atlas_travellers', { ids: [shown.id, hiddenUser.id] })) as {
people: Array<{ id: number }>;
};
expect(on.people.map((p) => p.id)).toEqual([shown.id]);
await setAppSetting(orm, 'atlas_compare_enabled', 'false');
expect(parseToolResult(await call(h, 'list_atlas_travellers', { ids: [shown.id] }))).toEqual({ people: [] });
const empty = await call(h, 'list_atlas_travellers', { ids: [] });
expect(empty.isError).toBe(true);
});
});
});