| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 3 | import { join, relative } from "node:path"; |
| 4 | |
| 5 | const ROOT = process.cwd(); |
| 6 | const MIGRATION_HOME = join("lib", "i18n"); |
| 7 | // One-way ceiling (#5519): the isZh migration converges or holds, never |
| 8 | // regresses. To lower the number: migrate branches into lib/i18n, then set |
| 9 | // CEILING to the new count reported by this test. |
| 10 | const CEILING = 28; |
| 11 | |
| 12 | function walk(dir: string, out: string[] = []): string[] { |
| 13 | for (const entry of readdirSync(dir)) { |
| 14 | if (entry === "node_modules" || entry === ".next" || entry === ".git") continue; |
| 15 | const full = join(dir, entry); |
| 16 | if (statSync(full).isDirectory()) walk(full, out); |
| 17 | else if (/\.[jt]sx?$/.test(entry)) out.push(full); |
| 18 | } |
| 19 | return out; |
| 20 | } |
| 21 | |
| 22 | describe("isZh one-way ceiling (#5519)", () => { |
| 23 | it(`keeps isZh branching outside ${MIGRATION_HOME} at <= ${CEILING} files`, () => { |
| 24 | const offenders = walk(ROOT) |
| 25 | .map((file) => relative(ROOT, file)) |
| 26 | .filter( |
| 27 | (file) => |
| 28 | !file.startsWith(MIGRATION_HOME) && |
| 29 | !/\.test\.[jt]sx?$/.test(file) && |
| 30 | readFileSync(join(ROOT, file), "utf8").includes("isZh"), |
| 31 | ) |
| 32 | .sort(); |
| 33 | expect( |
| 34 | offenders.length, |
| 35 | `isZh branching outside ${MIGRATION_HOME} (migrate, then lower the CEILING):\n${offenders.join("\n")}`, |
| 36 | ).toBeLessThanOrEqual(CEILING); |
| 37 | }); |
| 38 | }); |
| 39 |