File size: 11,120 Bytes
1f5ea39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
 * Unit tests for MCP addon gating and scope enforcement in tools.
 */
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';

const { testDb, dbMock } = vi.hoisted(() => {
  const Database = require('better-sqlite3');
  const db = new Database(':memory:');
  db.exec('PRAGMA journal_mode = WAL');
  db.exec('PRAGMA foreign_keys = ON');
  db.exec('PRAGMA busy_timeout = 5000');
  const mock = {
    db,
    closeDb: () => {},
    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),
    isOwner: (tripId: any, userId: number) =>
      !!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId),
  };
  return { testDb: db, dbMock: mock };
});

vi.mock('../../../src/db/database', () => dbMock);
vi.mock('../../../src/config', () => ({
  JWT_SECRET: 'test-jwt-secret-for-trek-testing-only',
  ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
  updateJwtSecret: () => {},
}));

const { broadcastMock } = vi.hoisted(() => ({ broadcastMock: vi.fn() }));
vi.mock('../../../src/websocket', () => ({ broadcast: broadcastMock }));

const { isAddonEnabledMock } = vi.hoisted(() => {
  const isAddonEnabledMock = vi.fn().mockReturnValue(true);
  return { isAddonEnabledMock };
});
vi.mock('../../../src/services/adminService', () => ({
  isAddonEnabled: isAddonEnabledMock,
  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);
});

beforeEach(() => {
  resetTestDb(testDb);
  broadcastMock.mockClear();
  isAddonEnabledMock.mockReturnValue(true);
});

afterAll(() => {
  testDb.close();
});

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(); }
}

// ---------------------------------------------------------------------------
// get_trip_summary β€” addon gating
// ---------------------------------------------------------------------------

describe('get_trip_summary β€” addon gating', () => {
  it('when all addons enabled: packing, budget, collab_notes, todos are present', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'Full Trip' });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      expect(result.isError).toBeFalsy();
      const data = parseToolResult(result) as any;
      expect(data.packing).toBeDefined();
      expect(data.budget).toBeDefined();
      expect(Array.isArray(data.collab_notes)).toBe(true);
      expect(Array.isArray(data.todos)).toBe(true);
    });
  });

  it('when budget disabled: budget is undefined in response', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'No Budget Trip' });

    isAddonEnabledMock.mockImplementation((id: string) => id !== 'budget');

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      expect(result.isError).toBeFalsy();
      const data = parseToolResult(result) as any;
      expect(data.budget).toBeUndefined();
      // packing and collab still present
      expect(data.packing).toBeDefined();
      expect(Array.isArray(data.collab_notes)).toBe(true);
    });
  });

  it('when packing disabled: packing is undefined and todos is empty array', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'No Packing Trip' });

    isAddonEnabledMock.mockImplementation((id: string) => id !== 'packing');

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      expect(result.isError).toBeFalsy();
      const data = parseToolResult(result) as any;
      expect(data.packing).toBeUndefined();
      expect(Array.isArray(data.todos)).toBe(true);
      expect(data.todos).toHaveLength(0);
    });
  });

  it('when collab disabled: collab_notes is empty array, pollCount is 0, messageCount is 0', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'No Collab Trip' });

    isAddonEnabledMock.mockImplementation((id: string) => id !== 'collab');

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      expect(result.isError).toBeFalsy();
      const data = parseToolResult(result) as any;
      expect(Array.isArray(data.collab_notes)).toBe(true);
      expect(data.collab_notes).toHaveLength(0);
      expect(data.pollCount).toBe(0);
      expect(data.messageCount).toBe(0);
    });
  });
});

// ---------------------------------------------------------------------------
// Budget tools β€” addon gating
// ---------------------------------------------------------------------------

describe('Budget tools β€” addon gating', () => {
  it('when budget addon disabled, create_budget_item is not registered', async () => {
    const { user } = createUser(testDb);

    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 } });
      expect(result.isError).toBe(true);
    });
  });
});

// ---------------------------------------------------------------------------
// Packing tools β€” addon gating
// ---------------------------------------------------------------------------

describe('Packing tools β€” addon gating', () => {
  it('when packing addon disabled, create_packing_item is not registered', async () => {
    const { user } = createUser(testDb);

    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' } });
      expect(result.isError).toBe(true);
    });
  });
});

// ---------------------------------------------------------------------------
// Collab tools β€” addon gating
// ---------------------------------------------------------------------------

describe('Collab tools β€” addon gating', () => {
  it('when collab addon disabled, create_collab_note is not registered', async () => {
    const { user } = createUser(testDb);

    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' } });
      expect(result.isError).toBe(true);
    });
  });
});

// ---------------------------------------------------------------------------
// Atlas tools β€” addon gating
// ---------------------------------------------------------------------------

describe('Atlas tools β€” addon gating', () => {
  it('when atlas addon disabled, mark_country_visited is not registered', async () => {
    const { user } = createUser(testDb);

    isAddonEnabledMock.mockImplementation((id: string) => id !== 'atlas');

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'mark_country_visited', arguments: { country_code: 'FR' } });
      expect(result.isError).toBe(true);
    });
  });

  it('when atlas addon disabled, create_bucket_list_item is not registered', async () => {
    const { user } = createUser(testDb);

    isAddonEnabledMock.mockImplementation((id: string) => id !== 'atlas');

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'create_bucket_list_item', arguments: { name: 'Paris' } });
      expect(result.isError).toBe(true);
    });
  });
});

// ---------------------------------------------------------------------------
// Scope enforcement in tools
// ---------------------------------------------------------------------------

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']);
  });

  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']);
  });

  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);
  });

  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']);
  });

  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']);
  });
});