workspaceSidebarNewTaskRouting.test.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. function readUiSource(relativePath: string): string {
  4. return readFileSync(
  5. new URL(`../src/${relativePath}`, import.meta.url),
  6. "utf8",
  7. );
  8. }
  9. describe("workspace sidebar new-task routing", () => {
  10. it("叶子 workspace row 只上报新建意图,不直接操作 session store", () => {
  11. const source = readUiSource("WorkspaceSidebarItem.tsx");
  12. expect(source).toContain("onStartDraftInWorkspace(");
  13. expect(source).not.toMatch(/const\s+startDraft\s*=\s*useZCodeSessionStore/);
  14. });
  15. it("桌面端统一新建路径先退出 group/split pane,再创建显式草稿", () => {
  16. const source = readUiSource("App.tsx");
  17. const deactivateIndex = source.indexOf(
  18. "useWorkbenchGroupStore.getState().deactivateActiveGroup()",
  19. );
  20. const resetPaneIndex = source.indexOf(
  21. "usePaneLayoutStore.getState().resetToPrimaryPane()",
  22. );
  23. const startDraftIndex = source.indexOf(
  24. "store.startDraft(",
  25. deactivateIndex,
  26. );
  27. expect(deactivateIndex).toBeGreaterThan(-1);
  28. expect(resetPaneIndex).toBeGreaterThan(deactivateIndex);
  29. expect(startDraftIndex).toBeGreaterThan(resetPaneIndex);
  30. });
  31. it("workspace 行展开复用草稿导航事务,不直接恢复 tab 的旧 session", () => {
  32. const source = readUiSource("WorkspaceSidebarItem.tsx");
  33. const openChangeStart = source.indexOf("const handleWorkspaceOpenChange");
  34. const openChangeEnd = source.indexOf("const handleSelectTask", openChangeStart);
  35. const openChangeSource = source.slice(openChangeStart, openChangeEnd);
  36. expect(openChangeSource).toContain("onStartDraftInWorkspace(");
  37. expect(openChangeSource).not.toContain("activateTab(tab.id)");
  38. });
  39. it("本地 workspace 打开后进入草稿,而不是沿用旧 active session", () => {
  40. const source = readUiSource("root/useRootWorkspaceActions.ts");
  41. const selectProjectStart = source.indexOf("const handleSelectProject");
  42. const selectProjectEnd = source.indexOf("const handleOpenWorkspace", selectProjectStart);
  43. const selectProjectSource = source.slice(selectProjectStart, selectProjectEnd);
  44. const addTabIndex = selectProjectSource.indexOf("addTab(path)");
  45. const startDraftIndex = selectProjectSource.indexOf("startDraftInWorkspace(path)");
  46. expect(addTabIndex).toBeGreaterThan(-1);
  47. expect(startDraftIndex).toBeGreaterThan(addTabIndex);
  48. });
  49. it("冷启动 workspace 进入草稿,但显式 taskId 保持 session 导航优先", () => {
  50. const source = readUiSource("root/useRootPlatformEffects.ts");
  51. const initialWorkspaceStart = source.indexOf("if (initialWorkspaceAbsPath)");
  52. const initialWorkspaceEnd = source.indexOf(
  53. "setIsBootstrappingInitialWorkspace(false)",
  54. initialWorkspaceStart,
  55. );
  56. const initialWorkspaceSource = source.slice(initialWorkspaceStart, initialWorkspaceEnd);
  57. const explicitTaskIndex = initialWorkspaceSource.indexOf("if (initialTaskId)");
  58. const coldDraftIndex = initialWorkspaceSource.indexOf(
  59. "else if (!isRendererReloadNavigation())",
  60. );
  61. const startDraftIndex = initialWorkspaceSource.indexOf("startDraftInWorkspace(");
  62. expect(explicitTaskIndex).toBeGreaterThan(-1);
  63. expect(coldDraftIndex).toBeGreaterThan(explicitTaskIndex);
  64. expect(startDraftIndex).toBeGreaterThan(coldDraftIndex);
  65. });
  66. it("远程 workspace 选定 canonical identity 后进入对应草稿桶", () => {
  67. const source = readUiSource("Root.tsx");
  68. const remoteDraftStart = source.indexOf("const handleRemoteWorkspaceActivated");
  69. const remoteDraftEnd = source.indexOf("});", remoteDraftStart);
  70. const remoteDraftSource = source.slice(remoteDraftStart, remoteDraftEnd);
  71. expect(remoteDraftSource).toContain(
  72. "startDraftInWorkspace(workspacePath, workspaceIdentity)",
  73. );
  74. });
  75. });