@pikku/inspector 0.12.43 → 0.12.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +261 -0
  2. package/dist/add/add-credential.js +2 -0
  3. package/dist/add/add-keyed-wiring.js +2 -0
  4. package/dist/add/add-workflow.js +9 -4
  5. package/dist/utils/schema-generator.js +19 -4
  6. package/dist/utils/workflow/dsl/deserialize-dsl-workflow.js +373 -91
  7. package/dist/utils/workflow/dsl/extract-dsl-workflow.js +218 -99
  8. package/dist/utils/workflow/graph/convert-dsl-to-graph.js +62 -5
  9. package/dist/utils/workflow/graph/workflow-graph.types.d.ts +3 -3
  10. package/package.json +3 -3
  11. package/run-tests.sh +1 -1
  12. package/src/add/add-auth.test.ts +4 -4
  13. package/src/add/add-credential.ts +2 -0
  14. package/src/add/add-keyed-wiring.ts +2 -0
  15. package/src/add/add-workflow-edge-cases.test.ts +201 -0
  16. package/src/add/add-workflow-fanout.test.ts +60 -0
  17. package/src/add/add-workflow-on-error.test.ts +118 -0
  18. package/src/add/add-workflow-return-switch.test.ts +121 -0
  19. package/src/add/add-workflow-silent-drops.test.ts +163 -0
  20. package/src/add/add-workflow-sleep-expression.test.ts +97 -0
  21. package/src/add/add-workflow.ts +9 -4
  22. package/src/add/config-value-metadata.test.ts +169 -0
  23. package/src/utils/schema-generator.test.ts +164 -0
  24. package/src/utils/schema-generator.ts +26 -3
  25. package/src/utils/workflow/dsl/deserialize-dsl-workflow.ts +480 -119
  26. package/src/utils/workflow/dsl/deserialize-fanout-body.test.ts +71 -0
  27. package/src/utils/workflow/dsl/deserialize-fanout-fidelity.test.ts +99 -0
  28. package/src/utils/workflow/dsl/deserialize-graph-next.test.ts +161 -0
  29. package/src/utils/workflow/dsl/deserialize-roundtrip-bindings.test.ts +121 -0
  30. package/src/utils/workflow/dsl/deserialize-roundtrip-values.test.ts +116 -0
  31. package/src/utils/workflow/dsl/deserialize-roundtrip.test.ts +145 -0
  32. package/src/utils/workflow/dsl/extract-dsl-workflow.ts +245 -105
  33. package/src/utils/workflow/graph/convert-dsl-to-graph.ts +67 -10
  34. package/src/utils/workflow/graph/fanout-node-identity.test.ts +105 -0
  35. package/src/utils/workflow/graph/workflow-graph.types.ts +3 -2
  36. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,201 @@
