|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { resetDbChainMock } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + getTableById: vi.fn(), |
| 9 | + getRowById: vi.fn(), |
| 10 | + updateRow: vi.fn(), |
| 11 | + pickNextEligibleGroupForRow: vi.fn(), |
| 12 | + stashCellContextForResume: vi.fn(), |
| 13 | + writeWorkflowGroupState: vi.fn(async () => 'wrote'), |
| 14 | + markWorkflowGroupPickedUp: vi.fn(async () => 'wrote'), |
| 15 | + createWorkflowCellProgressWriter: vi.fn(), |
| 16 | + buildCancelledExecution: vi.fn(), |
| 17 | + classifyWorkflowCellTerminalResult: vi.fn(), |
| 18 | + getEnrichment: vi.fn(), |
| 19 | + runEnrichment: vi.fn(), |
| 20 | + skippedEnrichmentDetail: vi.fn(() => ({})), |
| 21 | + checkAttributedUsageLimits: vi.fn(async () => ({ isExceeded: false })), |
| 22 | + loadTableRowSecretProvenance: vi.fn(async () => ({ scope: null, entries: [] })), |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/table/service', () => ({ getTableById: mocks.getTableById })) |
| 26 | +vi.mock('@/lib/table/rows/service', () => ({ |
| 27 | + getRowById: mocks.getRowById, |
| 28 | + updateRow: mocks.updateRow, |
| 29 | +})) |
| 30 | +vi.mock('@/lib/table/cell-write', () => ({ |
| 31 | + writeWorkflowGroupState: mocks.writeWorkflowGroupState, |
| 32 | + markWorkflowGroupPickedUp: mocks.markWorkflowGroupPickedUp, |
| 33 | + createWorkflowCellProgressWriter: mocks.createWorkflowCellProgressWriter, |
| 34 | + buildCancelledExecution: mocks.buildCancelledExecution, |
| 35 | +})) |
| 36 | +vi.mock('@/lib/table/workflow-cell-result', () => ({ |
| 37 | + classifyWorkflowCellTerminalResult: mocks.classifyWorkflowCellTerminalResult, |
| 38 | +})) |
| 39 | +vi.mock('@/enrichments/registry', () => ({ getEnrichment: mocks.getEnrichment })) |
| 40 | +vi.mock('@/enrichments/run', () => ({ |
| 41 | + runEnrichment: mocks.runEnrichment, |
| 42 | + skippedEnrichmentDetail: mocks.skippedEnrichmentDetail, |
| 43 | +})) |
| 44 | +vi.mock('@/lib/billing/core/billing-attribution', () => ({ |
| 45 | + assertBillingAttributionSnapshot: vi.fn((value) => value), |
| 46 | + checkAttributedUsageLimits: mocks.checkAttributedUsageLimits, |
| 47 | + toBillingContext: vi.fn(() => ({})), |
| 48 | +})) |
| 49 | +vi.mock('@/lib/table/rows/secret-provenance', () => ({ |
| 50 | + createExactEmptyTableRowSecretProvenance: vi.fn(() => undefined), |
| 51 | + createTableRowSecretProvenanceFromRegistry: vi.fn(() => undefined), |
| 52 | + loadTableRowSecretProvenance: mocks.loadTableRowSecretProvenance, |
| 53 | +})) |
| 54 | +vi.mock('@/executor/utils/resolved-secret-trace-registry', () => ({ |
| 55 | + ResolvedSecretTraceRegistry: class { |
| 56 | + async importCrossingProvenance() {} |
| 57 | + }, |
| 58 | +})) |
| 59 | +vi.mock('@/lib/table/events', () => ({ appendTableEvent: vi.fn() })) |
| 60 | + |
| 61 | +import { runRowCascadeLoop } from '@/background/workflow-column-execution' |
| 62 | + |
| 63 | +const GROUP = { |
| 64 | + id: 'group-1', |
| 65 | + type: 'enrichment' as const, |
| 66 | + enrichmentId: 'company-lookup', |
| 67 | + workflowId: '', |
| 68 | + outputs: [{ columnName: 'col-out', blockId: '', path: '' }], |
| 69 | + inputMappings: [{ columnName: 'col-in', inputName: 'domain' }], |
| 70 | +} |
| 71 | + |
| 72 | +const TABLE = { |
| 73 | + id: 'table-1', |
| 74 | + workspaceId: 'workspace-1', |
| 75 | + schema: { columns: [{ id: 'col-in', name: 'Domain', type: 'string' }], workflowGroups: [GROUP] }, |
| 76 | +} |
| 77 | + |
| 78 | +function payload(capabilityGovernedUserId: string | null, triggeredByUserId?: string) { |
| 79 | + return { |
| 80 | + tableId: 'table-1', |
| 81 | + tableName: 'Table', |
| 82 | + rowId: 'row-1', |
| 83 | + groupId: 'group-1', |
| 84 | + workflowId: '', |
| 85 | + workspaceId: 'workspace-1', |
| 86 | + executionId: 'exec-1', |
| 87 | + capabilityGovernedUserId, |
| 88 | + ...(triggeredByUserId ? { triggeredByUserId } : {}), |
| 89 | + billingAttribution: { |
| 90 | + /** The meter's subject: the payer a workspace-key run attributes to. */ |
| 91 | + actorUserId: triggeredByUserId ?? 'billing-owner', |
| 92 | + workspaceId: 'workspace-1', |
| 93 | + organizationId: null, |
| 94 | + billedAccountUserId: 'billing-owner', |
| 95 | + billingEntity: { type: 'user' as const, id: 'billing-owner' }, |
| 96 | + billingPeriod: { start: '2026-07-01T00:00:00.000Z', end: '2026-08-01T00:00:00.000Z' }, |
| 97 | + payerSubscription: null, |
| 98 | + }, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** The `userId` the cell handed the enrichment run — the per-tool gate subject. */ |
| 103 | +function gatedUserId(): unknown { |
| 104 | + expect(mocks.runEnrichment).toHaveBeenCalledTimes(1) |
| 105 | + return (mocks.runEnrichment.mock.calls[0][2] as { userId?: unknown }).userId |
| 106 | +} |
| 107 | + |
| 108 | +describe('enrichment cell capability subject', () => { |
| 109 | + beforeEach(() => { |
| 110 | + vi.clearAllMocks() |
| 111 | + resetDbChainMock() |
| 112 | + mocks.getTableById.mockResolvedValue(TABLE) |
| 113 | + mocks.getRowById.mockResolvedValue({ |
| 114 | + id: 'row-1', |
| 115 | + data: { 'col-in': 'example.com' }, |
| 116 | + executions: {}, |
| 117 | + updatedAt: new Date('2026-08-01T00:00:00.000Z'), |
| 118 | + }) |
| 119 | + mocks.checkAttributedUsageLimits.mockResolvedValue({ isExceeded: false }) |
| 120 | + mocks.markWorkflowGroupPickedUp.mockResolvedValue('wrote') |
| 121 | + mocks.writeWorkflowGroupState.mockResolvedValue('wrote') |
| 122 | + mocks.pickNextEligibleGroupForRow.mockResolvedValue(null) |
| 123 | + mocks.getEnrichment.mockReturnValue({ |
| 124 | + id: 'company-lookup', |
| 125 | + inputs: [{ id: 'domain', required: true }], |
| 126 | + providers: [], |
| 127 | + }) |
| 128 | + mocks.runEnrichment.mockResolvedValue({ result: {}, cost: 0, detail: {} }) |
| 129 | + }) |
| 130 | + |
| 131 | + /** |
| 132 | + * A workspace-key write is actorless: nobody's permission group governs it, |
| 133 | + * and the billing owner beside it on the payload is a bystander. Handing that |
| 134 | + * bystander to the enrichment would run their tool denylist against a request |
| 135 | + * they never made. |
| 136 | + */ |
| 137 | + it('runs a workspace-key dispatch ungated even though the payload names a payer', async () => { |
| 138 | + await runRowCascadeLoop(payload(null, 'billing-owner') as never) |
| 139 | + expect(gatedUserId()).toBeNull() |
| 140 | + }) |
| 141 | + |
| 142 | + it('governs a session-triggered dispatch by the acting person', async () => { |
| 143 | + await runRowCascadeLoop(payload('acting-user', 'acting-user') as never) |
| 144 | + expect(gatedUserId()).toBe('acting-user') |
| 145 | + }) |
| 146 | + |
| 147 | + /** |
| 148 | + * The shape a pre-0315 dispatch row has after the column is added: no governed |
| 149 | + * subject, attribution intact. New code reads that as actorless, which is why |
| 150 | + * the migration backfills the legacy subject onto non-terminal old rows rather |
| 151 | + * than letting the reader reconstruct it here. |
| 152 | + */ |
| 153 | + it('does not fall back to the attribution when the governed subject is absent', async () => { |
| 154 | + await runRowCascadeLoop(payload(null, 'legacy-trigger-user') as never) |
| 155 | + expect(gatedUserId()).toBeNull() |
| 156 | + }) |
| 157 | +}) |
0 commit comments