| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- // 任务分发模块 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");
- // 用户输入的需求原文必须进入提示词(五次对齐:输入驱动拆解)。
- expect(prompt).toContain("给订单模块加导出功能");
- }
- });
- it("两个语言版本内容不同(避免 locale 回归成同一份)", () => {
- expect(buildTaskDispatchPlannerPrompt("zh-CN", "x")).not.toBe(
- buildTaskDispatchPlannerPrompt("en-US", "x"),
- );
- });
- });
- 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([]);
- });
- });
|