Files
TREK/server/tests/unit/mcp/tools-journey-social.test.ts
T
Maurice 6ac15213d9 feat(mcp): add_journey_comment, the MCP side of commenting on an entry
The REST route let everyone on a journey comment, but MCP could only
list, hide and delete. The tool runs through the same service and
checks. The in-app assistant is not offered it, since a comment speaks
in the user's name to the journey's people and the link's readers.
2026-11-30 16:56:15 +01:00

200 lines
8.6 KiB
TypeScript

/**
* MCP tools of a journey's social side (#1702, #1669): the comments for the
* people on the journey, the followers of its link for its owner, and the two
* new switches of create_journey_share_link, each refused where the REST
* route refuses.
*/
import { ADDON_IDS } from '../../../src/addons';
import { JourneyEntryComments } from '../../../src/db/entities/JourneyEntryComments.entity';
import { JourneyShareSubscribers } from '../../../src/db/entities/JourneyShareSubscribers.entity';
import {
makeJourneyComment,
makeJourneyShareLink,
makeJourneySubscriber,
} from '../../helpers/factories/journey-social';
import { addJourneyContributor, makeJourney, makeJourneyEntry } from '../../helpers/factories/journeys';
import { countRows, findRow } from '../../helpers/factories/rows';
import { setAddonEnabled } from '../../helpers/factories/settings';
import { makeUser } from '../../helpers/factories/users';
import { FakeRealtimeService } from '../../helpers/fake-realtime';
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';
const realtime = new FakeRealtimeService();
let orm: MikroORM;
beforeAll(async () => {
orm = (await bootTestApp(realtime)).get(MikroORM);
});
beforeEach(async () => {
await resetDatabase(orm);
await setAddonEnabled(orm, ADDON_IDS.JOURNEY, true);
realtime.broadcastToUserMock.mockClear();
});
afterAll(async () => {
await (await bootTestApp(realtime)).close();
});
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
const h = await createMcpHarness({ realtime, userId, withResources: false });
try {
await fn(h);
} finally {
await h.cleanup();
}
}
async function journeyWithComment() {
const { user: owner } = await makeUser(orm);
const { user: viewer } = await makeUser(orm);
const journey = await makeJourney(orm, owner.id);
await addJourneyContributor(orm, journey.id, viewer.id, 'viewer');
const entry = await makeJourneyEntry(orm, journey.id, owner.id);
const comment = await makeJourneyComment(orm, { id: entry.id, journey_id: journey.id }, viewer.id, {
body: 'Lovely light',
});
return { owner, viewer, journey, entry, comment };
}
describe('Tools: journey comments', () => {
it('JSOCIAL-MCP-001: lists the comments, hidden ones only for an editor', async () => {
const { owner, viewer, journey, comment } = await journeyWithComment();
await withHarness(owner.id, async (h) => {
const hide = await h.client.callTool({
name: 'set_journey_comment_hidden',
arguments: { journeyId: journey.id, commentId: comment.id, hidden: true },
});
expect(hide.isError).toBeFalsy();
const listed = parseToolResult(
await h.client.callTool({ name: 'list_journey_comments', arguments: { journeyId: journey.id } }),
) as { comments: { id: number; hidden: boolean }[]; can_moderate: boolean };
expect(listed.can_moderate).toBe(true);
expect(listed.comments).toEqual([expect.objectContaining({ id: comment.id, hidden: true })]);
});
expect(realtime.broadcastToUserMock).toHaveBeenCalledWith(
owner.id,
expect.objectContaining({ type: 'journey:entry:commented', commentId: comment.id, change: 'hidden' }),
undefined,
);
await withHarness(viewer.id, async (h) => {
const listed = parseToolResult(
await h.client.callTool({ name: 'list_journey_comments', arguments: { journeyId: journey.id } }),
) as { comments: unknown[]; can_moderate: boolean };
expect(listed).toEqual({ comments: [], can_moderate: false });
});
});
it('JSOCIAL-MCP-003: a viewer comments like the REST route lets them; a stranger and a foreign entry are refused', async () => {
const { viewer, journey, entry } = await journeyWithComment();
const { user: stranger } = await makeUser(orm);
const other = await makeJourney(orm, stranger.id);
const foreignEntry = await makeJourneyEntry(orm, other.id, stranger.id);
await withHarness(viewer.id, async (h) => {
const added = parseToolResult(
await h.client.callTool({
name: 'add_journey_comment',
arguments: { journeyId: journey.id, entryId: entry.id, body: ' Wish I was there ' },
}),
) as { comment: { body: string; own: boolean; user_id: number } };
expect(added.comment).toMatchObject({ body: 'Wish I was there', own: true, user_id: viewer.id });
const foreign = await h.client.callTool({
name: 'add_journey_comment',
arguments: { journeyId: journey.id, entryId: foreignEntry.id, body: 'Nope' },
});
expect(foreign.isError).toBe(true);
const empty = await h.client.callTool({
name: 'add_journey_comment',
arguments: { journeyId: journey.id, entryId: entry.id, body: ' ' },
});
expect(empty.isError).toBe(true);
});
await withHarness(stranger.id, async (h) => {
const refused = await h.client.callTool({
name: 'add_journey_comment',
arguments: { journeyId: journey.id, entryId: entry.id, body: 'Hi' },
});
expect(refused.isError).toBe(true);
});
expect(await countRows(orm, JourneyEntryComments, { journey: journey.id })).toBe(2);
});
it('JSOCIAL-MCP-002: a viewer may not hide, but deletes their own; a stranger reaches nothing', async () => {
const { viewer, journey, comment } = await journeyWithComment();
const { user: stranger } = await makeUser(orm);
await withHarness(viewer.id, async (h) => {
const hide = await h.client.callTool({
name: 'set_journey_comment_hidden',
arguments: { journeyId: journey.id, commentId: comment.id, hidden: true },
});
expect(hide.isError).toBe(true);
const del = await h.client.callTool({
name: 'delete_journey_comment',
arguments: { journeyId: journey.id, commentId: comment.id },
});
expect(del.isError).toBeFalsy();
});
expect(await countRows(orm, JourneyEntryComments, { id: comment.id })).toBe(0);
await withHarness(stranger.id, async (h) => {
const listed = await h.client.callTool({ name: 'list_journey_comments', arguments: { journeyId: journey.id } });
expect(listed.isError).toBe(true);
});
});
});
describe('Tools: journey followers', () => {
it('JSOCIAL-MCP-010: the owner lists and removes followers; nobody else does', async () => {
const { owner, viewer, journey } = await journeyWithComment();
const link = await makeJourneyShareLink(orm, journey.id, owner.id, { allow_subscriptions: 1 });
const confirmed = await makeJourneySubscriber(orm, link.id, { confirmed_at: '2026-11-30 10:00:00' });
await makeJourneySubscriber(orm, link.id);
await withHarness(viewer.id, async (h) => {
const listed = await h.client.callTool({
name: 'list_journey_subscribers',
arguments: { journeyId: journey.id },
});
expect(listed.isError).toBe(true);
});
await withHarness(owner.id, async (h) => {
const listed = parseToolResult(
await h.client.callTool({ name: 'list_journey_subscribers', arguments: { journeyId: journey.id } }),
) as { count: number; pending: number; subscribers: { id: number }[] };
expect(listed.count).toBe(1);
expect(listed.pending).toBe(1);
const removed = await h.client.callTool({
name: 'remove_journey_subscriber',
arguments: { journeyId: journey.id, subscriberId: confirmed.id },
});
expect(removed.isError).toBeFalsy();
});
expect(await findRow(orm, JourneyShareSubscribers, { id: confirmed.id })).toBeNull();
});
it('JSOCIAL-MCP-011: create_journey_share_link takes both switches like the REST route', async () => {
const { owner, journey } = await journeyWithComment();
await withHarness(owner.id, async (h) => {
await h.client.callTool({
name: 'create_journey_share_link',
arguments: { journeyId: journey.id, allow_comments: true, allow_subscriptions: true },
});
const read = parseToolResult(
await h.client.callTool({ name: 'get_journey_share_link', arguments: { journeyId: journey.id } }),
) as { shareLink: { allow_comments: boolean; allow_subscriptions: boolean } };
expect(read.shareLink).toMatchObject({ allow_comments: true, allow_subscriptions: true });
const refused = await h.client.callTool({
name: 'create_journey_share_link',
arguments: { journeyId: journey.id, allow_comments: 'yes' },
});
expect(refused.isError).toBe(true);
});
});
});