1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("link-create-keys-modules-enum", rule, {
7 valid: [
8 // Canonical: computed key using the Modules enum.
9 {
10 code: `
11 import { Modules } from "@medusajs/framework/utils"
12 await link.create({
13 [Modules.PRODUCT]: { product_id: "1" },
14 [Modules.ORDER]: { order_id: "2" },
15 })
16 `,
17 },
18 // Custom module name (not in the Modules enum) — left alone.
19 {
20 code: `
21 await link.create({
22 blog: { post_id: "1" },
23 custom_module: { id: "x" },
24 })
25 `,
26 },
27 // Unrelated method on `link` — not flagged.
28 {
29 code: `
30 await link.list({ product: "x" })
31 `,
32 },
33 // Unrelated identifier call (not a tracked step) — even with a matching key.
34 {
35 code: `
36 somethingElse({ product: { id: "1" } })
37 `,
38 },
39 // Step from another package — not tracked.
40 {
41 code: `
42 import { createRemoteLinkStep } from "some-other-package"
43 createRemoteLinkStep({ product: { id: "1" } })
44 `,
45 },
46 // Third-party SDK call whose option name collides with a module value.
47 {
48 code: `
49 await this.mailchimp.campaigns.create({
50 type: "regular",
51 settings: { subject_line: "New Products" },
52 })
53 `,
54 },
55 // Third-party SDK call with several colliding option names.
56 {
57 code: `
58 await stripe.customers.create({ user: 1, order: 2 })
59 `,
60 },
61 // Colliding method name on an unrelated receiver.
62 {
63 code: `
64 await queue.delete({ index: "x" })
65 `,
66 },
67 // Receiver is a computed member — not resolvable to a link.
68 {
69 code: `
70 await services["link"].create({ product: { id: "1" } })
71 `,
72 },
73 // Array of objects, all enum-form.
74 {
75 code: `
76 import { Modules } from "@medusajs/framework/utils"
77 await link.create([
78 { [Modules.PRODUCT]: { product_id: "1" }, blog: { post_id: "2" } },
79 ])
80 `,
81 },
82 ],
83 invalid: [
84 // Identifier key for a known module → autofix + add import.
85 {
86 code: `await link.create({ product: { product_id: "1" } })`,
87 errors: [
88 {
89 messageId: "preferEnumKey",
90 data: { key: "product", enumMember: "PRODUCT" },
91 },
92 ],
93 output: `import { Modules } from "@medusajs/framework/utils"
94 await link.create({ [Modules.PRODUCT]: { product_id: "1" } })`,
95 },
96 // String-literal key.
97 {
98 code: `await link.create({ "product": { product_id: "1" } })`,
99 errors: [{ messageId: "preferEnumKey" }],
100 output: `import { Modules } from "@medusajs/framework/utils"
101 await link.create({ [Modules.PRODUCT]: { product_id: "1" } })`,
102 },
103 // Existing framework/utils import → append `Modules` to specifier list.
104 {
105 code: `
106 import { defineLink } from "@medusajs/framework/utils"
107 await link.create({ product: { product_id: "1" } })
108 `,
109 errors: [{ messageId: "preferEnumKey" }],
110 output: `
111 import { defineLink, Modules } from "@medusajs/framework/utils"
112 await link.create({ [Modules.PRODUCT]: { product_id: "1" } })
113 `,
114 },
115 // Modules already imported → reuse local binding, no second import added.
116 {
117 code: `
118 import { Modules } from "@medusajs/framework/utils"
119 await link.create({ product: { id: "1" } })
120 `,
121 errors: [{ messageId: "preferEnumKey" }],
122 output: `
123 import { Modules } from "@medusajs/framework/utils"
124 await link.create({ [Modules.PRODUCT]: { id: "1" } })
125 `,
126 },
127 // Aliased Modules import.
128 {
129 code: `
130 import { Modules as M } from "@medusajs/framework/utils"
131 await link.create({ order: { id: "1" } })
132 `,
133 errors: [{ messageId: "preferEnumKey" }],
134 output: `
135 import { Modules as M } from "@medusajs/framework/utils"
136 await link.create({ [M.ORDER]: { id: "1" } })
137 `,
138 },
139 // link.dismiss with snake_case enum value.
140 {
141 code: `
142 import { Modules } from "@medusajs/framework/utils"
143 await link.dismiss({ sales_channel: { id: "x" } })
144 `,
145 errors: [{ messageId: "preferEnumKey" }],
146 output: `
147 import { Modules } from "@medusajs/framework/utils"
148 await link.dismiss({ [Modules.SALES_CHANNEL]: { id: "x" } })
149 `,
150 },
151 // Receiver resolved from the container under an unrelated name.
152 {
153 code: `
154 import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
155 const l = container.resolve(ContainerRegistrationKeys.LINK)
156 await l.create({ product: { id: "1" } })
157 `,
158 errors: [{ messageId: "preferEnumKey" }],
159 output: `
160 import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
161 const l = container.resolve(ContainerRegistrationKeys.LINK)
162 await l.create({ [Modules.PRODUCT]: { id: "1" } })
163 `,
164 },
165 // Receiver typed as `Link`.
166 {
167 code: `
168 import { Modules } from "@medusajs/framework/utils"
169 const svc: Link = getIt()
170 await svc.create({ order: { id: "1" } })
171 `,
172 errors: [{ messageId: "preferEnumKey" }],
173 output: `
174 import { Modules } from "@medusajs/framework/utils"
175 const svc: Link = getIt()
176 await svc.create({ [Modules.ORDER]: { id: "1" } })
177 `,
178 },
179 // `remoteLink` receiver.
180 {
181 code: `
182 import { Modules } from "@medusajs/framework/utils"
183 await remoteLink.create({ product: { id: "1" } })
184 `,
185 errors: [{ messageId: "preferEnumKey" }],
186 output: `
187 import { Modules } from "@medusajs/framework/utils"
188 await remoteLink.create({ [Modules.PRODUCT]: { id: "1" } })
189 `,
190 },
191 // `this.link_` receiver inside a service.
192 {
193 code: `
194 import { Modules } from "@medusajs/framework/utils"
195 class Svc {
196 async run() {
197 await this.link_.create({ product: { id: "1" } })
198 }
199 }
200 `,
201 errors: [{ messageId: "preferEnumKey" }],
202 output: `
203 import { Modules } from "@medusajs/framework/utils"
204 class Svc {
205 async run() {
206 await this.link_.create({ [Modules.PRODUCT]: { id: "1" } })
207 }
208 }
209 `,
210 },
211 // Workflow step: createRemoteLinkStep.
212 {
213 code: `
214 import { Modules } from "@medusajs/framework/utils"
215 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
216 createRemoteLinkStep([
217 { product: { product_id: "1" }, blog: { post_id: "2" } },
218 ])
219 `,
220 errors: [{ messageId: "preferEnumKey" }],
221 output: `
222 import { Modules } from "@medusajs/framework/utils"
223 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
224 createRemoteLinkStep([
225 { [Modules.PRODUCT]: { product_id: "1" }, blog: { post_id: "2" } },
226 ])
227 `,
228 },
229 // Workflow step alias.
230 {
231 code: `
232 import { Modules } from "@medusajs/framework/utils"
233 import { dismissRemoteLinkStep as drls } from "@medusajs/medusa/core-flows"
234 drls({ order: { id: "1" } })
235 `,
236 errors: [{ messageId: "preferEnumKey" }],
237 output: `
238 import { Modules } from "@medusajs/framework/utils"
239 import { dismissRemoteLinkStep as drls } from "@medusajs/medusa/core-flows"
240 drls({ [Modules.ORDER]: { id: "1" } })
241 `,
242 },
243 // Shorthand property.
244 {
245 code: `
246 import { Modules } from "@medusajs/framework/utils"
247 await link.create({ product })
248 `,
249 errors: [{ messageId: "preferEnumKey" }],
250 output: `
251 import { Modules } from "@medusajs/framework/utils"
252 await link.create({ [Modules.PRODUCT]: product })
253 `,
254 },
255 // Multiple violations in one call.
256 {
257 code: `
258 import { Modules } from "@medusajs/framework/utils"
259 await link.create({
260 product: { id: "1" },
261 order: { id: "2" },
262 blog: { id: "3" },
263 })
264 `,
265 errors: [
266 {
267 messageId: "preferEnumKey",
268 data: { key: "product", enumMember: "PRODUCT" },
269 },
270 {
271 messageId: "preferEnumKey",
272 data: { key: "order", enumMember: "ORDER" },
273 },
274 ],
275 output: `
276 import { Modules } from "@medusajs/framework/utils"
277 await link.create({
278 [Modules.PRODUCT]: { id: "1" },
279 [Modules.ORDER]: { id: "2" },
280 blog: { id: "3" },
281 })
282 `,
283 },
284 ],
285 })