瀏覽代碼

test(ui): 任务分发模块 M5——解析器/提示词/分组规则单测 + 源码契约测试修正

- 新增 packages/ui/test/taskDispatchPlan.test.ts(13 用例):
  - parseTaskDispatchPlan:合法围栏块、无块/坏 JSON/schema 不符、多块取最后、
    >50 上限、dependsOn/parallelGroup 可省略
  - buildTaskDispatchPlannerPrompt:中英文都含 ```json 契约与关键字段,两版本不退化同文
  - buildGroups:组号升序、独立任务殿后并组、空列表(buildGroups 导出供测试)
  - taskDispatch.* i18n key 中英奇偶校验
- 修正 automationsMainDragRegion.test.ts 源码契约断言:M2 接线后
  WorkspaceHeader 隐藏条件含 task-dispatch、BreadcrumbFrame 用例 4→5、
  scrollbar-gutter 槽位 4→5(断言语义不变,纳入新主视图)

验证:vitest 定向 15 用例全过;全量回归对照 25c98133(M2 前)确认本轮
无新增失败——既有 8 个失败(workspaceSidebarWebRemoteMobileLayout 4、
workspaceSidebarFooterWebRemoteControlTooltip 3、zcodeProtocol 1)与
本轮改动无关,为遗留问题;其余全量失败均为已记录的 Windows 环境性失败
(nsis/app-asar-repack/files.test symlink 等)。pnpm typecheck / lint
63 警告 0 错误(基线一致)。
wubinggen 1 月之前
父節點
當前提交
373cb3e8c4

+ 2 - 2
packages/ui/src/task-dispatch/useTaskDispatchExecutor.ts

@@ -39,7 +39,7 @@ export interface TaskDispatchExecState {
   results: TaskDispatchExecResultItem[];
   results: TaskDispatchExecResultItem[];
 }
 }
 
 
-type Group = { tasks: TaskDispatchExecTask[]; pending: Set<string> };
+export type Group = { tasks: TaskDispatchExecTask[]; pending: Set<string> };
 
 
 const IDLE_STATE: TaskDispatchExecState = {
 const IDLE_STATE: TaskDispatchExecState = {
   phase: "idle",
   phase: "idle",
@@ -51,7 +51,7 @@ const IDLE_STATE: TaskDispatchExecState = {
 };
 };
 
 
 /** 分组规则:parallelGroup 升序;独立任务(无组)并入最后一组并发执行。 */
 /** 分组规则:parallelGroup 升序;独立任务(无组)并入最后一组并发执行。 */
-function buildGroups(tasks: TaskDispatchExecTask[]): Group[] {
+export function buildGroups(tasks: TaskDispatchExecTask[]): Group[] {
   const byGroup = new Map<number, TaskDispatchExecTask[]>();
   const byGroup = new Map<number, TaskDispatchExecTask[]>();
   const independent: TaskDispatchExecTask[] = [];
   const independent: TaskDispatchExecTask[] = [];
   for (const task of tasks) {
   for (const task of tasks) {

+ 6 - 3
packages/ui/test/automationsMainDragRegion.test.ts

@@ -16,8 +16,9 @@ describe("workspace main view drag region", () => {
     );
     );
 
 
     // 断言语义不绑定排版:条件可被格式化换行。
     // 断言语义不绑定排版:条件可被格式化换行。
+    // task-dispatch 主视图与 automations/plugin-store 同规则隐藏 WorkspaceHeader(M2 接线)。
     expect(layoutSource).toMatch(
     expect(layoutSource).toMatch(
-      /workspaceMainView !== "automations"\s*&&\s*workspaceMainView !== "plugin-store"/,
+      /workspaceMainView !== "automations"\s*&&\s*workspaceMainView !== "task-dispatch"\s*&&\s*workspaceMainView !== "plugin-store"/,
     );
     );
     expect(frameSource).toContain('data-testid="automations-main-drag-region"');
     expect(frameSource).toContain('data-testid="automations-main-drag-region"');
     expect(frameSource).toContain("h-12 shrink-0 [app-region:drag]");
     expect(frameSource).toContain("h-12 shrink-0 [app-region:drag]");
@@ -28,7 +29,7 @@ describe("workspace main view drag region", () => {
 
 
     expect(
     expect(
       layoutSource.match(/<AutomationsMainBreadcrumbFrame/gu)?.length,
       layoutSource.match(/<AutomationsMainBreadcrumbFrame/gu)?.length,
-    ).toBe(4);
+    ).toBe(5);
     expect(
     expect(
       frameSource.match(/data-testid="automations-main-drag-region"/gu)?.length,
       frameSource.match(/data-testid="automations-main-drag-region"/gu)?.length,
     ).toBe(1);
     ).toBe(1);
@@ -44,6 +45,8 @@ describe("workspace main view drag region", () => {
         /min-h-0 flex-1 overflow-y-auto \[scrollbar-gutter:stable\]/gu,
         /min-h-0 flex-1 overflow-y-auto \[scrollbar-gutter:stable\]/gu,
       )?.length ?? 0;
       )?.length ?? 0;
 
 
-    expect(stableScrollerCount).toBe(4);
+    // automations / plugins / task-dispatch 三处主视图外壳 + Web 远控 = 4 处源内
+    // + task-dispatch 分支 = 5;新增同构主视图时同步维护。
+    expect(stableScrollerCount).toBe(5);
   });
   });
 });
 });

+ 152 - 0
packages/ui/test/taskDispatchPlan.test.ts

@@ -0,0 +1,152 @@
+// 任务分发模块 M5:planSchema 解析器 + planPrompt 契约 + buildGroups 分组规则的单测。
+// 设计合同:docs/specs/workspace-task-dispatch.md §8。
+import { describe, expect, it } from "vitest";
+import zhCN from "@/i18n/locales/zh-CN.js";
+import enUS from "@/i18n/locales/en-US.js";
+import {
+  parseTaskDispatchPlan,
+  TASK_DISPATCH_PLAN_MAX_TASKS,
+  taskDispatchPlanSchema,
+} from "@/task-dispatch/planSchema.js";
+import { buildTaskDispatchPlannerPrompt } from "@/task-dispatch/planPrompt.js";
+import { buildGroups } from "@/task-dispatch/useTaskDispatchExecutor.js";
+
+describe("taskDispatch i18n key parity", () => {
+  it("中英文 taskDispatch.* key 完全对齐", () => {
+    const zhKeys = Object.keys(zhCN).filter((key) => key.startsWith("taskDispatch"));
+    const enKeys = Object.keys(enUS).filter((key) => key.startsWith("taskDispatch"));
+    expect(zhKeys.length).toBeGreaterThan(0);
+    expect(enKeys.filter((key) => !zhKeys.includes(key))).toEqual([]);
+    expect(zhKeys.filter((key) => !(key in enUS))).toEqual([]);
+  });
+});
+
+const VALID_PLAN_JSON = JSON.stringify({
+  tasks: [
+    {
+      title: "任务 A",
+      prompt: "执行 A 的完整指令",
+      dependsOn: [],
+      parallelGroup: 0,
+    },
+    {
+      title: "任务 B",
+      prompt: "执行 B 的完整指令",
+      dependsOn: ["任务 A"],
+      parallelGroup: 1,
+    },
+  ],
+});
+
+describe("parseTaskDispatchPlan", () => {
+  it("解析合法围栏块", () => {
+    const result = parseTaskDispatchPlan(`前置说明\n\`\`\`json\n${VALID_PLAN_JSON}\n\`\`\`\n`);
+    expect(result.ok).toBe(true);
+    if (result.ok) {
+      expect(result.plan.tasks).toHaveLength(2);
+      expect(result.plan.tasks[1].dependsOn).toEqual(["任务 A"]);
+      expect(result.plan.tasks[1].parallelGroup).toBe(1);
+    }
+  });
+
+  it("无围栏块 → noFencedBlock", () => {
+    const result = parseTaskDispatchPlan(`没有 JSON 块的普通回复:\n${VALID_PLAN_JSON}`);
+    expect(result).toMatchObject({ ok: false, reason: "noFencedBlock" });
+  });
+
+  it("JSON 语法错误 → invalidJson", () => {
+    const result = parseTaskDispatchPlan("```json\n{ tasks: [\n```");
+    expect(result).toMatchObject({ ok: false, reason: "invalidJson" });
+  });
+
+  it("schema 不符(空任务/缺 prompt)→ schemaMismatch", () => {
+    const empty = parseTaskDispatchPlan("```json\n{\"tasks\":[]}\n```");
+    expect(empty).toMatchObject({ ok: false, reason: "schemaMismatch" });
+    const missingPrompt = parseTaskDispatchPlan(
+      '```json\n{"tasks":[{"title":"t"}]}\n```',
+    );
+    expect(missingPrompt).toMatchObject({ ok: false, reason: "schemaMismatch" });
+  });
+
+  it("多个围栏块时取最后一个", () => {
+    const stale = '```json\n{"tasks":[{"title":"旧","prompt":"p"}]}\n```';
+    const latest = '```json\n{"tasks":[{"title":"新","prompt":"p"}]}\n```';
+    const result = parseTaskDispatchPlan(`${stale}\n中间说明\n${latest}`);
+    expect(result.ok).toBe(true);
+    if (result.ok) expect(result.plan.tasks[0].title).toBe("新");
+  });
+
+  it("超上限任务数被拒绝(>50)", () => {
+    const tasks = Array.from({ length: TASK_DISPATCH_PLAN_MAX_TASKS + 1 }, (_, i) => ({
+      title: `t${i}`,
+      prompt: "p",
+    }));
+    const parsed = taskDispatchPlanSchema.safeParse({ tasks });
+    expect(parsed.success).toBe(false);
+  });
+
+  it("dependsOn/parallelGroup 可省略", () => {
+    const result = parseTaskDispatchPlan(
+      '```json\n{"tasks":[{"title":"独立任务","prompt":"p"}]}\n```',
+    );
+    expect(result.ok).toBe(true);
+    if (result.ok) {
+      expect(result.plan.tasks[0].dependsOn).toBeUndefined();
+      expect(result.plan.tasks[0].parallelGroup).toBeUndefined();
+    }
+  });
+});
+
+describe("buildTaskDispatchPlannerPrompt", () => {
+  it("中英文版本都携带 json 围栏契约与关键字段", () => {
+    for (const locale of ["zh-CN", "en-US"] as const) {
+      const prompt = buildTaskDispatchPlannerPrompt(locale);
+      expect(prompt).toContain("```json");
+      expect(prompt).toContain('"tasks"');
+      expect(prompt).toContain("parallelGroup");
+      expect(prompt).toContain("dependsOn");
+    }
+  });
+
+  it("两个语言版本内容不同(避免 locale 回归成同一份)", () => {
+    expect(buildTaskDispatchPlannerPrompt("zh-CN")).not.toBe(
+      buildTaskDispatchPlannerPrompt("en-US"),
+    );
+  });
+});
+
+describe("buildGroups", () => {
+  const task = (id: string, parallelGroup?: number) => ({
+    id,
+    title: id,
+    prompt: "p",
+    ...(parallelGroup === undefined ? {} : { parallelGroup }),
+  });
+
+  it("组号升序、独立任务并入最后一组", () => {
+    const groups = buildGroups([
+      task("a", 2),
+      task("indep1"),
+      task("b", 0),
+      task("c", 2),
+      task("indep2"),
+    ]);
+    expect(groups.map((group) => group.tasks.map((item) => item.id))).toEqual([
+      ["b"],
+      ["a", "c"],
+      ["indep1", "indep2"],
+    ]);
+    // 每组初始 pending 为空(待 createSession 回填)。
+    for (const group of groups) expect(group.pending.size).toBe(0);
+  });
+
+  it("全部独立任务时只产生一组", () => {
+    const groups = buildGroups([task("x"), task("y")]);
+    expect(groups).toHaveLength(1);
+    expect(groups[0].tasks.map((item) => item.id)).toEqual(["x", "y"]);
+  });
+
+  it("空任务列表产生空分组", () => {
+    expect(buildGroups([])).toEqual([]);
+  });
+});