|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { buildChatTitleContext } from '@/lib/mothership/chat/title-context' |
| 3 | + |
| 4 | +const { getWorkspace, listWorkspaces } = vi.hoisted(() => ({ |
| 5 | + getWorkspace: vi.fn(), |
| 6 | + listWorkspaces: vi.fn(), |
| 7 | +})) |
| 8 | +vi.mock('@/lib/workspaces/application/get-public-workspace', () => ({ |
| 9 | + getPublicWorkspace: { execute: getWorkspace }, |
| 10 | +})) |
| 11 | +vi.mock('@/lib/workspaces/application/list-organization-workspaces', () => ({ |
| 12 | + listOrganizationWorkspaces: { execute: listWorkspaces }, |
| 13 | +})) |
| 14 | +const inventory = { |
| 15 | + workflows: [{ id: 'workflow', name: 'Invoice approval', deployed: true }], |
| 16 | + tables: [{ id: 'table', name: 'Customers' }], |
| 17 | + knowledgeBases: [], |
| 18 | + files: [{ path: 'invoice.csv' }], |
| 19 | + skills: [], |
| 20 | + customTools: [], |
| 21 | + mcpServers: [], |
| 22 | + credentials: [{ id: 'credential', name: 'SECRET CREDENTIAL' }], |
| 23 | + secrets: ['SECRET NAME'], |
| 24 | + truncated: [], |
| 25 | +} |
| 26 | + |
| 27 | +describe('buildChatTitleContext', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + getWorkspace.mockResolvedValue({ workspace: { name: 'Finance' } }) |
| 31 | + }) |
| 32 | + |
| 33 | + it('reuses prepared or recovered inventory names after current workspace authorization', async () => { |
| 34 | + const result = await buildChatTitleContext({ |
| 35 | + userId: 'user', |
| 36 | + workspaceId: 'workspace', |
| 37 | + inventory, |
| 38 | + }) |
| 39 | + expect(JSON.parse(result!)).toEqual({ |
| 40 | + scope: 'workspace', |
| 41 | + workspaceName: 'Finance', |
| 42 | + partial: true, |
| 43 | + resources: { workflows: ['Invoice approval'], tables: ['Customers'], files: ['invoice.csv'] }, |
| 44 | + }) |
| 45 | + expect(result).not.toContain('SECRET') |
| 46 | + expect(getWorkspace).toHaveBeenCalledWith({ |
| 47 | + principal: expect.objectContaining({ |
| 48 | + subjectUserId: 'user', |
| 49 | + workspaceId: 'workspace', |
| 50 | + audience: 'sim:workspaces', |
| 51 | + }), |
| 52 | + input: { workspaceId: 'workspace' }, |
| 53 | + }) |
| 54 | + expect(listWorkspaces).not.toHaveBeenCalled() |
| 55 | + }) |
| 56 | + |
| 57 | + it('uses a bounded authorized org listing and never fans out into workspace inventories', async () => { |
| 58 | + listWorkspaces.mockResolvedValue({ workspaces: [{ name: 'Marketing', id: 'hidden-id' }] }) |
| 59 | + const result = await buildChatTitleContext({ |
| 60 | + userId: 'user', |
| 61 | + organizationId: 'org', |
| 62 | + chatId: 'chat', |
| 63 | + inventory, |
| 64 | + }) |
| 65 | + expect(JSON.parse(result!)).toEqual({ |
| 66 | + scope: 'organization', |
| 67 | + partial: true, |
| 68 | + resources: { workspaces: ['Marketing'] }, |
| 69 | + }) |
| 70 | + expect(listWorkspaces).toHaveBeenCalledWith({ |
| 71 | + principal: expect.objectContaining({ |
| 72 | + subjectUserId: 'user', |
| 73 | + organizationId: 'org', |
| 74 | + resourceScope: { chatId: 'chat' }, |
| 75 | + audience: 'sim:workspaces', |
| 76 | + }), |
| 77 | + input: { organizationId: 'org', limit: 12 }, |
| 78 | + }) |
| 79 | + expect(getWorkspace).not.toHaveBeenCalled() |
| 80 | + }) |
| 81 | + |
| 82 | + it('omits metadata on revoked access, including previously prepared inventory', async () => { |
| 83 | + getWorkspace.mockRejectedValueOnce(new Error('forbidden')) |
| 84 | + expect( |
| 85 | + await buildChatTitleContext({ userId: 'user', workspaceId: 'workspace', inventory }) |
| 86 | + ).toBeUndefined() |
| 87 | + }) |
| 88 | + |
| 89 | + it('bounds escaped names and excludes all unprojected fields', async () => { |
| 90 | + const names = Array.from({ length: 100 }, () => ({ |
| 91 | + id: 'id', |
| 92 | + name: '\u0000'.repeat(500), |
| 93 | + deployed: true, |
| 94 | + })) |
| 95 | + const result = await buildChatTitleContext({ |
| 96 | + userId: 'user', |
| 97 | + workspaceId: 'workspace', |
| 98 | + inventory: { |
| 99 | + ...inventory, |
| 100 | + workflows: names, |
| 101 | + tables: names, |
| 102 | + knowledgeBases: names, |
| 103 | + body: 'PRIVATE BODY', |
| 104 | + inputSchema: { secret: 'PRIVATE SCHEMA' }, |
| 105 | + }, |
| 106 | + }) |
| 107 | + expect(result!.length).toBeLessThanOrEqual(6000) |
| 108 | + expect(() => JSON.parse(result!)).not.toThrow() |
| 109 | + expect(result).not.toContain('PRIVATE') |
| 110 | + }) |
| 111 | + |
| 112 | + it('does not invent authority for missing owners or ambiguous scope', async () => { |
| 113 | + expect(await buildChatTitleContext({ workspaceId: 'workspace', inventory })).toBeUndefined() |
| 114 | + expect( |
| 115 | + await buildChatTitleContext({ |
| 116 | + userId: 'user', |
| 117 | + workspaceId: 'workspace', |
| 118 | + organizationId: 'org', |
| 119 | + }) |
| 120 | + ).toBeUndefined() |
| 121 | + expect(getWorkspace).not.toHaveBeenCalled() |
| 122 | + }) |
| 123 | +}) |
0 commit comments