@pikku/inspector 0.12.42 → 0.12.44

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 (126) hide show
  1. package/CHANGELOG.md +266 -0
  2. package/dist/add/add-addon-bans.js +1 -0
  3. package/dist/add/add-ai-agent.d.ts +3 -1
  4. package/dist/add/add-ai-agent.js +82 -0
  5. package/dist/add/add-credential.js +5 -1
  6. package/dist/add/add-functions.js +10 -9
  7. package/dist/add/add-gateway.js +3 -0
  8. package/dist/add/add-http-route.js +2 -2
  9. package/dist/add/add-keyed-wiring.js +2 -0
  10. package/dist/add/add-mcp-prompt.js +0 -1
  11. package/dist/add/add-mcp-resource.js +0 -1
  12. package/dist/add/add-permission.d.ts +1 -1
  13. package/dist/add/add-permission.js +3 -177
  14. package/dist/add/add-queue-worker.js +3 -0
  15. package/dist/add/add-schedule.js +3 -0
  16. package/dist/add/add-scope.d.ts +2 -0
  17. package/dist/add/add-scope.js +146 -0
  18. package/dist/add/add-trigger.js +3 -0
  19. package/dist/add/add-wire-remote-addon.d.ts +10 -0
  20. package/dist/add/add-wire-remote-addon.js +66 -0
  21. package/dist/add/add-workflow-graph.js +32 -3
  22. package/dist/add/add-workflow.js +9 -4
  23. package/dist/error-codes.d.ts +3 -0
  24. package/dist/error-codes.js +4 -0
  25. package/dist/inspector.js +11 -5
  26. package/dist/types.d.ts +19 -13
  27. package/dist/utils/custom-types-generator.js +2 -1
  28. package/dist/utils/ensure-function-metadata.d.ts +15 -0
  29. package/dist/utils/ensure-function-metadata.js +24 -0
  30. package/dist/utils/filter-inspector-state.js +9 -0
  31. package/dist/utils/get-property-value.d.ts +7 -2
  32. package/dist/utils/get-property-value.js +48 -1
  33. package/dist/utils/load-addon-functions-meta.js +81 -6
  34. package/dist/utils/permissions.d.ts +1 -21
  35. package/dist/utils/permissions.js +17 -108
  36. package/dist/utils/post-process.d.ts +30 -1
  37. package/dist/utils/post-process.js +150 -72
  38. package/dist/utils/schema-generator.js +19 -4
  39. package/dist/utils/serialize-inspector-state.d.ts +13 -8
  40. package/dist/utils/serialize-inspector-state.js +12 -6
  41. package/dist/utils/serialize-permissions-groups-meta.d.ts +0 -2
  42. package/dist/utils/serialize-permissions-groups-meta.js +0 -26
  43. package/dist/utils/workflow/derive-workflow-plan.js +9 -3
  44. package/dist/utils/workflow/dsl/deserialize-dsl-workflow.js +373 -91
  45. package/dist/utils/workflow/dsl/extract-dsl-workflow.js +274 -100
  46. package/dist/utils/workflow/dsl/patterns.d.ts +4 -0
  47. package/dist/utils/workflow/dsl/patterns.js +11 -0
  48. package/dist/utils/workflow/graph/convert-dsl-to-graph.js +76 -5
  49. package/dist/utils/workflow/graph/finalize-workflows.d.ts +7 -0
  50. package/dist/utils/workflow/graph/finalize-workflows.js +11 -2
  51. package/dist/utils/workflow/graph/serialize-workflow-graph.d.ts +2 -0
  52. package/dist/utils/workflow/graph/serialize-workflow-graph.js +4 -0
  53. package/dist/utils/workflow/graph/workflow-graph.types.d.ts +8 -4
  54. package/dist/visit.js +4 -0
  55. package/package.json +3 -3
  56. package/run-tests.sh +1 -1
  57. package/src/add/add-addon-bans.ts +1 -0
  58. package/src/add/add-ai-agent.test.ts +87 -0
  59. package/src/add/add-ai-agent.ts +122 -0
  60. package/src/add/add-credential.ts +8 -0
  61. package/src/add/add-functions.ts +10 -12
  62. package/src/add/add-gateway.ts +11 -0
  63. package/src/add/add-http-route.ts +2 -8
  64. package/src/add/add-keyed-wiring.ts +2 -0
  65. package/src/add/add-mcp-prompt.ts +0 -1
  66. package/src/add/add-mcp-resource.ts +0 -1
  67. package/src/add/add-permission.ts +4 -242
  68. package/src/add/add-queue-worker.ts +11 -0
  69. package/src/add/add-schedule.ts +11 -0
  70. package/src/add/add-scope.test.ts +346 -0
  71. package/src/add/add-scope.ts +225 -0
  72. package/src/add/add-trigger.ts +11 -0
  73. package/src/add/add-wire-remote-addon.ts +77 -0
  74. package/src/add/add-workflow-edge-cases.test.ts +201 -0
  75. package/src/add/add-workflow-fanout.test.ts +60 -0
  76. package/src/add/add-workflow-graph-input.test.ts +94 -0
  77. package/src/add/add-workflow-graph-notes.test.ts +74 -0
  78. package/src/add/add-workflow-graph.ts +35 -3
  79. package/src/add/add-workflow-on-error.test.ts +118 -0
  80. package/src/add/add-workflow-return-switch.test.ts +121 -0
  81. package/src/add/add-workflow-silent-drops.test.ts +163 -0
  82. package/src/add/add-workflow-sleep-expression.test.ts +97 -0
  83. package/src/add/add-workflow.ts +9 -4
  84. package/src/add/config-value-metadata.test.ts +169 -0
  85. package/src/add/inline-wiring-function.test.ts +104 -0
  86. package/src/add/validate-workflow-graph-addons.test.ts +102 -0
  87. package/src/error-codes.ts +5 -0
  88. package/src/inspector.ts +14 -4
  89. package/src/types.ts +16 -17
  90. package/src/utils/custom-types-generator.test.ts +26 -1
  91. package/src/utils/custom-types-generator.ts +2 -1
  92. package/src/utils/ensure-function-metadata.ts +42 -0
  93. package/src/utils/filter-inspector-state.test.ts +0 -2
  94. package/src/utils/filter-inspector-state.ts +9 -0
  95. package/src/utils/get-property-value.test.ts +141 -0
  96. package/src/utils/get-property-value.ts +62 -1
  97. package/src/utils/load-addon-functions-meta.test.ts +157 -0
  98. package/src/utils/load-addon-functions-meta.ts +94 -6
  99. package/src/utils/load-addon-scopes.test.ts +138 -0
  100. package/src/utils/permissions.test.ts +7 -232
  101. package/src/utils/permissions.ts +20 -160
  102. package/src/utils/post-process.test.ts +269 -4
  103. package/src/utils/post-process.ts +216 -95
  104. package/src/utils/schema-generator.test.ts +164 -0
  105. package/src/utils/schema-generator.ts +26 -3
  106. package/src/utils/serialize-inspector-state.ts +22 -25
  107. package/src/utils/serialize-permissions-groups-meta.ts +0 -28
  108. package/src/utils/workflow/derive-workflow-plan.test.ts +41 -3
  109. package/src/utils/workflow/derive-workflow-plan.ts +11 -4
  110. package/src/utils/workflow/dsl/deserialize-dsl-workflow.ts +480 -119
  111. package/src/utils/workflow/dsl/deserialize-fanout-body.test.ts +71 -0
  112. package/src/utils/workflow/dsl/deserialize-fanout-fidelity.test.ts +99 -0
  113. package/src/utils/workflow/dsl/deserialize-graph-next.test.ts +161 -0
  114. package/src/utils/workflow/dsl/deserialize-roundtrip-bindings.test.ts +121 -0
  115. package/src/utils/workflow/dsl/deserialize-roundtrip-values.test.ts +116 -0
  116. package/src/utils/workflow/dsl/deserialize-roundtrip.test.ts +145 -0
  117. package/src/utils/workflow/dsl/extract-dsl-workflow.ts +311 -105
  118. package/src/utils/workflow/dsl/patterns.ts +18 -0
  119. package/src/utils/workflow/graph/convert-dsl-to-graph.ts +82 -10
  120. package/src/utils/workflow/graph/fanout-node-identity.test.ts +105 -0
  121. package/src/utils/workflow/graph/finalize-workflows.test.ts +50 -0
  122. package/src/utils/workflow/graph/finalize-workflows.ts +13 -2
  123. package/src/utils/workflow/graph/serialize-workflow-graph.ts +6 -0
  124. package/src/utils/workflow/graph/workflow-graph.types.ts +11 -2
  125. package/src/visit.ts +4 -0
  126. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,71 @@
