|
@@ -0,0 +1,172 @@
|
|
|
|
|
+import { describe, expect, it, vi } from "vitest";
|
|
|
|
|
+import { CoreErrorType } from "@zcode/contracts";
|
|
|
|
|
+import {
|
|
|
|
|
+ dispatchTasksHandler,
|
|
|
|
|
+ dispatchTasksToolEntry,
|
|
|
|
|
+} from "../src/tool/handlers/dispatch-tasks.js";
|
|
|
|
|
+import {
|
|
|
|
|
+ DispatchTasksInputSchema,
|
|
|
|
|
+ type DispatchTaskItem,
|
|
|
|
|
+ type DispatchTaskResult,
|
|
|
|
|
+ type TaskDispatchPort,
|
|
|
|
|
+ type TraceId,
|
|
|
|
|
+} from "@zcode/contracts";
|
|
|
|
|
+import { createToolRegistry, registerBuiltInTools } from "../src/tool/index.js";
|
|
|
|
|
+import type { ToolExecutionContext } from "../src/tool/types.js";
|
|
|
|
|
+
|
|
|
|
|
+// 设计合同:docs/specs/workspace-task-dispatch.md(验收场景 TD-1..TD-5 的单测面)。
|
|
|
|
|
+// Host 侧逐项失败隔离由 services 的协议 handler 承担,这里覆盖工具契约与
|
|
|
|
|
+// 汇总/拒绝/门语义。
|
|
|
|
|
+
|
|
|
|
|
+function createContext(
|
|
|
|
|
+ overrides?: Partial<ToolExecutionContext> & { taskDispatchPort?: TaskDispatchPort },
|
|
|
|
|
+): ToolExecutionContext {
|
|
|
|
|
+ return {
|
|
|
|
|
+ toolCallId: "tool-dispatch",
|
|
|
|
|
+ traceId: "trace-dispatch" as TraceId,
|
|
|
|
|
+ abortSignal: new AbortController().signal,
|
|
|
|
|
+ workingDirectory: "/workspace",
|
|
|
|
|
+ workspaceRoot: "/workspace",
|
|
|
|
|
+ sessionId: "sess-dispatch" as never,
|
|
|
|
|
+ ...overrides,
|
|
|
|
|
+ };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const ITEMS: DispatchTaskItem[] = [
|
|
|
|
|
+ {
|
|
|
|
|
+ id: "fix-login",
|
|
|
|
|
+ title: "修复登录页崩溃",
|
|
|
|
|
+ prompt: "Fix the crash on the login page when the token expires. Acceptance: login redirect works.",
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ id: "fix-export",
|
|
|
|
|
+ title: "修复导出乱码",
|
|
|
|
|
+ prompt: "Fix garbled CSV export. Acceptance: UTF-8 BOM present.",
|
|
|
|
|
+ },
|
|
|
|
|
+];
|
|
|
|
|
+
|
|
|
|
|
+function createPort(
|
|
|
|
|
+ results: DispatchTaskResult[],
|
|
|
|
|
+): TaskDispatchPort & { dispatch: ReturnType<typeof vi.fn> } {
|
|
|
|
|
+ return {
|
|
|
|
|
+ dispatch: vi.fn(async () => ({ results })),
|
|
|
|
|
+ };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+describe("dispatch-tasks tool", () => {
|
|
|
|
|
+ it("TD-1:确认执行模式汇总 started 任务与模型可见清单", async () => {
|
|
|
|
|
+ const port = createPort([
|
|
|
|
|
+ { id: "fix-login", title: "修复登录页崩溃", status: "started", sessionId: "s1" },
|
|
|
|
|
+ { id: "fix-export", title: "修复导出乱码", status: "started", sessionId: "s2" },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ const result = await dispatchTasksHandler(
|
|
|
|
|
+ { tasks: ITEMS, execute: true },
|
|
|
|
|
+ createContext({ taskDispatchPort: port }),
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ expect(port.dispatch).toHaveBeenCalledWith(ITEMS, {
|
|
|
|
|
+ sessionId: "sess-dispatch",
|
|
|
|
|
+ execute: true,
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(result.executed).toBe(true);
|
|
|
|
|
+ expect(result.createdCount).toBe(2);
|
|
|
|
|
+ expect(result.failedCount).toBe(0);
|
|
|
|
|
+ expect(result.message).toContain("[started] fix-login");
|
|
|
|
|
+ expect(result.message).toContain("[started] fix-export");
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("TD-2:仅创建模式不标记执行,汇总为 created", async () => {
|
|
|
|
|
+ const port = createPort([
|
|
|
|
|
+ { id: "fix-login", title: "修复登录页崩溃", status: "created" },
|
|
|
|
|
+ { id: "fix-export", title: "修复导出乱码", status: "created" },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ const result = await dispatchTasksHandler(
|
|
|
|
|
+ { tasks: ITEMS, execute: false },
|
|
|
|
|
+ createContext({ taskDispatchPort: port }),
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ expect(port.dispatch).toHaveBeenCalledWith(ITEMS, {
|
|
|
|
|
+ sessionId: "sess-dispatch",
|
|
|
|
|
+ execute: false,
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(result.executed).toBe(false);
|
|
|
|
|
+ expect(result.message).toContain("NOT executing");
|
|
|
|
|
+ expect(result.message).toContain("[created] fix-login");
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("TD-3:单项失败不阻塞汇总,失败项带原因", async () => {
|
|
|
|
|
+ const port = createPort([
|
|
|
|
|
+ { id: "fix-login", title: "修复登录页崩溃", status: "started", sessionId: "s1" },
|
|
|
|
|
+ { id: "fix-export", title: "修复导出乱码", status: "failed", failure: "createSession rejected" },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ const result = await dispatchTasksHandler(
|
|
|
|
|
+ { tasks: ITEMS, execute: true },
|
|
|
|
|
+ createContext({ taskDispatchPort: port }),
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ expect(result.createdCount).toBe(1);
|
|
|
|
|
+ expect(result.failedCount).toBe(1);
|
|
|
|
|
+ expect(result.message).toContain("[failed] fix-export");
|
|
|
|
|
+ expect(result.message).toContain("createSession rejected");
|
|
|
|
|
+ expect(result.message).toContain("[started] fix-login");
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("TD-4:闲时派发轮内拒绝并给出可恢复替代路径", async () => {
|
|
|
|
|
+ const port = createPort([]);
|
|
|
|
|
+ await expect(
|
|
|
|
|
+ dispatchTasksHandler(
|
|
|
|
|
+ { tasks: ITEMS, execute: true },
|
|
|
|
|
+ createContext({ taskDispatchPort: port, offPeakTurn: true }),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).rejects.toMatchObject({
|
|
|
|
|
+ type: CoreErrorType.PermissionDenied,
|
|
|
|
|
+ message: expect.stringContaining("idle-time task"),
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(port.dispatch).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("TD-5:端口缺席报配置错误(子会话不注入端口即工具不可见)", async () => {
|
|
|
|
|
+ await expect(
|
|
|
|
|
+ dispatchTasksHandler({ tasks: ITEMS, execute: true }, createContext()),
|
|
|
|
|
+ ).rejects.toMatchObject({ message: expect.stringContaining("TaskDispatchPort") });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("registerBuiltInTools:只有 includeDispatchTasks 门打开才注册", () => {
|
|
|
|
|
+ const withoutFlag = createToolRegistry();
|
|
|
|
|
+ registerBuiltInTools(withoutFlag, {});
|
|
|
|
|
+ expect(withoutFlag.has("DispatchTasks")).toBe(false);
|
|
|
|
|
+
|
|
|
|
|
+ const withFlag = createToolRegistry();
|
|
|
|
|
+ registerBuiltInTools(withFlag, { includeDispatchTasks: true });
|
|
|
|
|
+ expect(withFlag.has("DispatchTasks")).toBe(true);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("工具声明:统一确认 + workspace 副作用 + 自包含 prompt 指引", () => {
|
|
|
|
|
+ expect(dispatchTasksToolEntry.metadata.needsApproval).toBe(true);
|
|
|
|
|
+ expect(dispatchTasksToolEntry.metadata.sideEffectScope).toBe("workspace");
|
|
|
|
|
+ expect(dispatchTasksToolEntry.permission.permission).toBe("dispatchTasks");
|
|
|
|
|
+ expect(dispatchTasksToolEntry.permission.needsApproval).toBe(true);
|
|
|
|
|
+ // 子会话不继承本对话历史——指引必须显式告知模型 prompt 自包含。
|
|
|
|
|
+ expect(
|
|
|
|
|
+ dispatchTasksToolEntry.metadata.modelInstructions.join("\n"),
|
|
|
|
|
+ ).toContain("self-contained");
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it("schema 契约:1-50 任务、strict、execute 默认 true", () => {
|
|
|
|
|
+ expect(DispatchTasksInputSchema.safeParse({ tasks: [] }).success).toBe(false);
|
|
|
|
|
+ expect(
|
|
|
|
|
+ DispatchTasksInputSchema.safeParse({
|
|
|
|
|
+ tasks: [{ id: "a", title: "t", prompt: "p", unexpected: 1 }],
|
|
|
|
|
+ }).success,
|
|
|
|
|
+ ).toBe(false);
|
|
|
|
|
+ expect(DispatchTasksInputSchema.parse({ tasks: [{ id: "a", title: "t", prompt: "p" }] })).toEqual(
|
|
|
|
|
+ { tasks: [{ id: "a", title: "t", prompt: "p" }], execute: true },
|
|
|
|
|
+ );
|
|
|
|
|
+ const tooMany = Array.from({ length: 51 }, (_, i) => ({
|
|
|
|
|
+ id: `t${i}`,
|
|
|
|
|
+ title: "t",
|
|
|
|
|
+ prompt: "p",
|
|
|
|
|
+ }));
|
|
|
|
|
+ expect(DispatchTasksInputSchema.safeParse({ tasks: tooMany }).success).toBe(false);
|
|
|
|
|
+ });
|
|
|
|
|
+});
|