automationNewTaskHistoryInteraction.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it } from "vitest";
  3. /**
  4. * 读取仓库源码用于文本契约断言。
  5. *
  6. * Bugfix:下面断言里有跨行片段(`) : showCreateSettingsAction ? (\n ...`),而仓库只对
  7. * *.mjs / *.sh 声明了 `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 editViewSource = await readSource(
  15. new URL("../src/settings/AutomationEditView.tsx", import.meta.url),
  16. );
  17. const offPeakEditViewSource = await readSource(
  18. new URL("../src/settings/OffPeakEditView.tsx", import.meta.url),
  19. );
  20. const primitivesSource = await readSource(
  21. new URL("../src/settings/AutomationDesignPrimitives.tsx", import.meta.url),
  22. );
  23. const segmentedTabsSource = await readSource(
  24. new URL("../src/settings/SettingsSegmentedTabs.tsx", import.meta.url),
  25. );
  26. const zhLocaleSource = await readSource(new URL("../src/i18n/locales/zh-CN.ts", import.meta.url));
  27. describe("automation new-task history interaction", () => {
  28. it("allows opening empty history without loading runs or showing create action", () => {
  29. expect(editViewSource).toContain("<AutomationSettingsHistoryTabs");
  30. expect(editViewSource).not.toContain("disabled={!editing}");
  31. expect(editViewSource).toContain(
  32. 'const showCreateSettingsAction = !editing && tab === "settings";',
  33. );
  34. expect(editViewSource).toContain(") : showCreateSettingsAction ? (\n <Button");
  35. expect(editViewSource).toContain('if (tab !== "history" || !editing) {\n return;');
  36. });
  37. it("shares the Hooks pill tabs style and uses 设置 / 历史 labels", () => {
  38. expect(editViewSource).toContain("<AutomationSettingsHistoryTabs");
  39. expect(offPeakEditViewSource).toContain("<AutomationSettingsHistoryTabs");
  40. expect(primitivesSource).toContain("<SettingsSegmentedTabs");
  41. expect(segmentedTabsSource).toContain(
  42. 'className="flex h-8 rounded-full bg-surface p-0.5 group-data-horizontal/tabs:h-8"',
  43. );
  44. expect(segmentedTabsSource).toContain(
  45. "h-7 flex-none rounded-full border-transparent bg-transparent px-2.5",
  46. );
  47. expect(segmentedTabsSource).toContain(
  48. "data-active:border-transparent data-active:bg-background",
  49. );
  50. expect(zhLocaleSource).toContain('"automations.edit.tab.history": "历史"');
  51. });
  52. });