scrollbar-styles.client.spec.ts 25 KB

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