automationsPageTabs.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @vitest-environment jsdom
  2. // 自动化页顶级标签「自动化 / 工作流」(docs/dynamic-workflow/launch.md「Where it lives」「标签记忆」)。
  3. import { createElement, useState } from "react";
  4. import { readFileSync } from "node:fs";
  5. import { resolve } from "node:path";
  6. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  7. import { afterEach, describe, expect, it } from "vitest";
  8. import { TID_AUTOMATIONS_PAGE_TAB, testId } from "@zcode/shared";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. import {
  11. AutomationsPageTitleSwitch,
  12. type AutomationsPageTab,
  13. } from "@/settings/saved-workflows/AutomationsPageTitleSwitch.js";
  14. import {
  15. readAutomationsPageTab,
  16. writeAutomationsPageTab,
  17. } from "@/settings/saved-workflows/automationsPageTabMemory.js";
  18. function Harness({ initial = "automation" }: { initial?: AutomationsPageTab }) {
  19. const [tab, setTab] = useState<AutomationsPageTab>(initial);
  20. return createElement(
  21. ZCodeIntlProvider,
  22. { initialLocale: "zh-CN" },
  23. createElement(AutomationsPageTitleSwitch, { value: tab, onValueChange: setTab }),
  24. );
  25. }
  26. // jsdom 环境下 `new URL(…, import.meta.url)` 不是 file: URL;vitest 从仓库根运行,按 cwd 定位源码。
  27. const automationsSectionSource = readFileSync(
  28. resolve(process.cwd(), "packages/ui/src/settings/AutomationsSection.tsx"),
  29. "utf8",
  30. ).replaceAll("\r\n", "\n");
  31. describe("AutomationsPageTitleSwitch", () => {
  32. afterEach(() => {
  33. cleanup();
  34. sessionStorage.clear();
  35. });
  36. it("两个标题词是 tablist 里的 tab,选中态跟随 value", () => {
  37. render(createElement(Harness));
  38. const tabs = screen.getAllByRole("tab");
  39. expect(tabs.map((tab) => tab.textContent)).toEqual(["自动化", "工作流"]);
  40. expect(tabs[0]?.getAttribute("aria-selected")).toBe("true");
  41. expect(tabs[1]?.getAttribute("aria-selected")).toBe("false");
  42. // 只有选中项进 Tab 序;未选中项靠方向键。
  43. expect(tabs[0]?.getAttribute("tabindex")).toBe("0");
  44. expect(tabs[1]?.getAttribute("tabindex")).toBe("-1");
  45. fireEvent.click(screen.getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow")));
  46. expect(
  47. screen
  48. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"))
  49. .getAttribute("aria-selected"),
  50. ).toBe("true");
  51. });
  52. it("方向键在两个标签间循环切换", () => {
  53. render(createElement(Harness));
  54. const tablist = screen.getByRole("tablist");
  55. fireEvent.keyDown(tablist, { key: "ArrowRight" });
  56. expect(
  57. screen
  58. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"))
  59. .getAttribute("aria-selected"),
  60. ).toBe("true");
  61. fireEvent.keyDown(tablist, { key: "ArrowRight" });
  62. expect(
  63. screen
  64. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "automation"))
  65. .getAttribute("aria-selected"),
  66. ).toBe("true");
  67. fireEvent.keyDown(tablist, { key: "ArrowLeft" });
  68. expect(
  69. screen
  70. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"))
  71. .getAttribute("aria-selected"),
  72. ).toBe("true");
  73. });
  74. it("标题字号沿用原 h1 的 30/34 页面标题层级,未选中用次级色", () => {
  75. render(createElement(Harness));
  76. const active = screen.getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "automation"));
  77. const inactive = screen.getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"));
  78. expect(active.className).toContain("text-[30px]");
  79. expect(active.className).toContain("leading-[34px]");
  80. expect(active.className).toContain("text-foreground");
  81. expect(inactive.className).toContain("text-foreground-subtle");
  82. });
  83. });
  84. describe("automationsPageTabMemory", () => {
  85. afterEach(() => {
  86. sessionStorage.clear();
  87. });
  88. it("app 级单 key 记在 sessionStorage,不再按项目分桶;未知值回默认", () => {
  89. expect(readAutomationsPageTab()).toBe("automation");
  90. writeAutomationsPageTab("workflow");
  91. expect(readAutomationsPageTab()).toBe("workflow");
  92. // 只用一个 key,与活动项目无关。
  93. expect(sessionStorage.length).toBe(1);
  94. expect(sessionStorage.getItem("zcode-automations-page-tab")).toBe("workflow");
  95. sessionStorage.setItem("zcode-automations-page-tab", "bogus");
  96. expect(readAutomationsPageTab()).toBe("automation");
  97. writeAutomationsPageTab("automation");
  98. expect(readAutomationsPageTab()).toBe("automation");
  99. expect(sessionStorage.length).toBe(1);
  100. });
  101. });
  102. describe("AutomationsSection 顶级标签接线", () => {
  103. it('页标题即切换,且 openAutomationTab: "workflow" 深链直接落到工作流页', () => {
  104. // 标题组件按灰度决定是切换还是平铺 h1(灰度断言在 dynamicWorkflowGrayAutomationsPage.test.ts)。
  105. expect(automationsSectionSource).toMatch(
  106. /<AutomationsPageTitle\s+workflowTabEnabled=\{dynamicWorkflowEnabled\}\s+value=\{pageTab\}\s+onValueChange=\{setPageTab\}\s*\/>/,
  107. );
  108. // 记忆已 app 级化:不再有 pageTabStorageKey,读写都无参 / 单参。
  109. expect(automationsSectionSource).not.toContain("pageTabStorageKey");
  110. expect(automationsSectionSource).toContain("readAutomationsPageTab()");
  111. expect(automationsSectionSource).toContain("writeAutomationsPageTab(next)");
  112. expect(automationsSectionSource).toMatch(
  113. /if \(openAutomationTab === "workflow"\) \{\s*setPageTab\("workflow"\);\s*onOpenAutomationConsumed\?\.\(\);\s*return;/,
  114. );
  115. // 定时任务 / 闲时任务的胶囊行留在「自动化」内部:工作流页整页交给 SavedWorkflowsSection。
  116. expect(automationsSectionSource).toMatch(
  117. /if \(pageTab === "workflow"\) \{\s*return \(\s*<SavedWorkflowsSection/,
  118. );
  119. // 深链 openWorkflow / 消费回调透传给 SavedWorkflowsSection(不变式 8 多项目视图)。
  120. expect(automationsSectionSource).toContain("openWorkflow={openWorkflow}");
  121. expect(automationsSectionSource).toContain("onOpenWorkflowConsumed={onOpenWorkflowConsumed}");
  122. });
  123. });