|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { curateBlockDetail } from '@/lib/mothership/agent-cli/curation' |
| 3 | + |
| 4 | +const { permissionConfig, denied } = vi.hoisted(() => ({ |
| 5 | + permissionConfig: { current: null as { deniedTools?: string[] } | null }, |
| 6 | + denied: { |
| 7 | + current: { |
| 8 | + needsProjection: new Map<string, ReadonlySet<string>>(), |
| 9 | + fullyDenied: new Set<string>(), |
| 10 | + }, |
| 11 | + }, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/ee/access-control/utils/permission-check', () => ({ |
| 15 | + getUserPermissionConfig: vi.fn(async () => permissionConfig.current), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@/lib/mothership/integration-tool-projection', () => ({ |
| 19 | + resolveDeniedBlockOperations: vi.fn(() => denied.current), |
| 20 | +})) |
| 21 | + |
| 22 | +const viewer = { workspaceId: 'ws', userId: 'user' } |
| 23 | + |
| 24 | +function blockDetail() { |
| 25 | + return { |
| 26 | + type: 'slack', |
| 27 | + operations: { |
| 28 | + send: { toolId: 'slack_send' }, |
| 29 | + canvas: { toolId: 'slack_canvas' }, |
| 30 | + }, |
| 31 | + tools: [{ id: 'slack_send' }, { id: 'slack_canvas' }], |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function ok(stdout: string) { |
| 36 | + return { exitCode: 0, stdout, stderr: '' } |
| 37 | +} |
| 38 | + |
| 39 | +describe('curateBlockDetail', () => { |
| 40 | + beforeEach(() => { |
| 41 | + permissionConfig.current = null |
| 42 | + denied.current = { needsProjection: new Map(), fullyDenied: new Set() } |
| 43 | + }) |
| 44 | + |
| 45 | + it('passes through when the viewer has no denied tools', async () => { |
| 46 | + const input = ok(JSON.stringify(blockDetail())) |
| 47 | + expect(await curateBlockDetail(input, viewer)).toBe(input) |
| 48 | + }) |
| 49 | + |
| 50 | + it('passes through non-block output untouched', async () => { |
| 51 | + permissionConfig.current = { deniedTools: ['slack_canvas'] } |
| 52 | + const input = ok('not json') |
| 53 | + expect(await curateBlockDetail(input, viewer)).toBe(input) |
| 54 | + }) |
| 55 | + |
| 56 | + it('drops denied operations and their tools from a partially denied block', async () => { |
| 57 | + permissionConfig.current = { deniedTools: ['slack_canvas'] } |
| 58 | + denied.current = { |
| 59 | + needsProjection: new Map([['slack', new Set(['canvas'])]]), |
| 60 | + fullyDenied: new Set(), |
| 61 | + } |
| 62 | + const result = await curateBlockDetail(ok(JSON.stringify(blockDetail())), viewer) |
| 63 | + expect(result.exitCode).toBe(0) |
| 64 | + const curated = JSON.parse(result.stdout) |
| 65 | + expect(Object.keys(curated.operations)).toEqual(['send']) |
| 66 | + expect(curated.tools).toEqual([{ id: 'slack_send' }]) |
| 67 | + }) |
| 68 | + |
| 69 | + it('refuses a fully denied block', async () => { |
| 70 | + permissionConfig.current = { deniedTools: ['slack_send', 'slack_canvas'] } |
| 71 | + denied.current = { needsProjection: new Map(), fullyDenied: new Set(['slack']) } |
| 72 | + const result = await curateBlockDetail(ok(JSON.stringify(blockDetail())), viewer) |
| 73 | + expect(result.exitCode).toBe(1) |
| 74 | + expect(result.stderr).toContain('not available to you') |
| 75 | + }) |
| 76 | +}) |
0 commit comments