workspaceSidebarTaskViewSwitcher.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. import { resolveSidebarTaskGroupTogglePresentation } from "@/WorkspaceSidebar/taskGroupTogglePresentation.js";
  5. import enUS from "@/i18n/locales/en-US.js";
  6. import zhCN from "@/i18n/locales/zh-CN.js";
  7. function readWorkspaceSidebarSource(): string {
  8. return readFileSync(resolve(process.cwd(), "packages/ui/src/WorkspaceSidebar.tsx"), "utf8");
  9. }
  10. describe("WorkspaceSidebar task view switcher", () => {
  11. it("renders grouped before project while keeping project as the fallback default", () => {
  12. const source = readWorkspaceSidebarSource();
  13. const groupedTriggerIndex = source.indexOf('value="grouped"');
  14. const workspaceTriggerIndex = source.indexOf('value="workspace"');
  15. expect(groupedTriggerIndex).toBeGreaterThan(-1);
  16. expect(workspaceTriggerIndex).toBeGreaterThan(-1);
  17. expect(groupedTriggerIndex).toBeLessThan(workspaceTriggerIndex);
  18. expect(source).toMatch(
  19. /const activePrimaryTaskMode: PrimaryTaskMode =\s*!isWebRemoteControlSidebar && taskOrganizeBy === "grouped"\s*\? "grouped"\s*: "workspace";/,
  20. );
  21. });
  22. it("keeps the pill shell at h-7 in the active horizontal tabs state", () => {
  23. const source = readWorkspaceSidebarSource();
  24. expect(source).toContain(
  25. "relative h-7 w-fit overflow-hidden rounded-full bg-surface p-0.5 group-data-horizontal/tabs:h-7",
  26. );
  27. });
  28. it("keeps pinned tasks visible above the grouped task body", () => {
  29. const source = readWorkspaceSidebarSource();
  30. const pinnedVisibilityStart = source.indexOf("const shouldShowPinnedTasks");
  31. const pinnedVisibilityEnd = source.indexOf("const workspaceScrollRef", pinnedVisibilityStart);
  32. const pinnedSectionIndex = source.indexOf("<WorkspacePinnedTasksSection");
  33. const groupedSectionIndex = source.indexOf("<WorkspaceGroupedTasksSection");
  34. expect(pinnedVisibilityStart).toBeGreaterThan(-1);
  35. expect(pinnedVisibilityEnd).toBeGreaterThan(pinnedVisibilityStart);
  36. expect(source.slice(pinnedVisibilityStart, pinnedVisibilityEnd)).toContain(
  37. 'taskViewMode === "grouped"',
  38. );
  39. expect(pinnedSectionIndex).toBeGreaterThan(-1);
  40. expect(groupedSectionIndex).toBeGreaterThan(pinnedSectionIndex);
  41. });
  42. it("keeps the previous expand-all button presentation while the next task view hydrates", () => {
  43. const previousPresentation = {
  44. canToggle: true,
  45. areAllExpanded: true,
  46. messageId: "workspaceSidebar.collapseAllGroups" as const,
  47. };
  48. const presentation = resolveSidebarTaskGroupTogglePresentation({
  49. previous: previousPresentation,
  50. current: {
  51. visible: true,
  52. canToggle: false,
  53. areAllExpanded: false,
  54. transitionPending: true,
  55. },
  56. });
  57. expect(presentation).toEqual(previousPresentation);
  58. });
  59. it("uses the current expand-all button presentation after the task view hydrates", () => {
  60. const presentation = resolveSidebarTaskGroupTogglePresentation({
  61. previous: {
  62. canToggle: true,
  63. areAllExpanded: true,
  64. messageId: "workspaceSidebar.collapseAllGroups",
  65. },
  66. current: {
  67. visible: true,
  68. canToggle: false,
  69. areAllExpanded: false,
  70. transitionPending: false,
  71. },
  72. });
  73. expect(presentation).toEqual({
  74. canToggle: false,
  75. areAllExpanded: false,
  76. messageId: "workspaceSidebar.expandAllGroups",
  77. });
  78. });
  79. it("guards grouped collapsed preferences from transient empty hydration snapshots", () => {
  80. const source = readWorkspaceSidebarSource();
  81. expect(source).toContain("nextGroupIds.length === 0");
  82. expect(source).toContain("!groupedTaskGroupIdsHydrated");
  83. expect(source).toContain("lastNonEmptyGroupedTaskGroupIds.length > 0");
  84. expect(source).toContain("collapsedGroupedTaskGroupIds.size > 0");
  85. expect(source).toContain("重启后首进 Group");
  86. expect(source).toContain("不能当成“所有 group 已删除”");
  87. });
  88. it("Idle-time 收敛后由 grouped 树承载,侧栏不再独立渲染系统分组(D48-A)", () => {
  89. const source = readWorkspaceSidebarSource();
  90. expect(source).not.toContain("OffPeakSidebarGroup");
  91. expect(source).not.toContain("data-workspace-grouped-system-stack");
  92. // 闲时组头「+」路由到 Automations:Section 通过该 prop 拿到入口。
  93. expect(source).toContain("onOpenAutomations={handleOpenAutomationsMain}");
  94. expect(source).toContain("onClick={handleOpenPluginStoreMain}");
  95. expect(source).not.toContain("handleOpenPluginsSettings");
  96. });
  97. it("project view separates task-purpose rows and renders sections from persisted order", () => {
  98. const source = readWorkspaceSidebarSource();
  99. const projectSectionIndex = source.indexOf('id: "workspaceSidebar.projectsSection"');
  100. const conversationSectionIndex = source.indexOf('id: "workspaceSidebar.conversationsSection"');
  101. expect(source).toContain("partitionWorkspaceTabsByPurpose(workspaceTabs)");
  102. expect(projectSectionIndex).toBeGreaterThan(-1);
  103. expect(conversationSectionIndex).toBeGreaterThan(-1);
  104. expect(source).toContain("purposeSectionPreferences.sectionOrder.map(");
  105. expect(source).toContain("items={purposeSectionPreferences.sectionOrder}");
  106. expect(source).toContain("handlePurposeSectionDragEnd");
  107. expect(source).toContain("sortableKeyboardCoordinates");
  108. expect(source).toContain("workspaceTabs={conversationWorkspaceTabs}");
  109. expect(source).toContain("groupByDate={false}");
  110. expect(source).toContain('taskRowVariant="default"');
  111. expect(source).toContain("projectWorkspaceTabs.map((tab)");
  112. expect(source).not.toContain("workspaceTabs.length === 0 ? (");
  113. });
  114. it("conversation and project section actions use dedicated callbacks", () => {
  115. const source = readWorkspaceSidebarSource();
  116. expect(source).toContain("onCreateConversationTask");
  117. expect(source).toContain("onOpenFolderFromWorkspaceMenu");
  118. expect(source).toContain("onOpenRemoteWorkspace");
  119. });
  120. it("labels the conversation-purpose section as tasks in both shipped locales", () => {
  121. expect(enUS["workspaceSidebar.conversationsSection"]).toBe("Tasks");
  122. expect(enUS["workspaceSidebar.newConversation"]).toBe("New task");
  123. expect(enUS["workspaceSidebar.noConversations"]).toBe("No tasks yet");
  124. expect(zhCN["workspaceSidebar.conversationsSection"]).toBe("任务");
  125. expect(zhCN["workspaceSidebar.newConversation"]).toBe("新建任务");
  126. expect(zhCN["workspaceSidebar.noConversations"]).toBe("还没有任务");
  127. });
  128. it("persists independent purpose sections and includes projects in expand-all", () => {
  129. const source = readWorkspaceSidebarSource();
  130. expect(source).toContain("readSidebarPurposeSectionPreferences");
  131. expect(source).toContain("handleProjectSectionOpenChange");
  132. expect(source).toContain("handleConversationSectionOpenChange");
  133. expect(source).toContain("reorderSidebarPurposeSections");
  134. expect(source).toContain("purposeSectionPreferences.projectsExpanded &&");
  135. expect(source).toContain("areAllWorkspaceGroupsExpanded");
  136. expect(source).toContain("handleProjectSectionOpenChange(false)");
  137. expect(source).toContain("handleProjectSectionOpenChange(true)");
  138. });
  139. });