Spaces:
Running
Running
Download web/src/components/workflows/dsl.test.ts from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 7.47 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/workflows/dsl.test.ts
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/components/workflows/dsl.test.ts
-
curl -L -o dsl.test.ts https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/workflows/dsl.test.ts
7.47 kB
| import { describe as suite, expect, it } from "vitest"; | |
| import { | |
| addStep, | |
| blankStep, | |
| canonical, | |
| deleteStep, | |
| describe, | |
| describeCondition, | |
| issuesFromBody, | |
| lineTone, | |
| moveStep, | |
| parseDefinition, | |
| refText, | |
| sameDefinition, | |
| toFlow, | |
| TRIGGER_NODE_ID, | |
| updateStep, | |
| versionActions, | |
| type StepNodeData, | |
| } from "./dsl"; | |
| import type { WorkflowDefinition } from "./types"; | |
| const ORDER_CHECK: WorkflowDefinition = { | |
| id: "morning_order_check", | |
| name: "Morning order check", | |
| description: "", | |
| trigger: { type: "schedule", cron: "0 8 * * *", tz: "Asia/Karachi" }, | |
| steps: [ | |
| { id: "lookup", tool: "demo.lookup_order", args: { order_number: "ORD-1021" } }, | |
| { | |
| id: "note", | |
| tool: "demo.add_note", | |
| args: { order_number: "$.lookup.number", note: "Morning check: status {{ $.lookup.status }}" }, | |
| }, | |
| ], | |
| }; | |
| suite("describer (port of core/workflows/dsl.py describe)", () => { | |
| it("renders the demo template exactly like the server", () => { | |
| expect(describe(ORDER_CHECK)).toEqual([ | |
| "On schedule '0 8 * * *' (Asia/Karachi):", | |
| "1. [lookup] call demo.lookup_order(order_number=ORD-1021)", | |
| "2. [note] call demo.add_note(order_number=«lookup.number», note=Morning check: status {{ $.lookup.status }})", | |
| ]); | |
| }); | |
| it("handles events, conditions, for_each, approvals, waits and sign-offs", () => { | |
| const d: WorkflowDefinition = { | |
| id: "cod_followup", | |
| name: "COD follow-up", | |
| trigger: { | |
| type: "event", | |
| event_type: "order.created", | |
| when: [{ left: "$.event.payment", op: "eq", right: "cod" }], | |
| }, | |
| steps: [ | |
| { id: "find", tool: "shop.search", args: { tags: ["a", "$.event.tag"], opts: { deep: true, n: null } } }, | |
| { id: "pause", wait: { seconds: 300 } }, | |
| { id: "gate", approval: { summary: "Check the list" }, when: [{ left: "$.find.count", op: "gt", right: 0 }] }, | |
| { | |
| id: "msg", | |
| tool: "shop.send_message", | |
| for_each: "$.find.items", | |
| requires_approval: true, | |
| args: { to: "$.item.phone" }, | |
| when: [{ left: "$.item.phone", op: "exists" }], | |
| }, | |
| ], | |
| }; | |
| expect(describe(d)).toEqual([ | |
| "When event 'order.created' arrives when «event.payment» is cod:", | |
| "1. [find] call shop.search(tags=[a, «event.tag»], opts={deep: True, n: None})", | |
| "2. [pause] wait 300 seconds", | |
| "3. [gate] ask the owner to sign off: Check the list only if «find.count» > 0", | |
| "4. [msg] for each item in «find.items», call shop.send_message(to=«item.phone») after a human approves only if «item.phone» exists", | |
| ]); | |
| expect(describe({ ...d, trigger: { type: "manual" } })[0]).toBe("When started by hand:"); | |
| }); | |
| it("formats values like Python str()", () => { | |
| expect(refText("$.a.b")).toBe("«a.b»"); | |
| expect(refText(false)).toBe("False"); | |
| expect(refText(12)).toBe("12"); | |
| expect(describeCondition({ left: "$.x", op: "not_in", right: [1, 2] })).toBe("«x» is not one of [1, 2]"); | |
| expect(describeCondition({ left: "$.x", op: "falsy" })).toBe("«x» is not set"); | |
| }); | |
| }); | |
| suite("definition -> canvas graph", () => { | |
| it("makes a trigger node plus one node per step, chained in order", () => { | |
| const { nodes, edges } = toFlow(ORDER_CHECK, 1); | |
| expect(nodes.map((n) => n.type)).toEqual(["trigger", "step", "step"]); | |
| expect(nodes[0].id).toBe(TRIGGER_NODE_ID); | |
| expect(nodes.map((n) => n.position.y)).toEqual([0, 150, 300]); | |
| expect((nodes[2].data as StepNodeData).step.id).toBe("note"); | |
| expect((nodes[2].data as StepNodeData).kind).toBe("tool"); | |
| expect(nodes[2].selected).toBe(true); | |
| expect(edges.map((e) => [e.source, e.target])).toEqual([ | |
| [TRIGGER_NODE_ID, nodes[1].id], | |
| [nodes[1].id, nodes[2].id], | |
| ]); | |
| }); | |
| it("includes conditions as text", () => { | |
| const d = updateStep(ORDER_CHECK, 0, { when: [{ left: "$.event.x", op: "truthy" }] }); | |
| expect((toFlow(d).nodes[1].data as StepNodeData).conditions).toEqual(["«event.x» is set"]); | |
| }); | |
| }); | |
| suite("JSON sync and step edits", () => { | |
| it("compares definitions independent of key order", () => { | |
| const reordered = JSON.parse( | |
| '{"steps":' + JSON.stringify(ORDER_CHECK.steps) + ',"trigger":{"tz":"Asia/Karachi","cron":"0 8 * * *","type":"schedule"},"name":"Morning order check","id":"morning_order_check","description":""}', | |
| ); | |
| expect(sameDefinition(ORDER_CHECK, reordered)).toBe(true); | |
| expect(canonical({ b: 1, a: { d: 2, c: 3 } })).toBe('{"a":{"c":3,"d":2},"b":1}'); | |
| expect(sameDefinition(ORDER_CHECK, updateStep(ORDER_CHECK, 1, { args: { note: "x" } }))).toBe(false); | |
| }); | |
| it("parses editor text and reports errors", () => { | |
| expect(parseDefinition(JSON.stringify(ORDER_CHECK))).toEqual({ ok: true, value: ORDER_CHECK }); | |
| expect(parseDefinition("{").ok).toBe(false); | |
| expect(parseDefinition("[]").ok).toBe(false); | |
| expect(parseDefinition('{"trigger":{"type":"manual"}}')).toEqual({ | |
| ok: false, | |
| error: '"steps" must be a list.', | |
| }); | |
| }); | |
| it("moves, adds, deletes and updates steps without mutating the input", () => { | |
| const before = JSON.stringify(ORDER_CHECK); | |
| const moved = moveStep(ORDER_CHECK, 1, -1); | |
| expect(moved.steps.map((s) => s.id)).toEqual(["note", "lookup"]); | |
| expect(moveStep(ORDER_CHECK, 0, -1)).toBe(ORDER_CHECK); | |
| const added = addStep(ORDER_CHECK, blankStep(ORDER_CHECK, "wait"), 0); | |
| expect(added.steps.map((s) => s.id)).toEqual(["lookup", "step_3", "note"]); | |
| expect(added.steps[1].wait).toEqual({ seconds: 60 }); | |
| expect(addStep(ORDER_CHECK, blankStep(ORDER_CHECK, "approval")).steps[2].approval).toBeTruthy(); | |
| expect(deleteStep(ORDER_CHECK, 0).steps.map((s) => s.id)).toEqual(["note"]); | |
| const updated = updateStep(ORDER_CHECK, 0, { for_each: "$.x", tool: undefined }); | |
| expect(updated.steps[0]).toEqual({ id: "lookup", args: { order_number: "ORD-1021" }, for_each: "$.x" }); | |
| expect(JSON.stringify(ORDER_CHECK)).toBe(before); | |
| }); | |
| it("reads the structured 422 detail", () => { | |
| const body = { | |
| detail: { | |
| message: "check failed", | |
| ok: false, | |
| errors: [{ code: "unknown_tool", message: "no such tool", step: "note" }], | |
| warnings: [], | |
| }, | |
| }; | |
| expect(issuesFromBody(body)).toEqual({ | |
| message: "check failed", | |
| errors: [{ code: "unknown_tool", message: "no such tool", step: "note" }], | |
| warnings: [], | |
| }); | |
| expect(issuesFromBody({ detail: "nope" })).toBeNull(); | |
| }); | |
| it("gates lifecycle buttons by status", () => { | |
| expect(versionActions({ status: "dry_run_ok", dry_run_current: true }, null)).toEqual({ | |
| approve: true, | |
| activate: false, | |
| rollback: false, | |
| }); | |
| expect(versionActions({ status: "dry_run_ok", dry_run_current: false }, 1).approve).toBe(false); | |
| expect(versionActions({ status: "rolled_back", dry_run_current: true }, 1)).toEqual({ | |
| approve: false, | |
| activate: true, | |
| rollback: true, | |
| }); | |
| expect(lineTone(" + demo.add_note(x)")).toBe("execute"); | |
| expect(lineTone(" ? demo.refund(x) [needs_approval: big]")).toBe("approval"); | |
| expect(lineTone(" x demo.cancel()")).toBe("denied"); | |
| expect(lineTone("[lookup] read")).toBe("plain"); | |
| }); | |
| }); | |