Files
2026-11-25 18:39:58 +08:00

89 lines
4.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/* 图库底栏图标的静态闸门(v1.7.1)
* 只改尺寸,不改交互;这些断言把「设计 token 口径」钉住,防止以后调样式时又缩回 14px/20×20。
* 运行:node gallery/tools/harness.mjs(已并入根 npm test)
*/
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const HERE = path.dirname(fileURLToPath(import.meta.url))
const ROOT = path.resolve(HERE, '..')
const src = readFileSync(path.join(ROOT, 'src/index.js'), 'utf8')
const css = readFileSync(path.join(ROOT, 'styles.css'), 'utf8')
const pkg = JSON.parse(readFileSync(path.join(ROOT, 'package.json'), 'utf8'))
let failed = 0
const ok = (name, cond, actual) => {
if (!cond) failed++
console.log((cond ? '✓ ' : '✗ ') + name + (actual === undefined ? '' : ' → ' + JSON.stringify(actual)))
}
// ---- 版本两处一致 ----
ok('package.json version = 1.7.2', pkg.version === '1.7.2', pkg.version)
ok('manifest.version = 1.7.2', /version:\s*'1\.7\.2'/.test(src))
// ---- 图标尺寸:宿主 design-tokens.md §3.8 闭集 14/16/18,取最大档 ----
ok('SVG 尺寸常量 ICON_PX = 18', /\bvar ICON_PX = 18\b/.test(src))
ok('SVG width/height 都取 ICON_PX(不是写死 14)',
/setAttribute\('width', String\(ICON_PX\)\)/.test(src) &&
/setAttribute\('height', String\(ICON_PX\)\)/.test(src))
ok('描边按规范 stroke-width 1.6', /setAttribute\('stroke-width', '1\.6'\)/.test(src))
ok('CSS 图形尺寸走 --iwb-icon-lg(宿主 token 缺失才兜底 18px)',
/\.iwb-gal-foot \.iwb-gal-icobtn svg\s*\{[^}]*width:\s*var\(--iwb-icon-lg, 18px\)[^}]*height:\s*var\(--iwb-icon-lg, 18px\)/.test(css))
ok('命中区 28×28(§3.8「≥28×28」)',
/width:\s*28px;\s*height:\s*28px;\s*min-width:\s*28px/.test(css))
ok('工具组间距 4px(三个大图标不再紧贴)', /\.iwb-gal-tools\s*\{[^}]*gap:\s*4px/.test(css))
ok('设置浮层 bottom 随底栏长高到 40px(仍用 34 会被图标压住)',
/\.iwb-gal-settings\s*\{[^}]*bottom:\s*40px/.test(css))
ok('浮层层级走 --iwb-z-*(不写死裸 z-index,token-lint 会拦)',
/\.iwb-gal-settings\s*\{[^}]*z-index:\s*var\(--iwb-z-ctx-menu, 91\)/.test(css) &&
!/z-index:\s*\d+/.test(css))
// ---- 反向断言:旧的「小图标」写法不得回流 ----
ok('不再有 14px 的 SVG 属性', !/setAttribute\('(?:width|height)', '14'\)/.test(src))
ok('不再有 stroke-width 1.5', !/setAttribute\('stroke-width', '1\.5'\)/.test(src))
ok('不再有 20×20 命中区', !/width:\s*20px;\s*height:\s*20px;\s*min-width:\s*20px/.test(css))
ok('不再有 gap: 0 的紧贴按钮', !/gap:\s*0\s*;/.test(css))
ok('不再有 bottom: 34px', !/bottom:\s*34px/.test(css))
// ---- 交互与契约不变 ----
ok('三个图标仍是刷新 / 打开本地目录 / 设置',
/iconBtn\('刷新'/.test(src) && /iconBtn\('打开本地目录'/.test(src) && /iconBtn\('设置'/.test(src))
ok('按钮类名未变(宿主旧 .iwb-btn-sm 不会盖回来)',
/class:\s*'iwb-btn iwb-btn-sm iwb-gal-icobtn'/.test(src) &&
/\.iwb-gal-foot \.iwb-gal-icobtn/.test(css))
ok('悬停 / 键盘聚焦反馈仍在',
/\.iwb-gal-foot \.iwb-gal-icobtn:hover\s*\{[^}]*background/.test(css) &&
/\.iwb-gal-foot \.iwb-gal-icobtn:focus-visible/.test(css))
ok('左侧计数 / 选中 / 状态文字字号没被顺手改掉',
/\.iwb-gal-count\s*\{[^}]*font-size:\s*11px/.test(css) &&
/\.iwb-gal-sel\s*\{[^}]*font-size:\s*11px/.test(css))
// ---- v1.7.2:图片卡片只留下载;顶部/右键插入入口不回退 ----
ok('图片卡片上没有「插入」按钮', !/acts\.appendChild\(act\('插入'/.test(src))
ok('图片卡片保留「下载」按钮', /acts\.appendChild\(act\('下载'/.test(src))
ok('顶部工具栏「插入」仍在', /actions\.appendChild\(btn\('插入'/.test(src))
ok('右键菜单「插入画布」仍在', /item\('插入画布'/.test(src))
// ---- 尺寸闭集(对齐宿主 plugin-scale-contract,防止放大时又引入散值)----
const FONT = new Set([11, 12, 13, 14, 15, 17, 20, 24])
const RADIUS = new Set([6, 8, 10, 12, 14, 999])
const SPACE = new Set([0, 2, 4, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 64])
const stray = []
for (const m of css.matchAll(/([a-z-]+)\s*:\s*([^;{}]+)/g)) {
const prop = m[1].toLowerCase()
const val = m[2].trim()
if (val.includes('var(')) continue
const px = [...val.matchAll(/([0-9.]+)px/g)].map((x) => Number(x[1]))
if (prop === 'font-size' && px.some((n) => !FONT.has(n))) stray.push('font-size:' + val)
else if (prop === 'border-radius' && px.some((n) => !RADIUS.has(n))) stray.push('border-radius:' + val)
else if (/^(gap|row-gap|column-gap)$/.test(prop) || /^(padding|margin)/.test(prop)) {
if (px.some((n) => !SPACE.has(n))) stray.push(prop + ':' + val)
}
}
ok('CSS 字号 / 圆角 / 间距无新增散值', stray.length === 0, stray)
console.log(failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过(gallery 1.7.2 卡片只留下载)')
if (failed) process.exitCode = 1