|
| 1 | +/** |
| 2 | + * @ai-context shift.guard.test.ts — 位移上限 8px 的**扩域守卫**(批 6 T9 · R11.4 · 计划 V1/V4b)。 |
| 3 | + * |
| 4 | + * Why 单独成件:计划 Step 2 要「把 `style-seams.test.ts` 的位移判据域从 `primitives/*.css` 扩到全仓」, |
| 5 | + * 而该文件在 T5 之后已 **297 行**(余 3)⇒ 扩域要的遍历器与名单跟着挤不进 300 行。计划 Files 表因此 |
| 6 | + * 预置了本文件(`app/src/motion/shift.guard.test.ts`)作为**拆出去的落点**;`style-seams.test.ts` 只留 |
| 7 | + * 一条 2 行的常量对拍(防「两边各写一个 8」)。两处判据的**正则与换算逻辑逐字同源**(原样抄自 |
| 8 | + * `style-seams.test.ts` 的 `shiftViolations`),本件的域**严格包含**那一处的域。 |
| 9 | + * |
| 10 | + * 判据域(R11.4「裁决域 = 只判 `translate*()` + `motion/` 层的位移常量」): |
| 11 | + * ① `app/src/**` 的**生产** `.css` / `.ts` / `.tsx`(剥注释后)的 `translate*()` 数值实参 ≤ 8px; |
| 12 | + * ② `app/src/motion/**` 的**生产**模块里 `*_PX` 具名位移常量 ≤ 8px。 |
| 13 | + * 🔴 **测试文件(`*.test.ts` / `*.test.tsx`)不在域内** —— 实测依据:R8.1 的「唯一正解」harness 用 |
| 14 | + * **大位移样本**做测量(`motion.md` 的逐字例子是 `x: 0 → 100` ⇒ `translate3d(87.5px,0px,0px)`), |
| 15 | + * T3 的 `engine.test.ts` 就断言 `translate3d(50px, 0px, 0px)` ⇒ 把测试文件拉进域会让本判据**当场红在 |
| 16 | + * 别人的已提交文件上**,而那是**测量样本**不是交付的动效(§8.4 管的是后者)。⇒ 逐条登记在报告里。 |
| 17 | + * ③ `margin` / `left` / `top` 的 >8px **不判**(R11.4:实测全是布局量,判它们会造假阳性); |
| 18 | + * `%` / `calc()` / `var()` 形态**有意跳过**(居中与扫光,不是位移量 —— 既有口径逐字保留)。 |
| 19 | + * |
| 20 | + * 副作用:只读磁盘(整棵 `app/src`);不写任何文件。 |
| 21 | + * 边界:① 剥注释复用共享仪器 `ui/primitives/sliceScan.ts` 的状态机(R8.7 的正解先例;同批 T4 的 |
| 22 | + * `engine.guard.test.ts` 同款用法);② **文本级扫描,不做 AST** —— 它只能证「字面量在不在」, |
| 23 | + * 不证实现是否真的遵守(评审 I-4 的那条限度);③ 未按 `*_PX` 命名的位移常量(如 `CARD_LIFT = 12`) |
| 24 | + * 本判据抓不到,只由评审兜 —— **诚实登记,不声称封死**。 |
| 25 | + */ |
| 26 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 27 | +import { dirname, join } from "node:path"; |
| 28 | +import { fileURLToPath } from "node:url"; |
| 29 | +import { describe, expect, it } from "vitest"; |
| 30 | +import { relOf, stripComments } from "../ui/primitives/sliceScan"; |
| 31 | +import { SHIFT_MAX_PX } from "./shift"; |
| 32 | + |
| 33 | +const SRC_ROOT = dirname(dirname(fileURLToPath(import.meta.url))); // app/src |
| 34 | +const MOTION_ROOT = join(SRC_ROOT, "motion"); |
| 35 | +const DOMAIN_EXT = /\.(?:css|ts|tsx)$/; |
| 36 | +const TEST_FILE = /\.test\.tsx?$/; |
| 37 | +const SOURCE_FILE = /\.tsx?$/; |
| 38 | + |
| 39 | +/** 递归收集域内文件(生产面;`.d.ts` 之类也照扫 —— 它们不产位移,扫了无害) */ |
| 40 | +function walk(dir: string, out: string[] = []): string[] { |
| 41 | + for (const name of readdirSync(dir)) { |
| 42 | + const path = join(dir, name); |
| 43 | + if (statSync(path).isDirectory()) walk(path, out); |
| 44 | + else if (DOMAIN_EXT.test(name) && !TEST_FILE.test(name)) out.push(path); |
| 45 | + } |
| 46 | + return out; |
| 47 | +} |
| 48 | + |
| 49 | +/** 既有口径(`style-seams.test.ts` 原样):`translate*()` 的全部调用;百分比 / `calc()` / `var()` 由数值判定跳过 */ |
| 50 | +const TRANSLATE_CALL = /translate(?:3d|X|Y|Z)?\(([^)]*)\)/g; |
| 51 | +const NUMERIC_ARG = /^\s*(-?\d+(?:\.\d+)?)(px|rem)\s*$/; |
| 52 | + |
| 53 | +/** 一条 `translate*()` 调用里的**数值实参**(px / rem;rem 按 16 换算,与既有口径一致) */ |
| 54 | +interface ShiftSite { |
| 55 | + readonly call: string; |
| 56 | + readonly value: number; |
| 57 | +} |
| 58 | + |
| 59 | +function shiftSites(text: string): ShiftSite[] { |
| 60 | + const out: ShiftSite[] = []; |
| 61 | + for (const m of stripComments(text).matchAll(TRANSLATE_CALL)) { |
| 62 | + for (const arg of m[1].split(",")) { |
| 63 | + const px = NUMERIC_ARG.exec(arg); |
| 64 | + if (!px) continue; |
| 65 | + out.push({ call: m[0], value: px[2] === "rem" ? Math.abs(Number(px[1])) * 16 : Math.abs(Number(px[1])) }); |
| 66 | + } |
| 67 | + } |
| 68 | + return out; |
| 69 | +} |
| 70 | + |
| 71 | +/** 计划 V1 的判据体:域内逐文件找 > `SHIFT_MAX_PX` 的位移量,返回可读的犯规清单 */ |
| 72 | +function shiftViolations(text: string, file: string): string[] { |
| 73 | + return shiftSites(text) |
| 74 | + .filter((s) => s.value > SHIFT_MAX_PX) |
| 75 | + .map((s) => `${file}: ${s.call} = ${s.value}px > ${SHIFT_MAX_PX}px`); |
| 76 | +} |
| 77 | + |
| 78 | +/** `export const <name> = <number>` 形态的数值常量(R11.4 的「位移常量」面) */ |
| 79 | +const NUMERIC_CONST = /export const ([A-Za-z_$][\w$]*)\s*=\s*(-?\d+(?:\.\d+)?)\s*;/g; |
| 80 | +const PX_NAME = /_PX$/; |
| 81 | + |
| 82 | +interface NumericConst { |
| 83 | + readonly name: string; |
| 84 | + readonly value: number; |
| 85 | +} |
| 86 | + |
| 87 | +function numericConstants(text: string, onlyPxNames: boolean): NumericConst[] { |
| 88 | + const out: NumericConst[] = []; |
| 89 | + for (const m of stripComments(text).matchAll(NUMERIC_CONST)) { |
| 90 | + if (onlyPxNames && !PX_NAME.test(m[1])) continue; |
| 91 | + out.push({ name: m[1], value: Number(m[2]) }); |
| 92 | + } |
| 93 | + return out; |
| 94 | +} |
| 95 | + |
| 96 | +const readText = (file: string): string => readFileSync(file, "utf8"); |
| 97 | + |
| 98 | +describe("V1 全仓生产文件的 `translate*()` 数值实参 ≤ 8px(R11.4 的扩域半)", () => { |
| 99 | + it("域内生产文件逐条扫(实测 330 件),>8px 的位移量为 0(剥注释后)", () => { |
| 100 | + const files = walk(SRC_ROOT); |
| 101 | + const rels = files.map((f) => relOf(SRC_ROOT, f)); |
| 102 | + // 防空扫:遍历器必须看得见这几件(含动效宿主样式表与本模块自身),否则下面的 `[]` 一文不值 |
| 103 | + expect(files.length, "扫描域不该缩到空(T9 实测生产面 330 件)").toBeGreaterThanOrEqual(300); |
| 104 | + for (const known of ["main.tsx", "ui/primitives/motion.css", "motion/shift.ts"]) { |
| 105 | + expect(rels, `域内必须含 ${known}(遍历器静默失效时本判据会变恒真)`).toContain(known); |
| 106 | + } |
| 107 | + |
| 108 | + const violations = files.flatMap((f) => shiftViolations(readText(f), relOf(SRC_ROOT, f))); |
| 109 | + expect(violations, `全仓位移超过规格 §8.4 的 ${SHIFT_MAX_PX}px 上限:\n${violations.join("\n")}`).toEqual([]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("域收窄的守门:测试文件一个都不许进域(否则 R8.1 的测量样本会当场假红)", () => { |
| 113 | + const leaked = walk(SRC_ROOT) |
| 114 | + .map((f) => relOf(SRC_ROOT, f)) |
| 115 | + .filter((f) => TEST_FILE.test(f)); |
| 116 | + expect(leaked, "测试文件被拉进域 ⇒ `engine.test.ts` 实测的 50px 测量样本会假红(域收窄理由见文件头)").toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it("口径锚:>8px 命中 / =8px 不命中 / `%`·`var()` 跳过 / 注释里的字面量不算命中 / rem 按 16 换算", () => { |
| 120 | + // 犯规样本用拼接写(本文件文本里不出现完整犯规字面量 —— 防「全文件文本扫描」型守卫的误伤) |
| 121 | + const bad = "transform: rotate(0deg) translateY(" + "12px)"; |
| 122 | + expect(shiftViolations(bad, "synth.css")).toEqual(["synth.css: translateY(12px) = 12px > 8px"]); |
| 123 | + |
| 124 | + expect(shiftViolations("transform: translateY(" + "8px)", "synth.css"), "8px 恰在上限上(`>` 不是 `>=`)").toEqual([]); |
| 125 | + expect(shiftViolations("transform: translate(-50%, -50%)", "synth.css"), "居中技巧不是位移量").toEqual([]); |
| 126 | + expect(shiftViolations("transform: translateX(var(--ed-x, 4px))", "synth.css"), "var() 不是字面量").toEqual([]); |
| 127 | + |
| 128 | + const hidden = "/* translateY(" + "12px) */\n.x { transform: translateY(" + "1px); }"; |
| 129 | + expect(hidden, "锚自身必须含犯规样本,否则它在测空气").toContain("translateY("); |
| 130 | + expect(shiftViolations(hidden, "synth.css"), "剥注释失效 ⇒ 本守卫会在注释上假红").toEqual([]); |
| 131 | + |
| 132 | + expect(shiftViolations("transform: translateY(0.6rem)", "synth.css"), "0.6rem = 9.6px 应被判红").toHaveLength(1); |
| 133 | + expect(shiftViolations("transform: translateY(0.5rem)", "synth.css"), "0.5rem = 8px 不判").toEqual([]); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +describe("V4b `motion/` 层的位移常量 ≤ 8px(R11.4 的第二半)", () => { |
| 138 | + it("`motion/**` 生产模块的 `*_PX` 具名常量逐条 ≤ SHIFT_MAX_PX", () => { |
| 139 | + const files = walk(MOTION_ROOT).filter((f) => SOURCE_FILE.test(f)); |
| 140 | + const consts = files.flatMap((f) => |
| 141 | + numericConstants(readText(f), true).map((c) => ({ file: relOf(SRC_ROOT, f), ...c })), |
| 142 | + ); |
| 143 | + // 防空扫:本层的位移出口常量必须在这份名单里(否则是遍历器/正则失效) |
| 144 | + expect(consts.map((c) => c.name), "`motion/` 层的 `*_PX` 名单里必须有唯一出口常量").toContain("SHIFT_MAX_PX"); |
| 145 | + for (const c of consts) { |
| 146 | + expect(c.value, `${c.file}: ${c.name} = ${c.value} 越过 §8.4 的 ${SHIFT_MAX_PX}px`).toBeLessThanOrEqual(SHIFT_MAX_PX); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + it("`shift.ts` 自身的**全部**导出数值常量 ≤ 8px(不只 `_PX` 命名面 —— 它是位移模块,时长不该住这)", () => { |
| 151 | + const consts = numericConstants(readText(join(MOTION_ROOT, "shift.ts")), false); |
| 152 | + expect(consts.map((c) => c.name)).toContain("SHIFT_MAX_PX"); |
| 153 | + for (const c of consts) { |
| 154 | + expect(c.value, `motion/shift.ts: ${c.name} = ${c.value} 越过 §8.4 的 ${SHIFT_MAX_PX}px`).toBeLessThanOrEqual(SHIFT_MAX_PX); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + it("口径锚:合成的 `_PX` 常量 —— >8px 必须被抓到、=8px 不抓、非 `_PX` 名在 `_PX` 口径下不抓", () => { |
| 159 | + const bad = "export const CARD_LIFT_PX = " + "12;"; |
| 160 | + const ok = "export const SHIFT_MAX_PX = " + "8;"; |
| 161 | + const other = "export const STAGGER_MS = " + "60;"; |
| 162 | + expect(numericConstants(bad, true).map((c) => c.value)).toEqual([12]); |
| 163 | + expect(numericConstants(ok, true).map((c) => c.value)).toEqual([8]); |
| 164 | + expect(numericConstants(other, true), "`_PX` 口径只收位移常量(毫秒常量不在此列)").toEqual([]); |
| 165 | + expect(numericConstants(other, false).map((c) => c.value), "`shift.ts` 全量口径收得下它").toEqual([60]); |
| 166 | + }); |
| 167 | +}); |
0 commit comments