scrollbar-styles.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /**
  2. * Scrollbar stylesheet contract, asserted against the CSS text on disk: every
  3. * --dsw-alias-scrollbar-* token design-platform.css defines has a consumer,
  4. * scrollbar.css binds the base-surface pair through the rebindable
  5. * indirection, and elevated surfaces rebind that indirection in complete
  6. * pairs. The expected token set is scanned out of design-platform.css, so
  7. * adding, renaming, or dropping a scrollbar token moves these assertions with
  8. * it.
  9. */
  10. import { readdirSync, readFileSync } from 'node:fs'
  11. import { join } from 'node:path'
  12. import { fileURLToPath } from 'node:url'
  13. import { describe, expect, it } from 'vitest'
  14. /** One flattened CSS rule: its comma-separated selector parts and its declarations in source order. */
  15. interface CssRule {
  16. selectors: string[]
  17. declarations: [property: string, value: string][]
  18. }
  19. const STYLES = new URL('../src/styles/', import.meta.url)
  20. const PACKAGES_DIR = fileURLToPath(new URL('../../../', import.meta.url))
  21. const read = (name: string): string => readFileSync(fileURLToPath(new URL(name, STYLES)), 'utf8')
  22. const platformCss = read('design-platform.css')
  23. const scrollbarCss = read('scrollbar.css')
  24. /** Body attribute selecting the dark palette; ui-layout's ThemePresenter sets it. */
  25. const DARK_ATTRIBUTE = '[data-ds-dark-theme]'
  26. /** Alias tokens under test: the prefix the elevation pairs share. */
  27. const TOKEN_PREFIX = '--dsw-alias-scrollbar-'
  28. /** Prefix of the rebindable indirection scrollbar.css owns. */
  29. const INDIRECTION_PREFIX = '--dsh-scrollbar-'
  30. /** The one non-token rebind value: a surface that draws no thumb at all. */
  31. const HIDDEN_THUMB = 'transparent'
  32. /** The elevation rebind, spelled per property: value-wholeness, not token shape. */
  33. const ELEVATED_REBIND = new Map([
  34. ['--dsh-scrollbar-thumb', '--dsw-alias-scrollbar-bg-l2'],
  35. ['--dsh-scrollbar-thumb-hover', '--dsw-alias-scrollbar-hover-l2'],
  36. ].map(([property, token]) => [property!, `var(${token!})`]))
  37. /**
  38. * Flatten a stylesheet into rules. Whitespace, declaration order, and trailing
  39. * semicolons are normalized away; nesting and at-rules are not handled, which
  40. * no sheet under test uses for scrollbar declarations.
  41. * @param css - stylesheet text.
  42. * @returns one entry per rule, in source order.
  43. */
  44. function parseRules(css: string): CssRule[] {
  45. const withoutComments = css.replace(/\/\*[\s\S]*?\*\//g, ' ')
  46. const rules: CssRule[] = []
  47. // Destructuring defaults only satisfy noUncheckedIndexedAccess; both groups
  48. // are unconditional in the pattern.
  49. for (const [, selector = '', body = ''] of withoutComments.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
  50. const declarations = body
  51. .split(';')
  52. .map(part => part.trim())
  53. .filter(part => part.includes(':'))
  54. .map((part): [string, string] => {
  55. const colon = part.indexOf(':')
  56. return [part.slice(0, colon).trim(), part.slice(colon + 1).trim()]
  57. })
  58. rules.push({ selectors: selector.split(',').map(part => part.trim()), declarations })
  59. }
  60. return rules
  61. }
  62. /**
  63. * Half-open source span of one at-rule's block, excluding its prelude.
  64. * @param css - stylesheet text.
  65. * @param prelude - exact at-rule prelude to locate, without the opening brace.
  66. * @returns the block's brace offsets, or undefined when the prelude is absent.
  67. */
  68. function atRuleBlock(css: string, prelude: string): { start: number; end: number } | undefined {
  69. const opening = css.indexOf(`${prelude} {`)
  70. if (opening === -1) return undefined
  71. const start = css.indexOf('{', opening)
  72. let depth = 0
  73. for (let index = start; index < css.length; index += 1) {
  74. if (css[index] === '{') depth += 1
  75. else if (css[index] === '}') {
  76. depth -= 1
  77. if (depth === 0) return { start, end: index }
  78. }
  79. }
  80. throw new Error(`unbalanced braces after ${prelude}`)
  81. }
  82. /**
  83. * Custom-property names a value reads.
  84. * @param value - declaration value, possibly with nested var() calls.
  85. * @returns every referenced custom-property name, in source order.
  86. */
  87. function varReferences(value: string): string[] {
  88. return [...value.matchAll(/var\(\s*(--[\w-]+)/g)].map(([, name = '']) => name)
  89. }
  90. /**
  91. * Every CSS file shipped as package source, excluding build output and
  92. * installed dependencies.
  93. * @returns absolute paths of the stylesheets under packages/.
  94. */
  95. function packageStylesheets(): string[] {
  96. const found: string[] = []
  97. const walk = (dir: string): void => {
  98. for (const entry of readdirSync(dir, { withFileTypes: true })) {
  99. const path = join(dir, entry.name)
  100. if (entry.isDirectory()) {
  101. if (entry.name !== 'node_modules' && entry.name !== 'lib' && entry.name !== 'dist') walk(path)
  102. } else if (entry.name.endsWith('.css')) found.push(path)
  103. }
  104. }
  105. walk(PACKAGES_DIR)
  106. return found
  107. }
  108. /**
  109. * Tokens a stylesheet reads through its rendering declarations, following its
  110. * own custom-property definitions transitively so a token reached only through
  111. * an indirection counts. The walk starts from the standard-property
  112. * declarations, so a defined-but-unread indirection contributes nothing.
  113. * @param rules - parsed rules of one stylesheet.
  114. * @returns every `--dsw-*` token the sheet's rendering declarations depend on.
  115. */
  116. function tokensRendered(rules: CssRule[]): Set<string> {
  117. const definitions = new Map<string, string>()
  118. const pending: string[] = []
  119. for (const rule of rules) {
  120. for (const [property, value] of rule.declarations) {
  121. if (property.startsWith('--')) definitions.set(property, value)
  122. else pending.push(value)
  123. }
  124. }
  125. const reached = new Set<string>()
  126. const visited = new Set<string>()
  127. while (pending.length > 0) {
  128. for (const name of varReferences(pending.pop()!)) {
  129. if (name.startsWith('--dsw-')) reached.add(name)
  130. if (visited.has(name)) continue
  131. visited.add(name)
  132. const definition = definitions.get(name)
  133. if (definition !== undefined) pending.push(definition)
  134. }
  135. }
  136. return reached
  137. }
  138. const platformRules = parseRules(platformCss)
  139. const scrollbarRules = parseRules(scrollbarCss)
  140. const sorted = (names: Iterable<string>): string[] => [...names].sort()
  141. /**
  142. * Scrollbar tokens defined by the rules whose selectors carry (or do not
  143. * carry) the dark palette attribute.
  144. * @param dark - true to scan the dark blocks, false to scan the light blocks.
  145. * @returns the scrollbar token names defined there.
  146. */
  147. function definedTokens(dark: boolean): Set<string> {
  148. const names = new Set<string>()
  149. for (const rule of platformRules) {
  150. if (rule.selectors.every(selector => selector.includes(DARK_ATTRIBUTE)) !== dark) continue
  151. for (const [property] of rule.declarations) {
  152. if (property.startsWith(TOKEN_PREFIX)) names.add(property)
  153. }
  154. }
  155. return names
  156. }
  157. const lightTokens = definedTokens(false)
  158. const darkTokens = definedTokens(true)
  159. const allTokens = new Set([...lightTokens, ...darkTokens])
  160. /** Every scrollbar token any package stylesheet references, mapped to the files referencing it. */
  161. const referencedTokens = new Map<string, string[]>()
  162. /** Every indirection property any package stylesheet outside ui-theme declares, mapped to its declaring rules. */
  163. const rebindRules: { file: string; rule: CssRule }[] = []
  164. /**
  165. * What one stylesheet contributes to the elevated-surface question: which
  166. * elevated surfaces it paints, whether any rule scrolls, and whether it
  167. * rebinds. Kept per file rather than per rule because the elevated card and the
  168. * descendant that actually scrolls are separate rules in the same sheet, and
  169. * CSS text does not express which contains which.
  170. */
  171. interface SheetSurfaces {
  172. /** Elevated surface tokens this sheet paints anywhere. */
  173. elevated: Set<string>
  174. /** True when some rule declares `overflow*: auto|scroll`. */
  175. scrolls: boolean
  176. /**
  177. * True when some rule rebinds the indirection to an ELEVATION. A rule that
  178. * only hides the bar (`transparent`) does not count: it states no elevation,
  179. * so a sheet that hides its bars and also scrolls on an elevated surface
  180. * still owes the l2 pair for whatever draws a thumb there.
  181. */
  182. rebindsElevation: boolean
  183. }
  184. const sheetSurfaces = new Map<string, SheetSurfaces>()
  185. /** Properties whose `auto`/`scroll` value makes a rule a scroll container. */
  186. const OVERFLOW_PROPERTIES = ['overflow', 'overflow-x', 'overflow-y']
  187. /** Properties that paint a surface, and so identify the elevation a rule sits on. */
  188. const SURFACE_PROPERTIES = ['background', 'background-color']
  189. /**
  190. * Token families that name a SURFACE — a background an element is drawn on, and
  191. * so something a scrollbar can sit against. `--dsw-alias-button-*`,
  192. * `--dsw-alias-interactive-*`, and `--dsw-alias-markdown-*` reach the same dark
  193. * elevation rungs while naming a control or an inline span, which no scroll
  194. * container renders its bar against (ChatView's floating `.toBottom` pill,
  195. * CodeBlock's banner). Family, not geometry: a floating button legitimately
  196. * carries a radius, a shadow, and a fixed size, so shape cannot separate them.
  197. */
  198. const SURFACE_TOKEN_PATTERN = /^--dsw-(?:alias-bg-|specific-)/
  199. /**
  200. * The palette's own dark elevation ladder, resolved from `design-platform.css`:
  201. * `bg-layer-2` and `bg-layer-3` are the rungs above the base surfaces, and the
  202. * l1/l2 scrollbar split encodes exactly that step. Reading it from the palette
  203. * rather than from the sheets that happen to rebind is what lets the check flag
  204. * a surface NOBODY has rebound yet.
  205. * @returns surface tokens whose dark value sits on an elevated rung.
  206. */
  207. function elevatedRungs(): Set<string> {
  208. const definitions = new Map<string, string>()
  209. for (const rule of platformRules) {
  210. // Dark declarations come later in the sheet and overwrite the light ones,
  211. // which is the palette this distinction exists in.
  212. for (const [property, value] of rule.declarations) definitions.set(property, value)
  213. }
  214. const resolve = (name: string): string => {
  215. const seen = new Set<string>()
  216. let current = name
  217. while (definitions.has(current) && !seen.has(current)) {
  218. seen.add(current)
  219. const value = definitions.get(current)!
  220. const [reference] = varReferences(value)
  221. if (reference === undefined) return value
  222. current = reference
  223. }
  224. return current
  225. }
  226. const rungs = new Set([resolve('--dsw-alias-bg-layer-2'), resolve('--dsw-alias-bg-layer-3')])
  227. const tokens = new Set<string>()
  228. for (const name of definitions.keys()) {
  229. if (SURFACE_TOKEN_PATTERN.test(name) && rungs.has(resolve(name))) tokens.add(name)
  230. }
  231. return tokens
  232. }
  233. const elevatedSurfaces = elevatedRungs()
  234. for (const file of packageStylesheets()) {
  235. const rules = parseRules(readFileSync(file, 'utf8'))
  236. const surfaces: SheetSurfaces = { elevated: new Set(), scrolls: false, rebindsElevation: false }
  237. for (const rule of rules) {
  238. let rebinds = false
  239. let rebindsElevation = false
  240. const ruleSurfaces: string[] = []
  241. for (const [property, value] of rule.declarations) {
  242. if (property.startsWith(INDIRECTION_PREFIX) && file !== fileURLToPath(new URL('scrollbar.css', STYLES))) {
  243. rebinds = true
  244. if (value !== HIDDEN_THUMB) rebindsElevation = true
  245. }
  246. if (OVERFLOW_PROPERTIES.includes(property) && /\b(?:auto|scroll)\b/.test(value)) surfaces.scrolls = true
  247. if (SURFACE_PROPERTIES.includes(property)) ruleSurfaces.push(...varReferences(value))
  248. for (const token of varReferences(value)) {
  249. if (!token.startsWith(TOKEN_PREFIX)) continue
  250. referencedTokens.set(token, [...referencedTokens.get(token) ?? [], file])
  251. }
  252. }
  253. for (const token of ruleSurfaces) {
  254. if (elevatedSurfaces.has(token)) surfaces.elevated.add(token)
  255. }
  256. if (rebinds) rebindRules.push({ file, rule })
  257. if (rebindsElevation) surfaces.rebindsElevation = true
  258. }
  259. sheetSurfaces.set(file, surfaces)
  260. }
  261. describe('design-platform.css scrollbar tokens', () => {
  262. it('defines the same scrollbar token set in the light and the dark block', () => {
  263. // A token present only in the light block silently keeps its light value
  264. // under the dark palette, since the dark block only overrides.
  265. expect(allTokens.size).toBeGreaterThan(0)
  266. expect(sorted(lightTokens)).toEqual(sorted(allTokens))
  267. expect(sorted(darkTokens)).toEqual(sorted(allTokens))
  268. })
  269. it('resolves every scrollbar token to a static scale value, not to another alias', () => {
  270. // The alias layer is the only indirection in the token sheet: an alias
  271. // pointing at a second alias makes the dark override order-dependent.
  272. for (const rule of platformRules) {
  273. for (const [property, value] of rule.declarations) {
  274. if (!property.startsWith(TOKEN_PREFIX)) continue
  275. for (const reference of varReferences(value)) {
  276. expect(reference, `${property}: ${value}`).toMatch(/^--dsw-static-/)
  277. }
  278. }
  279. }
  280. })
  281. })
  282. describe('scrollbar token consumers', () => {
  283. it('every defined scrollbar token is referenced by some package stylesheet', () => {
  284. // Before scrollbar.css existed these tokens had no consumer at all and
  285. // every scroll container rendered the unthemed UA bar. A fifth token, or a
  286. // rename on one side only, leaves the new name unreferenced here.
  287. expect(sorted(referencedTokens.keys())).toEqual(sorted(allTokens))
  288. })
  289. it('every referenced scrollbar token is defined in design-platform.css', () => {
  290. // A dangling var() renders the UA default instead of failing loudly, so a
  291. // rename has to move the reference and the definition together.
  292. for (const [token, files] of referencedTokens) {
  293. expect(allTokens, files.join(', ')).toContain(token)
  294. }
  295. })
  296. })
  297. describe('scrollbar.css base-surface binding', () => {
  298. const rendered = tokensRendered(scrollbarRules)
  299. it('renders the l1 pair through the rebindable indirection', () => {
  300. // l1 is the base-surface default the indirection resolves to; the
  301. // indirection only counts as bound when a rendering declaration reads it.
  302. expect(rendered).toContain(`${TOKEN_PREFIX}bg-l1`)
  303. expect(rendered).toContain(`${TOKEN_PREFIX}hover-l1`)
  304. })
  305. it('routes the standard property and the WebKit thumb through the same indirection', () => {
  306. // A rebind on an elevated container has to move the Firefox and the WebKit
  307. // rendering together, which only holds while both read the same variable.
  308. const declaration = (property: string, selectorPart: string): string | undefined => scrollbarRules
  309. .filter(rule => rule.selectors.includes(selectorPart))
  310. .flatMap(rule => rule.declarations)
  311. .findLast(([name]) => name === property)?.[1]
  312. const thumbColor = declaration('scrollbar-color', 'body')
  313. expect(thumbColor).toBeDefined()
  314. const indirection = varReferences(thumbColor!)[0]
  315. expect(indirection).toBe(`${INDIRECTION_PREFIX}thumb`)
  316. expect(varReferences(declaration('background', '::-webkit-scrollbar-thumb')!)).toEqual([indirection])
  317. })
  318. })
  319. describe('scrollbar.css selectors', () => {
  320. const scrollbarColorSelectors = scrollbarRules
  321. .filter(rule => rule.declarations.some(([property]) => property === 'scrollbar-color'))
  322. .flatMap(rule => rule.selectors)
  323. it('declares scrollbar-color only where the body-scoped tokens are visible', () => {
  324. // design-platform.css defines the alias tokens on `body`, and custom
  325. // properties inherit downward only: the same declaration on `html` or
  326. // `:root` resolves to the guaranteed-invalid value, which computes
  327. // scrollbar-color to `auto` and drops the theming entirely.
  328. expect(scrollbarColorSelectors.length).toBeGreaterThan(0)
  329. for (const selector of scrollbarColorSelectors) {
  330. expect(selector, selector).toMatch(/^body\b/)
  331. }
  332. })
  333. it('defines the indirection where the alias tokens are visible', () => {
  334. const definesIndirection = ([property, value]: [string, string]): boolean =>
  335. property.startsWith(INDIRECTION_PREFIX) && value.includes(TOKEN_PREFIX)
  336. const hosts = scrollbarRules
  337. .filter(rule => rule.declarations.some(definesIndirection))
  338. .flatMap(rule => rule.selectors)
  339. expect(hosts.length).toBeGreaterThan(0)
  340. for (const selector of hosts) expect(selector, selector).toMatch(/^body\b/)
  341. })
  342. it('re-declares the scrollbar properties per element rather than inheriting them', () => {
  343. // scrollbar-width is not an inherited property, and an inherited
  344. // scrollbar-color carries the colour already substituted at `body`, which
  345. // a descendant rebinding the indirection could no longer change.
  346. expect(scrollbarColorSelectors).toContain('body *')
  347. const widthSelectors = scrollbarRules
  348. .filter(rule => rule.declarations.some(([property]) => property === 'scrollbar-width'))
  349. .flatMap(rule => rule.selectors)
  350. expect(widthSelectors).toContain('body *')
  351. })
  352. })
  353. describe('scrollbar.css rendering paths', () => {
  354. /** The gate prelude, spelled exactly as the sheet must spell it for the split to exist. */
  355. const GATE = '@supports not selector(::-webkit-scrollbar)'
  356. const withoutComments = scrollbarCss.replace(/\/\*[\s\S]*?\*\//g, ' ')
  357. const gate = atRuleBlock(withoutComments, GATE)
  358. /** Standard scrollbar properties, the ones whose non-`auto` values suppress the pseudo-elements. */
  359. const STANDARD_PROPERTIES = ['scrollbar-width', 'scrollbar-color']
  360. it('gates the standard properties behind the absence of the WebKit pseudo-element', () => {
  361. // A non-`auto` scrollbar-width or scrollbar-color makes Chromium and
  362. // Safari discard every ::-webkit-scrollbar* rule for that element,
  363. // ::-webkit-scrollbar-thumb:hover included. Declaring both paths
  364. // unconditionally therefore renders the hover token nowhere: the engines
  365. // implementing the hover pseudo-element are exactly the ones the standard
  366. // properties silence, and Firefox has no hover pseudo-element at all.
  367. expect(gate, GATE).toBeDefined()
  368. for (const property of STANDARD_PROPERTIES) {
  369. const offsets = [...withoutComments.matchAll(new RegExp(String.raw`(^|[;{\s])${property}\s*:`, 'g'))]
  370. .map(match => match.index)
  371. expect(offsets.length, property).toBeGreaterThan(0)
  372. for (const offset of offsets) {
  373. expect(offset, `${property} outside ${GATE}`).toBeGreaterThan(gate!.start)
  374. expect(offset, `${property} outside ${GATE}`).toBeLessThan(gate!.end)
  375. }
  376. }
  377. })
  378. it('leaves the WebKit pseudo-element rules outside the gate', () => {
  379. // Gating these in turn would only restate selector matching: an engine
  380. // without the pseudo-elements drops the rules as unknown selectors. Inside
  381. // the gate they would be dropped by the engines that do implement them,
  382. // which is every engine that can render them.
  383. const offsets = [...withoutComments.matchAll(/::-webkit-scrollbar/g)]
  384. .map(match => match.index)
  385. .filter(offset => withoutComments.slice(offset).search(/^[\w:-]*\s*[,{]/) === 0)
  386. expect(offsets.length).toBeGreaterThan(0)
  387. for (const offset of offsets) {
  388. expect(offset > gate!.start && offset < gate!.end, `::-webkit-scrollbar rule inside ${GATE}`).toBe(false)
  389. }
  390. })
  391. it('renders the hover token only through the pseudo-element path', () => {
  392. // The standard path has no hover counterpart — scrollbar-color states one
  393. // thumb colour and the engine derives its own hover treatment — so the
  394. // hover indirection has to be read outside the gate or it renders nowhere.
  395. const hoverOffsets = [...withoutComments.matchAll(new RegExp(String.raw`var\(\s*${INDIRECTION_PREFIX}thumb-hover`, 'g'))]
  396. .map(match => match.index)
  397. expect(hoverOffsets.length).toBeGreaterThan(0)
  398. for (const offset of hoverOffsets) {
  399. expect(offset > gate!.start && offset < gate!.end, 'hover indirection read inside the gate').toBe(false)
  400. }
  401. })
  402. })
  403. describe('elevated surface rebinds', () => {
  404. it('at least one surface rebinds the indirection', () => {
  405. expect(rebindRules.length).toBeGreaterThan(0)
  406. })
  407. it('each rebinding rule sets the thumb and the hover variable together', () => {
  408. // A surface rebinding only the resting colour keeps the l1 hover colour,
  409. // so the elevation is wrong only while the pointer is over the thumb.
  410. for (const { file, rule } of rebindRules) {
  411. const properties = rule.declarations.map(([property]) => property).filter(property => property.startsWith(INDIRECTION_PREFIX))
  412. expect(sorted(properties), `${file} ${rule.selectors.join(', ')}`).toEqual([
  413. `${INDIRECTION_PREFIX}thumb-hover`, `${INDIRECTION_PREFIX}thumb`,
  414. ].sort())
  415. }
  416. })
  417. it('each rebinding rule binds the indirection names scrollbar.css renders', () => {
  418. // A misspelled property name declares an unused variable, and the surface
  419. // silently keeps the base-surface colour.
  420. const rendered = new Set(
  421. scrollbarRules
  422. .flatMap(rule => rule.declarations)
  423. .filter(([property]) => !property.startsWith('--'))
  424. .flatMap(([, value]) => varReferences(value))
  425. .filter(name => name.startsWith(INDIRECTION_PREFIX)),
  426. )
  427. for (const { file, rule } of rebindRules) {
  428. for (const [property] of rule.declarations) {
  429. if (property.startsWith(INDIRECTION_PREFIX)) expect(rendered, `${file}: ${property}`).toContain(property)
  430. }
  431. }
  432. })
  433. it('rebinds the pair to one target: the l2 elevation pair, or transparent', () => {
  434. // The rule as a whole, not each declaration on its own. Per-declaration
  435. // checking accepts a MIXED rule — `thumb: transparent` beside
  436. // `thumb-hover: var(--dsw-alias-scrollbar-hover-l2)` — which repaints the
  437. // bar the moment the pointer reaches it while passing a gate that claims
  438. // the two targets are exclusive.
  439. //
  440. // The elevation half compares the whole value against the pair's canonical
  441. // spelling rather than checking that every token it mentions ends in `-l2`.
  442. // A shape check admits `color-mix(…, var(--dsw-alias-scrollbar-bg-l2) 85%,
  443. // white)` and a crossed pair (the hover token bound to the resting
  444. // property); neither is what the contract says.
  445. for (const { file, rule } of rebindRules) {
  446. const rebinds = rule.declarations.filter(([property]) => property.startsWith(INDIRECTION_PREFIX))
  447. const where = `${file} ${rule.selectors.join(', ')}`
  448. if (rebinds.every(([, value]) => value === HIDDEN_THUMB)) continue
  449. expect(rebinds.some(([, value]) => value === HIDDEN_THUMB), `${where}: mixes ${HIDDEN_THUMB} with an elevation`).toBe(false)
  450. for (const [property, value] of rebinds) {
  451. expect(value, `${where}: ${property}`).toBe(ELEVATED_REBIND.get(property))
  452. }
  453. }
  454. })
  455. it('resolves the elevated surface set from the palette ladder', () => {
  456. // The set has to come from the palette, not from the sheets that happen to
  457. // rebind: derived from rebinds it can only confirm what someone already
  458. // remembered, and a surface nobody has rebound yet — the case the check
  459. // exists for — would define itself as unelevated. Anchoring it here means a
  460. // new palette token on an elevated rung is in scope the moment it is
  461. // defined. `--dsw-specific-tip` is the regression that proved the point: it
  462. // resolves to the same dark rung as the menu surface, and the Todo panel
  463. // scrolled on it unrebound while a rebind-derived set stayed green.
  464. expect(elevatedSurfaces).toContain('--dsw-alias-bg-layer-2')
  465. expect(elevatedSurfaces).toContain('--dsw-alias-bg-layer-3')
  466. expect(elevatedSurfaces).toContain('--dsw-specific-menu')
  467. expect(elevatedSurfaces).toContain('--dsw-specific-input-major')
  468. expect(elevatedSurfaces).toContain('--dsw-specific-tip')
  469. // Base surfaces stay out, or every scroll container would be in scope and
  470. // the check would say nothing.
  471. expect(elevatedSurfaces).not.toContain('--dsw-alias-bg-base')
  472. expect(elevatedSurfaces).not.toContain('--dsw-alias-bg-layer-1')
  473. })
  474. it('every sheet that scrolls on an elevated surface rebinds', () => {
  475. // The failure this closes: a scroll container on an elevated surface that
  476. // nobody remembered to rebind renders the l1 thumb, which differs from l2
  477. // only in the dark palette and only for that one surface — invisible both in
  478. // review and in a light-palette screenshot. Four sheets shipped that way
  479. // (ui-primitives Menu, InputBar, QuestionComposer, TodoPanel) and review
  480. // caught them by hand, which is what this replaces.
  481. //
  482. // Surface-level, not element-level: the elevated card and the descendant
  483. // that scrolls are separate rules, and CSS text does not say which contains
  484. // which. What keeps that from over-reporting is the token FAMILY: only
  485. // `--dsw-alias-bg-*` and `--dsw-specific-*` name a surface, so a floating
  486. // button or an inline code span reaching the same rung is out of scope
  487. // (ChatView's `.toBottom`, CodeBlock's banner). Geometry cannot make that
  488. // call — a floating button carries a radius, a shadow, and a fixed size.
  489. for (const [file, surfaces] of sheetSurfaces) {
  490. if (!surfaces.scrolls || surfaces.rebindsElevation) continue
  491. expect([...surfaces.elevated], `${file} scrolls on an elevated surface without rebinding`).toEqual([])
  492. }
  493. })
  494. })