|
| 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 | + pickNextEligibleGroupForRow: vi.fn(), |
| 11 | + writeWorkflowGroupState: vi.fn(), |
| 12 | + markWorkflowGroupPickedUp: vi.fn(), |
| 13 | + runEnrichment: vi.fn(), |
| 14 | + getEnrichment: vi.fn(), |
| 15 | + readStampedCapabilitySubject: vi.fn(), |
| 16 | + checkAttributedUsageLimits: vi.fn(), |
| 17 | + loadTableRowSecretProvenance: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/table/service', () => ({ getTableById: mocks.getTableById })) |
| 21 | +vi.mock('@/lib/table/rows/service', () => ({ getRowById: mocks.getRowById, updateRow: vi.fn() })) |
| 22 | +vi.mock('@/lib/table/rows/executions', () => ({ |
| 23 | + readStampedCapabilitySubject: mocks.readStampedCapabilitySubject, |
| 24 | +})) |
| 25 | +vi.mock('@/lib/table/workflow-columns', () => ({ |
| 26 | + pickNextEligibleGroupForRow: mocks.pickNextEligibleGroupForRow, |
| 27 | + stashCellContextForResume: vi.fn(), |
| 28 | + buildWorkflowGroupExecutionCorrelation: vi.fn(), |
| 29 | +})) |
| 30 | +vi.mock('@/lib/table/cell-write', () => ({ |
| 31 | + buildCancelledExecution: vi.fn(), |
| 32 | + createWorkflowCellProgressWriter: vi.fn(), |
| 33 | + writeWorkflowGroupState: mocks.writeWorkflowGroupState, |
| 34 | + markWorkflowGroupPickedUp: mocks.markWorkflowGroupPickedUp, |
| 35 | +})) |
| 36 | +vi.mock('@/lib/table/workflow-cell-result', () => ({ |
| 37 | + classifyWorkflowCellTerminalResult: vi.fn(), |
| 38 | +})) |
| 39 | +vi.mock('@/enrichments/registry', () => ({ getEnrichment: mocks.getEnrichment })) |
| 40 | +vi.mock('@/enrichments/run', () => ({ |
| 41 | + runEnrichment: mocks.runEnrichment, |
| 42 | + skippedEnrichmentDetail: () => ({}), |
| 43 | +})) |
| 44 | +vi.mock('@/lib/billing/core/billing-attribution', () => ({ |
| 45 | + assertBillingAttributionSnapshot: (snapshot: unknown) => snapshot, |
| 46 | + checkAttributedUsageLimits: mocks.checkAttributedUsageLimits, |
| 47 | + toBillingContext: () => ({}), |
| 48 | +})) |
| 49 | +vi.mock('@/lib/table/rows/secret-provenance', () => ({ |
| 50 | + createExactEmptyTableRowSecretProvenance: () => ({ complete: true, columns: {} }), |
| 51 | + createTableRowSecretProvenanceFromRegistry: () => ({ complete: true, columns: {} }), |
| 52 | + loadTableRowSecretProvenance: mocks.loadTableRowSecretProvenance, |
| 53 | +})) |
| 54 | +vi.mock('@/lib/table/events', () => ({ appendTableEvent: vi.fn() })) |
| 55 | +vi.mock('@/lib/table/dispatcher', () => ({ |
| 56 | + readDispatch: vi.fn(async () => ({ id: 'tdsp_carrier', status: 'dispatching' })), |
| 57 | + completeDispatchIfActive: vi.fn(), |
| 58 | +})) |
| 59 | + |
| 60 | +import { runRowCascadeLoop } from '@/background/workflow-column-execution' |
| 61 | + |
| 62 | +function enrichmentGroup(id: string) { |
| 63 | + return { |
| 64 | + id, |
| 65 | + type: 'enrichment', |
| 66 | + enrichmentId: 'enrich-1', |
| 67 | + workflowId: '', |
| 68 | + outputs: [{ blockId: '', path: 'out', columnName: 'out' }], |
| 69 | + inputMappings: [{ inputName: 'domain', columnName: 'domain' }], |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const TABLE = { |
| 74 | + id: 'table-1', |
| 75 | + name: 'Table', |
| 76 | + workspaceId: 'workspace-1', |
| 77 | + schema: { |
| 78 | + columns: [{ id: 'domain', name: 'domain', type: 'string' }], |
| 79 | + workflowGroups: [enrichmentGroup('group-1'), enrichmentGroup('group-2')], |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +/** The carrier belongs to an actorless auto-fire: no subject, so no tool gate. */ |
| 84 | +const CARRIER = { |
| 85 | + tableId: 'table-1', |
| 86 | + tableName: 'Table', |
| 87 | + rowId: 'row-1', |
| 88 | + groupId: 'group-1', |
| 89 | + workflowId: '', |
| 90 | + enrichmentId: 'enrich-1', |
| 91 | + workspaceId: 'workspace-1', |
| 92 | + executionId: 'execution-1', |
| 93 | + dispatchId: 'tdsp_carrier', |
| 94 | + executionTimeoutMs: 10_000, |
| 95 | + capabilityGovernedUserId: null, |
| 96 | + billingAttribution: { |
| 97 | + actorUserId: 'user-1', |
| 98 | + workspaceId: 'workspace-1', |
| 99 | + organizationId: null, |
| 100 | + billedAccountUserId: 'user-1', |
| 101 | + billingEntity: { type: 'user' as const, id: 'user-1' }, |
| 102 | + billingPeriod: { start: '2026-07-01T00:00:00.000Z', end: '2026-08-01T00:00:00.000Z' }, |
| 103 | + payerSubscription: null, |
| 104 | + }, |
| 105 | +} as Parameters<typeof runRowCascadeLoop>[0] |
| 106 | + |
| 107 | +describe('draining another dispatch’s pre-stamped marker', () => { |
| 108 | + beforeEach(() => { |
| 109 | + vi.clearAllMocks() |
| 110 | + resetDbChainMock() |
| 111 | + mocks.getTableById.mockResolvedValue(TABLE) |
| 112 | + mocks.getEnrichment.mockReturnValue({ |
| 113 | + id: 'enrich-1', |
| 114 | + name: 'Enrich', |
| 115 | + inputs: [{ id: 'domain', required: true }], |
| 116 | + outputs: [{ id: 'out' }], |
| 117 | + }) |
| 118 | + mocks.checkAttributedUsageLimits.mockResolvedValue({ isExceeded: false }) |
| 119 | + mocks.markWorkflowGroupPickedUp.mockResolvedValue('picked-up') |
| 120 | + mocks.writeWorkflowGroupState.mockResolvedValue('wrote') |
| 121 | + mocks.loadTableRowSecretProvenance.mockResolvedValue({ |
| 122 | + scope: { userId: 'user-1', workspaceId: 'workspace-1' }, |
| 123 | + byRowId: {}, |
| 124 | + }) |
| 125 | + mocks.runEnrichment.mockResolvedValue({ result: { out: 'x' }, cost: 0, detail: {} }) |
| 126 | + mocks.readStampedCapabilitySubject.mockResolvedValue('requesting-member') |
| 127 | + // group-1 completes, then group-2 is picked up carrying an unclaimed marker. |
| 128 | + mocks.getRowById.mockResolvedValue({ |
| 129 | + id: 'row-1', |
| 130 | + data: { domain: 'example.com' }, |
| 131 | + executions: { 'group-2': { status: 'pending', executionId: null, workflowId: '' } }, |
| 132 | + }) |
| 133 | + mocks.pickNextEligibleGroupForRow |
| 134 | + .mockReturnValueOnce(enrichmentGroup('group-2')) |
| 135 | + .mockReturnValue(null) |
| 136 | + }) |
| 137 | + |
| 138 | + /** |
| 139 | + * The lock owner drains markers it did not stamp. Running them under its own |
| 140 | + * subject applies the wrong person's tool denylist — and when the owner is an |
| 141 | + * actorless auto-fire, no denylist at all. |
| 142 | + */ |
| 143 | + it('runs the drained cell under the subject stamped with it', async () => { |
| 144 | + await runRowCascadeLoop(CARRIER) |
| 145 | + |
| 146 | + expect(mocks.readStampedCapabilitySubject).toHaveBeenCalledWith('row-1', 'group-2') |
| 147 | + const subjects = mocks.runEnrichment.mock.calls.map( |
| 148 | + ([, , ctx]) => (ctx as { userId: string | null }).userId |
| 149 | + ) |
| 150 | + expect(subjects).toEqual([null, 'requesting-member']) |
| 151 | + }) |
| 152 | +}) |
0 commit comments