1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("prefer-workflow-event-over-module-event", rule, {
7 valid: [
8 // Workflow enum member in `config.event` — the desired form.
9 {
10 code: `
11 import { ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
12 export const config = {
13 event: ProductCategoryWorkflowEvents.DELETED,
14 }
15 `,
16 },
17 // Literal that names a workflow event value.
18 {
19 code: `export const config = { event: "order.placed" }`,
20 },
21 // Array of workflow-event literals.
22 {
23 code: `export const config = { event: ["cart.created", "cart.updated"] }`,
24 },
25 // A custom event
26 {
27 code: `export const config = { event: "my-custom.event" }`,
28 },
29 // An internal `*Events` enum whose value is `{module}.{action}` (no
30 // data-model segment) is NOT an internal service event — not flagged.
31 {
32 code: `
33 import { AuthEvents } from "@medusajs/framework/utils"
34 export const config = { event: AuthEvents.MFA_ENABLED }
35 `,
36 },
37 // Not a subscriber config
38 {
39 code: `
40 import { ProductEvents } from "@medusajs/framework/utils"
41 emitEvent(ProductEvents.PRODUCT_CATEGORY_DELETED)
42 `,
43 },
44 // `config` without an `event` property
45 {
46 code: `export const config = { context: { subscriberId: "x" } }`,
47 },
48 // Dynamically-computed event list skipped
49 {
50 code: `
51 const handlers = []
52 export const config = { event: handlers.map((h) => h.event) }
53 `,
54 },
55 // A locally-declared object that shares the enum name
56 {
57 code: `
58 const ProductEvents = { PRODUCT_CATEGORY_DELETED: "x" }
59 export const config = { event: ProductEvents.PRODUCT_CATEGORY_DELETED }
60 `,
61 },
62 ],
63 invalid: [
64 // Named import — rewrite the access and add the workflow enum import.
65 {
66 code: `
67 import { ProductEvents } from "@medusajs/framework/utils"
68 export const config = {
69 event: ProductEvents.PRODUCT_CATEGORY_DELETED,
70 }
71 `,
72 errors: [
73 {
74 messageId: "preferWorkflowEvent",
75 data: {
76 source: "ProductEvents.PRODUCT_CATEGORY_DELETED",
77 target: "ProductCategoryWorkflowEvents.DELETED",
78 },
79 },
80 ],
81 output: `
82 import { ProductEvents, ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
83 export const config = {
84 event: ProductCategoryWorkflowEvents.DELETED,
85 }
86 `,
87 },
88 // Aliased named import — append after the last specifier, rewrite the usage.
89 {
90 code: `
91 import { ProductEvents as PE } from "@medusajs/framework/utils"
92 export const config = { event: PE.PRODUCT_CATEGORY_DELETED }
93 `,
94 errors: [{ messageId: "preferWorkflowEvent" }],
95 output: `
96 import { ProductEvents as PE, ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
97 export const config = { event: ProductCategoryWorkflowEvents.DELETED }
98 `,
99 },
100 // Namespace import — keep the namespace prefix, no import change.
101 {
102 code: `
103 import * as utils from "@medusajs/framework/utils"
104 export const config = { event: utils.ProductEvents.PRODUCT_CATEGORY_DELETED }
105 `,
106 errors: [{ messageId: "preferWorkflowEvent" }],
107 output: `
108 import * as utils from "@medusajs/framework/utils"
109 export const config = { event: utils.ProductCategoryWorkflowEvents.DELETED }
110 `,
111 },
112 // Computed member access is normalized to dot access.
113 {
114 code: `
115 import { ProductEvents } from "@medusajs/framework/utils"
116 export const config = { event: ProductEvents["PRODUCT_CATEGORY_DELETED"] }
117 `,
118 errors: [{ messageId: "preferWorkflowEvent" }],
119 output: `
120 import { ProductEvents, ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
121 export const config = { event: ProductCategoryWorkflowEvents.DELETED }
122 `,
123 },
124 // Literal internal-event value — same map lookup, fixed to the enum with a
125 // fresh import added.
126 {
127 code: `export const config = { event: "product.product-category.deleted" }`,
128 errors: [
129 {
130 messageId: "preferWorkflowEvent",
131 data: {
132 source: `"product.product-category.deleted"`,
133 target: "ProductCategoryWorkflowEvents.DELETED",
134 },
135 },
136 ],
137 output: `import { ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
138 export const config = { event: ProductCategoryWorkflowEvents.DELETED }`,
139 },
140 // A different module enum with a workflow equivalent.
141 {
142 code: `
143 import { UserEvents } from "@medusajs/framework/utils"
144 export const config = { event: UserEvents.USER_CREATED }
145 `,
146 errors: [
147 {
148 messageId: "preferWorkflowEvent",
149 data: {
150 source: "UserEvents.USER_CREATED",
151 target: "UserWorkflowEvents.CREATED",
152 },
153 },
154 ],
155 output: `
156 import { UserEvents, UserWorkflowEvents } from "@medusajs/framework/utils"
157 export const config = { event: UserWorkflowEvents.CREATED }
158 `,
159 },
160 // Internal event inside an array — only that entry is flagged and fixed.
161 {
162 code: `
163 import { ProductEvents } from "@medusajs/framework/utils"
164 export const config = { event: ["order.placed", ProductEvents.PRODUCT_CATEGORY_DELETED] }
165 `,
166 errors: [{ messageId: "preferWorkflowEvent" }],
167 output: `
168 import { ProductEvents, ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
169 export const config = { event: ["order.placed", ProductCategoryWorkflowEvents.DELETED] }
170 `,
171 },
172 // `config` declared separately and exported by specifier.
173 {
174 code: `
175 import { ProductEvents } from "@medusajs/framework/utils"
176 const config = { event: ProductEvents.PRODUCT_CATEGORY_DELETED }
177 export { config }
178 `,
179 errors: [{ messageId: "preferWorkflowEvent" }],
180 output: `
181 import { ProductEvents, ProductCategoryWorkflowEvents } from "@medusajs/framework/utils"
182 const config = { event: ProductCategoryWorkflowEvents.DELETED }
183 export { config }
184 `,
185 },
186 // Internal service event with NO workflow equivalent (enum form) — warn, no fix.
187 {
188 code: `
189 import { FulfillmentEvents } from "@medusajs/framework/utils"
190 export const config = { event: FulfillmentEvents.FULFILLMENT_SET_CREATED }
191 `,
192 errors: [
193 {
194 messageId: "internalModuleEvent",
195 data: { source: "FulfillmentEvents.FULFILLMENT_SET_CREATED" },
196 },
197 ],
198 output: null,
199 },
200 // Internal service event with NO workflow equivalent (literal form) — warn, no fix.
201 {
202 code: `export const config = { event: "fulfillment.fulfillment-set.created" }`,
203 errors: [
204 {
205 messageId: "internalModuleEvent",
206 data: { source: `"fulfillment.fulfillment-set.created"` },
207 },
208 ],
209 output: null,
210 },
211 ],
212 })