|
| 1 | +import type { ActorSignal } from '@mastra/core/auth/ee'; |
| 2 | +import { Mastra } from '@mastra/core/mastra'; |
| 3 | +import { MockStore } from '@mastra/core/storage'; |
| 4 | +import { Inngest } from 'inngest'; |
| 5 | +import { describe, expect, it, vi } from 'vitest'; |
| 6 | +import { z } from 'zod'; |
| 7 | + |
| 8 | +import { InngestExecutionEngine } from './execution-engine'; |
| 9 | +import { init } from './index'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Hermetic tests for FGA `actor` signal threading through @mastra/inngest. |
| 13 | + * |
| 14 | + * These do NOT require an Inngest dev server. They exercise the two seams the |
| 15 | + * engine owns directly: |
| 16 | + * 1. The run/start path serializes `actor` into the Inngest event payload. |
| 17 | + * 2. `executeWorkflowStep` forwards `actor` across the nested-workflow |
| 18 | + * `step.invoke()` serialization boundary so the nested run re-threads it. |
| 19 | + */ |
| 20 | +describe('@mastra/inngest actor signal threading (hermetic)', () => { |
| 21 | + const actor: ActorSignal = { actorKind: 'system', sourceWorkflow: 'nightly-workflow' }; |
| 22 | + |
| 23 | + it('forwards actor into the nested-workflow invoke payload (durable step boundary)', async () => { |
| 24 | + const inngest = new Inngest({ id: 'mastra-test' }); |
| 25 | + const { createWorkflow, createStep } = init(inngest); |
| 26 | + |
| 27 | + const nestedStep = createStep({ |
| 28 | + id: 'nested-step', |
| 29 | + inputSchema: z.object({ value: z.string() }), |
| 30 | + outputSchema: z.object({ value: z.string() }), |
| 31 | + execute: async ({ inputData }) => inputData, |
| 32 | + }); |
| 33 | + |
| 34 | + const nestedWorkflow = createWorkflow({ |
| 35 | + id: 'nested-workflow', |
| 36 | + inputSchema: z.object({ value: z.string() }), |
| 37 | + outputSchema: z.object({ value: z.string() }), |
| 38 | + steps: [nestedStep], |
| 39 | + }) |
| 40 | + .then(nestedStep) |
| 41 | + .commit(); |
| 42 | + |
| 43 | + // Capture the data handed to inngestStep.invoke (the serialization boundary). |
| 44 | + const invokeData: any[] = []; |
| 45 | + const fakeStep: any = { |
| 46 | + run: async (_id: string, fn: () => Promise<any>) => fn(), |
| 47 | + invoke: async (_id: string, opts: { function: any; data: any }) => { |
| 48 | + invokeData.push(opts.data); |
| 49 | + return { result: { status: 'success', result: { value: 'ok' }, state: {} }, runId: 'nested-run' }; |
| 50 | + }, |
| 51 | + sleep: async () => {}, |
| 52 | + sleepUntil: async () => {}, |
| 53 | + }; |
| 54 | + |
| 55 | + const engine = new InngestExecutionEngine({} as Mastra, fakeStep, 0, {}); |
| 56 | + const pubsub: any = { publish: vi.fn().mockResolvedValue(undefined) }; |
| 57 | + |
| 58 | + const result = await engine.executeWorkflowStep({ |
| 59 | + step: nestedWorkflow as any, |
| 60 | + stepResults: {}, |
| 61 | + executionContext: { |
| 62 | + workflowId: 'parent-workflow', |
| 63 | + runId: 'parent-run', |
| 64 | + executionPath: [0], |
| 65 | + suspendedPaths: {}, |
| 66 | + state: {}, |
| 67 | + } as any, |
| 68 | + prevOutput: {}, |
| 69 | + inputData: { value: 'ok' }, |
| 70 | + pubsub, |
| 71 | + startedAt: Date.now(), |
| 72 | + actor, |
| 73 | + } as any); |
| 74 | + |
| 75 | + expect(result?.status).toBe('success'); |
| 76 | + expect(invokeData).toHaveLength(1); |
| 77 | + expect(invokeData[0].actor).toEqual(actor); |
| 78 | + }); |
| 79 | + |
| 80 | + it('serializes actor into the Inngest event payload on the start path', async () => { |
| 81 | + const inngest = new Inngest({ id: 'mastra-test' }); |
| 82 | + const { createWorkflow, createStep } = init(inngest); |
| 83 | + |
| 84 | + const step = createStep({ |
| 85 | + id: 'step', |
| 86 | + inputSchema: z.object({ value: z.string() }), |
| 87 | + outputSchema: z.object({ value: z.string() }), |
| 88 | + execute: async ({ inputData }) => inputData, |
| 89 | + }); |
| 90 | + |
| 91 | + const workflow = createWorkflow({ |
| 92 | + id: 'actor-start-workflow', |
| 93 | + inputSchema: z.object({ value: z.string() }), |
| 94 | + outputSchema: z.object({ value: z.string() }), |
| 95 | + steps: [step], |
| 96 | + }) |
| 97 | + .then(step) |
| 98 | + .commit(); |
| 99 | + |
| 100 | + const mastra = new Mastra({ |
| 101 | + logger: false, |
| 102 | + storage: new MockStore(), |
| 103 | + workflows: { 'actor-start-workflow': workflow as any }, |
| 104 | + }); |
| 105 | + workflow.__registerMastra(mastra); |
| 106 | + |
| 107 | + const sendSpy = vi.spyOn(inngest, 'send').mockResolvedValue({ ids: ['evt-1'] } as any); |
| 108 | + |
| 109 | + const run = await workflow.createRun(); |
| 110 | + await run.startAsync({ inputData: { value: 'ok' }, actor }); |
| 111 | + |
| 112 | + expect(sendSpy).toHaveBeenCalledTimes(1); |
| 113 | + const sentData = (sendSpy.mock.calls[0]![0] as any).data; |
| 114 | + expect(sentData.actor).toEqual(actor); |
| 115 | + }); |
| 116 | + |
| 117 | + it('forwards a re-supplied actor through the resume event payload (per-call contract)', async () => { |
| 118 | + // `actor` is intentionally NOT rehydrated from the snapshot (matching the default |
| 119 | + // engine — see packages/core/src/workflows/workflow.ts `_resume`). A trusted resumer |
| 120 | + // re-supplies it on each resume; this locks in that per-call contract. |
| 121 | + const inngest = new Inngest({ id: 'mastra-test' }); |
| 122 | + const { createWorkflow, createStep } = init(inngest); |
| 123 | + |
| 124 | + const step = createStep({ |
| 125 | + id: 'suspendable-step', |
| 126 | + inputSchema: z.object({ value: z.string() }), |
| 127 | + outputSchema: z.object({ value: z.string() }), |
| 128 | + execute: async ({ inputData }) => inputData, |
| 129 | + }); |
| 130 | + |
| 131 | + const workflow = createWorkflow({ |
| 132 | + id: 'actor-resume-workflow', |
| 133 | + inputSchema: z.object({ value: z.string() }), |
| 134 | + outputSchema: z.object({ value: z.string() }), |
| 135 | + steps: [step], |
| 136 | + }) |
| 137 | + .then(step) |
| 138 | + .commit(); |
| 139 | + |
| 140 | + const mastra = new Mastra({ |
| 141 | + logger: false, |
| 142 | + storage: new MockStore(), |
| 143 | + workflows: { 'actor-resume-workflow': workflow as any }, |
| 144 | + }); |
| 145 | + workflow.__registerMastra(mastra); |
| 146 | + |
| 147 | + const run = await workflow.createRun(); |
| 148 | + |
| 149 | + // Persist a suspended snapshot so the resume path has something to load. |
| 150 | + const workflowsStore = await mastra.getStorage()!.getStore('workflows'); |
| 151 | + await workflowsStore!.persistWorkflowSnapshot({ |
| 152 | + workflowName: 'actor-resume-workflow', |
| 153 | + runId: run.runId, |
| 154 | + snapshot: { |
| 155 | + runId: run.runId, |
| 156 | + serializedStepGraph: [], |
| 157 | + status: 'suspended', |
| 158 | + value: {}, |
| 159 | + context: { input: { value: 'ok' } }, |
| 160 | + activePaths: [], |
| 161 | + suspendedPaths: { 'suspendable-step': [0] }, |
| 162 | + activeStepsPath: {}, |
| 163 | + resumeLabels: {}, |
| 164 | + waitingPaths: {}, |
| 165 | + timestamp: Date.now(), |
| 166 | + } as any, |
| 167 | + }); |
| 168 | + |
| 169 | + const sendSpy = vi.spyOn(inngest, 'send').mockResolvedValue({ ids: ['evt-resume'] } as any); |
| 170 | + |
| 171 | + await run.resumeAsync({ resumeData: { value: 'ok' }, step: 'suspendable-step', actor }); |
| 172 | + |
| 173 | + expect(sendSpy).toHaveBeenCalledTimes(1); |
| 174 | + const sentData = (sendSpy.mock.calls[0]![0] as any).data; |
| 175 | + expect(sentData.actor).toEqual(actor); |
| 176 | + }); |
| 177 | +}); |
0 commit comments