1
+ import { strict as assert } from 'assert'
2
+ import { describe, test } from 'node:test'
3
+ import { deserializeDslWorkflow } from './deserialize-dsl-workflow.js'
4
+
5
+ /**
6
+ * A fanout whose per-iteration body is two chained steps: the first captures
7
+ * its result into `digestData`, the second consumes it.
8
+ */
9
+ const MULTI_STEP_FANOUT_GRAPH: any = {
10
+ name: 'dailyDigestWorkflow',
11
+ pikkuFuncId: 'dailyDigestWorkflow',
12
+ source: 'dsl',
13
+ entryNodeIds: ['start'],
14
+ nodes: {
15
+ start: {
16
+ nodeId: 'start',
17
+ flow: 'fanout',
18
+ sourceVar: 'users',
19
+ itemVar: 'u',
20
+ mode: 'parallel',
21
+ childEntry: 'start_item_0',
22
+ },
23
+ start_item_0: {
24
+ nodeId: 'start_item_0',
25
+ rpcName: 'getDigestData',
26
+ stepName: 'Get pipeline',
27
+ outputVar: 'digestData',
28
+ input: { userId: { $ref: 'u.id' } },
29
+ next: 'start_item_1',
30
+ },
31
+ start_item_1: {
32
+ nodeId: 'start_item_1',
33
+ rpcName: 'sendDigestEmail',
34
+ stepName: 'Send digest',
35
+ input: { dealsByStage: { $ref: 'digestData.dealsByStage' } },
36
+ },
37
+ },
38
+ }
39
+
40
+ describe('deserializeDslWorkflow — multi-step fanout body', () => {
41
+ test('emits every step in the map body, not just the entry step', () => {
42
+ const code = deserializeDslWorkflow(MULTI_STEP_FANOUT_GRAPH)
43
+
44
+ assert.ok(
45
+ code.includes("'getDigestData'"),
46
+ 'the entry step of the body should be emitted'
47
+ )
48
+ assert.ok(
49
+ code.includes("'sendDigestEmail'"),
50
+ 'steps chained after the entry step must not be dropped'
51
+ )
52
+ })
53
+
54
+ test('re-emits a captured step result as a `const` binding', () => {
55
+ const code = deserializeDslWorkflow(MULTI_STEP_FANOUT_GRAPH)
56
+
57
+ assert.ok(
58
+ code.includes('const digestData = await workflow.do('),
59
+ `a step with an outputVar should round-trip as a const binding, got:\n${code}`
60
+ )
61
+ })
62
+
63
+ test('uses a block-bodied map callback so multiple statements are valid', () => {
64
+ const code = deserializeDslWorkflow(MULTI_STEP_FANOUT_GRAPH)
65
+
66
+ assert.ok(
67
+ code.includes('users.map(async (u) => {'),
68
+ `map callback must be block-bodied, got:\n${code}`
69
+ )
70
+ })
71
+ })
@@ -0,0 +1,99 @@
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 './deserialize-dsl-workflow.js'
9
+
10
+ function makeLogger(): InspectorLogger {
11
+ return {
12
+ debug: () => {},
13
+ info: () => {},
14
+ warn: () => {},
15
+ error: () => {},
16
+ diagnostic: () => {},
17
+ critical: () => {},
18
+ hasCriticalErrors: () => false,
19
+ } as any
20
+ }
21
+
22
+ const STEP_FILE = [
23
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
24
+ 'export const notify = pikkuSessionlessFunc({',
25
+ " func: async () => ({ id: 'n' }),",
26
+ '})',
27
+ ].join('\n')
28
+
29
+ async function roundTrip(body: string): Promise<string> {
30
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-rtf-'))
31
+ const stepFile = join(rootDir, 'w.steps.ts')
32
+ const wfFile = join(rootDir, 'w.workflow.ts')
33
+ await writeFile(stepFile, STEP_FILE)
34
+ await writeFile(
35
+ wfFile,
36
+ [
37
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
38
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
39
+ body,
40
+ '})',
41
+ ].join('\n')
42
+ )
43
+ try {
44
+ const state: any = await inspect(makeLogger(), [stepFile, wfFile], {
45
+ rootDir,
46
+ })
47
+ const gm = state.workflows.graphMeta
48
+ const graph = (
49
+ gm instanceof Map ? [...gm.values()] : Object.values(gm)
50
+ )[0] as any
51
+ assert.ok(graph, 'a graph should have been produced')
52
+ return deserializeDslWorkflow(graph)
53
+ } finally {
54
+ await rm(rootDir, { recursive: true, force: true })
55
+ }
56
+ }
57
+
58
+ describe('DSL round-trip — a fanout body keeps everything in it', () => {
59
+ test('a step option inside a fanout body survives', async () => {
60
+ const code = await roundTrip(
61
+ [
62
+ ' await Promise.all(',
63
+ ' data.users.map(async (u: any) => {',
64
+ " await workflow.do('Notify', 'notify', { id: u.id }, { retries: 3 })",
65
+ ' })',
66
+ ' )',
67
+ ' return { ok: true }',
68
+ ].join('\n')
69
+ )
70
+
71
+ assert.ok(
72
+ code.includes('retries: 3'),
73
+ `retries must not be dropped from a fanout body step, got:\n${code}`
74
+ )
75
+ })
76
+
77
+ test('a non-RPC step inside a fanout body survives', async () => {
78
+ const code = await roundTrip(
79
+ [
80
+ ' await Promise.all(',
81
+ ' data.users.map(async (u: any) => {',
82
+ " await workflow.sleep('Stagger', '1s')",
83
+ " await workflow.do('Notify', 'notify', { id: u.id })",
84
+ ' })',
85
+ ' )',
86
+ ' return { ok: true }',
87
+ ].join('\n')
88
+ )
89
+
90
+ assert.ok(
91
+ code.includes("workflow.sleep('Stagger', '1s')"),
92
+ `a sleep inside a fanout body must not be deleted, got:\n${code}`
93
+ )
94
+ assert.ok(
95
+ code.includes("'notify'"),
96
+ `the rpc step must still be there, got:\n${code}`
97
+ )
98
+ })
99
+ })
@@ -0,0 +1,161 @@
1
+ import { strict as assert } from 'assert'
2
+ import { describe, test } from 'node:test'
3
+ import {
4
+ deserializeDslWorkflow,
5
+ deserializeGraphWorkflow,
6
+ } from './deserialize-dsl-workflow.js'
7
+ import type { SerializedWorkflowGraph } from '../graph/workflow-graph.types.js'
8
+
9
+ function graph(
10
+ nodes: SerializedWorkflowGraph['nodes']
11
+ ): SerializedWorkflowGraph {
12
+ return {
13
+ name: 'wf',
14
+ pikkuFuncId: 'wf',
15
+ source: 'graph',
16
+ nodes,
17
+ entryNodeIds: ['start'],
18
+ }
19
+ }
20
+
21
+ describe('graph round-trip — a non-string next must not be stringified', () => {
22
+ test('a parallel fan-out keeps every target', () => {
23
+ const code = deserializeGraphWorkflow(
24
+ graph({
25
+ start: { nodeId: 'start', rpcName: 'begin', next: ['left', 'right'] },
26
+ left: { nodeId: 'left', rpcName: 'goLeft' },
27
+ right: { nodeId: 'right', rpcName: 'goRight' },
28
+ })
29
+ )
30
+
31
+ assert.ok(
32
+ !code.includes("next: 'left,right'"),
33
+ `an array next must not be joined into a single bogus node id, got:\n${code}`
34
+ )
35
+ assert.ok(
36
+ /next: \['left', 'right'\]/.test(code),
37
+ `both parallel targets must survive, got:\n${code}`
38
+ )
39
+ })
40
+
41
+ test('key-based branching keeps its routing table', () => {
42
+ const code = deserializeGraphWorkflow(
43
+ graph({
44
+ start: {
45
+ nodeId: 'start',
46
+ rpcName: 'classify',
47
+ next: { approved: 'ship', rejected: 'refund' },
48
+ },
49
+ ship: { nodeId: 'ship', rpcName: 'ship' },
50
+ refund: { nodeId: 'refund', rpcName: 'refund' },
51
+ })
52
+ )
53
+
54
+ assert.ok(
55
+ !code.includes('[object Object]'),
56
+ `a record next must not be stringified, got:\n${code}`
57
+ )
58
+ assert.ok(
59
+ code.includes("approved: 'ship'") && code.includes("rejected: 'refund'"),
60
+ `every branch key must survive, got:\n${code}`
61
+ )
62
+ })
63
+ })
64
+
65
+ describe('DSL round-trip — an unbound array step must not emit a broken binding', () => {
66
+ test('a filter with no outputVar does not declare `const undefined`', () => {
67
+ const code = deserializeDslWorkflow({
68
+ ...graph({
69
+ start: {
70
+ nodeId: 'start',
71
+ flow: 'filter',
72
+ sourceVar: 'data.users',
73
+ itemVar: 'u',
74
+ condition: { type: 'simple', expression: 'u.active' },
75
+ },
76
+ }),
77
+ source: 'dsl',
78
+ })
79
+
80
+ assert.ok(
81
+ !code.includes('const undefined'),
82
+ `an unbound filter must not emit a syntactically invalid binding, got:\n${code}`
83
+ )
84
+ })
85
+
86
+ test('an unbound some/every does not declare `const undefined`', () => {
87
+ const code = deserializeDslWorkflow({
88
+ ...graph({
89
+ start: {
90
+ nodeId: 'start',
91
+ flow: 'arrayPredicate',
92
+ mode: 'some',
93
+ sourceVar: 'data.users',
94
+ itemVar: 'u',
95
+ condition: { type: 'simple', expression: 'u.active' },
96
+ },
97
+ }),
98
+ source: 'dsl',
99
+ })
100
+
101
+ assert.ok(
102
+ !code.includes('const undefined'),
103
+ `an unbound predicate must not emit a syntactically invalid binding, got:\n${code}`
104
+ )
105
+ })
106
+ })
107
+
108
+ describe('graph round-trip — runtime-honoured node config must survive', () => {
109
+ test('onError routing is preserved', () => {
110
+ const code = deserializeGraphWorkflow(
111
+ graph({
112
+ start: {
113
+ nodeId: 'start',
114
+ rpcName: 'charge',
115
+ next: 'ship',
116
+ onError: 'refund',
117
+ },
118
+ ship: { nodeId: 'ship', rpcName: 'ship' },
119
+ refund: { nodeId: 'refund', rpcName: 'refund' },
120
+ })
121
+ )
122
+
123
+ assert.ok(
124
+ code.includes("onError: 'refund'"),
125
+ `error routing is honoured at runtime and must not be dropped, got:\n${code}`
126
+ )
127
+ })
128
+
129
+ test('retries and retryDelay are preserved', () => {
130
+ const code = deserializeGraphWorkflow(
131
+ graph({
132
+ start: {
133
+ nodeId: 'start',
134
+ rpcName: 'charge',
135
+ options: { retries: 3, retryDelay: '5s' },
136
+ },
137
+ })
138
+ )
139
+
140
+ assert.ok(
141
+ code.includes('retries: 3'),
142
+ `retries must not be dropped, got:\n${code}`
143
+ )
144
+ assert.ok(
145
+ code.includes("retryDelay: '5s'"),
146
+ `retryDelay must not be dropped, got:\n${code}`
147
+ )
148
+ })
149
+
150
+ test('graph-level notes are preserved', () => {
151
+ const code = deserializeGraphWorkflow({
152
+ ...graph({ start: { nodeId: 'start', rpcName: 'charge' } }),
153
+ notes: ['imported from n8n'],
154
+ })
155
+
156
+ assert.ok(
157
+ code.includes('imported from n8n'),
158
+ `graph notes must not be dropped, got:\n${code}`
159
+ )
160
+ })
161
+ })
@@ -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
+ import { deserializeDslWorkflow } from './deserialize-dsl-workflow.js'
9
+
10
+ function makeLogger(): InspectorLogger {
11
+ return {
12
+ debug: () => {},
13
+ info: () => {},
14
+ warn: () => {},
15
+ error: () => {},
16
+ diagnostic: () => {},
17
+ critical: () => {},
18
+ hasCriticalErrors: () => false,
19
+ } as any
20
+ }
21
+
22
+ const STEP_FILE = [
23
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
24
+ 'export const createOrg = pikkuSessionlessFunc({',
25
+ " func: async () => ({ id: 'o' }),",
26
+ '})',
27
+ 'export const createUser = pikkuSessionlessFunc({',
28
+ " func: async () => ({ id: 'u' }),",
29
+ '})',
30
+ 'export const ship = pikkuSessionlessFunc({',
31
+ " func: async () => ({ id: 's' }),",
32
+ '})',
33
+ ].join('\n')
34
+
35
+ async function roundTrip(body: string): Promise<string> {
36
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-rtb-'))
37
+ const stepFile = join(rootDir, 'w.steps.ts')
38
+ const wfFile = join(rootDir, 'w.workflow.ts')
39
+ await writeFile(stepFile, STEP_FILE)
40
+ await writeFile(
41
+ wfFile,
42
+ [
43
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
44
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
45
+ body,
46
+ '})',
47
+ ].join('\n')
48
+ )
49
+ try {
50
+ const state: any = await inspect(makeLogger(), [stepFile, wfFile], {
51
+ rootDir,
52
+ })
53
+ const gm = state.workflows.graphMeta
54
+ const graph = (
55
+ gm instanceof Map ? [...gm.values()] : Object.values(gm)
56
+ )[0] as any
57
+ assert.ok(graph, 'a graph should have been produced')
58
+ return deserializeDslWorkflow(graph)
59
+ } finally {
60
+ await rm(rootDir, { recursive: true, force: true })
61
+ }
62
+ }
63
+
64
+ describe('DSL round-trip — regenerated code must still bind its variables', () => {
65
+ test('a Promise.all group keeps the destructured result bindings', async () => {
66
+ const code = await roundTrip(
67
+ [
68
+ ' const [org, user] = await Promise.all([',
69
+ " workflow.do('create org', 'createOrg', {}),",
70
+ " workflow.do('create user', 'createUser', {}),",
71
+ ' ])',
72
+ ' return { orgId: org.id, userId: user.id }',
73
+ ].join('\n')
74
+ )
75
+
76
+ assert.ok(
77
+ /const \[org, user\] = await Promise\.all\(\[/.test(code),
78
+ `parallel results must stay bound or the regenerated code cannot compile, got:\n${code}`
79
+ )
80
+ })
81
+
82
+ test('a step result bound inside a branch is hoisted so later references compile', async () => {
83
+ const code = await roundTrip(
84
+ [
85
+ ' let shipment: { id: string } | undefined',
86
+ ' if (data.needsShipping) {',
87
+ " shipment = await workflow.do('Ship', 'ship', {})",
88
+ ' }',
89
+ ' return { shipmentId: shipment }',
90
+ ].join('\n')
91
+ )
92
+
93
+ const declaredOutside = /let shipment/.test(code)
94
+ const constInsideBranch = /\{\s*\n\s*const shipment =/.test(code)
95
+ assert.ok(
96
+ declaredOutside && !constInsideBranch,
97
+ `a variable referenced after the branch must be hoisted, not const-scoped inside it, got:\n${code}`
98
+ )
99
+ })
100
+ })
101
+
102
+ describe('DSL round-trip — step names are data, not structure', () => {
103
+ test('a step whose name contains a structural substring is not deleted', async () => {
104
+ const code = await roundTrip(
105
+ [
106
+ " await workflow.do('sync_case_files', 'createOrg', {})",
107
+ " await workflow.do('map_item_batch', 'createUser', {})",
108
+ ' return { ok: true }',
109
+ ].join('\n')
110
+ )
111
+
112
+ assert.ok(
113
+ code.includes("'sync_case_files'"),
114
+ `a top-level step named like a structural node id must survive, got:\n${code}`
115
+ )
116
+ assert.ok(
117
+ code.includes("'map_item_batch'"),
118
+ `a top-level step named like a structural node id must survive, got:\n${code}`
119
+ )
120
+ })
121
+ })
@@ -0,0 +1,116 @@
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 './deserialize-dsl-workflow.js'
9
+
10
+ function makeLogger(): InspectorLogger {
11
+ return {
12
+ debug: () => {},
13
+ info: () => {},
14
+ warn: () => {},
15
+ error: () => {},
16
+ diagnostic: () => {},
17
+ critical: () => {},
18
+ hasCriticalErrors: () => false,
19
+ } as any
20
+ }
21
+
22
+ const STEP_FILE = [
23
+ "import { pikkuSessionlessFunc } from '@pikku/core'",
24
+ 'export const ship = pikkuSessionlessFunc({',
25
+ " func: async () => ({ id: 's' }),",
26
+ '})',
27
+ ].join('\n')
28
+
29
+ async function roundTrip(body: string): Promise<string> {
30
+ const rootDir = await mkdtemp(join(tmpdir(), 'pikku-rtv-'))
31
+ const stepFile = join(rootDir, 'w.steps.ts')
32
+ const wfFile = join(rootDir, 'w.workflow.ts')
33
+ await writeFile(stepFile, STEP_FILE)
34
+ await writeFile(
35
+ wfFile,
36
+ [
37
+ "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
38
+ 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
39
+ body,
40
+ '})',
41
+ ].join('\n')
42
+ )
43
+ try {
44
+ const state: any = await inspect(makeLogger(), [stepFile, wfFile], {
45
+ rootDir,
46
+ })
47
+ const gm = state.workflows.graphMeta
48
+ const graph = (
49
+ gm instanceof Map ? [...gm.values()] : Object.values(gm)
50
+ )[0] as any
51
+ assert.ok(graph, 'a graph should have been produced')
52
+ return deserializeDslWorkflow(graph)
53
+ } finally {
54
+ await rm(rootDir, { recursive: true, force: true })
55
+ }
56
+ }
57
+
58
+ describe('DSL round-trip — values keep their type', () => {
59
+ test('a numeric sleep duration is not turned into a string', async () => {
60
+ const code = await roundTrip(
61
+ [" await workflow.sleep('Wait', 5000)", ' return { ok: true }'].join(
62
+ '\n'
63
+ )
64
+ )
65
+
66
+ assert.ok(
67
+ code.includes("workflow.sleep('Wait', 5000)"),
68
+ `a numeric duration must stay numeric, got:\n${code}`
69
+ )
70
+ })
71
+
72
+ test('a string sleep duration keeps its quotes', async () => {
73
+ const code = await roundTrip(
74
+ [" await workflow.sleep('Wait', '5s')", ' return { ok: true }'].join(
75
+ '\n'
76
+ )
77
+ )
78
+
79
+ assert.ok(
80
+ code.includes("workflow.sleep('Wait', '5s')"),
81
+ `a string duration must stay quoted, got:\n${code}`
82
+ )
83
+ })
84
+
85
+ test('an assignment expression is not turned into a string literal', async () => {
86
+ const code = await roundTrip(
87
+ [' let count = 0', ' count = count + 1', ' return { count }'].join(
88
+ '\n'
89
+ )
90
+ )
91
+
92
+ assert.ok(
93
+ code.includes('count = count + 1'),
94
+ `an assigned expression must be regenerated as an expression, got:\n${code}`
95
+ )
96
+ assert.ok(
97
+ !code.includes("count = 'count + 1'"),
98
+ 'an expression must not be quoted into a string literal'
99
+ )
100
+ })
101
+
102
+ test('an assigned string literal keeps its quotes', async () => {
103
+ const code = await roundTrip(
104
+ [
105
+ " let status = 'pending'",
106
+ " status = 'done'",
107
+ ' return { status }',
108
+ ].join('\n')
109
+ )
110
+
111
+ assert.ok(
112
+ code.includes("status = 'done'"),
113
+ `a string literal must stay a string literal, got:\n${code}`
114
+ )
115
+ })
116
+ })