forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker-registry.test.ts
More file actions
290 lines (243 loc) · 11.4 KB
/
Copy pathtracker-registry.test.ts
File metadata and controls
290 lines (243 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// src/data/__tests__/tracker-registry.test.ts
//
// Validates all tracker registry entries for consistency.
// Catches platform/apiPath mismatches, duplicate slugs, invalid fields, etc.
// Runs in CI — a bad tracker file will fail the PR.
//
// Draft trackers (draft: true) are exempt from strict validation.
// Non-draft trackers must pass all required checks.
// Warnings are printed but do not fail the test.
import fs from "node:fs"
import path from "node:path"
import { afterAll, describe, expect, it } from "vitest"
import {
getTrackerBySlug,
TRACKER_REGISTRY,
type TrackerRegistryEntry,
} from "@/data/tracker-registry"
import {
isEmpty,
LOGO_NAME_RE,
normalizeTrackerUrl,
PLACEHOLDER_RE,
SLUG_RE,
VALID_CONTENT_CATEGORIES,
} from "@/data/tracker-validation-rules"
import { ALL_TRACKERS } from "@/data/trackers"
import { DEFAULT_API_PATHS } from "@/lib/adapters"
import { VALID_PLATFORM_TYPES } from "@/lib/adapters/constants"
import { isValidHex } from "@/lib/validators"
const VALID_PLATFORMS = VALID_PLATFORM_TYPES
const TRACKER_DIR = path.resolve(__dirname, "../../data/trackers")
const LOGO_DIR = path.resolve(__dirname, "../../../public/tracker-logos")
// ---------------------------------------------------------------------------
// Warning collector — printed after all tests, does not fail CI
// ---------------------------------------------------------------------------
const warnings: string[] = []
function warn(tracker: string, msg: string) {
warnings.push(`WARN: ${tracker} — ${msg}`)
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const PRODUCTION_TRACKERS = ALL_TRACKERS.filter((t: TrackerRegistryEntry) => !t.draft)
const DRAFT_TRACKERS = ALL_TRACKERS.filter((t: TrackerRegistryEntry) => t.draft)
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("tracker registry", () => {
// ── Global uniqueness (all trackers, including drafts) ────────────────
it("has no duplicate slugs", () => {
const slugs = ALL_TRACKERS.map((t: TrackerRegistryEntry) => t.slug)
const dupes = slugs.filter((s: string, i: number) => slugs.indexOf(s) !== i)
expect(dupes, `Duplicate slugs: ${dupes.join(", ")}`).toEqual([])
})
it("has no duplicate URLs", () => {
const urls = ALL_TRACKERS.map((t: TrackerRegistryEntry) => normalizeTrackerUrl(t.url))
const dupes = urls.filter((u: string, i: number) => urls.indexOf(u) !== i)
expect(dupes, `Duplicate URLs: ${dupes.join(", ")}`).toEqual([])
})
it("every tracker has an explicit draft field (true or false)", () => {
for (const tracker of ALL_TRACKERS) {
expect(
typeof tracker.draft === "boolean",
`${tracker.slug} is missing an explicit draft: true/false field`
).toBe(true)
}
})
it("every tracker file in src/data/trackers/ is registered in the barrel", () => {
const files = fs
.readdirSync(TRACKER_DIR)
.filter((f) => f.endsWith(".ts") && f !== "index.ts" && !f.startsWith("_"))
.map((f) => f.replace(/\.ts$/, ""))
// Read barrel to get actual imported filenames
const barrelContent = fs.readFileSync(path.join(TRACKER_DIR, "index.ts"), "utf8")
const unregistered = files.filter((f) => !barrelContent.includes(`./${f}`))
expect(
unregistered,
`Tracker files not imported in index.ts: ${unregistered.join(", ")}. Did you forget to add them to the barrel?`
).toEqual([])
})
// ── Draft trackers — basic format checks only ─────────────────────────
describe("draft trackers", () => {
for (const tracker of DRAFT_TRACKERS) {
describe(tracker.slug, () => {
it("has a valid slug format", () => {
expect(tracker.slug).toMatch(SLUG_RE)
})
it("has a valid platform", () => {
expect(VALID_PLATFORMS).toContain(tracker.platform)
})
it("has a valid https URL", () => {
expect(tracker.url).toMatch(/^https:\/\//)
})
})
}
})
// ── Non-draft trackers — strict validation ────────────────────────────
describe("production trackers", () => {
for (const tracker of PRODUCTION_TRACKERS) {
describe(tracker.slug, () => {
// ── Required fields (fail) ────────────────────────────────────
it("has a valid slug format", () => {
expect(
tracker.slug,
`"${tracker.slug}" must be lowercase alphanumeric with hyphens`
).toMatch(SLUG_RE)
})
it("has a non-empty name", () => {
expect(tracker.name.trim().length).toBeGreaterThan(0)
})
it("has a valid platform", () => {
expect(VALID_PLATFORMS).toContain(tracker.platform)
})
it("has an apiPath matching its platform", () => {
if (tracker.platform === "custom") return
const expected = DEFAULT_API_PATHS[tracker.platform]
expect(
tracker.apiPath,
`${tracker.name} is platform "${tracker.platform}" but uses apiPath "${tracker.apiPath}" — expected "${expected}"`
).toBe(expected)
})
it("has a valid https URL", () => {
expect(tracker.url).toMatch(/^https:\/\//)
expect(() => new URL(tracker.url)).not.toThrow()
})
it("has at least one content category", () => {
expect(tracker.contentCategories.length).toBeGreaterThan(0)
})
it("uses only allowed content categories", () => {
const invalid = tracker.contentCategories.filter(
(c: string) => !VALID_CONTENT_CATEGORIES.has(c)
)
expect(
invalid,
`Invalid categories: ${invalid.join(", ")}. Allowed: ${[...VALID_CONTENT_CATEGORIES].join(", ")}`
).toEqual([])
})
it("has a language", () => {
expect(tracker.language?.trim().length, "language is required").toBeGreaterThan(0)
})
it("has rules.minimumRatio as a non-negative number", () => {
const r = tracker.rules?.minimumRatio
expect(r, "rules.minimumRatio is required").not.toBeUndefined()
expect(r, "rules.minimumRatio must be a number").toBeTypeOf("number")
expect(r, "rules.minimumRatio must be >= 0").toBeGreaterThanOrEqual(0)
})
it("has rules.seedTimeHours as a non-negative integer", () => {
const r = tracker.rules?.seedTimeHours
expect(r, "rules.seedTimeHours is required").not.toBeUndefined()
expect(r, "rules.seedTimeHours must be a number").toBeTypeOf("number")
expect(r, "rules.seedTimeHours must be >= 0").toBeGreaterThanOrEqual(0)
expect(Number.isInteger(r), "rules.seedTimeHours must be an integer").toBe(true)
})
it("has rules.loginIntervalDays as a non-negative integer (0 = no policy)", () => {
const r = tracker.rules?.loginIntervalDays
expect(r, "rules.loginIntervalDays is required").not.toBeUndefined()
expect(r, "rules.loginIntervalDays must be a number").toBeTypeOf("number")
expect(r, "rules.loginIntervalDays must be >= 0").toBeGreaterThanOrEqual(0)
expect(Number.isInteger(r), "rules.loginIntervalDays must be an integer").toBe(true)
})
it("has a non-placeholder description", () => {
expect(
PLACEHOLDER_RE.test(tracker.description.trim()),
`description is still "TODO" — fill it in before removing draft`
).toBe(false)
})
it("has no duplicate content categories", () => {
const seen = new Set<string>()
const dupes = tracker.contentCategories.filter((c: string) => {
if (seen.has(c)) return true
seen.add(c)
return false
})
expect(dupes, `Duplicate categories: ${dupes.join(", ")}`).toEqual([])
})
// ── Format checks (fail) ─────────────────────────────────────
if (tracker.color) {
it("has a valid hex color", () => {
expect(isValidHex(tracker.color as string)).toBe(true)
})
}
if (tracker.logo) {
it("logo follows naming convention (lowercase_logo.svg|png)", () => {
expect(
tracker.logo,
`Logo "${tracker.logo}" must match pattern /tracker-logos/<name>_logo.(svg|png)`
).toMatch(LOGO_NAME_RE)
})
it("logo file exists on disk", () => {
// biome-ignore lint/style/noNonNullAssertion: test only runs for trackers with logos
const logoFile = path.join(LOGO_DIR, path.basename(tracker.logo!))
expect(fs.existsSync(logoFile), `Logo file not found: public${tracker.logo}`).toBe(true)
})
}
// ── Warn-level fields (collected, not asserted) ──────────────
it("collects warnings for incomplete fields", () => {
const missing: string[] = []
if (isEmpty(tracker.abbreviation)) missing.push("abbreviation")
if (isEmpty(tracker.specialty)) missing.push("specialty")
if (isEmpty(tracker.userClasses)) missing.push("userClasses")
if (isEmpty(tracker.releaseGroups)) missing.push("releaseGroups")
if (isEmpty(tracker.notableMembers)) missing.push("notableMembers")
if (isEmpty(tracker.bannedGroups)) missing.push("bannedGroups")
if (!tracker.stats || Object.values(tracker.stats).every((v) => v === undefined)) {
missing.push("stats")
}
if (isEmpty(tracker.rules?.fullRulesMarkdown)) missing.push("rules.fullRulesMarkdown")
if (isEmpty(tracker.color)) missing.push("color")
if (isEmpty(tracker.logo)) missing.push("logo")
if (isEmpty(tracker.trackerHubSlug)) missing.push("trackerHubSlug")
if (missing.length > 0) {
warn(tracker.slug, `missing ${missing.join(", ")}`)
}
// Always passes — warnings don't fail the build
expect(true).toBe(true)
})
})
}
})
// ── Migrated from src/data/tracker-registry.test.ts ──────────────────
it("has at least 2 trackers", () => {
expect(TRACKER_REGISTRY.length).toBeGreaterThanOrEqual(2)
})
describe("getTrackerBySlug", () => {
it("returns correct tracker", () => {
const aither = getTrackerBySlug("aither")
expect(aither?.name).toBe("Aither")
})
it("returns undefined for unknown slug", () => {
expect(getTrackerBySlug("nonexistent")).toBeUndefined()
})
})
// ── Print collected warnings after all tests ──────────────────────────
afterAll(() => {
if (warnings.length > 0) {
console.warn("\n── Tracker registry warnings ──────────────────────────")
for (const w of warnings) {
console.warn(w)
}
console.warn("──────────────────────────────────────────────────────\n")
}
})
})