|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import type { BaseServerTool, ServerToolContext } from '@/lib/mothership/tools/server/base-tool' |
| 3 | +import { changelogServerTool } from '@/lib/mothership/tools/server/changelog' |
| 4 | +import { dashboardsServerTool } from '@/lib/mothership/tools/server/dashboards' |
| 5 | +import { ffmpegServerTool } from '@/lib/mothership/tools/server/media/ffmpeg' |
| 6 | +import { routeExecution } from '@/lib/mothership/tools/server/router' |
| 7 | +import { organizationSearchSourcesServerTool } from '@/lib/mothership/tools/server/search-sources' |
| 8 | +import { settingsServerTool } from '@/lib/mothership/tools/server/settings' |
| 9 | +import { testsServerTool } from '@/lib/mothership/tools/server/tests' |
| 10 | +import { workspacesServerTool } from '@/lib/mothership/tools/server/workspaces' |
| 11 | + |
| 12 | +const tools: BaseServerTool[] = [ |
| 13 | + settingsServerTool, |
| 14 | + dashboardsServerTool, |
| 15 | + changelogServerTool, |
| 16 | + testsServerTool, |
| 17 | + workspacesServerTool, |
| 18 | + organizationSearchSourcesServerTool, |
| 19 | + ffmpegServerTool, |
| 20 | +] |
| 21 | + |
| 22 | +/** The shape an unattributed inbox message runs with: the owner as principal, capped to read. */ |
| 23 | +const cappedWorkspaceRun: ServerToolContext = { |
| 24 | + userId: 'owner', |
| 25 | + workspaceId: 'workspace', |
| 26 | + chatId: 'chat', |
| 27 | + toolCallId: 'call', |
| 28 | + copilotToolExecution: true, |
| 29 | + requestMode: 'agent', |
| 30 | + userPermission: 'read', |
| 31 | +} |
| 32 | + |
| 33 | +const writes: Array<[string, Record<string, unknown>]> = [ |
| 34 | + [ |
| 35 | + 'settings', |
| 36 | + { |
| 37 | + action: 'execute', |
| 38 | + scope: 'workspace', |
| 39 | + section: 'teammates', |
| 40 | + operation: 'invite', |
| 41 | + input: { emails: ['outsider@example.com'], permission: 'admin' }, |
| 42 | + }, |
| 43 | + ], |
| 44 | + ['settings', { action: 'update', scope: 'workspace', section: 'general', changes: {} }], |
| 45 | + ['dashboards', { action: 'set', content: '# Revenue' }], |
| 46 | + [ |
| 47 | + 'changelog', |
| 48 | + { |
| 49 | + action: 'publish', |
| 50 | + title: 'Release', |
| 51 | + body: 'Notes', |
| 52 | + bump: 'patch', |
| 53 | + bumpReason: 'Fixes', |
| 54 | + changes: [{ text: 'Fix' }], |
| 55 | + }, |
| 56 | + ], |
| 57 | + ['changelog', { action: 'update', releaseId: 'release', expectedRevision: '1' }], |
| 58 | + ['tests', { action: 'create', name: 'billing', title: 'Billing' }], |
| 59 | + ['tests', { action: 'update', name: 'billing', title: 'Billing' }], |
| 60 | + ['tests', { action: 'delete', name: 'billing' }], |
| 61 | + ['tests', { action: 'run', version: 'draft' }], |
| 62 | + ['workspaces', { action: 'create', name: 'Research' }], |
| 63 | + ['search_sources', { action: 'approve', connectorType: 'slack', approved: true }], |
| 64 | + ['ffmpeg', { operation: 'probe', inputs: { files: [{ path: 'files/a.mp4' }] } }], |
| 65 | +] |
| 66 | + |
| 67 | +const reads: Array<[string, Record<string, unknown>]> = [ |
| 68 | + ['settings', { action: 'list', scope: 'workspace' }], |
| 69 | + [ |
| 70 | + 'settings', |
| 71 | + { |
| 72 | + action: 'execute', |
| 73 | + scope: 'workspace', |
| 74 | + section: 'teammates', |
| 75 | + operation: 'list_invitations', |
| 76 | + input: {}, |
| 77 | + }, |
| 78 | + ], |
| 79 | + ['dashboards', { action: 'get' }], |
| 80 | + ['changelog', { action: 'list' }], |
| 81 | + ['tests', { action: 'list', version: 'draft' }], |
| 82 | + ['search_sources', { action: 'list' }], |
| 83 | +] |
| 84 | + |
| 85 | +const dispatched = vi.fn() |
| 86 | + |
| 87 | +beforeEach(() => { |
| 88 | + dispatched.mockReset() |
| 89 | + for (const tool of tools) { |
| 90 | + vi.spyOn(tool, 'execute').mockImplementation(async () => { |
| 91 | + dispatched(tool.name) |
| 92 | + return { success: true } |
| 93 | + }) |
| 94 | + } |
| 95 | +}) |
| 96 | + |
| 97 | +/** Dispatch is what is under test; the stubbed result need not satisfy each tool's output schema. */ |
| 98 | +async function routeIgnoringOutput( |
| 99 | + toolName: string, |
| 100 | + payload: Record<string, unknown>, |
| 101 | + context: ServerToolContext |
| 102 | +) { |
| 103 | + await routeExecution(toolName, payload, context).catch(() => undefined) |
| 104 | +} |
| 105 | + |
| 106 | +describe('a run capped below its principal’s role', () => { |
| 107 | + it.each(writes)('refuses %s %j before dispatch', async (toolName, payload) => { |
| 108 | + await expect(routeExecution(toolName, payload, cappedWorkspaceRun)).rejects.toThrow( |
| 109 | + "requires write access. You have 'read' permission." |
| 110 | + ) |
| 111 | + expect(dispatched).not.toHaveBeenCalled() |
| 112 | + }) |
| 113 | + |
| 114 | + it('refuses a write when the run carries no permission at all', async () => { |
| 115 | + await expect( |
| 116 | + routeExecution( |
| 117 | + 'dashboards', |
| 118 | + { action: 'set', content: '# Revenue' }, |
| 119 | + { |
| 120 | + ...cappedWorkspaceRun, |
| 121 | + userPermission: undefined, |
| 122 | + } |
| 123 | + ) |
| 124 | + ).rejects.toThrow("You have 'none' permission.") |
| 125 | + expect(dispatched).not.toHaveBeenCalled() |
| 126 | + }) |
| 127 | + |
| 128 | + it.each(reads)('still answers %s %j', async (toolName, payload) => { |
| 129 | + await routeIgnoringOutput(toolName, payload, cappedWorkspaceRun) |
| 130 | + expect(dispatched).toHaveBeenCalledExactlyOnceWith(toolName) |
| 131 | + }) |
| 132 | +}) |
| 133 | + |
| 134 | +describe('a run whose permission admits writes', () => { |
| 135 | + it('dispatches a write at write permission', async () => { |
| 136 | + await routeIgnoringOutput( |
| 137 | + 'dashboards', |
| 138 | + { action: 'set', content: '# Revenue' }, |
| 139 | + { ...cappedWorkspaceRun, userPermission: 'write' } |
| 140 | + ) |
| 141 | + expect(dispatched).toHaveBeenCalledExactlyOnceWith('dashboards') |
| 142 | + }) |
| 143 | + |
| 144 | + it.each([ |
| 145 | + ['an organization chat', { organizationId: 'org', workspaceId: undefined }], |
| 146 | + ['an organization chat acting on a workspace', { chatOrganizationId: 'org' }], |
| 147 | + ])( |
| 148 | + 'leaves %s to the live role its operations authorize', |
| 149 | + async (_label, scope: Partial<ServerToolContext>) => { |
| 150 | + await routeIgnoringOutput( |
| 151 | + 'search_sources', |
| 152 | + { action: 'approve', connectorType: 'slack', approved: true }, |
| 153 | + { ...cappedWorkspaceRun, userPermission: undefined, ...scope } |
| 154 | + ) |
| 155 | + expect(dispatched).toHaveBeenCalledExactlyOnceWith('search_sources') |
| 156 | + } |
| 157 | + ) |
| 158 | +}) |
0 commit comments