1 import { rule } from "../rule"
2 import {
3 cleanupFixtureWorkspaces,
4 createFixtureWorkspace,
5 createRuleTester,
6 type FixtureFile,
7 } from "../../../test-utils"
8
9 afterAll(cleanupFixtureWorkspaces)
10
11 /**
12 * Writes a set of job files under `<tmp>/src/jobs/` and returns the workspace.
13 * The current file under test must also be written so the cross-file scan sees
14 * the same content the rule lints.
15 */
16 const makeJobs = (files: FixtureFile[]) =>
17 createFixtureWorkspace("src/jobs", files)
18
19 const jobCode = (name: string): string =>
20 `export default async function () {}
21 export const config = { name: "${name}", schedule: "0 0 * * *" }`
22
23 const ruleTester = createRuleTester()
24
25 // --- Valid fixtures -------------------------------------------------------
26
27 // Two jobs with distinct names.
28 const uniqueNames = makeJobs([
29 { rel: "sync-products.ts", content: jobCode("sync-products") },
30 { rel: "send-newsletter.ts", content: jobCode("send-newsletter") },
31 ])
32
33 // Only one job file in the project — nothing to collide with.
34 const singleJob = makeJobs([
35 { rel: "sync-products.ts", content: jobCode("sync-products") },
36 ])
37
38 // Same name appears once here and once in a nested non-jobs sibling that the
39 // scan still walks — but the names differ, so it's valid. (Sanity: nested dirs
40 // are walked.)
41 const nestedDistinct = makeJobs([
42 { rel: "sync-products.ts", content: jobCode("sync-products") },
43 { rel: "nested/cleanup.ts", content: jobCode("cleanup") },
44 ])
45
46 // A sibling file has no resolvable name (config built by a call) — can't
47 // collide, so the current file is valid.
48 const siblingUnresolvable = makeJobs([
49 { rel: "sync-products.ts", content: jobCode("sync-products") },
50 {
51 rel: "other.ts",
52 content: `export const config = buildConfig()`,
53 },
54 ])
55
56 // Current file's name isn't a string literal — rule can't compare, so valid.
57 const nonLiteralName = makeJobs([
58 {
59 rel: "dynamic.ts",
60 content: `const JOB_NAME = "sync-products"
61 export const config = { name: JOB_NAME, schedule: "0 0 * * *" }`,
62 },
63 { rel: "sync-products.ts", content: jobCode("sync-products") },
64 ])
65
66 // --- Invalid fixtures -----------------------------------------------------
67
68 // Two job files declare the same name.
69 const duplicateNames = makeJobs([
70 { rel: "sync-products.ts", content: jobCode("sync-products") },
71 { rel: "sync-products-copy.ts", content: jobCode("sync-products") },
72 ])
73
74 // Duplicate where the colliding file lives in a nested directory.
75 const duplicateNested = makeJobs([
76 { rel: "sync-products.ts", content: jobCode("sync-products") },
77 { rel: "archive/old-sync.ts", content: jobCode("sync-products") },
78 ])
79
80 // Duplicate where the config is declared then exported by specifier.
81 const duplicateViaSpecifier = makeJobs([
82 { rel: "sync-products.ts", content: jobCode("sync-products") },
83 {
84 rel: "alias.ts",
85 content: `const config = { name: "sync-products", schedule: "* * * * *" }
86 export { config }`,
87 },
88 ])
89
90 ruleTester.run("scheduled-job-name-unique", rule, {
91 valid: [
92 {
93 code: jobCode("sync-products"),
94 filename: uniqueNames.resolve("sync-products.ts"),
95 },
96 {
97 code: jobCode("sync-products"),
98 filename: singleJob.resolve("sync-products.ts"),
99 },
100 {
101 code: jobCode("sync-products"),
102 filename: nestedDistinct.resolve("sync-products.ts"),
103 },
104 {
105 code: jobCode("sync-products"),
106 filename: siblingUnresolvable.resolve("sync-products.ts"),
107 },
108 {
109 code: `const JOB_NAME = "sync-products"
110 export const config = { name: JOB_NAME, schedule: "0 0 * * *" }`,
111 filename: nonLiteralName.resolve("dynamic.ts"),
112 },
113 // File outside any jobs/ directory — rule is a no-op.
114 {
115 code: jobCode("sync-products"),
116 filename: "/some/other/path/file.ts",
117 },
118 // Synthetic filename — rule is a no-op (can't scan disk).
119 {
120 code: jobCode("sync-products"),
121 },
122 ],
123 invalid: [
124 {
125 code: jobCode("sync-products"),
126 filename: duplicateNames.resolve("sync-products.ts"),
127 errors: [{ messageId: "duplicateName" }],
128 },
129 {
130 code: jobCode("sync-products"),
131 filename: duplicateNested.resolve("sync-products.ts"),
132 errors: [{ messageId: "duplicateName" }],
133 },
134 {
135 code: jobCode("sync-products"),
136 filename: duplicateViaSpecifier.resolve("sync-products.ts"),
137 errors: [{ messageId: "duplicateName" }],
138 },
139 ],
140 })