|
| 1 | +import { strict as assert } from "assert" |
| 2 | +import nock from "nock" |
| 3 | +import MetafoldClient from "../metafold.js" |
| 4 | +import type { Workflow, WorkflowJSON } from "./Workflows.js" |
| 5 | + |
| 6 | +const defaultDate = new Date("Mon, 01 Jan 2024 00:00:00 GMT") |
| 7 | + |
| 8 | +// Default sort order is descending by id |
| 9 | +const workflowList: WorkflowJSON[] = [ |
| 10 | + { |
| 11 | + "id": "3", |
| 12 | + "jobs": ["1", "2"], |
| 13 | + "state": "success", |
| 14 | + "created": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 15 | + "started": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 16 | + "finished": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 17 | + "definition": "...", |
| 18 | + "project_id": "1", |
| 19 | + }, |
| 20 | + { |
| 21 | + "id": "2", |
| 22 | + "jobs": ["1", "2"], |
| 23 | + "state": "started", |
| 24 | + "created": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 25 | + "started": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 26 | + "finished": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 27 | + "definition": "...", |
| 28 | + "project_id": "1", |
| 29 | + }, |
| 30 | + { |
| 31 | + "id": "1", |
| 32 | + "jobs": ["1", "2"], |
| 33 | + "state": "success", |
| 34 | + "created": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 35 | + "started": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 36 | + "finished": "Mon, 01 Jan 2024 00:00:00 GMT", |
| 37 | + "definition": "...", |
| 38 | + "project_id": "1", |
| 39 | + }, |
| 40 | +] |
| 41 | + |
| 42 | +const newWorkflow: WorkflowJSON = { |
| 43 | + id: "1", |
| 44 | + jobs: ["1", "2"], |
| 45 | + state: "success", |
| 46 | + created: "Mon, 01 Jan 2024 00:00:00 GMT", |
| 47 | + started: "Mon, 01 Jan 2024 00:00:00 GMT", |
| 48 | + finished: "Mon, 01 Jan 2024 00:00:00 GMT", |
| 49 | + definition: "foo", |
| 50 | + project_id: "1", |
| 51 | +} |
| 52 | + |
| 53 | +describe("Workflows", function() { |
| 54 | + const metafold = new MetafoldClient("testtoken", "1") |
| 55 | + |
| 56 | + nock("https://api.metafold3d.com", { |
| 57 | + reqheaders: { "Authorization": "Bearer testtoken" } |
| 58 | + }) |
| 59 | + .get("/projects/1/workflows") |
| 60 | + .reply(200, workflowList) |
| 61 | + .get("/projects/1/workflows") |
| 62 | + .query({ sort: "id:1" }) |
| 63 | + .reply(200, workflowList.slice().reverse()) |
| 64 | + .get("/projects/1/workflows") |
| 65 | + .query({ q: "state:started" }) |
| 66 | + .reply(200, workflowList.slice().filter( |
| 67 | + (w: WorkflowJSON) => w.state === "started")) |
| 68 | + .get("/projects/1/workflows/1") |
| 69 | + .reply(200, workflowList[workflowList.length - 1]) |
| 70 | + // Workflow success |
| 71 | + .post("/projects/1/workflows") |
| 72 | + .reply(202, { |
| 73 | + ...newWorkflow, |
| 74 | + link: "https://api.metafold3d.com/projects/1/workflows/1/status", |
| 75 | + }) |
| 76 | + .get("/projects/1/workflows/1/status") |
| 77 | + .times(2) |
| 78 | + .reply(202, { |
| 79 | + ...newWorkflow, |
| 80 | + state: "started", |
| 81 | + }) |
| 82 | + .get("/projects/1/workflows/1/status") |
| 83 | + .reply(201, { |
| 84 | + ...newWorkflow, |
| 85 | + state: "success", |
| 86 | + }) |
| 87 | + // Workflow failure |
| 88 | + .post("/projects/1/workflows") |
| 89 | + .reply(202, { |
| 90 | + ...newWorkflow, |
| 91 | + link: "https://api.metafold3d.com/projects/1/workflows/1/status", |
| 92 | + }) |
| 93 | + .get("/projects/1/workflows/1/status") |
| 94 | + .reply(400, { msg: "Bad request" }) |
| 95 | + |
| 96 | + describe("#list()", function() { |
| 97 | + it("should retrieve workflow for project 1", async function() { |
| 98 | + const workflows = await metafold.workflows.list() |
| 99 | + assert.deepEqual(workflows.map((w: Workflow) => w.id), ["3", "2", "1"]) |
| 100 | + }) |
| 101 | + it("should retrieve workflow for project 1 sorted by id asc", async function() { |
| 102 | + const workflows = await metafold.workflows.list({ sort: "id:1" }) |
| 103 | + assert.deepEqual(workflows.map((w: Workflow) => w.id), ["1", "2", "3"]) |
| 104 | + }) |
| 105 | + it("should retrieve workflow for project 1 with a given state", async function() { |
| 106 | + const workflows = await metafold.workflows.list({ q: "state:started" }) |
| 107 | + assert.ok( |
| 108 | + workflows.every((w: Workflow) => w.state === "started"), |
| 109 | + "Workflow.state !== state:started") |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + describe("#get()", function() { |
| 114 | + it("should retrieve workflow 1", async function() { |
| 115 | + const workflow = await metafold.workflows.get("1") |
| 116 | + assert.deepEqual(workflow, { |
| 117 | + id: "1", |
| 118 | + jobs: ["1", "2"], |
| 119 | + state: "success", |
| 120 | + created: defaultDate, |
| 121 | + started: defaultDate, |
| 122 | + finished: defaultDate, |
| 123 | + definition: "...", |
| 124 | + project_id: "1", |
| 125 | + }) |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + describe("#run()", function() { |
| 130 | + it("should dispatch a workflow and wait for success", async function() { |
| 131 | + this.timeout(1000 * 8) // 8 secs |
| 132 | + |
| 133 | + const definition = "foo" |
| 134 | + const workflow = await metafold.workflows.run(definition) |
| 135 | + assert.deepEqual(workflow, { |
| 136 | + id: "1", |
| 137 | + jobs: ["1", "2"], |
| 138 | + state: "success", |
| 139 | + created: defaultDate, |
| 140 | + started: defaultDate, |
| 141 | + finished: defaultDate, |
| 142 | + definition: "foo", |
| 143 | + project_id: "1", |
| 144 | + }) |
| 145 | + }) |
| 146 | + it("should dispatch a workflow and throw an error on failure", async function() { |
| 147 | + this.timeout(1000 * 3) // 3 secs |
| 148 | + await assert.rejects( |
| 149 | + metafold.workflows.run("foo"), |
| 150 | + /^Error: Bad request$/, |
| 151 | + ) |
| 152 | + }) |
| 153 | + it("should dispatch a workflow and throw an error on timeout", async function() { |
| 154 | + this.timeout(1000 * 3) // 3 secs |
| 155 | + const scope = nock("https://api.metafold3d.com") |
| 156 | + .matchHeader("Authorization", "Bearer testtoken") |
| 157 | + .post("/projects/1/workflows") |
| 158 | + .reply(202, { |
| 159 | + ...newWorkflow, |
| 160 | + link: "https://api.metafold3d.com/projects/1/workflows/1/status", |
| 161 | + }) |
| 162 | + .get("/projects/1/workflows/1/status") |
| 163 | + .reply(202, { |
| 164 | + ...newWorkflow, |
| 165 | + state: "started", |
| 166 | + }) |
| 167 | + |
| 168 | + // Simulate timeout after polling status once. |
| 169 | + // FIXME(ryan): Since this is all time-based this test may be a little flakey. |
| 170 | + await assert.rejects( |
| 171 | + metafold.workflows.run("foo", {}, {}, 2500), |
| 172 | + /Workflow failed to complete within 2500 ms/, |
| 173 | + ) |
| 174 | + assert.ok(scope.isDone(), "GET /projects/1/workflows/1/status not called") |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + describe("#delete()", function() { |
| 179 | + it("should delete workflow 1", async function() { |
| 180 | + const scope = nock("https://api.metafold3d.com") |
| 181 | + .matchHeader("Authorization", "Bearer testtoken") |
| 182 | + .delete("/projects/1/workflows/1") |
| 183 | + .reply(200, "OK") |
| 184 | + await metafold.workflows.delete("1") |
| 185 | + assert.ok(scope.isDone(), "DELETE not called") |
| 186 | + }) |
| 187 | + }) |
| 188 | +}) |
0 commit comments