| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- // 已保存工作流 GUI 中枢的五个 workspace 级协议方法(docs/dynamic-workflow/launch.md)。
- // 真实 tmp 目录 + core 的真实 store / codec;只有 journal 用能力探测式的假实现。
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
- import { createHash } from "node:crypto";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
- import {
- zcodeProtocolMethods,
- zcodeWorkflowsDeleteResultSchema,
- zcodeWorkflowsGetResultSchema,
- zcodeWorkflowsListResultSchema,
- zcodeWorkflowsMoveResultSchema,
- zcodeWorkflowsRunsParamsSchema,
- zcodeWorkflowsRunsResultSchema,
- zcodeWorkflowsUpdateMetaResultSchema,
- } from "@zcode/shared";
- import { SavedWorkflowEntrySchema, SavedWorkflowMetaSchema } from "@zcode/contracts";
- import { parseSavedWorkflow, serializeSavedWorkflow } from "@zcode/core";
- import {
- deleteSavedWorkflowOp,
- getSavedWorkflowOp,
- listSavedWorkflowRunsOp,
- listSavedWorkflowsOp,
- moveSavedWorkflowOp,
- updateSavedWorkflowMetaOp,
- } from "../src/zcode-protocol/saved-workflows.js";
- import type { ZCodeProtocolAgentServerContext } from "../src/zcode-protocol/server-types.js";
- const SCRIPT = [
- 'const reviewer = agent("检查员");',
- "const found = await reviewer.ask<string>(`检查 ${String(args.target)}`);",
- "return found;",
- "",
- ].join("\n");
- let cwd = "";
- // 每个用例一份隔离的 HOME:全局档落在 `<HOME>/.zcode/workflows/`,core 恒取 `os.homedir()`
- // (posix 上即 `$HOME`)。不隔离的话 list/get/move 的全局变体会读到真实家目录、结果非确定。
- let home = "";
- let prevHome: string | undefined;
- let prevUserProfile: string | undefined;
- function workspace() {
- return { workspacePath: cwd, workspaceKey: cwd };
- }
- function contextWith(sessionStore?: unknown): ZCodeProtocolAgentServerContext {
- return {
- deps: { env: {}, sessionStore },
- sessions: new Map(),
- } as unknown as ZCodeProtocolAgentServerContext;
- }
- function projectDir() {
- return join(cwd, ".zcode", "workflows");
- }
- function globalDir() {
- return join(home, ".zcode", "workflows");
- }
- async function seedWorkflow(name: string, meta: Parameters<typeof serializeSavedWorkflow>[0]) {
- await mkdir(projectDir(), { recursive: true });
- const path = join(projectDir(), `${name}.dwf.ts`);
- await writeFile(path, serializeSavedWorkflow(meta, SCRIPT), "utf8");
- return path;
- }
- async function seedGlobalWorkflow(
- name: string,
- meta: Parameters<typeof serializeSavedWorkflow>[0],
- ) {
- await mkdir(globalDir(), { recursive: true });
- const path = join(globalDir(), `${name}.dwf.ts`);
- await writeFile(path, serializeSavedWorkflow(meta, SCRIPT), "utf8");
- return path;
- }
- beforeEach(async () => {
- cwd = await mkdtemp(join(tmpdir(), "zcode-saved-workflows-protocol-"));
- home = await mkdtemp(join(tmpdir(), "zcode-saved-workflows-home-"));
- prevHome = process.env.HOME;
- prevUserProfile = process.env.USERPROFILE;
- process.env.HOME = home;
- process.env.USERPROFILE = home;
- });
- afterEach(async () => {
- if (prevHome === undefined) delete process.env.HOME;
- else process.env.HOME = prevHome;
- if (prevUserProfile === undefined) delete process.env.USERPROFILE;
- else process.env.USERPROFILE = prevUserProfile;
- await rm(cwd, { recursive: true, force: true });
- await rm(home, { recursive: true, force: true });
- });
- describe("workflows/* protocol methods", () => {
- it("registers the method names", () => {
- expect(zcodeProtocolMethods.workflowsList).toBe("workflows/list");
- expect(zcodeProtocolMethods.workflowsGet).toBe("workflows/get");
- expect(zcodeProtocolMethods.workflowsUpdateMeta).toBe("workflows/updateMeta");
- expect(zcodeProtocolMethods.workflowsDelete).toBe("workflows/delete");
- expect(zcodeProtocolMethods.workflowsRuns).toBe("workflows/runs");
- expect(zcodeProtocolMethods.workflowsMove).toBe("workflows/move");
- });
- it("list:目录不存在回空(带 dir);坏文件进 invalid;legacy .workflow.js 不可见;不回脚本正文", async () => {
- expect(await listSavedWorkflowsOp(contextWith(), { workspace: workspace() })).toEqual({
- workflows: [],
- invalid: [],
- dir: projectDir(),
- });
- await seedWorkflow("release-check", {
- description: "发布前检查",
- whenToUse: "要发版时",
- args: { target: { type: "string", required: true } },
- });
- await writeFile(join(cwd, ".zcode", "workflows", "broken.dwf.ts"), "const x = 1;\n", "utf8");
- await writeFile(
- join(cwd, ".zcode", "workflows", "legacy.workflow.js"),
- "export const meta = {};\n",
- );
- const result = await listSavedWorkflowsOp(contextWith(), { workspace: workspace() });
- // 协议 schema(shared)与 contracts 的 entry schema 都要认同一份结果——两边形状互相钉住。
- expect(() => zcodeWorkflowsListResultSchema.parse(result)).not.toThrow();
- for (const entry of result.workflows) {
- expect(() => SavedWorkflowEntrySchema.parse(entry)).not.toThrow();
- }
- expect(result.workflows.map((entry) => entry.name)).toEqual(["release-check"]);
- expect(result.workflows[0]).toMatchObject({
- description: "发布前检查",
- whenToUse: "要发版时",
- scope: "project",
- path: join(cwd, ".zcode", "workflows", "release-check.dwf.ts"),
- });
- expect(JSON.stringify(result)).not.toContain("reviewer.ask");
- expect(result.invalid).toHaveLength(1);
- expect(result.invalid[0]?.path).toBe(join(cwd, ".zcode", "workflows", "broken.dwf.ts"));
- expect(result.invalid[0]?.reason).toContain("missing_frontmatter");
- });
- it("get:命中回 meta + 脚本本体;invalid_name / not_found / parse_error 各自可分辨", async () => {
- await seedWorkflow("release-check", { description: "发布前检查" });
- await writeFile(
- join(cwd, ".zcode", "workflows", "broken.dwf.ts"),
- "/* zcode-workflow\nx: [\n*/\n",
- );
- const hit = await getSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "release-check",
- });
- expect(() => zcodeWorkflowsGetResultSchema.parse(hit)).not.toThrow();
- expect(hit).toMatchObject({
- ok: true,
- name: "release-check",
- scope: "project",
- meta: { description: "发布前检查" },
- script: SCRIPT,
- });
- expect(
- await getSavedWorkflowOp(contextWith(), { workspace: workspace(), name: "../etc" }),
- ).toMatchObject({
- ok: false,
- reason: "invalid_name",
- });
- expect(
- await getSavedWorkflowOp(contextWith(), { workspace: workspace(), name: "missing" }),
- ).toEqual({
- ok: false,
- reason: "not_found",
- });
- expect(
- await getSavedWorkflowOp(contextWith(), { workspace: workspace(), name: "broken" }),
- ).toMatchObject({
- ok: false,
- reason: "parse_error",
- });
- });
- it("updateMeta:覆写后可读回新 meta,脚本正文逐字节不变;strict 拒未知键;不存在 → not_found 且不建文件", async () => {
- const path = await seedWorkflow("release-check", {
- description: "旧说明",
- args: { target: { type: "string", required: true } },
- });
- const scriptHashBefore = createHash("sha256").update(SCRIPT).digest("hex");
- const meta = {
- description: "新说明",
- whenToUse: "要发版、release、上线前检查时",
- args: {
- target: { type: "string", required: true, description: "要检查的包名或路径" },
- skipTests: { type: "boolean", default: false },
- },
- };
- const updated = await updateSavedWorkflowMetaOp(contextWith(), {
- workspace: workspace(),
- name: "release-check",
- meta,
- });
- expect(() => zcodeWorkflowsUpdateMetaResultSchema.parse(updated)).not.toThrow();
- expect(updated).toEqual({ ok: true, path });
- const parsed = parseSavedWorkflow(await readFile(path, "utf8"));
- expect(parsed.ok).toBe(true);
- if (!parsed.ok) throw new Error("unreachable");
- expect(parsed.meta).toEqual(SavedWorkflowMetaSchema.parse(meta));
- expect(createHash("sha256").update(parsed.script).digest("hex")).toBe(scriptHashBefore);
- await expect(
- updateSavedWorkflowMetaOp(contextWith(), {
- workspace: workspace(),
- name: "release-check",
- meta: { description: "x", permissionMode: "yolo" },
- }),
- ).rejects.toThrow();
- expect(
- await updateSavedWorkflowMetaOp(contextWith(), {
- workspace: workspace(),
- name: "ghost",
- meta: { description: "x" },
- }),
- ).toEqual({ ok: false, reason: "not_found" });
- await expect(readFile(join(cwd, ".zcode", "workflows", "ghost.dwf.ts"))).rejects.toThrow();
- });
- it("delete:删后 list 不再列出;not_found;非法名字在触盘前拒绝;不删同名 .workflow.js", async () => {
- const path = await seedWorkflow("release-check", { description: "发布前检查" });
- const legacyPath = join(cwd, ".zcode", "workflows", "release-check.workflow.js");
- await writeFile(legacyPath, "export const meta = {};\n");
- const deleted = await deleteSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "release-check",
- });
- expect(() => zcodeWorkflowsDeleteResultSchema.parse(deleted)).not.toThrow();
- expect(deleted).toEqual({ ok: true, path });
- expect(
- (await listSavedWorkflowsOp(contextWith(), { workspace: workspace() })).workflows,
- ).toEqual([]);
- await expect(readFile(legacyPath, "utf8")).resolves.toContain("export const meta");
- expect(
- await deleteSavedWorkflowOp(contextWith(), { workspace: workspace(), name: "release-check" }),
- ).toEqual({
- ok: false,
- reason: "not_found",
- });
- for (const name of ["..", "../x", "a/b", "a\\b", "."]) {
- expect(await deleteSavedWorkflowOp(contextWith(), { workspace: workspace(), name })).toEqual({
- ok: false,
- reason: "invalid_name",
- });
- }
- });
- it("runs:journal 缺席回空页;有 journal 时按 cwd + name 下推、多取一条判 truncated、行带归属字段", async () => {
- expect(
- await listSavedWorkflowRunsOp(contextWith(undefined), { workspace: workspace(), limit: 5 }),
- ).toEqual({
- runs: [],
- });
- const queries: unknown[] = [];
- const rows = [
- {
- runId: "run_2",
- name: "release-check",
- status: "completed",
- spentTokens: 38_200,
- timeCreated: 100,
- timeUpdated: 400,
- parentSessionId: "session-a",
- toolCallId: "call-2",
- args: { target: "packages/ui" },
- caps: { maxConcurrency: 1 },
- },
- {
- runId: "run_1",
- status: "errored",
- spentTokens: 6_100,
- timeCreated: 10,
- timeUpdated: 20,
- caps: { maxConcurrency: 1 },
- },
- {
- runId: "run_0",
- status: "stopped",
- spentTokens: 0,
- timeCreated: 1,
- timeUpdated: 2,
- caps: { maxConcurrency: 1 },
- },
- ];
- const journal = {
- countNodesByStatus: () => ({ completed: 0, failed: 0, running: 0 }),
- getRunRow: () => undefined,
- listRecentLogEvents: () => [],
- listRuns: (query: unknown) => {
- queries.push(query);
- return rows.slice(0, (query as { limit: number }).limit);
- },
- };
- const sessionStore = { workflowJournalStore: () => journal };
- const params = zcodeWorkflowsRunsParamsSchema.parse({
- workspace: workspace(),
- name: "release-check",
- limit: 2,
- });
- const result = await listSavedWorkflowRunsOp(contextWith(sessionStore), params);
- expect(() => zcodeWorkflowsRunsResultSchema.parse(result)).not.toThrow();
- expect(queries).toEqual([{ cwd, limit: 3, name: "release-check" }]);
- expect(result).toEqual({
- runs: [
- {
- runId: "run_2",
- name: "release-check",
- status: "completed",
- createdAt: 100,
- updatedAt: 400,
- spentTokens: 38_200,
- parentSessionId: "session-a",
- toolCallId: "call-2",
- args: { target: "packages/ui" },
- },
- { runId: "run_1", status: "errored", createdAt: 10, updatedAt: 20, spentTokens: 6_100 },
- ],
- truncated: true,
- });
- // 恰好 limit 条:没有探测行 → 不带 truncated 键。
- const exact = await listSavedWorkflowRunsOp(contextWith(sessionStore), {
- workspace: workspace(),
- limit: 3,
- });
- expect(exact).not.toHaveProperty("truncated");
- expect(exact.runs).toHaveLength(3);
- expect(queries.at(-1)).toEqual({ cwd, limit: 4 });
- // 上面那个假 journal 没有 listArtifactRows:能力缺席 ⇒ 每行的 artifacts 整字段缺席
- // (老 CLI / 注入的测试 store 逐字不变)。上面的 toEqual 已经把这一点钉死了。
- });
- // 用户面产物的中枢摘要(docs/dynamic-workflow/authoring.md「How the user sees them」)。
- // ⚠ 术语:这里的 artifact 是脚本经 `artifact.*` 发布给用户看的产出,不是脚本的顶层返回值。
- it("runs:能力在场时按 runId 逐行汇总 artifacts,只取 completed 行、取最新版、≤ 8 件", async () => {
- const artifactRow = (
- runId: string,
- id: string,
- version: number,
- overrides: Record<string, unknown> = {},
- ) => ({
- runId,
- siteId: `artifact#${version}`,
- ordinal: version,
- kind: "artifact" as const,
- inputHash: "h",
- status: "completed" as const,
- artifactId: id,
- result: { id, version, publishedAt: version, ...overrides },
- });
- const artifactRows: Record<string, unknown[]> = {
- run_2: [
- artifactRow("run_2", "audit", 1, { kind: "file", title: "旧版", bytes: 10 }),
- artifactRow("run_2", "audit", 2, {
- kind: "file",
- title: "审计报告",
- contentType: "application/pdf",
- bytes: 4_096,
- }),
- // 失败的发布不占版本号,也不该进 chips。
- {
- runId: "run_2",
- siteId: "artifact#3",
- ordinal: 3,
- kind: "artifact",
- inputHash: "h",
- status: "errored",
- artifactId: "broken",
- error: { code: "ArtifactSourceMissing", message: "gone" },
- },
- artifactRow("run_2", "perf", 1, { kind: "chart", spec: { x: { field: "round" } } }),
- ],
- run_1: [],
- };
- const journal = {
- countNodesByStatus: () => ({ completed: 0, failed: 0, running: 0 }),
- getRunRow: () => undefined,
- listRecentLogEvents: () => [],
- listRuns: (query: unknown) =>
- [
- { runId: "run_2", status: "completed", spentTokens: 1, timeCreated: 1, timeUpdated: 2 },
- { runId: "run_1", status: "errored", spentTokens: 1, timeCreated: 1, timeUpdated: 2 },
- ].slice(0, (query as { limit: number }).limit),
- listArtifactRows: (runId: string) => artifactRows[runId] ?? [],
- // 预置看板在场时才会被调到:标签 report 行的计数。
- listNodes: (runId: string) =>
- runId === "run_2"
- ? [
- { runId, siteId: "report#1", ordinal: 1, kind: "report", inputHash: "h", status: "completed", artifactId: "perf", result: {} },
- { runId, siteId: "report#2", ordinal: 2, kind: "report", inputHash: "h", status: "completed", artifactId: "perf", result: {} },
- // 无标签的 report 不计入任何产物。
- { runId, siteId: "report#3", ordinal: 3, kind: "report", inputHash: "h", status: "completed", result: {} },
- ]
- : [],
- };
- const sessionStore = { workflowJournalStore: () => journal };
- const result = await listSavedWorkflowRunsOp(contextWith(sessionStore), {
- workspace: workspace(),
- limit: 5,
- });
- expect(() => zcodeWorkflowsRunsResultSchema.parse(result)).not.toThrow();
- // 顶层字段取最新版(title 是 v2 的「审计报告」而不是 v1 的「旧版」);失败行不出现。
- expect(result.runs[0]?.artifacts).toEqual([
- { id: "audit", kind: "file", title: "审计报告", version: 2, contentType: "application/pdf" },
- { id: "perf", kind: "chart", version: 1 },
- ]);
- // 字节 / 条目 / spec 刻意不进中枢载荷:chip 上放不下,点开侧板即可。
- expect(JSON.stringify(result.runs[0]?.artifacts)).not.toContain("4096");
- expect(JSON.stringify(result.runs[0]?.artifacts)).not.toContain("round");
- // 零件的 run:整字段缺席,不是空数组。
- expect(result.runs[1]).not.toHaveProperty("artifacts");
- });
- });
- // 全局作用域(docs/dynamic-workflow/launch.md):五个方法的定向 `scope` 变体 + workflows/move。
- // 全局档落在隔离的 `<HOME>/.zcode/workflows/`(见顶部 beforeEach);同名的项目档不受牵连。
- describe("workflows/* 全局作用域与 workflows/move", () => {
- it("list:项目变体只列项目档 + dir=项目根(即使目录不存在);全局变体只列全局档 + dir=全局根", async () => {
- // 项目根不存在也回 dir(GUI 靠它 watch)。
- expect(
- await listSavedWorkflowsOp(contextWith(), { workspace: workspace(), scope: "project" }),
- ).toEqual({
- workflows: [],
- invalid: [],
- dir: projectDir(),
- });
- expect(
- await listSavedWorkflowsOp(contextWith(), { workspace: workspace(), scope: "global" }),
- ).toEqual({
- workflows: [],
- invalid: [],
- dir: globalDir(),
- });
- await seedWorkflow("proj-only", { description: "本项目档" });
- await seedGlobalWorkflow("global-only", { description: "全局档" });
- const proj = await listSavedWorkflowsOp(contextWith(), {
- workspace: workspace(),
- scope: "project",
- });
- expect(() => zcodeWorkflowsListResultSchema.parse(proj)).not.toThrow();
- expect(proj.dir).toBe(projectDir());
- expect(proj.workflows.map((entry) => entry.name)).toEqual(["proj-only"]);
- expect(proj.workflows[0]).toMatchObject({ scope: "project" });
- const glob = await listSavedWorkflowsOp(contextWith(), {
- workspace: workspace(),
- scope: "global",
- });
- expect(() => zcodeWorkflowsListResultSchema.parse(glob)).not.toThrow();
- expect(glob.dir).toBe(globalDir());
- expect(glob.workflows.map((entry) => entry.name)).toEqual(["global-only"]);
- expect(glob.workflows[0]).toMatchObject({
- scope: "global",
- path: join(globalDir(), "global-only.dwf.ts"),
- });
- });
- it("list:同名在两档都在时——项目变体只见项目档、全局变体只见全局档(定向列表不做遮蔽)", async () => {
- await seedWorkflow("shared", { description: "项目那份" });
- await seedGlobalWorkflow("shared", { description: "全局那份" });
- const proj = await listSavedWorkflowsOp(contextWith(), {
- workspace: workspace(),
- scope: "project",
- });
- expect(proj.workflows).toMatchObject([
- { name: "shared", description: "项目那份", scope: "project" },
- ]);
- const glob = await listSavedWorkflowsOp(contextWith(), {
- workspace: workspace(),
- scope: "global",
- });
- expect(glob.workflows).toMatchObject([
- { name: "shared", description: "全局那份", scope: "global" },
- ]);
- });
- it("get:scope:global 读全局档、不被同名项目档遮蔽;默认(无 scope)读项目档", async () => {
- await seedWorkflow("shared", { description: "项目那份" });
- await seedGlobalWorkflow("shared", { description: "全局那份" });
- const glob = await getSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "shared",
- scope: "global",
- });
- expect(() => zcodeWorkflowsGetResultSchema.parse(glob)).not.toThrow();
- expect(glob).toMatchObject({
- ok: true,
- name: "shared",
- scope: "global",
- path: join(globalDir(), "shared.dwf.ts"),
- meta: { description: "全局那份" },
- });
- const def = await getSavedWorkflowOp(contextWith(), { workspace: workspace(), name: "shared" });
- expect(def).toMatchObject({ ok: true, scope: "project", meta: { description: "项目那份" } });
- // 只在全局档存在:项目变体 not_found,全局变体命中。
- await seedGlobalWorkflow("global-only", { description: "只全局" });
- expect(
- await getSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "global-only",
- scope: "project",
- }),
- ).toEqual({ ok: false, reason: "not_found" });
- expect(
- await getSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "global-only",
- scope: "global",
- }),
- ).toMatchObject({ ok: true, scope: "global" });
- });
- it("updateMeta:scope:global 改全局档、同名项目档逐字不动", async () => {
- const projPath = await seedWorkflow("shared", { description: "项目原样" });
- await seedGlobalWorkflow("shared", { description: "全局旧说明" });
- const projBefore = await readFile(projPath, "utf8");
- const updated = await updateSavedWorkflowMetaOp(contextWith(), {
- workspace: workspace(),
- name: "shared",
- meta: { description: "全局新说明" },
- scope: "global",
- });
- expect(() => zcodeWorkflowsUpdateMetaResultSchema.parse(updated)).not.toThrow();
- expect(updated).toEqual({ ok: true, path: join(globalDir(), "shared.dwf.ts") });
- const globParsed = parseSavedWorkflow(
- await readFile(join(globalDir(), "shared.dwf.ts"), "utf8"),
- );
- expect(globParsed.ok).toBe(true);
- if (!globParsed.ok) throw new Error("unreachable");
- expect(globParsed.meta.description).toBe("全局新说明");
- // 项目档逐字不变。
- expect(await readFile(projPath, "utf8")).toBe(projBefore);
- });
- it("delete:scope:global 删全局档、同名项目档留存;按 scope 选根(不写死 roots[0])", async () => {
- const projPath = await seedWorkflow("shared", { description: "项目那份" });
- const globPath = await seedGlobalWorkflow("shared", { description: "全局那份" });
- const deleted = await deleteSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "shared",
- scope: "global",
- });
- expect(() => zcodeWorkflowsDeleteResultSchema.parse(deleted)).not.toThrow();
- expect(deleted).toEqual({ ok: true, path: globPath });
- await expect(readFile(globPath, "utf8")).rejects.toThrow();
- // 项目档留存。
- await expect(readFile(projPath, "utf8")).resolves.toContain("项目那份");
- expect(
- (await listSavedWorkflowsOp(contextWith(), { workspace: workspace(), scope: "project" }))
- .workflows,
- ).toHaveLength(1);
- expect(
- (await listSavedWorkflowsOp(contextWith(), { workspace: workspace(), scope: "global" }))
- .workflows,
- ).toEqual([]);
- });
- it("runs:全局变体跨 cwd(省 cwd 谓词)、每行带 cwd;项目变体仍按 cwd 过滤", async () => {
- const queries: unknown[] = [];
- const rows = [
- {
- runId: "run_b",
- name: "research",
- status: "completed",
- spentTokens: 100,
- timeCreated: 20,
- timeUpdated: 30,
- cwd: "/repos/beta",
- caps: { maxConcurrency: 1 },
- },
- {
- runId: "run_a",
- name: "research",
- status: "errored",
- spentTokens: 50,
- timeCreated: 5,
- timeUpdated: 10,
- cwd: "/repos/alpha",
- caps: { maxConcurrency: 1 },
- },
- ];
- const journal = {
- countNodesByStatus: () => ({ completed: 0, failed: 0, running: 0 }),
- getRunRow: () => undefined,
- listRecentLogEvents: () => [],
- listRuns: (query: unknown) => {
- queries.push(query);
- return rows.slice(0, (query as { limit: number }).limit);
- },
- };
- const sessionStore = { workflowJournalStore: () => journal };
- const glob = await listSavedWorkflowRunsOp(contextWith(sessionStore), {
- workspace: workspace(),
- name: "research",
- limit: 10,
- scope: "global",
- });
- expect(() => zcodeWorkflowsRunsResultSchema.parse(glob)).not.toThrow();
- // 全局变体:查询袋没有 cwd 键(跨所有项目)。
- expect(queries).toEqual([{ limit: 11, name: "research" }]);
- expect(glob.runs).toEqual([
- {
- runId: "run_b",
- name: "research",
- status: "completed",
- createdAt: 20,
- updatedAt: 30,
- spentTokens: 100,
- cwd: "/repos/beta",
- },
- {
- runId: "run_a",
- name: "research",
- status: "errored",
- createdAt: 5,
- updatedAt: 10,
- spentTokens: 50,
- cwd: "/repos/alpha",
- },
- ]);
- // 项目变体:仍带 cwd 谓词。
- await listSavedWorkflowRunsOp(contextWith(sessionStore), {
- workspace: workspace(),
- name: "research",
- limit: 10,
- scope: "project",
- });
- expect(queries.at(-1)).toEqual({ cwd, limit: 11, name: "research" });
- });
- // move 只有全局→项目一向(docs/dynamic-workflow/launch.md「Promote to global」):项目→全局
- // 是模型的概括「提升为全局」,协议上没有那一向。
- it("move:全局→项目成功——逐字节相同、源删除、结果 {ok, from, to}(无 to 参数)", async () => {
- const globPath = await seedGlobalWorkflow("mover", {
- description: "回项目",
- args: { target: { type: "string", required: true } },
- });
- const before = await readFile(globPath, "utf8");
- const result = await moveSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "mover",
- });
- expect(() => zcodeWorkflowsMoveResultSchema.parse(result)).not.toThrow();
- const projPath = join(projectDir(), "mover.dwf.ts");
- expect(result).toEqual({ ok: true, from: globPath, to: projPath });
- // 源已删。
- await expect(readFile(globPath, "utf8")).rejects.toThrow();
- // 目标逐字节相同。
- expect(await readFile(projPath, "utf8")).toBe(before);
- });
- it("move:带 to 参数被 -32602 拒(那一向已不存在)", async () => {
- await seedWorkflow("mover", { description: "项目那份" });
- await expect(
- moveSavedWorkflowOp(contextWith(), { workspace: workspace(), name: "mover", to: "global" }),
- ).rejects.toThrow();
- // 项目档原样不动。
- await expect(readFile(join(projectDir(), "mover.dwf.ts"), "utf8")).resolves.toContain(
- "项目那份",
- );
- });
- it("move:target_exists(项目里已有同名,不覆盖)带项目落点 path", async () => {
- const projPath = await seedWorkflow("dup", { description: "项目那份" });
- const globPath = await seedGlobalWorkflow("dup", { description: "全局那份" });
- const result = await moveSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "dup",
- });
- expect(() => zcodeWorkflowsMoveResultSchema.parse(result)).not.toThrow();
- expect(result).toEqual({ ok: false, reason: "target_exists", path: projPath });
- // 两份都还在。
- await expect(readFile(join(projectDir(), "dup.dwf.ts"), "utf8")).resolves.toContain("项目那份");
- await expect(readFile(globPath, "utf8")).resolves.toContain("全局那份");
- });
- it("move:not_found(全局根没有这个名字;项目档不是源)与 invalid_name(名字先验)", async () => {
- await seedWorkflow("ghost", { description: "只在项目里" });
- expect(
- await moveSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "ghost",
- }),
- ).toEqual({ ok: false, reason: "not_found" });
- const invalid = await moveSavedWorkflowOp(contextWith(), {
- workspace: workspace(),
- name: "../etc",
- });
- expect(invalid).toMatchObject({ ok: false, reason: "invalid_name" });
- expect(() => zcodeWorkflowsMoveResultSchema.parse(invalid)).not.toThrow();
- });
- });
|