automationTypographyTokens.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it } from "vitest";
  3. /**
  4. * 读取仓库源码用于文本契约断言。
  5. *
  6. * Bugfix:本文件的断言里有跨行片段(例如 `TOKEN =\n "..."`),而仓库只对 *.mjs / *.sh
  7. * 声明了 `eol=lf`,其余源码在 `core.autocrlf=true` 的 Windows 检出里是 CRLF。
  8. * 直接 `toContain` 跨行片段时 `\n` 匹配不到 `\r\n`,用例在 Windows 上必然失败,
  9. * 而它想断言的其实是源码内容契约,与换行字节无关。统一归一成 LF 再比对。
  10. */
  11. async function readSource(url: URL): Promise<string> {
  12. return (await readFile(url, "utf8")).replaceAll("\r\n", "\n");
  13. }
  14. const typographySurfaceFiles = [
  15. "AutomationDesignPrimitives.tsx",
  16. "AutomationEditView.tsx",
  17. "AutomationInstructionsComposer.tsx",
  18. "AutomationScheduleBadge.tsx",
  19. "AutomationSwitchToggle.tsx",
  20. "AutomationsSection.tsx",
  21. "OffPeakEditActionsMenu.tsx",
  22. "OffPeakEditView.tsx",
  23. "OffPeakHistoryTab.tsx",
  24. "OffPeakTaskList.tsx",
  25. ] as const;
  26. const typographySurfaceSources = await Promise.all(
  27. typographySurfaceFiles.map(async (fileName) => ({
  28. fileName,
  29. source: await readSource(new URL(`../src/settings/${fileName}`, import.meta.url)),
  30. })),
  31. );
  32. const sourceByFile = new Map(
  33. typographySurfaceSources.map(({ fileName, source }) => [fileName, source]),
  34. );
  35. const chatEmptyStateSource = await readSource(
  36. new URL("../src/ChatEmptyState.tsx", import.meta.url),
  37. );
  38. const cronCreateRendererSource = await readSource(
  39. new URL("../src/ToolCallBlocks/renderers/cron-create.tsx", import.meta.url),
  40. );
  41. const chatInputDisplaySource = await readSource(
  42. new URL("../src/chat-input-toolbar/display.tsx", import.meta.url),
  43. );
  44. describe("automation typography tokens", () => {
  45. it.each(typographySurfaceSources)("$fileName 不再使用迁移范围内的旧字号", ({ source }) => {
  46. expect(source).not.toMatch(/text-ui-xs/);
  47. expect(source).not.toMatch(/text-\[(?:12|13|14|16)px\]/);
  48. expect(source).not.toMatch(/(?:^|\s)text-(?:base|sm|xs)(?=\s|["'`/])/m);
  49. });
  50. it("定时与闲时 Instructions 工具栏使用同一套 trigger 视觉", () => {
  51. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  52. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  53. const automationToolbarSource = sourceByFile.get("AutomationInstructionsComposer.tsx") ?? "";
  54. expect(automationEditSource).toContain('containerClassName="contents"');
  55. expect(automationEditSource).toContain("AUTOMATION_INSTRUCTIONS_TOOLBAR_TRIGGER_CLASSNAME");
  56. expect(automationEditSource).toContain(
  57. '"w-fit max-w-72 min-w-0 shrink @max-sm/composer:size-7 @max-sm/composer:justify-center @max-sm/composer:gap-0 @max-sm/composer:p-0"',
  58. );
  59. expect(offPeakEditSource).toContain('containerClassName="contents"');
  60. expect(offPeakEditSource).toContain("items-center gap-1 rounded-full px-2 text-ui-base");
  61. expect(automationToolbarSource).toContain(
  62. "h-7 rounded-full text-ui-base font-normal leading-5 text-foreground-subtle",
  63. );
  64. expect(automationToolbarSource).not.toContain(
  65. "leading-5 text-foreground hover:bg-surface-hover",
  66. );
  67. expect(automationToolbarSource).toContain("hover:bg-hover");
  68. expect(automationToolbarSource).toContain(
  69. "aria-expanded:bg-hover aria-expanded:text-foreground",
  70. );
  71. expect(offPeakEditSource).not.toContain("focus-within:bg-hover");
  72. expect(chatEmptyStateSource).toContain("containerClassName?: string");
  73. expect(chatEmptyStateSource).toContain("triggerClassName?: string");
  74. });
  75. it("定时任务模型与 Think 触发器复用会话 composer 的响应式构成", () => {
  76. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  77. const automationComposerSource = sourceByFile.get("AutomationInstructionsComposer.tsx") ?? "";
  78. expect(automationComposerSource).toContain("@container/composer");
  79. expect(automationEditSource).toContain(
  80. 'labelVisibilityClassName="hidden @sm/composer:inline-flex"',
  81. );
  82. expect(automationEditSource).toContain('indicatorClassName="hidden @sm/composer:block"');
  83. expect(automationEditSource).toContain(
  84. 'triggerIconClassName="inline-flex @sm/composer:hidden"',
  85. );
  86. expect(automationEditSource).toContain(
  87. 'labelVisibilityClassName="hidden @xl/composer:inline-flex"',
  88. );
  89. expect(automationEditSource).toContain('indicatorClassName="hidden @xl/composer:block"');
  90. expect(automationEditSource).not.toContain('labelVisibilityClassName="inline-flex min-w-0"');
  91. expect(automationEditSource).toContain('id: "chat.toolbar.model.label"');
  92. expect(automationEditSource).not.toContain('contentAlign="end"');
  93. });
  94. it("首页卡片辅助文字与会话创建跳转按钮统一使用 text-ui-base", () => {
  95. const automationsSectionSource = sourceByFile.get("AutomationsSection.tsx") ?? "";
  96. expect(automationsSectionSource).toContain(
  97. "text-wrap-phrase line-clamp-2 flex-1 text-ui-base font-normal leading-5",
  98. );
  99. expect(cronCreateRendererSource).toContain(
  100. "px-3 py-1.5 text-ui-base text-foreground hover:bg-hover",
  101. );
  102. expect(cronCreateRendererSource).not.toContain("px-2.5 text-ui-xs text-foreground-subtle");
  103. expect(cronCreateRendererSource).not.toContain("ArrowRightIcon");
  104. });
  105. it("定时与闲时权限菜单直接复用首页 mode selector", () => {
  106. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  107. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  108. for (const source of [automationEditSource, offPeakEditSource]) {
  109. expect(source).toContain("<ConfigSelect");
  110. expect(source).toContain("option={modeOption}");
  111. expect(source).toContain('triggerVariant="ghost"');
  112. expect(source).toContain('triggerSize="default"');
  113. expect(source).toContain('labelVisibilityClassName="inline-flex min-w-0 truncate text-left"');
  114. expect(source).toContain("provider={ZCODE_AGENT_PROVIDER}");
  115. expect(source).toContain("restoreFocusSelector={null}");
  116. expect(source).toContain("w-fit max-w-56 min-w-0 shrink justify-start gap-1 px-2");
  117. }
  118. expect(chatInputDisplaySource).toContain(
  119. 'className: option.category === "mode" ? "w-64" : undefined',
  120. );
  121. expect(chatInputDisplaySource).toContain("min-h-13 items-start gap-3 py-2 pl-2 pr-8");
  122. expect(chatInputDisplaySource).toContain("truncate text-ui-base/relaxed text-foreground");
  123. expect(chatInputDisplaySource).toContain(
  124. "line-clamp-2 text-ui-sm/relaxed text-foreground-subtle whitespace-nowrap",
  125. );
  126. expect(offPeakEditSource).not.toContain("renderDescription={(value)");
  127. expect(automationEditSource).not.toContain("权限 trigger 曾固定 160px");
  128. // 闲时与定时任务共享 builder、provider 和会话权限词表,避免配置文案漂移。
  129. expect(offPeakEditSource).toContain("buildAutomationModeOption(mode)");
  130. expect(offPeakEditSource).not.toContain("offPeak.mode.");
  131. // 手写复刻已删除:两页都不再自行维护权限菜单结构与 warning 语义。
  132. expect(automationEditSource).not.toContain('mode === "yolo"');
  133. expect(offPeakEditSource).not.toContain('mode === "yolo"');
  134. expect(offPeakEditSource).not.toContain("OffPeakToolbarSelect");
  135. });
  136. it("定时与闲时详情通过面包屑返回并展示标题层级", () => {
  137. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  138. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  139. expect(automationEditSource).toContain("<SettingsBreadcrumbReporter");
  140. expect(offPeakEditSource).toContain("<SettingsBreadcrumbReporter");
  141. expect(automationEditSource).toContain('data-testid="automation-edit-title"');
  142. expect(automationEditSource).toContain('data-testid="automation-edit-subtitle"');
  143. expect(automationEditSource).not.toContain("AUTOMATION_BACK_TRIGGER_CLASSNAME");
  144. expect(offPeakEditSource).not.toContain("AUTOMATION_BACK_TRIGGER_CLASSNAME");
  145. });
  146. it("定时与闲时字段标题到下方组件统一使用 6px flex 间距", () => {
  147. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  148. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  149. const primitivesSource = sourceByFile.get("AutomationDesignPrimitives.tsx") ?? "";
  150. expect(primitivesSource).toContain('AUTOMATION_FORM_FIELD_CLASSNAME = "flex flex-col gap-1.5"');
  151. expect(
  152. automationEditSource.match(/className=\{AUTOMATION_FORM_FIELD_CLASSNAME\}/g)?.length ?? 0,
  153. ).toBeGreaterThanOrEqual(5);
  154. // 修复原因:45a0078032(会话内创建闲时任务)新增了条件渲染的「绑定会话」字段,
  155. // 表单字段从 2 个变为 3 个(绑定会话 / 任务标题 / 任务指令),旧断言未同步导致 staging 红测。
  156. expect(
  157. offPeakEditSource.match(/className=\{AUTOMATION_FORM_FIELD_CLASSNAME\}/g)?.length ?? 0,
  158. ).toBe(3);
  159. });
  160. it("定时与闲时设置页的输入内容统一使用 14px UI 字号", () => {
  161. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  162. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  163. const instructionsSource = sourceByFile.get("AutomationInstructionsComposer.tsx") ?? "";
  164. // 只校验常量取值,不绑定换行格式;格式化工具可能把声明折到一行。
  165. expect(instructionsSource).toMatch(
  166. /AUTOMATION_FORM_INPUT_TYPOGRAPHY_CLASSNAME =\s*"text-ui-base leading-5"/,
  167. );
  168. expect(instructionsSource).toContain("AUTOMATION_FORM_INPUT_TYPOGRAPHY_CLASSNAME,");
  169. expect(automationEditSource).toContain("AUTOMATION_FORM_INPUT_TYPOGRAPHY_CLASSNAME");
  170. expect(offPeakEditSource).toContain("AUTOMATION_FORM_INPUT_TYPOGRAPHY_CLASSNAME");
  171. expect(automationEditSource).not.toContain("gap-1 text-ui-sm text-foreground-subtle");
  172. expect(automationEditSource).not.toMatch(/w-(?:14|16).*text-ui-sm tabular-nums/);
  173. });
  174. it("闲时任务无人值守提示使用中性圆形图标与 14px UI 字号", () => {
  175. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  176. expect(offPeakEditSource).toContain(
  177. "items-start gap-1.5 text-ui-base leading-5 text-foreground-subtle",
  178. );
  179. expect(offPeakEditSource).toContain('<AutomationInfoIcon className="size-4" />');
  180. expect(offPeakEditSource).not.toContain("TriangleAlert");
  181. });
  182. it("会话创建任务在编辑页展示只读调度摘要", () => {
  183. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  184. expect(automationEditSource).toContain("preserveSessionCreatedSchedule");
  185. expect(automationEditSource).toContain(
  186. 'intl.formatMessage({ id: "automations.frequency.custom" })',
  187. );
  188. expect(automationEditSource).toMatch(
  189. /preserveSessionCreatedSchedule\s*\? \{\}\s*: \{ scheduleRule: currentScheduleRule \?\? null \}/u,
  190. );
  191. });
  192. it("未保存返回弹窗复用 Automation confirmation presentation", () => {
  193. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  194. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  195. expect(automationEditSource).toContain('presentation: "automation-confirmation"');
  196. expect(offPeakEditSource).toContain('presentation: "automation-confirmation"');
  197. expect(automationEditSource).not.toContain(
  198. 'className="w-[min(352px,calc(100vw-2rem))] max-w-none gap-4"',
  199. );
  200. });
  201. it("完全访问模式复用会话 composer 的 warning 强调色", () => {
  202. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  203. const offPeakEditSource = sourceByFile.get("OffPeakEditView.tsx") ?? "";
  204. expect(automationEditSource).toContain("<ConfigSelect");
  205. expect(offPeakEditSource).toContain("<ConfigSelect");
  206. expect(chatInputDisplaySource).toContain("isHighPermissionModeValue(option.currentValue)");
  207. expect(chatInputDisplaySource).toContain(
  208. "text-warning hover:text-warning aria-expanded:text-warning",
  209. );
  210. });
  211. it("全部调度频率都通过同一非透明语义背景展示选中值", () => {
  212. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  213. const triggerStart = automationEditSource.indexOf(
  214. "data-testid={TID_AUTOMATION_FREQUENCY_SELECT}",
  215. );
  216. const triggerEnd = automationEditSource.indexOf("</SelectTrigger>", triggerStart);
  217. const frequencyTriggerSource = automationEditSource.slice(triggerStart, triggerEnd);
  218. expect(triggerStart).toBeGreaterThanOrEqual(0);
  219. expect(frequencyTriggerSource).toContain("bg-hover");
  220. expect(frequencyTriggerSource).not.toContain("bg-tag");
  221. expect(frequencyTriggerSource).toContain("h-auto");
  222. expect(frequencyTriggerSource).toContain("py-px");
  223. expect(frequencyTriggerSource).toContain("rounded-full");
  224. expect(frequencyTriggerSource).toContain("border-0");
  225. expect(frequencyTriggerSource).toContain("hover:bg-selected");
  226. expect(frequencyTriggerSource).not.toContain("bg-[#363636]");
  227. expect(frequencyTriggerSource).not.toContain("bg-[#404040]");
  228. expect(frequencyTriggerSource).not.toContain("bg-transparent");
  229. });
  230. it("状态胶囊使用 32px 高度和 20px frame 内的 6px 圆点", () => {
  231. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  232. const statusStart = automationEditSource.indexOf("{editStatus ? (");
  233. const statusEnd = automationEditSource.indexOf("{/* Task title */}", statusStart);
  234. const statusSource = automationEditSource.slice(statusStart, statusEnd);
  235. expect(statusSource).toContain("inline-flex h-8 max-w-full items-center gap-1");
  236. expect(statusSource).toContain("flex size-5 shrink-0 items-center justify-center");
  237. expect(statusSource).toContain('"size-1.5 shrink-0 rounded-full"');
  238. expect(statusSource).not.toContain("inline-flex h-9");
  239. expect(statusSource).not.toContain('"size-2 shrink-0 rounded-full"');
  240. });
  241. it("调度 tag 之间的连接词与 tag 内文字使用同一主文字样式", () => {
  242. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  243. const scheduleStart = automationEditSource.indexOf("调度栏原先使用 min-height");
  244. const scheduleEnd = automationEditSource.indexOf("<CustomRepeatDialog", scheduleStart);
  245. const scheduleSource = automationEditSource.slice(scheduleStart, scheduleEnd);
  246. expect(scheduleSource).toMatch(
  247. /<span className="text-foreground">\s*\{intl\.formatMessage\(\{\s*id: "automations\.form\.schedule\.at"/,
  248. );
  249. expect(scheduleSource).toContain(
  250. "flex items-center gap-1 text-ui-base leading-5 text-foreground",
  251. );
  252. expect(scheduleSource).not.toMatch(
  253. /<span className="text-foreground-subtle">\s*\{intl\.formatMessage\(\{\s*id: "automations\.form\.schedule\.at"/,
  254. );
  255. const weekdayPickerStart = automationEditSource.indexOf("function WeekdayPicker");
  256. const weekdayPickerEnd = automationEditSource.indexOf(
  257. "function toDateInputValue",
  258. weekdayPickerStart,
  259. );
  260. const weekdayPickerSource = automationEditSource.slice(weekdayPickerStart, weekdayPickerEnd);
  261. expect(weekdayPickerStart).toBeGreaterThanOrEqual(0);
  262. expect(weekdayPickerSource).toContain("text-ui-base leading-5 text-foreground");
  263. expect(weekdayPickerSource).not.toContain("text-[#F8F8F8]");
  264. const weekdayOptionStart = weekdayPickerSource.indexOf(
  265. 'className="flex h-7 w-full items-center',
  266. );
  267. const weekdayOptionEnd = weekdayPickerSource.indexOf("</button>", weekdayOptionStart);
  268. const weekdayOptionSource = weekdayPickerSource.slice(weekdayOptionStart, weekdayOptionEnd);
  269. const weekdayLabelPosition = weekdayOptionSource.indexOf('className="min-w-0 flex-1 truncate"');
  270. const weekdayCheckPosition = weekdayOptionSource.indexOf(
  271. 'className="flex size-4 shrink-0 items-center justify-center"',
  272. );
  273. expect(weekdayOptionStart).toBeGreaterThanOrEqual(0);
  274. expect(weekdayLabelPosition).toBeGreaterThanOrEqual(0);
  275. expect(weekdayCheckPosition).toBeGreaterThan(weekdayLabelPosition);
  276. });
  277. it("添加计划菜单在明暗主题都使用语义 hover 背景", () => {
  278. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  279. const addScheduleStart = automationEditSource.indexOf(
  280. "data-testid={TID_AUTOMATION_SCHEDULE_ADD}",
  281. );
  282. const addScheduleEnd = automationEditSource.indexOf(") : (", addScheduleStart);
  283. const addScheduleSource = automationEditSource.slice(addScheduleStart, addScheduleEnd);
  284. expect(addScheduleStart).toBeGreaterThanOrEqual(0);
  285. expect(addScheduleSource).toContain("data-[highlighted]:bg-menu-hover");
  286. expect(addScheduleSource).not.toContain("data-[highlighted]:bg-white/5");
  287. });
  288. it("自定义调度的频率、重复周期和时间 tag 共用 text/primary", () => {
  289. const automationEditSource = sourceByFile.get("AutomationEditView.tsx") ?? "";
  290. const customScheduleStart = automationEditSource.indexOf('{builder.frequency === "custom" ? (');
  291. const customScheduleEnd = automationEditSource.indexOf(
  292. '{builder.frequency !== "custom"',
  293. customScheduleStart,
  294. );
  295. const customScheduleSource = automationEditSource.slice(customScheduleStart, customScheduleEnd);
  296. const timePickerStart = automationEditSource.indexOf("function TimeOfDayPicker");
  297. const timePickerEnd = automationEditSource.indexOf("/** 月/日单列滚动列表", timePickerStart);
  298. const timePickerSource = automationEditSource.slice(timePickerStart, timePickerEnd);
  299. const timeUnitStart = automationEditSource.indexOf("function TimeUnitColumn");
  300. const timeUnitSource = automationEditSource.slice(timeUnitStart, timePickerStart);
  301. expect(automationEditSource).toContain(
  302. "rounded-full border-0 bg-hover py-px pl-2 text-ui-base leading-5 text-foreground",
  303. );
  304. expect(customScheduleSource).toContain("rounded-full bg-hover py-px");
  305. expect(customScheduleSource).toContain("text-ui-base leading-5 text-foreground");
  306. expect(customScheduleSource).not.toContain("rounded-[8px]");
  307. expect(customScheduleSource).not.toContain("text-[#F8F8F8]");
  308. expect(timePickerSource).toContain("rounded-full bg-hover py-px");
  309. expect(timePickerSource).not.toContain("bg-tag");
  310. expect(timePickerSource).toContain("text-ui-base leading-5 tabular-nums text-foreground");
  311. expect(timeUnitSource).toContain('? "bg-menu-hover text-foreground"');
  312. expect(timeUnitSource).toContain(
  313. '"text-foreground-subtle hover:bg-menu-hover hover:text-foreground"',
  314. );
  315. expect(timeUnitSource).not.toContain('? "bg-primary text-primary-foreground"');
  316. expect(automationEditSource).not.toContain("bg-[#363636]");
  317. expect(automationEditSource).not.toContain("bg-[#404040]");
  318. expect(automationEditSource).not.toContain("text-[#F8F8F8]");
  319. expect(timePickerSource).not.toContain("rounded-md");
  320. });
  321. });