|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Copilot's `@log` mention context, projected for the chatting user. |
| 5 | + * |
| 6 | + * `logs.cost` and `logs.trace_spans` are PROJECTIONS, not gates, and Copilot is |
| 7 | + * deliberately not exempt from them: it acts as the person, so a run inlined as |
| 8 | + * mention context must be withheld exactly as the person's own log surfaces |
| 9 | + * withhold it. This path resolved the run row directly with a role-only |
| 10 | + * authorization and inlined the run total, every span's own cost, and the whole |
| 11 | + * block overview — so a member withheld all three on `/api/logs/**` read them by |
| 12 | + * typing `@` in chat. |
| 13 | + * |
| 14 | + * Kept in its own file because it mocks `config-scope.server`, which the sibling |
| 15 | + * `process-contents.test.ts` deliberately leaves real so its integration-allowlist |
| 16 | + * tests exercise `getUserPermissionConfig`. |
| 17 | + */ |
| 18 | +import { |
| 19 | + dbChainMockFns, |
| 20 | + permissionGroupScopeMock, |
| 21 | + permissionGroupScopeMockFns, |
| 22 | + resetPermissionGroupScopeMock, |
| 23 | + workflowAuthzMockFns, |
| 24 | +} from '@sim/testing' |
| 25 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 26 | +import type { ChatContext } from '@/stores/panel' |
| 27 | + |
| 28 | +vi.mock('@/lib/permission-groups/config-scope.server', () => permissionGroupScopeMock) |
| 29 | + |
| 30 | +import { processContextsServer } from '@/lib/copilot/chat/process-contents' |
| 31 | +import { DEFAULT_PERMISSION_GROUP_CONFIG } from '@/lib/permission-groups/fields' |
| 32 | + |
| 33 | +function queueRun(): void { |
| 34 | + dbChainMockFns.limit.mockResolvedValueOnce([ |
| 35 | + { |
| 36 | + id: 'log-1', |
| 37 | + workflowId: 'wf-1', |
| 38 | + workspaceId: 'ws-1', |
| 39 | + executionId: 'exec-1', |
| 40 | + level: 'error', |
| 41 | + trigger: 'manual', |
| 42 | + startedAt: new Date('2026-01-01T00:00:00.000Z'), |
| 43 | + endedAt: new Date('2026-01-01T00:00:01.000Z'), |
| 44 | + totalDurationMs: 1000, |
| 45 | + executionData: { |
| 46 | + traceSpans: [ |
| 47 | + { |
| 48 | + id: 'span-1', |
| 49 | + blockId: 'block-1', |
| 50 | + name: 'Agent 1', |
| 51 | + type: 'agent', |
| 52 | + status: 'failed', |
| 53 | + duration: 500, |
| 54 | + cost: { total: 0.04 }, |
| 55 | + children: [ |
| 56 | + { id: 'span-2', name: 'tool', type: 'tool', duration: 10, cost: { total: 0.01 } }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + costTotal: '0.05', |
| 62 | + workflowName: 'My Flow', |
| 63 | + }, |
| 64 | + ]) |
| 65 | + workflowAuthzMockFns.mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValueOnce({ |
| 66 | + allowed: true, |
| 67 | + workflow: { workspaceId: 'ws-1' }, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +async function mentionSummary(userId?: string) { |
| 72 | + const result = await processContextsServer( |
| 73 | + [{ kind: 'logs', executionId: 'exec-1', label: 'My Flow' } as ChatContext], |
| 74 | + userId as string, |
| 75 | + 'hello', |
| 76 | + 'ws-1' |
| 77 | + ) |
| 78 | + expect(result).toHaveLength(1) |
| 79 | + return JSON.parse(result[0].content) |
| 80 | +} |
| 81 | + |
| 82 | +describe('@log mention context projection', () => { |
| 83 | + beforeEach(() => { |
| 84 | + vi.clearAllMocks() |
| 85 | + resetPermissionGroupScopeMock() |
| 86 | + }) |
| 87 | + |
| 88 | + it('inlines spend whole for a member no group governs', async () => { |
| 89 | + queueRun() |
| 90 | + |
| 91 | + const summary = await mentionSummary('user-1') |
| 92 | + |
| 93 | + expect(summary.cost).toEqual({ total: 0.05 }) |
| 94 | + expect(summary.overview[0].cost).toEqual({ total: 0.04 }) |
| 95 | + expect(summary.overview[0].children[0].cost).toEqual({ total: 0.01 }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('withholds the run total and every span cost when the group hides spend', async () => { |
| 99 | + permissionGroupScopeMockFns.mockResolvePermissionGroupConfig.mockResolvedValue({ |
| 100 | + ...DEFAULT_PERMISSION_GROUP_CONFIG, |
| 101 | + hideCostInfo: true, |
| 102 | + }) |
| 103 | + queueRun() |
| 104 | + |
| 105 | + const summary = await mentionSummary('user-1') |
| 106 | + |
| 107 | + expect(summary.cost).toBeUndefined() |
| 108 | + expect(summary.overview).toHaveLength(1) |
| 109 | + expect(summary.overview[0].name).toBe('Agent 1') |
| 110 | + expect(summary.overview[0].cost).toBeUndefined() |
| 111 | + expect(summary.overview[0].children[0].cost).toBeUndefined() |
| 112 | + }) |
| 113 | + |
| 114 | + /** |
| 115 | + * The overview is derived from `traceSpans`, which is on the withheld list the |
| 116 | + * log-detail path strips outright — so it is withheld entirely rather than |
| 117 | + * merely thinned. The run's identity, level and timings stay: these are |
| 118 | + * projections, not gates. |
| 119 | + */ |
| 120 | + it('withholds the whole block overview when the group hides trace spans', async () => { |
| 121 | + permissionGroupScopeMockFns.mockResolvePermissionGroupConfig.mockResolvedValue({ |
| 122 | + ...DEFAULT_PERMISSION_GROUP_CONFIG, |
| 123 | + hideTraceSpans: true, |
| 124 | + }) |
| 125 | + queueRun() |
| 126 | + |
| 127 | + const summary = await mentionSummary('user-1') |
| 128 | + |
| 129 | + expect(summary.overview).toBeUndefined() |
| 130 | + expect(summary.cost).toEqual({ total: 0.05 }) |
| 131 | + expect(summary.executionId).toBe('exec-1') |
| 132 | + expect(JSON.stringify(summary)).not.toContain('Agent 1') |
| 133 | + }) |
| 134 | + |
| 135 | + /** No subject, no group — never a bystander's. */ |
| 136 | + it('resolves no group when the mention carries no subject', async () => { |
| 137 | + permissionGroupScopeMockFns.mockResolvePermissionGroupConfig.mockResolvedValue({ |
| 138 | + ...DEFAULT_PERMISSION_GROUP_CONFIG, |
| 139 | + hideCostInfo: true, |
| 140 | + hideTraceSpans: true, |
| 141 | + }) |
| 142 | + queueRun() |
| 143 | + |
| 144 | + const summary = await mentionSummary(undefined) |
| 145 | + |
| 146 | + expect(summary.cost).toEqual({ total: 0.05 }) |
| 147 | + expect(summary.overview).toHaveLength(1) |
| 148 | + expect(permissionGroupScopeMockFns.mockResolvePermissionGroupConfig).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | +}) |
0 commit comments