useRootWorkspaceActionsTargetWorkspace.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // 跨项目发起已保存工作流:新任务必须落在工作流归属项目而非活动项目
  2. // (docs/dynamic-workflow/launch.md「The detail page」)。这里单测纯解析 helper 与源码接线契约。
  3. import { readFileSync } from "node:fs";
  4. import { describe, expect, it, vi } from "vitest";
  5. import { resolveNewTaskTargetFromRequest } from "@/root/useRootWorkspaceActions.js";
  6. import type { WorkbenchNewTaskTarget } from "@/v4/workbenchNewTaskTarget.js";
  7. function source(relative: string): string {
  8. return readFileSync(new URL(relative, import.meta.url), "utf8").replaceAll("\r\n", "\n");
  9. }
  10. describe("resolveNewTaskTargetFromRequest", () => {
  11. it("request 带 targetWorkspace 时采用它,且不询问 workbench 焦点解析", () => {
  12. const fallback = vi.fn<[], WorkbenchNewTaskTarget | null>(() => ({
  13. workspacePath: "/active",
  14. }));
  15. const target = resolveNewTaskTargetFromRequest(
  16. {
  17. initialPrompt: "运行工作流",
  18. targetWorkspace: {
  19. workspacePath: "/owner",
  20. workspaceIdentity: "remote:ssh:dev:/owner",
  21. },
  22. },
  23. fallback,
  24. );
  25. expect(target).toEqual({
  26. workspacePath: "/owner",
  27. workspaceIdentity: "remote:ssh:dev:/owner",
  28. });
  29. expect(fallback).not.toHaveBeenCalled();
  30. });
  31. it("targetWorkspace 缺省 identity 时归一化为 null", () => {
  32. const fallback = vi.fn<[], WorkbenchNewTaskTarget | null>(() => null);
  33. const target = resolveNewTaskTargetFromRequest(
  34. { targetWorkspace: { workspacePath: "/owner" } },
  35. fallback,
  36. );
  37. expect(target).toEqual({ workspacePath: "/owner", workspaceIdentity: null });
  38. expect(fallback).not.toHaveBeenCalled();
  39. });
  40. it("无 targetWorkspace 时回退到 workbench 解析,并把缺省 identity 归一化为 null", () => {
  41. const fallback = vi.fn<[], WorkbenchNewTaskTarget | null>(() => ({
  42. workspacePath: "/active",
  43. }));
  44. const target = resolveNewTaskTargetFromRequest({ initialPrompt: "普通新任务" }, fallback);
  45. expect(fallback).toHaveBeenCalledTimes(1);
  46. expect(target).toEqual({ workspacePath: "/active", workspaceIdentity: null });
  47. });
  48. it("回退目标带 identity 时原样透传", () => {
  49. const fallback = vi.fn<[], WorkbenchNewTaskTarget | null>(() => ({
  50. workspacePath: "/active",
  51. workspaceIdentity: "remote:ssh:dev:/active",
  52. }));
  53. const target = resolveNewTaskTargetFromRequest("zcode", fallback);
  54. expect(target).toEqual({
  55. workspacePath: "/active",
  56. workspaceIdentity: "remote:ssh:dev:/active",
  57. });
  58. });
  59. it("provider 字符串请求与 undefined 请求都走回退,且回退返回 null 时透传 null", () => {
  60. const nullFallback = vi.fn<[], WorkbenchNewTaskTarget | null>(() => null);
  61. expect(resolveNewTaskTargetFromRequest("zcode", nullFallback)).toBeNull();
  62. expect(resolveNewTaskTargetFromRequest(undefined, nullFallback)).toBeNull();
  63. expect(nullFallback).toHaveBeenCalledTimes(2);
  64. });
  65. });
  66. describe("startNewTaskFromActiveWorkspace 接线契约", () => {
  67. it("newTaskTarget 经 resolveNewTaskTargetFromRequest 解析,惰性包裹 resolveWorkbenchNewTaskTarget", () => {
  68. const hook = source("../src/root/useRootWorkspaceActions.ts");
  69. expect(hook).toContain("const newTaskTarget = resolveNewTaskTargetFromRequest(request, () =>");
  70. expect(hook).toContain("resolveWorkbenchNewTaskTarget({");
  71. // resolveWorkbenchNewTaskTarget 只应作为 helper 的回退闭包出现一次,不再直接赋给 newTaskTarget。
  72. expect(hook).not.toContain("const newTaskTarget = resolveWorkbenchNewTaskTarget(");
  73. });
  74. it("中枢发起 handler 带 target 时跳过活动 workspace 只读检查并透传 targetWorkspace", () => {
  75. const shell = source("../src/app-shell/WorkspaceShellLayout.tsx");
  76. expect(shell).toContain("if (!targetWorkspace && workspaceReadOnlyReason) return;");
  77. // 单一 object literal,仅在 target 存在时 spread,不给 request 塞 undefined 键。
  78. expect(shell).toMatch(
  79. /handleCreateTaskInChat\(\{\s*initialPrompt: prompt,\s*\.\.\.\(targetWorkspace \? \{ targetWorkspace \} : \{\}\),\s*\}\)/,
  80. );
  81. // 工作流「运行」不再走 autoSubmit 对话文案:直接启动(docs/dynamic-workflow/launch.md),
  82. // 该分支应已删除,autoSubmit 通道全线移除。
  83. expect(shell).not.toContain("autoSubmit: true");
  84. expect(shell).not.toContain("handleLaunchWorkflowInChat");
  85. });
  86. it("handleCreateTaskInChat 仅在无 targetWorkspace 时应用活动 workspace 只读守卫,带 target 时仍到达 onCreateTask", () => {
  87. // WorkspaceShellLayout 组件体量大且依赖繁多,直接挂载不实际;此处用源码契约(正则)
  88. // 证明守卫按 targetWorkspace 短路。契约:先算 hasTargetWorkspace,再 `!hasTargetWorkspace &&
  89. // workspaceReadOnlyReason` 才 return,随后 showChatMainView() + onCreateTask(request)。
  90. const shell = source("../src/app-shell/WorkspaceShellLayout.tsx");
  91. expect(shell).toMatch(
  92. /const hasTargetWorkspace\s*=\s*typeof request === "object" && request !== null && Boolean\(request\.targetWorkspace\);/,
  93. );
  94. expect(shell).toMatch(
  95. /if \(!hasTargetWorkspace && workspaceReadOnlyReason\) \{\s*return;\s*\}\s*showChatMainView\(\);\s*onCreateTask\(request\);/,
  96. );
  97. // 旧的“无条件只读 return”守卫必须被移除,否则带 target 的跨项目发起会在活动只读时被挡下。
  98. expect(shell).not.toMatch(
  99. /const handleCreateTaskInChat = useCallback\(\s*\(request\?: [^)]*\) => \{\s*if \(workspaceReadOnlyReason\) \{/,
  100. );
  101. });
  102. it("handleOpenSavedWorkflowRun 用 params 携带的目标 workspace 打开实例,而非活动项目", () => {
  103. // 中枢是跨项目视图:实例必须开在发起它的项目(不变式 8)。用源码契约证明目标取自 params,
  104. // 仅缺省时兜底回退活动 workspace;remoteSessionId 按目标 workspace 从 tab 反查。
  105. const shell = source("../src/app-shell/WorkspaceShellLayout.tsx");
  106. expect(shell).toMatch(
  107. /const targetWorkspacePath = params\.workspacePath \|\| workspaceAbsPath;/,
  108. );
  109. expect(shell).toMatch(/const targetWorkspaceIdentity =\s*params\.workspaceIdentity \?\?/);
  110. // 打开实例用的是解析出的目标,不再直接用活动 workspaceAbsPath。
  111. expect(shell).toMatch(/handleOpenWorkflowRun\(\{\s*workspacePath: targetWorkspacePath,/);
  112. expect(shell).not.toContain(
  113. "handleSelectTaskInChat(workspaceAbsPath, params.sessionId, workspaceIdentity)",
  114. );
  115. });
  116. });