|
| 1 | +// scripts/__tests__/check-localstorage-allowlist.test.mjs |
| 2 | +// |
| 3 | +// Unit tests for the localStorage allowlist budget guard. |
| 4 | +// Run with: |
| 5 | +// node --test scripts/__tests__/check-localstorage-allowlist.test.mjs |
| 6 | +// |
| 7 | +// We test the pure parsing helpers (no FS, no env). The CLI runner |
| 8 | +// itself is exercised end-to-end via `pnpm lint:localstorage-allowlist` |
| 9 | +// in CI. |
| 10 | + |
| 11 | +import { describe, it } from "node:test"; |
| 12 | +import assert from "node:assert/strict"; |
| 13 | + |
| 14 | +import { |
| 15 | + extractWebIgnoresBlock, |
| 16 | + countProductionEntries, |
| 17 | + parseBudgetFile, |
| 18 | +} from "../check-localstorage-allowlist.mjs"; |
| 19 | + |
| 20 | +// ── extractWebIgnoresBlock ─────────────────────────────────────────────────── |
| 21 | + |
| 22 | +describe("extractWebIgnoresBlock", () => { |
| 23 | + it("returns null when the rule wiring is absent", () => { |
| 24 | + const source = ` |
| 25 | + module.exports = [ |
| 26 | + { rules: { "no-console": "warn" } }, |
| 27 | + ]; |
| 28 | + `; |
| 29 | + assert.equal(extractWebIgnoresBlock(source), null); |
| 30 | + }); |
| 31 | + |
| 32 | + it("locates the web app's ignores block adjacent to the rule wiring", () => { |
| 33 | + const source = [ |
| 34 | + "export default [", |
| 35 | + " {", |
| 36 | + ' files: ["apps/web/src/**/*.{js,jsx,ts,tsx}"],', |
| 37 | + " ignores: [", |
| 38 | + ' "apps/web/src/**/*.test.{js,jsx,ts,tsx}",', |
| 39 | + ' "apps/web/src/shared/lib/storage/storage.ts",', |
| 40 | + ' "apps/web/src/shared/hooks/useDarkMode.ts",', |
| 41 | + " ],", |
| 42 | + " rules: {", |
| 43 | + ' "sergeant-design/no-raw-local-storage": "error",', |
| 44 | + " },", |
| 45 | + " },", |
| 46 | + "];", |
| 47 | + "", |
| 48 | + ].join("\n"); |
| 49 | + |
| 50 | + const block = extractWebIgnoresBlock(source); |
| 51 | + assert.ok(block, "block should be located"); |
| 52 | + assert.match(block, /storage\.ts/); |
| 53 | + assert.match(block, /useDarkMode\.ts/); |
| 54 | + }); |
| 55 | + |
| 56 | + it("does NOT match the mobile rule wiring", () => { |
| 57 | + const source = [ |
| 58 | + "export default [", |
| 59 | + " {", |
| 60 | + ' files: ["apps/mobile/src/**/*.{js,jsx,ts,tsx}"],', |
| 61 | + " ignores: [", |
| 62 | + ' "apps/mobile/src/**/*.test.{js,jsx,ts,tsx}",', |
| 63 | + " ],", |
| 64 | + " rules: {", |
| 65 | + ' "sergeant-design/no-raw-local-storage": "error",', |
| 66 | + " },", |
| 67 | + " },", |
| 68 | + "];", |
| 69 | + "", |
| 70 | + ].join("\n"); |
| 71 | + |
| 72 | + assert.equal( |
| 73 | + extractWebIgnoresBlock(source), |
| 74 | + null, |
| 75 | + "must not match the mobile glob", |
| 76 | + ); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +// ── countProductionEntries ─────────────────────────────────────────────────── |
| 81 | + |
| 82 | +describe("countProductionEntries", () => { |
| 83 | + it("returns 0 for an empty block", () => { |
| 84 | + assert.equal(countProductionEntries("[]"), 0); |
| 85 | + }); |
| 86 | + |
| 87 | + it("ignores the two test-fixture entries", () => { |
| 88 | + const block = ` |
| 89 | + ignores: [ |
| 90 | + "apps/web/src/**/*.test.{js,jsx,ts,tsx}", |
| 91 | + "apps/web/src/**/__tests__/**", |
| 92 | + "apps/web/src/shared/lib/storage/storage.ts", |
| 93 | + "apps/web/src/shared/hooks/useDarkMode.ts", |
| 94 | + ], |
| 95 | + `; |
| 96 | + assert.equal(countProductionEntries(block), 2); |
| 97 | + }); |
| 98 | + |
| 99 | + it("ignores comments so reviewer notes don't shift the count", () => { |
| 100 | + const block = ` |
| 101 | + ignores: [ |
| 102 | + // Tests can use localStorage freely as fixtures. |
| 103 | + "apps/web/src/**/*.test.{js,jsx,ts,tsx}", |
| 104 | + // "apps/web/src/shared/hooks/oldHook.ts" — migrated PR #999 |
| 105 | + "apps/web/src/shared/hooks/useDarkMode.ts", |
| 106 | + ], |
| 107 | + `; |
| 108 | + assert.equal(countProductionEntries(block), 1); |
| 109 | + }); |
| 110 | + |
| 111 | + it("counts every non-test path exactly once", () => { |
| 112 | + const block = ` |
| 113 | + ignores: [ |
| 114 | + "apps/web/src/**/*.test.{js,jsx,ts,tsx}", |
| 115 | + "apps/web/src/**/__tests__/**", |
| 116 | + "apps/web/src/a.ts", |
| 117 | + "apps/web/src/b.ts", |
| 118 | + "apps/web/src/c.ts", |
| 119 | + ], |
| 120 | + `; |
| 121 | + assert.equal(countProductionEntries(block), 3); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +// ── parseBudgetFile ────────────────────────────────────────────────────────── |
| 126 | + |
| 127 | +describe("parseBudgetFile", () => { |
| 128 | + it("accepts a well-formed budget", () => { |
| 129 | + const json = JSON.stringify({ |
| 130 | + production: 17, |
| 131 | + rationale: "Baseline 2026-05-04: 11 primitives + 4 cloud-sync + 2 misc.", |
| 132 | + }); |
| 133 | + const out = parseBudgetFile(json); |
| 134 | + assert.equal(out.production, 17); |
| 135 | + assert.match(out.rationale, /Baseline/); |
| 136 | + }); |
| 137 | + |
| 138 | + it("floors fractional production counts", () => { |
| 139 | + const json = JSON.stringify({ |
| 140 | + production: 17.9, |
| 141 | + rationale: "Reasonable rationale text long enough.", |
| 142 | + }); |
| 143 | + assert.equal(parseBudgetFile(json).production, 17); |
| 144 | + }); |
| 145 | + |
| 146 | + it("rejects negative production counts", () => { |
| 147 | + const json = JSON.stringify({ |
| 148 | + production: -1, |
| 149 | + rationale: "Reasonable rationale text long enough.", |
| 150 | + }); |
| 151 | + assert.throws(() => parseBudgetFile(json), /≥ 0/); |
| 152 | + }); |
| 153 | + |
| 154 | + it("rejects non-numeric production counts", () => { |
| 155 | + const json = JSON.stringify({ |
| 156 | + production: "17", |
| 157 | + rationale: "Reasonable rationale text long enough.", |
| 158 | + }); |
| 159 | + assert.throws(() => parseBudgetFile(json), /finite number/); |
| 160 | + }); |
| 161 | + |
| 162 | + it("rejects a missing or too-short rationale", () => { |
| 163 | + assert.throws( |
| 164 | + () => parseBudgetFile(JSON.stringify({ production: 1, rationale: "" })), |
| 165 | + /≥ 8 chars/, |
| 166 | + ); |
| 167 | + assert.throws( |
| 168 | + () => |
| 169 | + parseBudgetFile(JSON.stringify({ production: 1, rationale: "short" })), |
| 170 | + /≥ 8 chars/, |
| 171 | + ); |
| 172 | + assert.throws( |
| 173 | + () => parseBudgetFile(JSON.stringify({ production: 1 })), |
| 174 | + /rationale/, |
| 175 | + ); |
| 176 | + }); |
| 177 | +}); |
0 commit comments