mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-22 06:41:46 +00:00
chore: apply prettier on the entire project
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
/**
|
||||
* Unit tests for MCP addon gating and scope enforcement in tools.
|
||||
*/
|
||||
import { runMigrations } from '../../../src/db/migrations';
|
||||
import { createTables } from '../../../src/db/schema';
|
||||
import { createUser, createTrip } from '../../helpers/factories';
|
||||
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
|
||||
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
|
||||
const { testDb, dbMock } = vi.hoisted(() => {
|
||||
@@ -15,7 +21,11 @@ const { testDb, dbMock } = vi.hoisted(() => {
|
||||
reinitialize: () => {},
|
||||
getPlaceWithTags: () => null,
|
||||
canAccessTrip: (tripId: any, userId: number) =>
|
||||
db.prepare(`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`).get(userId, tripId, userId),
|
||||
db
|
||||
.prepare(
|
||||
`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`,
|
||||
)
|
||||
.get(userId, tripId, userId),
|
||||
isOwner: (tripId: any, userId: number) =>
|
||||
!!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId),
|
||||
};
|
||||
@@ -41,12 +51,6 @@ vi.mock('../../../src/services/adminService', () => ({
|
||||
getCollabFeatures: vi.fn().mockReturnValue({ chat: true, notes: true, polls: true, whatsnext: true }),
|
||||
}));
|
||||
|
||||
import { createTables } from '../../../src/db/schema';
|
||||
import { runMigrations } from '../../../src/db/migrations';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
import { createUser, createTrip } from '../../helpers/factories';
|
||||
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
|
||||
|
||||
beforeAll(() => {
|
||||
createTables(testDb);
|
||||
runMigrations(testDb);
|
||||
@@ -62,13 +66,13 @@ afterAll(() => {
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
async function withHarness(
|
||||
userId: number,
|
||||
fn: (h: McpHarness) => Promise<void>,
|
||||
scopes?: string[] | null
|
||||
) {
|
||||
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>, scopes?: string[] | null) {
|
||||
const h = await createMcpHarness({ userId, withResources: false, scopes: scopes ?? null });
|
||||
try { await fn(h); } finally { await h.cleanup(); }
|
||||
try {
|
||||
await fn(h);
|
||||
} finally {
|
||||
await h.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -153,7 +157,10 @@ describe('Budget tools — addon gating', () => {
|
||||
isAddonEnabledMock.mockImplementation((id: string) => id !== 'budget');
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_budget_item', arguments: { tripId: 1, name: 'Test', total_price: 100 } });
|
||||
const result = await h.client.callTool({
|
||||
name: 'create_budget_item',
|
||||
arguments: { tripId: 1, name: 'Test', total_price: 100 },
|
||||
});
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -170,7 +177,10 @@ describe('Packing tools — addon gating', () => {
|
||||
isAddonEnabledMock.mockImplementation((id: string) => id !== 'packing');
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_packing_item', arguments: { tripId: 1, name: 'Sunscreen' } });
|
||||
const result = await h.client.callTool({
|
||||
name: 'create_packing_item',
|
||||
arguments: { tripId: 1, name: 'Sunscreen' },
|
||||
});
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -187,7 +197,10 @@ describe('Collab tools — addon gating', () => {
|
||||
isAddonEnabledMock.mockImplementation((id: string) => id !== 'collab');
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_collab_note', arguments: { tripId: 1, title: 'Test Note' } });
|
||||
const result = await h.client.callTool({
|
||||
name: 'create_collab_note',
|
||||
arguments: { tripId: 1, title: 'Test Note' },
|
||||
});
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -229,51 +242,74 @@ describe('Scope enforcement in tools', () => {
|
||||
it('with scopes trips:read, create_trip is not registered (write not in scopes)', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Should Fail' } });
|
||||
expect(result.isError).toBe(true);
|
||||
}, ['trips:read']);
|
||||
await withHarness(
|
||||
user.id,
|
||||
async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Should Fail' } });
|
||||
expect(result.isError).toBe(true);
|
||||
},
|
||||
['trips:read'],
|
||||
);
|
||||
});
|
||||
|
||||
it('with scopes trips:write, create_trip is registered and works', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'My Trip' } });
|
||||
expect(result.isError).toBeFalsy();
|
||||
const data = parseToolResult(result) as any;
|
||||
expect(data.trip.title).toBe('My Trip');
|
||||
}, ['trips:write']);
|
||||
await withHarness(
|
||||
user.id,
|
||||
async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'My Trip' } });
|
||||
expect(result.isError).toBeFalsy();
|
||||
const data = parseToolResult(result) as any;
|
||||
expect(data.trip.title).toBe('My Trip');
|
||||
},
|
||||
['trips:write'],
|
||||
);
|
||||
});
|
||||
|
||||
it('with scopes null (full access), create_trip is registered', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Full Access Trip' } });
|
||||
expect(result.isError).toBeFalsy();
|
||||
}, null);
|
||||
await withHarness(
|
||||
user.id,
|
||||
async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Full Access Trip' } });
|
||||
expect(result.isError).toBeFalsy();
|
||||
},
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
it('with scopes trips:read, create_budget_item is not registered (budget:write not in scopes)', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({ name: 'create_budget_item', arguments: { tripId: 1, name: 'Hotel', total_price: 200 } });
|
||||
expect(result.isError).toBe(true);
|
||||
}, ['trips:read']);
|
||||
await withHarness(
|
||||
user.id,
|
||||
async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'create_budget_item',
|
||||
arguments: { tripId: 1, name: 'Hotel', total_price: 200 },
|
||||
});
|
||||
expect(result.isError).toBe(true);
|
||||
},
|
||||
['trips:read'],
|
||||
);
|
||||
});
|
||||
|
||||
it('with scopes budget:write and trips:read, create_budget_item is registered (budget addon enabled)', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: 'Budget Trip' });
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'create_budget_item',
|
||||
arguments: { tripId: trip.id, name: 'Hotel', total_price: 200 },
|
||||
});
|
||||
expect(result.isError).toBeFalsy();
|
||||
}, ['budget:write', 'trips:read']);
|
||||
await withHarness(
|
||||
user.id,
|
||||
async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'create_budget_item',
|
||||
arguments: { tripId: trip.id, name: 'Hotel', total_price: 200 },
|
||||
});
|
||||
expect(result.isError).toBeFalsy();
|
||||
},
|
||||
['budget:write', 'trips:read'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user