1
+ import { strict as assert } from 'assert'
2
+ import { describe, test } from 'node:test'
3
+ import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { inspect } from '../inspector.js'
7
+ import type { InspectorLogger } from '../types.js'
8
+ import { deserializeDslWorkflow } from '../utils/workflow/dsl/deserialize-dsl-workflow.js'
9
+
10
+ const STEP_FILE = [
11
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
12
+ 'export const chargeCard = pikkuSessionlessFunc({',
13
+ " func: async () => ({ id: 'c' }),",
14
+ '})',
15
+ 'export const refundOrder = pikkuSessionlessFunc({',
16
+ " func: async () => ({ id: 'r' }),",
17
+ '})',
18
+ 'export const notify = pikkuSessionlessFunc({',
19
+ " func: async () => ({ id: 'n' }),",
20
+ '})',
21
+ 'export const audit = pikkuSessionlessFunc({',
22
+ " func: async () => ({ id: 'a' }),",
23
+ '})',
24
+ ].join('\n')
25
+
26
+ async function run(body: string): Promise<{
27
+ nodes: Record<string, any>
28
+ diags: string[]
29
+ code: string
30
+ }> {
31
+ const diags: string[] = []
32
+ const logger = {
33
+ debug: () => {},
34
+ info: () => {},
35
+ warn: () => {},
36
+ error: () => {},
37
+ diagnostic: (...a: any[]) => diags.push(a.map(String).join(' ')),
38
+ critical: (...a: any[]) => diags.push(a.map(String).join(' ')),
39
+ hasCriticalErrors: () => false,
40
+ } as unknown as InspectorLogger
41
+
42
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-edge-'))
43
+ const stepFile = join(rootDir, 'w.steps.ts')
44
+ const wfFile = join(rootDir, 'w.workflow.ts')
45
+ await writeFile(stepFile, STEP_FILE)
46
+ await writeFile(
47
+ wfFile,
48
+ [
49
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
50
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
51
+ body,
52
+ '})',
53
+ ].join('\n')
54
+ )
55
+ try {
56
+ const state: any = await inspect(logger, [stepFile, wfFile], { rootDir })
57
+ const gm = state.workflows.graphMeta
58
+ const graph = (
59
+ gm instanceof Map ? [...gm.values()] : Object.values(gm)
60
+ )[0] as any
61
+ return {
62
+ nodes: graph?.nodes ?? {},
63
+ diags,
64
+ code: graph ? deserializeDslWorkflow(graph) : '',
65
+ }
66
+ } finally {
67
+ await rm(rootDir, { recursive: true, force: true })
68
+ }
69
+ }
70
+
71
+ describe('onError — edge cases', () => {
72
+ test('the handler is not also emitted as a step that always runs', async () => {
73
+ const { code } = await run(
74
+ [
75
+ " await workflow.do('Charge', 'chargeCard', {}, { onError: 'refundOrder' })",
76
+ ' return { ok: true }',
77
+ ].join('\n')
78
+ )
79
+
80
+ const refundCalls = code.match(/'refundOrder'/g) ?? []
81
+ assert.equal(
82
+ refundCalls.length,
83
+ 1,
84
+ `the handler must appear only as onError, never as its own sequential step, got:\n${code}`
85
+ )
86
+ })
87
+
88
+ test('a non-literal onError is reported, not silently ignored', async () => {
89
+ const { diags } = await run(
90
+ [
91
+ ' const target = data.handler',
92
+ " await workflow.do('Charge', 'chargeCard', {}, { onError: target })",
93
+ ' return { ok: true }',
94
+ ].join('\n')
95
+ )
96
+
97
+ assert.ok(
98
+ diags.some((d) => d.includes('onError')),
99
+ `a non-literal onError must be diagnosed, got: ${JSON.stringify(diags)}`
100
+ )
101
+ })
102
+
103
+ test('onError inside a branch still resolves', async () => {
104
+ const { nodes } = await run(
105
+ [
106
+ ' if (data.paid) {',
107
+ " await workflow.do('Charge', 'chargeCard', {}, { onError: 'refundOrder' })",
108
+ ' }',
109
+ ' return { ok: true }',
110
+ ].join('\n')
111
+ )
112
+
113
+ const charge = Object.values(nodes).find(
114
+ (n: any) => n.rpcName === 'chargeCard'
115
+ ) as any
116
+ assert.ok(
117
+ charge?.onError,
118
+ 'a branch step should still carry its error route'
119
+ )
120
+ assert.equal((nodes[charge.onError] as any)?.rpcName, 'refundOrder')
121
+ })
122
+ })
123
+
124
+ describe('switch — fall-through to default', () => {
125
+ test('a trailing empty case routes to the default body', async () => {
126
+ const { nodes } = await run(
127
+ [
128
+ ' switch (data.kind) {',
129
+ " case 'a':",
130
+ " await workflow.do('Notify', 'notify', {})",
131
+ ' break',
132
+ " case 'b':",
133
+ ' default:',
134
+ " await workflow.do('Audit', 'audit', {})",
135
+ ' }',
136
+ ' return { ok: true }',
137
+ ].join('\n')
138
+ )
139
+
140
+ const sw = Object.values(nodes).find((n: any) => n.flow === 'switch') as any
141
+ assert.ok(sw, 'a switch node should exist')
142
+ const caseB = (sw.cases ?? []).find((c: any) => c.value === 'b')
143
+ assert.ok(
144
+ caseB,
145
+ `a case falling through to default must not be dropped, got: ${JSON.stringify(sw.cases)}`
146
+ )
147
+ assert.equal(
148
+ caseB.entry,
149
+ sw.defaultEntry,
150
+ 'it should route to the default body it falls into'
151
+ )
152
+ })
153
+ })
154
+
155
+ describe('options — numeric values keep their type', () => {
156
+ test('a numeric retryDelay is not quoted', async () => {
157
+ const { code } = await run(
158
+ [
159
+ " await workflow.do('Charge', 'chargeCard', {}, { retries: 2, retryDelay: 500 })",
160
+ ' return { ok: true }',
161
+ ].join('\n')
162
+ )
163
+
164
+ assert.ok(
165
+ code.includes('retryDelay: 500'),
166
+ `a numeric retryDelay must stay numeric, got:\n${code}`
167
+ )
168
+ })
169
+ })
170
+
171
+ describe('sequential fanout — for..of bodies', () => {
172
+ test('a multi-step for..of body keeps every step', async () => {
173
+ const { nodes } = await run(
174
+ [
175
+ ' for (const u of data.users) {',
176
+ " const c = await workflow.do('Charge', 'chargeCard', { id: u.id })",
177
+ " await workflow.do('Notify', 'notify', { id: c.id })",
178
+ ' }',
179
+ ' return { ok: true }',
180
+ ].join('\n')
181
+ )
182
+
183
+ const fanout = Object.values(nodes).find(
184
+ (n: any) => n.flow === 'fanout'
185
+ ) as any
186
+ assert.ok(fanout, 'a for..of should produce a fanout node')
187
+ assert.notEqual(
188
+ fanout.nodeId,
189
+ fanout.childEntry,
190
+ 'the loop and its body entry must not share an id'
191
+ )
192
+
193
+ const rpcs = Object.values(nodes)
194
+ .filter((n: any) => n.rpcName)
195
+ .map((n: any) => n.rpcName)
196
+ assert.ok(
197
+ rpcs.includes('chargeCard') && rpcs.includes('notify'),
198
+ `both body steps must survive, got: ${JSON.stringify(rpcs)}`
199
+ )
200
+ })
201
+ })
@@ -103,4 +103,64 @@ describe('addWorkflow — Promise.all fanout RPC detection', () => {
103
103
  await rm(rootDir, { recursive: true, force: true })
104
104
  }
105
105
  })
