base-styles.spec.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Shell base sheet contract, asserted against the CSS text on disk: base.css is
  3. * where the ui-theme token sheets enter the bundle, every sheet it names exists,
  4. * and scrollbar.css follows design-platform.css because it reads that sheet's
  5. * tokens.
  6. */
  7. import { existsSync, readFileSync } from 'node:fs'
  8. import { fileURLToPath } from 'node:url'
  9. import { describe, expect, it } from 'vitest'
  10. const THEME_PACKAGE = '@deepseek-ai/dsh-client-ui-theme'
  11. const baseCss = readFileSync(fileURLToPath(new URL('../src/base.css', import.meta.url)), 'utf8')
  12. /**
  13. * Import specifiers of the sheet, in source order. Quote style and surrounding
  14. * whitespace are normalized away.
  15. * @param css - stylesheet text.
  16. * @returns each `@import` target in the order the sheet lists it.
  17. */
  18. function importOrder(css: string): string[] {
  19. // The destructuring default only satisfies noUncheckedIndexedAccess; the
  20. // group is unconditional in the pattern.
  21. return [...css.matchAll(/@import\s+['"]([^'"]+)['"]/g)].map(([, specifier = '']) => specifier)
  22. }
  23. /**
  24. * Resolve a `<package>/styles/<file>` specifier to its path in the workspace.
  25. * The theme package maps `./styles/*` to `./src/styles/*`, so the sheets stay
  26. * on the source plane rather than needing a build.
  27. * @param specifier - import specifier from base.css.
  28. * @returns absolute path of the file the specifier names.
  29. */
  30. function resolveThemeSheet(specifier: string): string {
  31. const name = specifier.slice(`${THEME_PACKAGE}/styles/`.length)
  32. return fileURLToPath(new URL(`../../ui-theme/src/styles/${name}`, import.meta.url))
  33. }
  34. const imports = importOrder(baseCss)
  35. describe('web shell base.css', () => {
  36. it('imports every sheet from the theme package and each one exists', () => {
  37. expect(imports.length).toBeGreaterThan(0)
  38. for (const specifier of imports) {
  39. expect(specifier.startsWith(`${THEME_PACKAGE}/styles/`), specifier).toBe(true)
  40. expect(existsSync(resolveThemeSheet(specifier)), specifier).toBe(true)
  41. }
  42. })
  43. it('imports the scrollbar sheet after the token sheet it reads', () => {
  44. // Both sheets bind on `body`, so with scrollbar.css first the alias tokens
  45. // would still resolve; the order encodes the dependency direction so a
  46. // later specificity or selector change cannot silently invert it.
  47. const platform = imports.indexOf(`${THEME_PACKAGE}/styles/design-platform.css`)
  48. const scrollbar = imports.indexOf(`${THEME_PACKAGE}/styles/scrollbar.css`)
  49. expect(platform).toBeGreaterThanOrEqual(0)
  50. expect(scrollbar).toBeGreaterThan(platform)
  51. })
  52. })