| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- import { describe, expect, it, vi } from "vitest";
- import { AutomationCreateLimitError, isAutomationCreateLimitError } from "@zcode/contracts";
- import { AUTOMATION_CREATE_LIMIT_ERROR_CODE, zcodeProtocolMethods } from "@zcode/shared";
- import {
- AUTOMATION_CREATE_BOUND_SESSION_CHECK_ERROR,
- AUTOMATION_CREATE_FROM_AUTOMATION_RUN_ERROR,
- AUTOMATION_CREATE_IN_BOUND_SESSION_ERROR,
- createProtocolAutomationPort,
- } from "../src/zcode-protocol/automation-port.js";
- import {
- ProtocolRequestError,
- type ZCodeProtocolAgentServerContext,
- } from "../src/zcode-protocol/server-types.js";
- describe("createProtocolAutomationPort", () => {
- it("拒绝 automation 执行 turn 里再次 CronCreate,且不调用 automation/create", async () => {
- const requestClient = vi.fn();
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map([
- [
- "session-1",
- {
- activeAutomationId: "automation-parent",
- app: {
- runtime: {
- getSessionModelSelection: () => ({ providerId: "zai-api", modelId: "GLM-5" }),
- },
- getModel: () => "zai-api/GLM-5",
- getMode: () => "yolo",
- getThoughtLevel: () => "nothink",
- setCustomSessionTitle: vi.fn(async () => undefined),
- },
- traceContext: { traceId: "trace-session-1" },
- },
- ],
- ]),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.create(
- {
- cron: "*/5 * * * *",
- delayMinutes: null,
- prompt: "nested",
- title: "nested",
- },
- { sessionId: "session-1" },
- ),
- ).rejects.toThrow(AUTOMATION_CREATE_FROM_AUTOMATION_RUN_ERROR);
- expect(requestClient).not.toHaveBeenCalled();
- });
- it("拒绝在已归属定时任务的会话里再次 CronCreate(按 targetTaskId 兜底),且不调用 automation/create", async () => {
- // Bugfix 回归:桌面交互输入直连 CLI,绕过 host adapter 的 toolDenylist 注入;用户在一个已归属
- // 定时任务的会话里继续输入时,CronCreate 仍会被注册。这里在创建入口按 targetTaskId 兜底拒绝。
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) {
- return { bound: true };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map([
- [
- "session-1",
- {
- app: {
- runtime: { getSessionModelSelection: () => undefined },
- getModel: () => undefined,
- getMode: () => undefined,
- getThoughtLevel: () => undefined,
- setCustomSessionTitle: vi.fn(async () => undefined),
- },
- traceContext: { traceId: "trace-session-1" },
- },
- ],
- ]),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.create(
- {
- cron: "*/5 * * * *",
- delayMinutes: null,
- prompt: "再帮我建一个",
- title: "再帮我建一个",
- },
- { sessionId: "session-1" },
- ),
- ).rejects.toThrow(AUTOMATION_CREATE_IN_BOUND_SESSION_ERROR);
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCheckTaskBinding,
- { targetTaskId: "session-1" },
- expect.anything(),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationList,
- expect.anything(),
- expect.anything(),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.anything(),
- expect.anything(),
- );
- });
- it.each([undefined, "yolo", "guarded"] as const)(
- "普通会话创建 automation 保留显式模式 %s,不迁移未设置值",
- async (mode) => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) return { bound: false };
- if (method === zcodeProtocolMethods.automationCreate) {
- return {
- automation: {
- automationId: "automation-new",
- title: "每5分钟提醒",
- cronExpr: "*/5 * * * *",
- prompt: "提醒我",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- mode,
- },
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map([
- [
- "session-1",
- {
- app: {
- runtime: { getSessionModelSelection: () => undefined },
- getModel: () => undefined,
- getMode: () => mode,
- getThoughtLevel: () => undefined,
- setCustomSessionTitle: vi.fn(async () => undefined),
- },
- traceContext: { traceId: "trace-session-1" },
- },
- ],
- ]),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.create(
- {
- cron: "*/5 * * * *",
- delayMinutes: null,
- prompt: "提醒我",
- title: "每5分钟提醒",
- },
- { sessionId: "session-1" },
- ),
- ).resolves.toEqual(expect.objectContaining({ automationId: "automation-new", mode }));
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.objectContaining({ targetTaskId: "session-1", ...(mode ? { mode } : {}) }),
- expect.anything(),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationList,
- expect.anything(),
- expect.anything(),
- );
- },
- );
- it("CronCreate、CronUpdate 与 CronList 透传 interval carrier 并保留权威 scheduleRule", async () => {
- const minuteRule = {
- unit: "minute" as const,
- interval: 200,
- hour: 10,
- minute: 30,
- anchorAt: 1_700_000_000_000,
- };
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) return { bound: false };
- if (method === zcodeProtocolMethods.automationCreate) {
- return {
- automation: {
- automationId: "automation-interval",
- title: "每200分钟提醒",
- cronExpr: "* * * * *",
- prompt: "提醒我",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- scheduleRule: minuteRule,
- },
- };
- }
- if (method === zcodeProtocolMethods.automationUpdate) {
- return {
- automation: {
- automationId: "automation-interval",
- title: "每200小时的第49分提醒",
- cronExpr: "49 * * * *",
- prompt: "提醒我",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- scheduleRule: {
- unit: "hourly" as const,
- interval: 200,
- hour: 0,
- minute: 49,
- anchorAt: 1_700_000_060_000,
- },
- },
- };
- }
- if (method === zcodeProtocolMethods.automationList) {
- return {
- automations: [
- {
- automationId: "automation-interval",
- title: "每200小时的第49分提醒",
- cronExpr: "49 * * * *",
- prompt: "提醒我",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- scheduleRule: {
- unit: "hourly" as const,
- interval: 200,
- hour: 0,
- minute: 49,
- anchorAt: 1_700_000_060_000,
- },
- },
- ],
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map([
- [
- "session-1",
- {
- app: {
- runtime: { getSessionModelSelection: () => undefined },
- getModel: () => undefined,
- getMode: () => undefined,
- getThoughtLevel: () => undefined,
- setCustomSessionTitle: vi.fn(async () => undefined),
- },
- traceContext: { traceId: "trace-session-1" },
- },
- ],
- ]),
- } as unknown as ZCodeProtocolAgentServerContext);
- const created = await port.create(
- {
- cron: "* * * * *",
- delayMinutes: null,
- prompt: "提醒我",
- title: "每200分钟提醒",
- intervalUnit: "minute",
- interval: 200,
- },
- { sessionId: "session-1" },
- );
- const updated = await port.update({
- id: "automation-interval",
- cron: "49 * * * *",
- intervalUnit: "hourly",
- interval: 200,
- });
- const listed = await port.list();
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.objectContaining({
- intervalUnit: "minute",
- interval: 200,
- recurring: true,
- }),
- expect.anything(),
- );
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationUpdate,
- expect.objectContaining({
- intervalUnit: "hourly",
- interval: 200,
- recurring: true,
- maxRuns: null,
- }),
- expect.anything(),
- );
- expect(created.scheduleRule).toEqual(minuteRule);
- expect(updated.scheduleRule).toMatchObject({ unit: "hourly", interval: 200, minute: 49 });
- expect(listed[0]?.scheduleRule).toMatchObject({ unit: "hourly", interval: 200, minute: 49 });
- });
- it("将当前 Bot turn 的回推目标注入 automation/create,普通模型输入不能自行指定", async () => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) return { bound: false };
- if (method === zcodeProtocolMethods.automationCreate) {
- return {
- automation: {
- automationId: "automation-bot",
- title: "Bot 日报",
- cronExpr: "0 9 * * *",
- prompt: "生成日报",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const botDeliveryTarget = {
- provider: "feishu" as const,
- botId: "bot-feishu",
- providerUserId: "chat-1",
- chatType: "group" as const,
- };
- const port = createProtocolAutomationPort(
- { requestClient, sessions: new Map() } as unknown as ZCodeProtocolAgentServerContext,
- () =>
- ({
- activeBotDeliveryTarget: botDeliveryTarget,
- app: {
- runtime: { getSessionModelSelection: () => undefined },
- sessionId: "session-1",
- getModel: () => undefined,
- getMode: () => undefined,
- getThoughtLevel: () => undefined,
- setCustomSessionTitle: vi.fn(async () => undefined),
- },
- traceContext: { traceId: "trace-session-1" },
- }) as never,
- );
- await port.create(
- { cron: "0 9 * * *", delayMinutes: null, prompt: "生成日报", title: "Bot 日报" },
- { sessionId: "session-1" },
- );
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.objectContaining({ botDeliveryTarget }),
- expect.anything(),
- );
- });
- it("旧 Host 缺少归属查询方法时回退 automation/list,并允许未绑定会话 CronCreate", async () => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) {
- throw new ProtocolRequestError(-32601, "Method not found");
- }
- if (method === zcodeProtocolMethods.automationList) {
- return {
- automations: [
- {
- automationId: "automation-other",
- title: "其他会话任务",
- cronExpr: "0 8 * * *",
- prompt: "提醒其他会话",
- targetTaskId: "session-other",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- ],
- };
- }
- if (method === zcodeProtocolMethods.automationCreate) {
- return {
- automation: {
- automationId: "automation-new",
- title: "每5分钟提醒",
- cronExpr: "*/5 * * * *",
- prompt: "提醒我",
- targetTaskId: "session-1",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.create(
- {
- cron: "*/5 * * * *",
- delayMinutes: null,
- prompt: "提醒我",
- title: "每5分钟提醒",
- },
- { sessionId: "session-1" },
- ),
- ).resolves.toEqual(expect.objectContaining({ automationId: "automation-new" }));
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationList,
- {},
- expect.anything(),
- );
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.objectContaining({ targetTaskId: "session-1" }),
- expect.anything(),
- );
- });
- it("旧 Host 回退列表命中当前会话绑定时仍拒绝 CronCreate", async () => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) {
- throw new ProtocolRequestError(-32601, "Method not found");
- }
- if (method === zcodeProtocolMethods.automationList) {
- return {
- automations: [
- {
- automationId: "automation-owner",
- title: "当前会话任务",
- cronExpr: "*/10 * * * *",
- prompt: "提醒当前会话",
- targetTaskId: "session-1",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- ],
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.create(
- {
- cron: "*/5 * * * *",
- delayMinutes: null,
- prompt: "再建一个任务",
- title: "再建一个任务",
- },
- { sessionId: "session-1" },
- ),
- ).rejects.toThrow(AUTOMATION_CREATE_IN_BOUND_SESSION_ERROR);
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.anything(),
- expect.anything(),
- );
- });
- it("相对时间通过 relativeDelayMinutes 交给 Host,模型不再计算绝对 cron", async () => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCreate) {
- return {
- automation: {
- automationId: "automation-relative",
- title: "3分钟后接水提醒",
- cronExpr: "47 14 28 7 *",
- prompt: "提醒我接水",
- enabled: true,
- lifecycleStatus: "active" as const,
- nextRunAt: 1_785_221_068_000,
- runCount: 0,
- recurring: false,
- },
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await port.create({
- delayMinutes: 3,
- prompt: "提醒我接水",
- title: "3分钟后接水提醒",
- recurring: false,
- });
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.objectContaining({
- cronExpr: "* * * * *",
- relativeDelayMinutes: 3,
- recurring: false,
- }),
- expect.anything(),
- );
- });
- it("省略 delayMinutes 的普通 cron 创建保持 recurring 默认,不误走相对分支", async () => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCreate) {
- return {
- automation: {
- automationId: "automation-plain",
- title: "每天9点喝水提醒",
- cronExpr: "0 9 * * *",
- prompt: "提醒我喝水",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- // Bug 原因:`delayMinutes !== null` 判定下,省略字段(很多 provider 不稳定保留
- // null 字段)会被误当成相对任务,把普通 recurring 调度错建成一次性。
- await port.create({
- cron: "0 9 * * *",
- prompt: "提醒我喝水",
- title: "每天9点喝水提醒",
- });
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.objectContaining({ cronExpr: "0 9 * * *", recurring: true }),
- expect.anything(),
- );
- const createPayload = requestClient.mock.calls.find(
- ([method]) => method === zcodeProtocolMethods.automationCreate,
- )?.[1] as Record<string, unknown>;
- expect(createPayload).not.toHaveProperty("relativeDelayMinutes");
- });
- it("将完整 task mode 归一化为 Cron 工具兼容的权限模式", async () => {
- const modeCases = [
- ["default", "build"],
- ["auto", "build"],
- ["acceptEdits", "build"],
- ["autoEdit", "build"],
- ["dontAsk", "yolo"],
- ["bypassPermissions", "yolo"],
- ["build", "build"],
- ["edit", "edit"],
- ["plan", "plan"],
- ["yolo", "yolo"],
- ] as const;
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationList) {
- return {
- automations: modeCases.map(([mode], index) => ({
- automationId: `automation-${index}`,
- title: `automation ${index}`,
- cronExpr: "0 9 * * *",
- prompt: "生成晨报",
- mode,
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- })),
- };
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- const automations = await port.list();
- expect(automations.map(({ mode }) => mode)).toEqual(
- modeCases.map(([, expectedMode]) => expectedMode),
- );
- });
- it("将 automation/create 上限错误映射为 CLI 稳定领域错误", async () => {
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) return { bound: false };
- if (method === zcodeProtocolMethods.automationCreate) {
- throw new Error(
- `[${AUTOMATION_CREATE_LIMIT_ERROR_CODE}] At most 20 automations may be retained.`,
- );
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- const error = await port
- .create(
- { cron: "0 9 * * *", prompt: "生成晨报", title: "每天 9 点晨报" },
- { sessionId: "session-1" },
- )
- .catch((caught: unknown) => caught);
- expect(error).toBeInstanceOf(AutomationCreateLimitError);
- expect(isAutomationCreateLimitError(error)).toBe(true);
- });
- it("automation 归属查询失败时 fail-closed,且不调用 automation/create", async () => {
- const logger = { warn: vi.fn() };
- const requestClient = vi.fn(async (method: string) => {
- if (method === zcodeProtocolMethods.automationCheckTaskBinding) {
- throw new Error("automation storage unavailable");
- }
- throw new Error(`unexpected method ${method}`);
- });
- const port = createProtocolAutomationPort({
- logger,
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.create(
- {
- cron: "*/5 * * * *",
- delayMinutes: null,
- prompt: "提醒我",
- title: "每5分钟提醒",
- },
- { sessionId: "session-unknown" },
- ),
- ).rejects.toThrow(AUTOMATION_CREATE_BOUND_SESSION_CHECK_ERROR);
- expect(logger.warn).toHaveBeenCalledWith(
- "Failed to check bound automations before CronCreate",
- expect.objectContaining({
- event: "automation.create.bound_session_check.failed",
- sessionId: "session-unknown",
- }),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationList,
- expect.anything(),
- expect.anything(),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.anything(),
- expect.anything(),
- );
- });
- it("CronCreate 协议请求继承活跃会话的模型、权限和思考等级", async () => {
- const setCustomSessionTitle = vi.fn(async () => undefined);
- const requestClient = vi.fn(async () => ({
- automation: {
- automationId: "automation-1",
- title: "daily report",
- cronExpr: "0 9 * * *",
- prompt: "write daily report",
- model: "zai-api/GLM-5",
- provider: "glm",
- mode: "yolo" as const,
- thoughtLevel: "nothink",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- }));
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map([
- [
- "session-1",
- {
- app: {
- runtime: {
- getSessionModelSelection: () => ({
- providerId: "zai-api",
- modelId: "GLM-5",
- options: { reasoningLevel: "nothink" },
- }),
- },
- getModel: () => "zai-api/GLM-5",
- getMode: () => "yolo",
- getThoughtLevel: () => "nothink",
- setCustomSessionTitle,
- },
- traceContext: { traceId: "trace-session-1" },
- },
- ],
- ]),
- } as unknown as ZCodeProtocolAgentServerContext);
- await port.create(
- {
- cron: "0 9 * * *",
- delayMinutes: null,
- prompt: "write daily report",
- title: "daily report",
- },
- { model: "stale-provider/stale-model", sessionId: "session-1" },
- );
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- {
- cronExpr: "0 9 * * *",
- prompt: "write daily report",
- title: "daily report",
- recurring: true,
- modelSelection: {
- providerId: "zai-api",
- modelId: "GLM-5",
- options: { reasoningLevel: "nothink" },
- },
- mode: "yolo",
- targetTaskId: "session-1",
- },
- expect.anything(),
- );
- expect(setCustomSessionTitle).toHaveBeenCalledWith({
- title: "daily report",
- traceContext: { traceId: "trace-session-1" },
- });
- });
- it("CronUpdate 将严格 patch 映射到 automation/update,并保留 maxRuns=null", async () => {
- const requestClient = vi.fn(async () => ({
- automation: {
- automationId: "automation-1",
- title: "updated report",
- cronExpr: "0 10 * * *",
- prompt: "write updated report",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 3,
- recurring: true,
- },
- }));
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(
- port.update({
- id: "automation-1",
- title: "updated report",
- cron: "0 10 * * *",
- recurring: true,
- maxRuns: null,
- }),
- ).resolves.toMatchObject({
- automationId: "automation-1",
- title: "updated report",
- runCount: 3,
- });
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationUpdate,
- {
- automationId: "automation-1",
- title: "updated report",
- cronExpr: "0 10 * * *",
- recurring: true,
- maxRuns: null,
- },
- expect.anything(),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationDelete,
- expect.anything(),
- expect.anything(),
- );
- expect(requestClient).not.toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- expect.anything(),
- expect.anything(),
- );
- });
- it("CronDelete 返回 protocol 的真实删除结果", async () => {
- const requestClient = vi.fn(async () => ({ deleted: false }));
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(port.delete({ id: "stale-id" })).resolves.toBe(false);
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationDelete,
- { automationId: "stale-id" },
- expect.anything(),
- );
- });
- it("CronPauseSelf 通过 automation/pause 透传执行器注入的本轮 automationId 并返回真实暂停结果", async () => {
- const requestClient = vi.fn(async () => ({ paused: true }));
- const port = createProtocolAutomationPort({
- requestClient,
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext);
- await expect(port.pause({ id: "automation-1" })).resolves.toBe(true);
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationPause,
- { automationId: "automation-1" },
- expect.anything(),
- );
- });
- it("会话未登记在 context.sessions 时,仍从归属会话解析器读取权限/思考等级", async () => {
- // Bugfix 回归:V4/desktop 会话可能不在 legacy context.sessions map 里,activeSession 命中失败
- // 会导致 mode/thoughtLevel 丢失(model 靠 createContext 兜底才幸存)。改为绑定归属会话解析器。
- const setCustomSessionTitle = vi.fn(async () => undefined);
- const requestClient = vi.fn(async () => ({
- automation: {
- automationId: "automation-2",
- title: "daily report",
- cronExpr: "0 9 * * *",
- prompt: "write daily report",
- model: "zai-api/GLM-5",
- provider: "glm",
- mode: "plan" as const,
- thoughtLevel: "think-hard",
- enabled: true,
- lifecycleStatus: "active" as const,
- runCount: 0,
- recurring: true,
- },
- }));
- const ownSession = {
- app: {
- runtime: {
- getSessionModelSelection: () => ({
- providerId: "zai-api",
- modelId: "GLM-5",
- options: { reasoningLevel: "think-hard" },
- }),
- },
- getModel: () => "zai-api/GLM-5",
- getMode: () => "plan",
- getThoughtLevel: () => "think-hard",
- setCustomSessionTitle,
- },
- traceContext: { traceId: "trace-session-1" },
- };
- const port = createProtocolAutomationPort(
- {
- requestClient,
- // 归属会话故意不在 sessions map 中,模拟 V4/desktop 场景。
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext,
- () => ownSession as unknown as never,
- );
- await port.create(
- {
- cron: "0 9 * * *",
- delayMinutes: null,
- prompt: "write daily report",
- title: "daily report",
- },
- { model: "stale-provider/stale-model", sessionId: "session-1" },
- );
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationCreate,
- {
- cronExpr: "0 9 * * *",
- prompt: "write daily report",
- title: "daily report",
- recurring: true,
- modelSelection: {
- providerId: "zai-api",
- modelId: "GLM-5",
- options: { reasoningLevel: "think-hard" },
- },
- mode: "plan",
- targetTaskId: "session-1",
- },
- expect.anything(),
- );
- });
- });
- it.each([false, true])("定时任务列表按协商能力携带自身工作区:%s", async (enabled) => {
- const workspace = { workspaceKey: "/tasks/b", workspacePath: "/tasks/b" };
- const requestClient = vi.fn(async () => ({ automations: [] }));
- const port = createProtocolAutomationPort(
- {
- requestClient,
- sessionWorkspaceRoutingEnabled: enabled,
- } as unknown as ZCodeProtocolAgentServerContext,
- () =>
- ({ workspace }) as import("../src/zcode-protocol/server-types.js").ZCodeProtocolSessionRecord,
- );
- await port.list({ sessionId: "session-b" });
- expect(requestClient).toHaveBeenCalledWith(
- zcodeProtocolMethods.automationList,
- enabled ? { workspace } : {},
- expect.anything(),
- );
- });
|