106
+
107
+ test('registers every RPC in a multi-step map body, including a `const`-captured step feeding the next', async () => {
108
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-fanout-multi-'))
109
+ const wfFile = join(rootDir, 'digest.workflow.ts')
110
+ const stepFile = join(rootDir, 'digest.steps.ts')
111
+
112
+ await writeFile(
113
+ stepFile,
114
+ [
115
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
116
+ 'export const getDigestData = pikkuSessionlessFunc({',
117
+ ' func: async ({ logger }) => ({ dealsByStage: {}, totalDealValue: 0 }),',
118
+ '})',
119
+ 'export const sendDigestEmail = pikkuSessionlessFunc({',
120
+ ' func: async ({ logger }) => ({ sent: true }),',
121
+ '})',
122
+ ].join('\n')
123
+ )
124
+ await writeFile(
125
+ wfFile,
126
+ [
127
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
128
+ 'export const dailyDigestWorkflow = pikkuWorkflowFunc(async (_, data, { workflow }) => {',
129
+ ' const users = [{ id: "1", email: "a@b.c" }]',
130
+ ' await Promise.all(',
131
+ ' users.map(async (u) => {',
132
+ ' const digestData = await workflow.do(',
133
+ ' `Get pipeline for ${u.email}`,',
134
+ " 'getDigestData',",
135
+ ' { userId: u.id },',
136
+ ' )',
137
+ " await workflow.do(`Send digest to ${u.email}`, 'sendDigestEmail', {",
138
+ ' email: u.email,',
139
+ ' dealsByStage: digestData.dealsByStage,',
140
+ ' totalDealValue: digestData.totalDealValue,',
141
+ ' })',
142
+ ' }),',
143
+ ' )',
144
+ ' return { sent: users.length }',
145
+ '})',
146
+ ].join('\n')
147
+ )
148
+
149
+ const criticals: Array<{ code: string; message: string }> = []
150
+ try {
151
+ const state = await inspect(makeLogger(criticals), [stepFile, wfFile], {
152
+ rootDir,
153
+ })
154
+ assert.ok(
155
+ state.rpc.invokedFunctions.has('sendDigestEmail'),
156
+ 'sendDigestEmail should be registered'
157
+ )
158
+ assert.ok(
159
+ state.rpc.invokedFunctions.has('getDigestData'),
160
+ 'getDigestData should be registered — a `const`-captured workflow.do inside the map body must not be dropped'
161
+ )
162
+ } finally {
163
+ await rm(rootDir, { recursive: true, force: true })
164
+ }
165
+ })
106
166
  })
