1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("route-file-naming", rule, {
7 valid: [
8 // route.ts with arrow GET handler.
9 {
10 code: `export const GET = async (req, res) => { res.json({}) }`,
11 filename: "src/api/store/products/route.ts",
12 },
13 // route.ts with function-declaration POST handler.
14 {
15 code: `export async function POST(req, res) { res.json({}) }`,
16 filename: "src/api/admin/orders/route.ts",
17 },
18 // route.js variant.
19 {
20 code: `export const GET = (req, res) => { res.json({}) }`,
21 filename: "src/api/store/cart/route.js",
22 },
23 // route.ts with multiple HTTP method exports.
24 {
25 code: `
26 export const GET = async (req, res) => { res.json({}) }
27 export const POST = async (req, res) => { res.json({}) }
28 export const DELETE = async (req, res) => { res.json({}) }
29 `,
30 filename: "src/api/admin/products/route.ts",
31 },
32 // route.ts with re-exported handler.
33 {
34 code: `
35 const handler = async (req, res) => { res.json({}) }
36 export { handler as GET }
37 `,
38 filename: "src/api/admin/products/route.ts",
39 },
40 // route.ts with HTTP method + helpers (helpers don't need to be HTTP methods).
41 {
42 code: `
43 export const GET = async (req, res) => { res.json({}) }
44 export const validator = (input) => input
45 `,
46 filename: "src/api/store/products/route.ts",
47 },
48 // File outside api/ — rule is a no-op.
49 {
50 code: `export const GET = async (req, res) => { res.json({}) }`,
51 filename: "src/workflows/some-file.ts",
52 },
53 // helpers file in api/ that does NOT export an HTTP method — fine.
54 {
55 code: `export const buildFilters = (req) => ({})`,
56 filename: "src/api/admin/products/helpers.ts",
57 },
58 // Lowercase exports look like HTTP methods but aren't recognized — not flagged here.
59 // (That's the job of `route-handler-exports-uppercase`.)
60 {
61 code: `export const get = (req, res) => { res.json({}) }`,
62 filename: "src/api/store/products/handlers.ts",
63 },
64 // route.ts with `export { X as GET }` from a renamed local.
65 {
66 code: `
67 function localGet(req, res) { res.json({}) }
68 export { localGet as GET }
69 `,
70 filename: "src/api/admin/things/route.ts",
71 },
72 // Synthetic filename — rule bails out.
73 {
74 code: `export const GET = async (req, res) => { res.json({}) }`,
75 filename: "<input>",
76 },
77 ],
78 invalid: [
79 // HTTP-method export in non-route file under src/api/.
80 {
81 code: `export const GET = async (req, res) => { res.json({}) }`,
82 filename: "src/api/store/products/handlers.ts",
83 errors: [{ messageId: "wrongFileName", data: { name: "GET" } }],
84 },
85 // function-declaration POST in a non-route file.
86 {
87 code: `export async function POST(req, res) { res.json({}) }`,
88 filename: "src/api/admin/orders/post.ts",
89 errors: [{ messageId: "wrongFileName", data: { name: "POST" } }],
90 },
91 // Multiple HTTP-method exports in a non-route file — one error per export.
92 {
93 code: `
94 export const GET = async (req, res) => {}
95 export const POST = async (req, res) => {}
96 `,
97 filename: "src/api/store/products/handlers.ts",
98 errors: [
99 { messageId: "wrongFileName", data: { name: "GET" } },
100 { messageId: "wrongFileName", data: { name: "POST" } },
101 ],
102 },
103 // Re-export of HTTP method from a non-route file.
104 {
105 code: `
106 const handler = async (req, res) => {}
107 export { handler as GET }
108 `,
109 filename: "src/api/store/products/handlers.ts",
110 errors: [{ messageId: "wrongFileName", data: { name: "GET" } }],
111 },
112 // route.ts with no HTTP method exports.
113 {
114 code: `export const helper = () => {}`,
115 filename: "src/api/store/products/route.ts",
116 errors: [{ messageId: "noHttpExports" }],
117 },
118 // route.js with no exports at all.
119 {
120 code: `const x = 1`,
121 filename: "src/api/store/products/route.js",
122 errors: [{ messageId: "noHttpExports" }],
123 },
124 // Nested api/ path (no leading src/).
125 {
126 code: `export const DELETE = async (req, res) => {}`,
127 filename: "api/admin/orders/delete-handler.ts",
128 errors: [{ messageId: "wrongFileName", data: { name: "DELETE" } }],
129 },
130 ],
131 })