1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("no-service-mutations-in-api-route", rule, {
7 valid: [
8 // Read-only method on a resolved module service — fine.
9 {
10 code: `
11 export const GET = async (req, res) => {
12 const productModule = req.scope.resolve(Modules.PRODUCT)
13 const product = await productModule.retrieveProduct(req.params.id)
14 res.json({ product })
15 }
16 `,
17 filename: "src/api/admin/products/[id]/route.ts",
18 },
19 // Workflow invocation — fine.
20 {
21 code: `
22 export const POST = async (req, res) => {
23 await createProductWorkflow(req.scope).run({ input: req.body })
24 res.status(200).json({})
25 }
26 `,
27 filename: "src/api/admin/products/route.ts",
28 },
29 // Query service resolved via ContainerRegistrationKeys — not a module service.
30 {
31 code: `
32 export const GET = async (req, res) => {
33 const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
34 const data = await query.graph({ entity: "product", fields: ["id"] })
35 res.json(data)
36 }
37 `,
38 filename: "src/api/store/products/route.ts",
39 },
40 // Mutation outside of an HTTP handler export — out of scope.
41 {
42 code: `
43 const helper = async (scope) => {
44 const productModule = scope.resolve(Modules.PRODUCT)
45 await productModule.createProducts({})
46 }
47 `,
48 filename: "src/api/admin/products/route.ts",
49 },
50 // Verb that doesn't match the mutation pattern (`createdAt`-style).
51 {
52 code: `
53 export const GET = async (req, res) => {
54 const productModule = req.scope.resolve(Modules.PRODUCT)
55 const x = productModule.created
56 res.json({ x })
57 }
58 `,
59 filename: "src/api/admin/products/route.ts",
60 },
61 // Not under api/ — rule bails.
62 {
63 code: `
64 export const POST = async (req, res) => {
65 const productModule = req.scope.resolve(Modules.PRODUCT)
66 await productModule.createProducts({})
67 }
68 `,
69 filename: "src/workflows/products/route.ts",
70 },
71 // Under api/ but wrong basename — rule bails.
72 {
73 code: `
74 export const POST = async (req, res) => {
75 const productModule = req.scope.resolve(Modules.PRODUCT)
76 await productModule.createProducts({})
77 }
78 `,
79 filename: "src/api/admin/products/handlers.ts",
80 },
81 // allowedMutations option suppresses the call.
82 {
83 code: `
84 export const POST = async (req, res) => {
85 const productModule = req.scope.resolve(Modules.PRODUCT)
86 await productModule.createProducts({})
87 res.status(200).json({})
88 }
89 `,
90 filename: "src/api/admin/products/route.ts",
91 options: [{ allowedMutations: ["createProducts"] }],
92 },
93 // Synthetic filename — rule bails.
94 {
95 code: `
96 export const POST = async (req, res) => {
97 const productModule = req.scope.resolve(Modules.PRODUCT)
98 await productModule.createProducts({})
99 }
100 `,
101 filename: "<input>",
102 },
103 ],
104 invalid: [
105 // Canonical violation: `Modules.PRODUCT` + `createProducts`.
106 {
107 code: `
108 export const POST = async (req, res) => {
109 const productModule = req.scope.resolve(Modules.PRODUCT)
110 await productModule.createProducts(req.body)
111 res.status(200).json({})
112 }
113 `,
114 filename: "src/api/admin/products/route.ts",
115 errors: [
116 {
117 messageId: "noMutation",
118 data: { service: "productModule", method: "createProducts" },
119 },
120 ],
121 },
122 // Module-key string literal also tracked.
123 {
124 code: `
125 export const DELETE = async (req, res) => {
126 const orderModule = req.scope.resolve("order")
127 await orderModule.deleteOrders([req.params.id])
128 res.status(200).json({})
129 }
130 `,
131 filename: "src/api/admin/orders/[id]/route.ts",
132 errors: [
133 {
134 messageId: "noMutation",
135 data: { service: "orderModule", method: "deleteOrders" },
136 },
137 ],
138 },
139 // Multiple mutation verbs across the prefix list.
140 {
141 code: `
142 export const POST = async (req, res) => {
143 const customerModule = req.scope.resolve(Modules.CUSTOMER)
144 await customerModule.updateCustomers(req.body)
145 await customerModule.softDeleteCustomers([req.body.id])
146 await customerModule.restoreCustomers([req.body.id])
147 res.status(200).json({})
148 }
149 `,
150 filename: "src/api/admin/customers/route.ts",
151 errors: [
152 {
153 messageId: "noMutation",
154 data: { service: "customerModule", method: "updateCustomers" },
155 },
156 {
157 messageId: "noMutation",
158 data: { service: "customerModule", method: "softDeleteCustomers" },
159 },
160 {
161 messageId: "noMutation",
162 data: { service: "customerModule", method: "restoreCustomers" },
163 },
164 ],
165 },
166 // Snake-cased mutation method (suffix starts with `_`).
167 {
168 code: `
169 export const POST = async (req, res) => {
170 const cartModule = req.scope.resolve(Modules.CART)
171 await cartModule.create_line_items(req.body)
172 res.status(200).json({})
173 }
174 `,
175 filename: "src/api/store/carts/route.ts",
176 errors: [
177 {
178 messageId: "noMutation",
179 data: { service: "cartModule", method: "create_line_items" },
180 },
181 ],
182 },
183 // container.resolve(...) alias works too.
184 {
185 code: `
186 export const POST = async (req, res) => {
187 const container = req.scope
188 const productModule = container.resolve(Modules.PRODUCT)
189 await productModule.createProducts(req.body)
190 res.status(200).json({})
191 }
192 `,
193 filename: "src/api/admin/products/route.ts",
194 errors: [
195 {
196 messageId: "noMutation",
197 data: { service: "productModule", method: "createProducts" },
198 },
199 ],
200 },
201 // Function-declaration handler (not arrow).
202 {
203 code: `
204 export async function POST(req, res) {
205 const productModule = req.scope.resolve(Modules.PRODUCT)
206 await productModule.upsertProducts(req.body)
207 res.status(200).json({})
208 }
209 `,
210 filename: "src/api/admin/products/route.ts",
211 errors: [
212 {
213 messageId: "noMutation",
214 data: { service: "productModule", method: "upsertProducts" },
215 },
216 ],
217 },
218 // additionalMutationPrefixes option extends the verb list.
219 {
220 code: `
221 export const POST = async (req, res) => {
222 const productModule = req.scope.resolve(Modules.PRODUCT)
223 await productModule.publishProducts(req.body)
224 res.status(200).json({})
225 }
226 `,
227 filename: "src/api/admin/products/route.ts",
228 options: [{ additionalMutationPrefixes: ["publish"] }],
229 errors: [
230 {
231 messageId: "noMutation",
232 data: { service: "productModule", method: "publishProducts" },
233 },
234 ],
235 },
236 // Custom service resolved by string key — also flagged (not just module keys).
237 {
238 code: `
239 export const POST = async (req, res) => {
240 const helloService = req.scope.resolve("helloService")
241 await helloService.createGreeting(req.body)
242 res.json({})
243 }
244 `,
245 filename: "src/api/store/hello/route.ts",
246 errors: [
247 {
248 messageId: "noMutation",
249 data: { service: "helloService", method: "createGreeting" },
250 },
251 ],
252 },
253 // Nested api/ path without src/.
254 {
255 code: `
256 export const POST = async (req, res) => {
257 const productModule = req.scope.resolve(Modules.PRODUCT)
258 await productModule.createProducts(req.body)
259 res.status(200).json({})
260 }
261 `,
262 filename: "api/admin/products/route.ts",
263 errors: [
264 {
265 messageId: "noMutation",
266 data: { service: "productModule", method: "createProducts" },
267 },
268 ],
269 },
270 ],
271 })