import { expect, test } from "bun:test"; import ky from "ky"; import { deleteWorkflow, updateWorkflowChooses } from "./api.js"; import { refreshWorkflowChooses, updateWorkflow } from "./services.js"; import { WorkflowDetailSchema } from "./types.js"; import { WorkflowSavePartialError } from "./workflow/save-error.js"; import { AuthenticationError } from "../../core/errors.js"; test("Piper 无返回值写操作兼容空成功,拒绝业务失败与无效 JSON", async () => { for (const action of [deleteWorkflow, updateWorkflowChooses]) { for (const body of ["", JSON.stringify({ code: 200, data: [] })]) { const client = ky.create({ prefix: "https://piper.invalid", retry: 0, fetch: async () => new Response(body) }); await action(client, "p"); } for (const body of [JSON.stringify({ code: 500, msg: "拒绝写入" }), "{broken"]) { const client = ky.create({ prefix: "https://piper.invalid", retry: 0, fetch: async () => new Response(body) }); await expect(action(client, "p")).rejects.toThrow(); } const client = ky.create({ prefix: "https://piper.invalid", retry: 0, fetch: async () => Response.json({ code: 401, msg: "凭证失效" }) }); await expect(action(client, "p")).rejects.toBeInstanceOf(AuthenticationError); } }); test("两阶段保存保留已提交版本,恢复只刷新 chooses", async () => { const workflow = WorkflowDetailSchema.parse({ id: "p", version: "v1", tasksVersion: 1 }); const calls: string[] = []; let failRefresh = true; const client = ky.create({ prefix: "https://piper.invalid", retry: 0, fetch: async input => { const path = new URL(input instanceof Request ? input.url : input.toString()).pathname; calls.push(path); return path.endsWith("/chooses") ? Response.json(failRefresh ? { code: 500, msg: "刷新拒绝" } : { code: 200, data: [] }) : Response.json({ code: 200, data: { ...workflow, tasksVersion: 2 } }); } }); let partial: WorkflowSavePartialError | undefined; try { await updateWorkflow(client, workflow); } catch (error) { expect(error).toBeInstanceOf(WorkflowSavePartialError); partial = error as WorkflowSavePartialError; } expect(partial?.savedWorkflow.tasksVersion).toBe(2); expect(partial?.details).toMatchObject({ remoteSaved: true, failedStep: "chooses", saved: { tasksVersion: 2 } }); failRefresh = false; await refreshWorkflowChooses(client, "p"); expect(calls).toEqual(["/api/easo/pipelines/p", "/api/easo/pipelines/p/chooses", "/api/easo/pipelines/p/chooses"]); }); test("主保存失败不会刷新 chooses,也不报告部分成功", async () => { const calls: string[] = []; const client = ky.create({ prefix: "https://piper.invalid", retry: 0, fetch: async input => { calls.push(input instanceof Request ? input.url : input.toString()); return Response.json({ code: 500, msg: "主保存拒绝" }); } }); const error = await updateWorkflow(client, WorkflowDetailSchema.parse({ id: "p" })).catch(error => error); expect(error).not.toBeInstanceOf(WorkflowSavePartialError); expect(calls).toHaveLength(1); });