remoteUiDesignTokenConformance.test.ts 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { readFileSync } from "node:fs";
  2. import { readFile } from "node:fs/promises";
  3. import { describe, expect, it } from "vitest";
  4. /**
  5. * R3-ISSUE-018:远程 UI 的原型像素值不能绕过设计系统直接进入组件 class。
  6. * 只扫描本次远程 UI 入口,避免把历史遗留组件的迁移工作混入本 issue。
  7. * continuity panel 的 var token、已登记的响应式断点和动画 opacity 不属于本规则。
  8. */
  9. const remoteUiSources = [
  10. "../src/RemoteConnectionDialogContent.tsx",
  11. "../src/lib/remoteConnectionPresentation.ts",
  12. "../src/remote-connection/RemoteConnectionTags.tsx",
  13. "../src/remote-connection/RemoteConnectionStateIcon.tsx",
  14. "../src/WorkspaceSidebar/RemoteProjectConnectionDetails.tsx",
  15. "../src/SettingsPage.tsx",
  16. "../src/remote-connection/RemoteConnectionConnectingStep.tsx",
  17. "../src/remote-connection/RemoteLinkRail.tsx",
  18. "../src/RemoteConnectionFields.tsx",
  19. "../src/remote-connection/RemotePasswordInput.tsx",
  20. "../src/project-sidebar/WorkspaceDragOverlay.tsx",
  21. "../src/WorkspaceSidebar/WorkspacePurposeSection.tsx",
  22. "../src/RemoteConnectionWizardChrome.tsx",
  23. "../src/remote-connection/RemoteConnectionSelection.tsx",
  24. "../src/remote-connection/RemoteConnectionHistoryInput.tsx",
  25. "../src/remote-connection-flow/ui/RemoteConnectionProgress.tsx",
  26. "../src/remote-connection/RemoteConnectionNavigationDot.tsx",
  27. "../src/remote-connection/EditConnectionProfileDialog.tsx",
  28. "../src/settings/SourceServerManagementPanel.tsx",
  29. "../src/settings/SourceServerUpdateActivity.tsx",
  30. "../src/WorkspaceSidebar/RemoteProjectDetailsPopover.tsx",
  31. "../src/project-sidebar/RemoteProjectMenu.tsx",
  32. ] as const;
  33. const forbiddenPatterns = [
  34. /\btext-(?:xs|sm|base|lg|xl|[2-9]xl)\b/u,
  35. /\btext-\[\d+(?:\.\d+)?(?:px|rem)\]/u,
  36. /\b(?:bg|text|border)-(?:slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-\d+/u,
  37. /\b(?:bg|text|border)-\[(?:#|rgba?\(|hsla?\()/u,
  38. /\bsize-(?:2\.5|4\.5|7|9)\b/u,
  39. /\brounded(?:\s|["'])/u,
  40. /\brounded-\[\d+(?:\.\d+)?(?:px|rem)\]/u,
  41. /\bsize-\[\d+(?:\.\d+)?(?:px|rem)\]/u,
  42. /\b(?:min|max)-[wh]-\[\d+(?:\.\d+)?(?:px|rem)\]/u,
  43. /\b(?:border|bg|text)-(?:success|warning|destructive|foreground-subtle|popover)\/\d+\b/u,
  44. ] as const;
  45. describe("remote UI design token conformance", () => {
  46. it("preserves the responsive settings heading size through scalable semantic tokens", async () => {
  47. const [source, styles] = await Promise.all([
  48. readFile(new URL("../src/SettingsPage.tsx", import.meta.url), "utf8"),
  49. readFile(new URL("../src/styles.css", import.meta.url), "utf8"),
  50. ]);
  51. const headingClass = source.match(/<h2 className="([^"]+)">\s*\{activeSectionLabel\}/u)?.[1];
  52. expect(headingClass?.split(/\s+/u)).toEqual(
  53. expect.arrayContaining(["text-ui-settings-title", "lg:text-ui-settings-title-lg"]),
  54. );
  55. // 直接恢复通用大字号会触发门禁;专用 token 同时保留默认尺寸和界面字号缩放。
  56. expect(styles).toContain("--text-ui-settings-title: calc(var(--ui-font-size) + 10px);");
  57. expect(styles).toContain("--text-ui-settings-title--line-height: calc(4 / 3);");
  58. expect(styles).toContain("--text-ui-settings-title-lg: calc(var(--ui-font-size) + 16px);");
  59. expect(styles).toContain("--text-ui-settings-title-lg--line-height: 1.2;");
  60. });
  61. it("does not emit arbitrary geometry or component-level semantic alpha", () => {
  62. const violations: string[] = [];
  63. for (const relativePath of remoteUiSources) {
  64. let source = readFileSync(new URL(relativePath, import.meta.url), "utf8");
  65. // RemoteConnectionDirectoryStep 的旧 loading 占位高度不属于本次 R3 改动,单独留给历史迁移。
  66. if (relativePath === "../src/RemoteConnectionDialogContent.tsx") {
  67. source = source.replace(/min-h-\[22rem\]/gu, "");
  68. }
  69. // Windows 设置页外框沿用 workspaceShellWindowChrome 的 5px 系统窗口圆角,属于桌面窗口外沿而非 R3 远程 UI。
  70. if (relativePath === "../src/SettingsPage.tsx") {
  71. source = source.replace(/rounded-\[5px\]/gu, "");
  72. }
  73. for (const pattern of forbiddenPatterns) {
  74. const match = pattern.exec(source);
  75. if (match) {
  76. violations.push(`${relativePath}: ${match[0]}`);
  77. }
  78. }
  79. }
  80. expect(violations).toEqual([]);
  81. const model = readFileSync(
  82. new URL("../src/lib/remoteConnectionPresentation.ts", import.meta.url),
  83. "utf8",
  84. );
  85. expect(model).not.toMatch(/(?:bg|text)-remote-(?:ssh|wsl|docker)/u);
  86. });
  87. });