1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("no-direct-variable-mutation-in-workflow", rule, {
7 valid: [
8 // No mutations or recomputations in the constructor.
9 {
10 code: `
11 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
12 const step1 = createStep("s1", () => 1)
13 createWorkflow("my-workflow", (input) => {
14 const result = step1(input)
15 return result
16 })
17 `,
18 },
19 // Mutation/recomputation INSIDE a nested transform callback is fine.
20 {
21 code: `
22 import { createWorkflow, createStep, transform } from "@medusajs/framework/workflows-sdk"
23 const step1 = createStep("s1", () => ({ count: 1 }))
24 createWorkflow("my-workflow", (input) => {
25 const result = step1(input)
26 const doubled = transform({ result }, (data) => {
27 data.result.count = data.result.count + 1
28 return \`count is \${data.result.count}\`
29 })
30 return doubled
31 })
32 `,
33 },
34 // Mutation inside a nested createStep callback is fine (it's runtime, not def-time).
35 {
36 code: `
37 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
38 const step1 = createStep("s1", () => ({ count: 1 }))
39 createWorkflow("my-workflow", (input) => {
40 const result = step1(input)
41 const step2 = createStep("s2", (data) => {
42 data.count = data.count + 1
43 return data
44 })
45 const final = step2(result)
46 return final
47 })
48 `,
49 },
50 // Pass-through usage is fine (no mutation, no recomputation).
51 {
52 code: `
53 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
54 const step1 = createStep("s1", () => 1)
55 const step2 = createStep("s2", (data) => data + 1)
56 createWorkflow("my-workflow", (input) => {
57 const result = step1(input)
58 const next = step2(result)
59 return next
60 })
61 `,
62 },
63 // Template literal with no interpolation is fine.
64 {
65 code: `
66 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
67 const step1 = createStep("s1", () => 1)
68 createWorkflow("my-workflow", (input) => {
69 const result = step1(\`hello world\`)
70 return result
71 })
72 `,
73 },
74 // Not the workflows-sdk import — nothing tracked.
75 {
76 code: `
77 import { createWorkflow, createStep } from "some-other-lib"
78 const step1 = createStep("s1", () => 1)
79 createWorkflow("my-workflow", (input) => {
80 const result = step1(input)
81 result.value = 5
82 const next = result.value + 1
83 return result
84 })
85 `,
86 },
87 ],
88 invalid: [
89 // Object property assignment.
90 {
91 code: `
92 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
93 const step1 = createStep("s1", () => ({ count: 1 }))
94 createWorkflow("my-workflow", (input) => {
95 const result = step1(input)
96 result.count = 5
97 return result
98 })
99 `,
100 errors: [{ messageId: "mutateInWorkflow" }],
101 },
102 // Increment.
103 {
104 code: `
105 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
106 const step1 = createStep("s1", () => ({ count: 1 }))
107 createWorkflow("my-workflow", (input) => {
108 const result = step1(input)
109 result.count++
110 return result
111 })
112 `,
113 errors: [{ messageId: "mutateInWorkflow" }],
114 },
115 // Let-reassignment.
116 {
117 code: `
118 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
119 createWorkflow("my-workflow", (input) => {
120 let counter = 0
121 counter = 5
122 return input
123 })
124 `,
125 errors: [{ messageId: "mutateInWorkflow" }],
126 },
127 // Arithmetic on a step output.
128 {
129 code: `
130 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
131 const step1 = createStep("s1", () => 1)
132 createWorkflow("my-workflow", (input) => {
133 const result = step1(input)
134 const doubled = result + 1
135 return doubled
136 })
137 `,
138 errors: [{ messageId: "recomputeInWorkflow" }],
139 },
140 // Arithmetic on the workflow input — also a placeholder.
141 {
142 code: `
143 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
144 const step1 = createStep("s1", () => 1)
145 createWorkflow("my-workflow", (input) => {
146 const value = input.value + 1
147 const result = step1(value)
148 return result
149 })
150 `,
151 errors: [{ messageId: "recomputeInWorkflow" }],
152 },
153 // Template literal interpolating a placeholder.
154 {
155 code: `
156 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
157 const step1 = createStep("s1", () => ({ name: "x" }))
158 createWorkflow("my-workflow", (input) => {
159 const result = step1(input)
160 const label = \`hello \${result.name}\`
161 return label
162 })
163 `,
164 errors: [{ messageId: "recomputeInWorkflow" }],
165 },
166 // Aliased createWorkflow import still tracked.
167 {
168 code: `
169 import { createWorkflow as cw } from "@medusajs/framework/workflows-sdk"
170 cw("my-workflow", (input) => {
171 input.value = 5
172 return input
173 })
174 `,
175 errors: [{ messageId: "mutateInWorkflow" }],
176 },
177 // Mutation inside a when().then() callback also flagged.
178 {
179 code: `
180 import { createWorkflow, createStep, when } from "@medusajs/framework/workflows-sdk"
181 const step1 = createStep("s1", () => ({ count: 1 }))
182 createWorkflow("my-workflow", (input) => {
183 const result = step1(input)
184 when({ result }, (data) => data.result.count > 0).then(() => {
185 result.count = 10
186 })
187 return result
188 })
189 `,
190 errors: [{ messageId: "mutateInWorkflow" }],
191 },
192 // Multiple violations in one workflow.
193 {
194 code: `
195 import { createWorkflow, createStep } from "@medusajs/framework/workflows-sdk"
196 const step1 = createStep("s1", () => ({ count: 1 }))
197 createWorkflow("my-workflow", (input) => {
198 const result = step1(input)
199 result.count = 5
200 const doubled = result.count + 1
201 const label = \`hello \${result.count}\`
202 return result
203 })
204 `,
205 errors: [
206 { messageId: "mutateInWorkflow" },
207 { messageId: "recomputeInWorkflow" },
208 { messageId: "recomputeInWorkflow" },
209 ],
210 },
211 ],
212 })