@@ -0,0 +1,118 @@
1
+ import { strict as assert } from 'assert'
2
+ import { describe, test } from 'node:test'
3
+ import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { inspect } from '../inspector.js'
7
+ import type { InspectorLogger } from '../types.js'
8
+ import { deserializeDslWorkflow } from '../utils/workflow/dsl/deserialize-dsl-workflow.js'
9
+
10
+ const STEP_FILE = [
11
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
12
+ 'export const chargeCard = pikkuSessionlessFunc({',
13
+ " func: async () => ({ id: 'c' }),",
14
+ '})',
15
+ 'export const refundOrder = pikkuSessionlessFunc({',
16
+ " func: async () => ({ id: 'r' }),",
17
+ '})',
18
+ ].join('\n')
19
+
20
+ const WORKFLOW_BODY = [
21
+ " await workflow.do('Charge', 'chargeCard', { id: data.id }, {",
22
+ ' retries: 3,',
23
+ " onError: 'refundOrder',",
24
+ ' })',
25
+ ' return { ok: true }',
26
+ ].join('\n')
27
+
28
+ async function inspectWorkflow(body: string): Promise<{
29
+ nodes: Record<string, any>
30
+ invoked: string[]
31
+ code: string
32
+ }> {
33
+ const logger = {
34
+ debug: () => {},
35
+ info: () => {},
36
+ warn: () => {},
37
+ error: () => {},
38
+ diagnostic: () => {},
39
+ critical: () => {},
40
+ hasCriticalErrors: () => false,
41
+ } as unknown as InspectorLogger
42
+
43
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-oe-'))
44
+ const stepFile = join(rootDir, 'w.steps.ts')
45
+ const wfFile = join(rootDir, 'w.workflow.ts')
46
+ await writeFile(stepFile, STEP_FILE)
47
+ await writeFile(
48
+ wfFile,
49
+ [
50
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
51
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
52
+ body,
53
+ '})',
54
+ ].join('\n')
55
+ )
56
+ try {
57
+ const state: any = await inspect(logger, [stepFile, wfFile], { rootDir })
58
+ const gm = state.workflows.graphMeta
59
+ const graph = (
60
+ gm instanceof Map ? [...gm.values()] : Object.values(gm)
61
+ )[0] as any
62
+ assert.ok(graph, 'a graph should have been produced')
63
+ return {
64
+ nodes: graph.nodes,
65
+ invoked: [...(state.rpc?.invokedFunctions ?? [])],
66
+ code: deserializeDslWorkflow(graph),
67
+ }
68
+ } finally {
69
+ await rm(rootDir, { recursive: true, force: true })
70
+ }
71
+ }
72
+
73
+ describe('DSL onError — compensation is part of the graph', () => {
74
+ test('the handler becomes a node the failing step points at', async () => {
75
+ const { nodes } = await inspectWorkflow(WORKFLOW_BODY)
76
+
77
+ const charge = Object.values(nodes).find(
78
+ (n: any) => n.rpcName === 'chargeCard'
79
+ ) as any
80
+ assert.ok(charge, 'the charging step should exist')
81
+ assert.ok(
82
+ charge.onError,
83
+ `the step must record its error route so the graph can render it, got: ${JSON.stringify(charge)}`
84
+ )
85
+
86
+ const handlerId = Array.isArray(charge.onError)
87
+ ? charge.onError[0]
88
+ : charge.onError
89
+ const handler = nodes[handlerId]
90
+ assert.ok(
91
+ handler,
92
+ `onError must point at a real node so an edge can be drawn, got id '${handlerId}' in ${JSON.stringify(Object.keys(nodes))}`
93
+ )
94
+ assert.equal(handler.rpcName, 'refundOrder')
95
+ })
96
+
97
+ test('the handler RPC is registered as invoked so it gets wired', async () => {
98
+ const { invoked } = await inspectWorkflow(WORKFLOW_BODY)
99
+
100
+ assert.ok(
101
+ invoked.includes('refundOrder'),
102
+ `the compensation RPC must be wired, got: ${JSON.stringify(invoked)}`
103
+ )
104
+ })
105
+
106
+ test('onError survives regeneration back to code', async () => {
107
+ const { code } = await inspectWorkflow(WORKFLOW_BODY)
108
+
109
+ assert.ok(
110
+ code.includes("onError: 'refundOrder'"),
111
+ `onError must round-trip as an rpc name, got:\n${code}`
112
+ )
113
+ assert.ok(
114
+ code.includes('retries: 3'),
115
+ 'other options must still round-trip'
116
+ )
117
+ })
118
+ })
@@ -0,0 +1,121 @@
1
+ import { strict as assert } from 'assert'
2
+ import { describe, test } from 'node:test'
3
+ import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { inspect } from '../inspector.js'
7
+ import type { InspectorLogger } from '../types.js'
8
+
9
+ const STEP_FILE = [
10
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
11
+ 'export const ship = pikkuSessionlessFunc({',
12
+ " func: async () => ({ id: 's', ok: true }),",
13
+ '})',
14
+ ].join('\n')
15
+
16
+ async function inspectWorkflow(body: string): Promise<{
17
+ nodes: Record<string, any>
18
+ diags: string[]
19
+ }> {
20
+ const diags: string[] = []
21
+ const logger = {
22
+ debug: () => {},
23
+ info: () => {},
24
+ warn: () => {},
25
+ error: () => {},
26
+ diagnostic: (d: any) => diags.push(d?.message ?? String(d)),
27
+ critical: (d: any) => diags.push(d?.message ?? String(d)),
28
+ hasCriticalErrors: () => false,
29
+ } as unknown as InspectorLogger
30
+
31
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-rs-'))
32
+ const stepFile = join(rootDir, 'w.steps.ts')
33
+ const wfFile = join(rootDir, 'w.workflow.ts')
34
+ await writeFile(stepFile, STEP_FILE)
35
+ await writeFile(
36
+ wfFile,
37
+ [
38
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
39
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
40
+ body,
41
+ '})',
42
+ ].join('\n')
43
+ )
44
+ try {
45
+ const state: any = await inspect(logger, [stepFile, wfFile], { rootDir })
46
+ const gm = state.workflows.graphMeta
47
+ const graph = (
48
+ gm instanceof Map ? [...gm.values()] : Object.values(gm)
49
+ )[0] as any
50
+ return { nodes: graph?.nodes ?? {}, diags }
51
+ } finally {
52
+ await rm(rootDir, { recursive: true, force: true })
53
+ }
54
+ }
55
+
56
+ describe('switch extraction — a fall-through case is a real case', () => {
57
+ test("case 'a': case 'b': records both values", async () => {
58
+ const { nodes } = await inspectWorkflow(
59
+ [
60
+ ' switch (data.kind) {',
61
+ " case 'a':",
62
+ " case 'b':",
63
+ " await workflow.do('Ship', 'ship', {})",
64
+ ' break',
65
+ ' }',
66
+ ' return { ok: true }',
67
+ ].join('\n')
68
+ )
69
+
70
+ const switchNode = Object.values(nodes).find(
71
+ (n: any) => n.flow === 'switch'
72
+ ) as any
73
+ assert.ok(switchNode, 'a switch node should exist')
74
+ const values = (switchNode.cases ?? []).map((c: any) => c.value)
75
+ assert.deepEqual(
76
+ values.sort(),
77
+ ['a', 'b'],
78
+ `a fall-through case must not be dropped, got: ${JSON.stringify(switchNode.cases)}`
79
+ )
80
+ const entries = new Set((switchNode.cases ?? []).map((c: any) => c.entry))
81
+ assert.equal(entries.size, 1, 'both cases should share the same entry')
82
+ })
83
+ })
84
+
85
+ describe('return extraction — a spread output is not silently dropped', () => {
86
+ test('return { ...r, extra } keeps the spread', async () => {
87
+ const { nodes } = await inspectWorkflow(
88
+ [
89
+ " const r = await workflow.do('Ship', 'ship', {})",
90
+ ' return { ...r, extra: 1 }',
91
+ ].join('\n')
92
+ )
93
+
94
+ const returnNode = Object.values(nodes).find(
95
+ (n: any) => n.flow === 'return'
96
+ ) as any
97
+ assert.ok(returnNode, 'a return node should exist')
98
+ assert.deepEqual(
99
+ returnNode.spread,
100
+ ['r'],
101
+ `the spread source must be recorded, got: ${JSON.stringify(returnNode)}`
102
+ )
103
+ })
104
+
105
+ test('return r produces a return node', async () => {
106
+ const { nodes } = await inspectWorkflow(
107
+ [" const r = await workflow.do('Ship', 'ship', {})", ' return r'].join(
108
+ '\n'
109
+ )
110
+ )
111
+
112
+ const returnNode = Object.values(nodes).find(
113
+ (n: any) => n.flow === 'return'
114
+ ) as any
115
+ assert.ok(
116
+ returnNode,
117
+ `returning a variable must still produce a return node, got: ${JSON.stringify(nodes)}`
118
+ )
119
+ assert.deepEqual(returnNode.spread, ['r'])
120
+ })
121
+ })
@@ -0,0 +1,163 @@
1
+ import { strict as assert } from 'assert'
2
+ import { describe, test } from 'node:test'
3
+ import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { inspect } from '../inspector.js'
7
+ import type { InspectorLogger } from '../types.js'
8
+
9
+ function makeLogger(diags: string[]): InspectorLogger {
10
+ return {
11
+ debug: () => {},
12
+ info: () => {},
13
+ warn: () => {},
14
+ error: () => {},
15
+ diagnostic: ({ code, message }: any) => {
16
+ diags.push(`${code}: ${message}`)
17
+ },
18
+ critical: (code: any, message: string) => {
19
+ diags.push(`${code}: ${message}`)
20
+ },
21
+ hasCriticalErrors: () => false,
22
+ }
23
+ }
24
+
25
+ const STEP_FILE = [
26
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
27
+ 'export const listUsers = pikkuSessionlessFunc({',
28
+ " func: async () => ({ users: [{ id: '1', email: 'a@b.c' }] }),",
29
+ '})',
30
+ 'export const rpcA = pikkuSessionlessFunc({',
31
+ " func: async () => ({ id: 'a' }),",
32
+ '})',
33
+ 'export const rpcB = pikkuSessionlessFunc({',
34
+ " func: async () => ({ id: 'b' }),",
35
+ '})',
36
+ 'export const rpcC = pikkuSessionlessFunc({',
37
+ ' func: async () => ({ ok: true }),',
38
+ '})',
39
+ ].join('\n')
40
+
41
+ async function inspectWorkflow(body: string) {
42
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-drops-'))
43
+ const stepFile = join(rootDir, 'w.steps.ts')
44
+ const wfFile = join(rootDir, 'w.workflow.ts')
45
+ await writeFile(stepFile, STEP_FILE)
46
+ await writeFile(
47
+ wfFile,
48
+ [
49
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
50
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
51
+ body,
52
+ '})',
53
+ ].join('\n')
54
+ )
55
+ const diags: string[] = []
56
+ try {
57
+ const state: any = await inspect(makeLogger(diags), [stepFile, wfFile], {
58
+ rootDir,
59
+ })
60
+ const metaMap = state.workflows.meta
61
+ const meta = (
62
+ metaMap instanceof Map ? [...metaMap.values()] : Object.values(metaMap)
63
+ )[0] as any
64
+ return {
65
+ invoked: [...state.rpc.invokedFunctions] as string[],
66
+ steps: (meta?.steps ?? []) as any[],
67
+ diags,
68
+ }
69
+ } finally {
70
+ await rm(rootDir, { recursive: true, force: true })
71
+ }
72
+ }
73
+
74
+ describe('DSL extraction — constructs that must not be silently dropped', () => {
75
+ test('a property access on the loop item (u.id) survives as an item input', async () => {
76
+ const { steps } = await inspectWorkflow(
77
+ [
78
+ " const audience = await workflow.do('collect', 'listUsers', {})",
79
+ ' await Promise.all(',
80
+ ' audience.users.map(async (u) => {',
81
+ " await workflow.do('send', 'rpcC', { userId: u.id, tag: 'x' })",
82
+ ' })',
83
+ ' )',
84
+ ' return { ok: true }',
85
+ ].join('\n')
86
+ )
87
+
88
+ const fanout = steps.find((s) => s.type === 'fanout')
89
+ assert.ok(fanout, 'fanout step should be extracted')
90
+ const send = fanout.body?.[0]
91
+ assert.ok(send, 'the fanout body step should be extracted')
92
+ assert.deepEqual(
93
+ send.inputs?.userId,
94
+ { from: 'item', path: 'id' },
95
+ `u.id must map to the loop item, got: ${JSON.stringify(send.inputs)}`
96
+ )
97
+ assert.deepEqual(
98
+ send.inputs?.tag,
99
+ { from: 'literal', value: 'x' },
100
+ 'sibling literal inputs must still survive'
101
+ )
102
+ })
103
+
104
+ test('array destructuring of a Promise.all group keeps both steps and binds each result', async () => {
105
+ const { invoked, steps } = await inspectWorkflow(
106
+ [
107
+ ' const [org, user] = await Promise.all([',
108
+ " workflow.do('create org', 'rpcA', {}),",
109
+ " workflow.do('create user', 'rpcB', {}),",
110
+ ' ])',
111
+ ' return { orgId: org.id, userId: user.id }',
112
+ ].join('\n')
113
+ )
114
+
115
+ assert.ok(invoked.includes('rpcA'), 'rpcA must not be dropped')
116
+ assert.ok(invoked.includes('rpcB'), 'rpcB must not be dropped')
117
+
118
+ const parallel = steps.find((s) => s.type === 'parallel')
119
+ assert.ok(parallel, 'a parallel step should be extracted')
120
+ assert.deepEqual(
121
+ parallel.children?.map((c: any) => c.outputVar),
122
+ ['org', 'user'],
123
+ 'each destructured name should bind to its matching child step'
124
+ )
125
+
126
+ const ret = steps.find((s) => s.type === 'return')
127
+ assert.deepEqual(
128
+ ret?.outputs?.orgId,
129
+ { from: 'outputVar', name: 'org', path: 'id' },
130
+ 'org.id must resolve to the bound step output, not degrade to a trigger input'
131
+ )
132
+ })
133
+
134
+ test('object destructuring of a step result reports a diagnostic instead of silently dropping', async () => {
135
+ const { invoked, diags } = await inspectWorkflow(
136
+ [
137
+ " const { id } = await workflow.do('s', 'rpcA', {})",
138
+ ' return { id }',
139
+ ].join('\n')
140
+ )
141
+
142
+ const dropped = !invoked.includes('rpcA')
143
+ assert.ok(
144
+ !dropped || diags.length > 0,
145
+ 'if the step is dropped the user must be told — silence is the bug'
146
+ )
147
+ })
148
+
149
+ test('a brace-less for-of body is still extracted as a sequential fanout', async () => {
150
+ const { invoked, steps } = await inspectWorkflow(
151
+ [
152
+ ' for (const email of data.emails)',
153
+ " await workflow.do('invite', 'rpcA', { email })",
154
+ ' return { ok: true }',
155
+ ].join('\n')
156
+ )
157
+
158
+ assert.ok(invoked.includes('rpcA'), 'rpcA must not be dropped')
159
+ const fanout = steps.find((s) => s.type === 'fanout')
160
+ assert.ok(fanout, 'a brace-less for-of should still produce a fanout step')
161
+ assert.equal(fanout.mode, 'sequential')
162
+ })
163
+ })