base-styles.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const themeManifest = JSON.parse(
  13. readFileSync(fileURLToPath(new URL('../../ui-theme/package.json', import.meta.url)), 'utf8'),
  14. ) as { exports: Record<string, string>; files: string[] }
  15. /**
  16. * Import specifiers of the sheet, in source order. Quote style and surrounding
  17. * whitespace are normalized away.
  18. * @param css - stylesheet text.
  19. * @returns each `@import` target in the order the sheet lists it.
  20. */
  21. function importOrder(css: string): string[] {
  22. // The destructuring default only satisfies noUncheckedIndexedAccess; the
  23. // group is unconditional in the pattern.
  24. return [...css.matchAll(/@import\s+['"]([^'"]+)['"]/g)].map(([, specifier = '']) => specifier)
  25. }
  26. /**
  27. * Resolve a `<package>/styles/<file>` specifier to its source path for a
  28. * clean-tree test. The package build copies these sheets to their public
  29. * `lib/styles` export.
  30. * @param specifier - import specifier from base.css.
  31. * @returns absolute path of the file the specifier names.
  32. */
  33. function resolveThemeSheet(specifier: string): string {
  34. const name = specifier.slice(`${THEME_PACKAGE}/styles/`.length)
  35. return fileURLToPath(new URL(`../../ui-theme/src/styles/${name}`, import.meta.url))
  36. }
  37. const imports = importOrder(baseCss)
  38. describe('web shell base.css', () => {
  39. it('publishes theme sheets from the built artifact plane', () => {
  40. expect(themeManifest.exports['./styles/*']).toBe('./lib/styles/*')
  41. expect(themeManifest.files).toContain('lib/styles')
  42. })
  43. it('imports every sheet from the theme package and each one exists', () => {
  44. expect(imports.length).toBeGreaterThan(0)
  45. for (const specifier of imports) {
  46. expect(specifier.startsWith(`${THEME_PACKAGE}/styles/`), specifier).toBe(true)
  47. expect(existsSync(resolveThemeSheet(specifier)), specifier).toBe(true)
  48. }
  49. })
  50. it('imports the scrollbar sheet after the token sheet it reads', () => {
  51. // Both sheets bind on `body`, so with scrollbar.css first the alias tokens
  52. // would still resolve; the order encodes the dependency direction so a
  53. // later specificity or selector change cannot silently invert it.
  54. const platform = imports.indexOf(`${THEME_PACKAGE}/styles/design-platform.css`)
  55. const scrollbar = imports.indexOf(`${THEME_PACKAGE}/styles/scrollbar.css`)
  56. expect(platform).toBeGreaterThanOrEqual(0)
  57. expect(scrollbar).toBeGreaterThan(platform)
  58. })
  59. })