import { readFileSync } from 'node:fs' import { test } from 'node:test' import assert from 'node:assert/strict' import { findRuleBlocks, matchesSelectorText, fontSizeFor } from '../src/client/core/css-rules.ts' import { LAYOUT_CSS } from '../src/client/styles/layout.css.ts' import { BASE_CSS } from '../src/client/styles/base.css.ts' import { COMPAT_CSS } from '../src/client/styles/compat.css.ts' import { MISC_CSS } from '../src/client/styles/misc.css.ts' const MESSAGE_P: any = { tag: 'p', ancestors: [{ tag: 'div', classes: ['abc_scroll'] }], } test('findRuleBlocks descends into at-rules and keeps selector/body pairs', () => { const css = '@keyframes spin { from { opacity: 0 } to { opacity: 1 } }\n@media (max-width: 700px) { .x:has(p) { font-size: 15px !important; } }' const blocks = findRuleBlocks(css) assert.equal(blocks.length, 1) assert.match(blocks[0].selector, /:has\(p\)/) assert.match(blocks[0].body, /15px/) }) test('matchesSelectorText handles plain, class-substring and :has() arms', () => { assert.equal(matchesSelectorText('[class*="_scroll"]:has(p)', MESSAGE_P), true) assert.equal(matchesSelectorText('[class*="_scrollBody"]:has(p)', MESSAGE_P), false) // The wrapped spelling this repo writes for long arms (layout.css.ts:327-329) // must stay readable, or a px rule whose only subject arm is wrapped is invisible. assert.equal(matchesSelectorText('[ class*="abc_scroll" ]', MESSAGE_P), true) assert.equal(matchesSelectorText('p', MESSAGE_P), true) assert.equal(matchesSelectorText('li', MESSAGE_P), false) }) test('the message text family carries no hardcoded px font-size', () => { const hit = fontSizeFor(LAYOUT_CSS, MESSAGE_P) assert.ok(hit !== null, 'expected a message text rule to match the fixture') assert.doesNotMatch(hit.value, /^\s*[\d.]+px/, `hardcoded size in: ${hit.selector}`) assert.match(hit.value, /var\(/) }) test('long-conversation message blocks skip off-screen layout (2026-10-07 实测)', () => { // 真宿主取证:点抽屉在长会话上 236ms longtask、短会话 0ms;profile 显示代价在宿主渲染 // ((program) 1216ms + 宿主 getAnimations 75ms),插件 JS 仅 ~8ms。 // 靠给会话滚动区里的直接子块 content-visibility:auto 削掉(A/B:247/62/143ms → 57/61/70ms)。 const selector = '[data-mobile-nav="frame"] [class*="scrollBody"] [class*="_scroll"]:not([class*="scrollBody"]) > *' assert.ok(LAYOUT_CSS.includes(selector), '窄选择器(只打滚动区的直接子块)在位') assert.ok(LAYOUT_CSS.includes('content-visibility: auto'), 'content-visibility 在位') assert.ok(LAYOUT_CSS.includes('contain-intrinsic-size: auto 320px'), '记住真实高度、只给未渲染块兜底') // 滚动容器自身绝不能被设成 content-visibility(那会把整段会话藏掉)。 assert.ok(!LAYOUT_CSS.includes('[class*="scrollBody"] {'), '没有把 scrollBody 自身当选择器') }) test('document preview code lines skip off-screen layout (2026-10-09 实测)', () => { // 报障:打开 13.4k 行文件的文档预览(DOM 23,662 节点)后整页卡死。 // 单帧最差 269/310/395/485/530ms、rAF 帧率 2/5/17、长任务每秒最高 1031ms; // 加规则后最长帧 33ms、帧率 88–119、长任务归零。 // 代码行行高统一(约 20px),所以估值用 20px —— 与消息块那条的 320px 粗兜底 // 分开写;scrollBody 那条不适合复用(消息高度差异大)。 const selector = '[data-mobile-nav="frame"] [class*="_textDocument"] [class*="_line"]' assert.ok(LAYOUT_CSS.includes(selector), '窄选择器:只在移动 frame 的文档预览代码行容器内') assert.ok(LAYOUT_CSS.includes('contain-intrinsic-size: auto 20px'), '代码行估值 20px') // 泛词根不许进:_preview 同模块也有,但 chat / conversation / deliverables // 这些别的宿主模块同样带这个名字,纳入就会越界改到它们的面板。 assert.ok( !LAYOUT_CSS.includes('[class*="_preview"] [class*="_line"]'), '不收 _preview 词根', ) }) test('the Statistics table keeps readable metric columns only on mobile (#294)', () => { // 宿主最窄列占 5.5%,单元格左右各 8px,数值占 62px;只开 overflow 不能阻止挤列。 const mobile = /@media \(max-width: 1023px\) and \(pointer: coarse\) \{[^{}]*\[class\*="OiOZ0a_table"\] \{([^{}]*)\}\s*\}/.exec(COMPAT_CSS) assert.ok(mobile !== null, '统计表宽度下限必须限定在触屏手机/平板媒体查询内') const minWidth = Number(/min-width:\s*(\d+)px/.exec(mobile[1])?.[1]) assert.ok(minWidth * 0.055 - 16 >= 62, '最窄指标列必须容得下宿主的 62px 数值') const rules = findRuleBlocks(BASE_CSS + LAYOUT_CSS + COMPAT_CSS + MISC_CSS) .filter((block) => block.selector.includes('OiOZ0a_table')) assert.equal(rules.length, 1, '统计表只允许这一条移动端规则,不向桌面泄漏') }) test('the phone stats row keeps its tightened spacing (2026-10-07)', () => { // 宿主三层叠出来的空隙:root gap 12 + sep margin 6/6 + pill padding 8/8。 // 手机档收成 6 / 2 / 6 —— 三条都必须带 stats 标记作用域,换代后只会惰性化。 const scope = '[data-mobile-nav="frame"] [data-phase] [data-mobile-nav="stats"]' const blocks = findRuleBlocks(LAYOUT_CSS).filter((block) => block.selector.includes(scope)) const row = blocks.find((block) => block.body.includes('gap:')) assert.ok(row !== undefined, 'stats 行的手机规则块在位') assert.match(row.body, /gap: 6px !important/) const sep = blocks.find((block) => block.selector.includes('[class*="_sep"]')) assert.ok(sep !== undefined, '分隔符规则块在位') assert.match(sep.body, /margin: 0 2px !important/) const pill = blocks.find((block) => block.selector.includes('[class*="_pill"]')) assert.ok(pill !== undefined, '指标胶囊规则块在位') assert.match(pill.body, /padding-left: 6px !important/) assert.match(pill.body, /padding-right: 6px !important/) }) /** Last `property:` value in a rule body — the one the cascade applies when the * same rule declares the property twice. Declarations sit one per line, and a * comment may precede the first one, so anchor on line starts. */ function lastDeclaration(body: string, property: string): string | null { const values = [...body.matchAll(new RegExp(`^\\s*${property}:\\s*([^;]+);`, 'gm'))] return values.at(-1)?.[1].trim() ?? null } test('the composer file button paints one pill, not two (2026-10-07 店主报障)', () => { // 真机实测(按下瞬间像素剖面):外圈 130 设备px 的 233(宿主按钮盒底色,圆角 8) // + 内圈 108 设备px 的 222(我们 ::before 胶囊)—— 半透明叠半透明,重叠区更深。 // 压层叠(!important)在真机上压不住那一层,于是改成**结构上只有一层**: // 可见胶囊 = 按钮盒本身(28x28、全圆),::before 永久透明。 const blocks = findRuleBlocks(LAYOUT_CSS) const box = blocks.find((block) => !block.selector.includes('::before') && !block.selector.includes(':hover') && block.selector.includes('[data-mobile-nav="file-upload"]') && /width:\s*28px !important/.test(block.body)) assert.ok(box !== undefined, '盒子必须是可见胶囊尺寸 28x28') // 「块内出现过 999px」不是层叠判据:同规则后面再写一条 `border-radius: 4px // !important`,渲染值就变成 4px,而先出现的那条仍在(2026-10-07「双层胶囊」 // 回归正是这个形状)。同规则内后者胜,故读最后一条声明。 const radius = lastDeclaration(box.body, 'border-radius') assert.ok(radius !== null, '盒子必须有 border-radius 声明') assert.equal(radius, '999px', '盒子的生效圆角必须是全圆(后写的同名声明会覆盖它)') assert.match(box.body, /margin:\s*0 0 0 -8px !important/, '盒宽 34→28 后 margin 要跟着收,图标中心不动') const before = blocks.find((block) => block.selector.includes('[data-mobile-nav="file-upload"]::before') && block.selector.includes(':hover')) assert.equal(before, undefined, '胶囊不再由 ::before 画(否则又是两层)') const painted = blocks.find((block) => block.selector.includes('[data-mobile-nav="file-upload"]:hover') && block.selector.includes('file-upload"]:focus-visible')) assert.ok(painted !== undefined, '盒子的按下反馈规则在位') assert.match(painted.body, /background:\s*var\(--dsw-alias-interactive-bg-hover/) assert.match(painted.body, /box-shadow:\s*none !important/, '万一那一层是 inset shadow 画的,一并压掉') const hit = blocks.find((block) => block.selector.includes('[data-mobile-nav="file-upload"]::after')) assert.match(hit.body, /inset:\s*-7px/, '命中区仍外扩(28 盒 ⇒ -7 保持约 42)') }) test('plugin-manager cards and rows are tappable as a whole on mobile (2026-10-07 店主报障)', () => { // 宿主只把标题做成