@powerhousedao/switchboard 6.2.3-dev.84 → 6.2.3-dev.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
- package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
- package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +13 -0
- package/dist/index.mjs +11 -8
- package/dist/index.mjs.map +1 -1
- package/dist/plugins/attachments.mjs +3 -3
- package/dist/plugins/workflows.mjs +3 -3
- package/dist/{server-B3qhPKNn.mjs → server-YNY3Qw9a.mjs} +4 -4
- package/dist/{server-B3qhPKNn.mjs.map → server-YNY3Qw9a.mjs.map} +1 -1
- package/dist/server.mjs +3 -3
- package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -1
- package/dist/{workflows-B1VsL5Lo.mjs → workflows-DkIrfmdn.mjs} +445 -9
- package/dist/workflows-DkIrfmdn.mjs.map +1 -0
- package/package.json +16 -16
- package/test/goldens/packages.graphql +1 -1
- package/test/goldens/privacy.graphql +1 -1
- package/test/goldens/reactor-drive-nodes.graphql +1 -1
- package/test/goldens/resolvers.json +4 -0
- package/test/goldens/supergraph.graphql +95 -1
- package/test/goldens/workflow-runtime.graphql +95 -1
- package/test/redact-webhook-path.test.ts +9 -0
- package/test/workflow/resolvers.test.ts +106 -0
- package/test/workflow/waitpoint-routes.test.ts +284 -0
- package/test/workflow/waitpoint-runtime.test.ts +204 -0
- package/dist/workflows-B1VsL5Lo.mjs.map +0 -1
|
@@ -28,6 +28,27 @@ scalar Amount_Percentage
|
|
|
28
28
|
|
|
29
29
|
scalar Amount_Tokens
|
|
30
30
|
|
|
31
|
+
"""
|
|
32
|
+
Who may decide an approval, and how the editor renders it.
|
|
33
|
+
"""
|
|
34
|
+
type ApprovalRequest {
|
|
35
|
+
title: String!
|
|
36
|
+
"""
|
|
37
|
+
Markdown.
|
|
38
|
+
"""
|
|
39
|
+
body: String
|
|
40
|
+
approveLabel: String!
|
|
41
|
+
rejectLabel: String!
|
|
42
|
+
"""
|
|
43
|
+
none | reject | both
|
|
44
|
+
"""
|
|
45
|
+
reasonRequiredOn: String!
|
|
46
|
+
"""
|
|
47
|
+
DIDs allowed to decide; empty means anyone with write access.
|
|
48
|
+
"""
|
|
49
|
+
approvers: [String!]!
|
|
50
|
+
}
|
|
51
|
+
|
|
31
52
|
scalar AttachmentRef
|
|
32
53
|
|
|
33
54
|
"""
|
|
@@ -687,6 +708,47 @@ scalar Unknown
|
|
|
687
708
|
|
|
688
709
|
scalar Upload
|
|
689
710
|
|
|
711
|
+
"""
|
|
712
|
+
A point where a run paused, and how it resumes.
|
|
713
|
+
"""
|
|
714
|
+
type WaitpointRecord {
|
|
715
|
+
id: String!
|
|
716
|
+
runId: String!
|
|
717
|
+
workflowId: String!
|
|
718
|
+
stepId: String!
|
|
719
|
+
stepKey: String!
|
|
720
|
+
stepPath: String!
|
|
721
|
+
pieceName: String!
|
|
722
|
+
blockName: String!
|
|
723
|
+
"""
|
|
724
|
+
DELAY | WEBHOOK
|
|
725
|
+
"""
|
|
726
|
+
type: String!
|
|
727
|
+
"""
|
|
728
|
+
approval | webhook | delay
|
|
729
|
+
"""
|
|
730
|
+
kind: String!
|
|
731
|
+
"""
|
|
732
|
+
PENDING | COMPLETED | CONSUMED | EXPIRED | CANCELLED
|
|
733
|
+
"""
|
|
734
|
+
status: String!
|
|
735
|
+
createdAt: String!
|
|
736
|
+
"""
|
|
737
|
+
DELAY only: when the run resumes.
|
|
738
|
+
"""
|
|
739
|
+
resumeAt: String
|
|
740
|
+
expiresAt: String
|
|
741
|
+
resolvedAt: String
|
|
742
|
+
"""
|
|
743
|
+
The DID that resolved it from the editor.
|
|
744
|
+
"""
|
|
745
|
+
decidedBy: String
|
|
746
|
+
"""
|
|
747
|
+
Set by the Request approval block; null otherwise.
|
|
748
|
+
"""
|
|
749
|
+
approval: ApprovalRequest
|
|
750
|
+
}
|
|
751
|
+
|
|
690
752
|
type WebhookEndpointRecord {
|
|
691
753
|
workflowId: String!
|
|
692
754
|
url: String!
|
|
@@ -731,6 +793,10 @@ type WorkflowRunRecord {
|
|
|
731
793
|
workflowVersion: Int!
|
|
732
794
|
triggerKind: String!
|
|
733
795
|
triggerPayload: Unknown
|
|
796
|
+
"""
|
|
797
|
+
PENDING | RUNNING | WAITING | SUCCEEDED | FAILED | CANCELLED. WAITING: paused
|
|
798
|
+
at a waitpoint (approval, webhook or delay); the run holds no worker.
|
|
799
|
+
"""
|
|
734
800
|
status: String!
|
|
735
801
|
error: String
|
|
736
802
|
errorName: String
|
|
@@ -749,6 +815,10 @@ type WorkflowRunRecord {
|
|
|
749
815
|
"""
|
|
750
816
|
warningNotes: [String!]!
|
|
751
817
|
steps: [WorkflowStepRunRecord!]!
|
|
818
|
+
"""
|
|
819
|
+
Where the run paused, resolved ones included.
|
|
820
|
+
"""
|
|
821
|
+
waitpoints: [WaitpointRecord!]!
|
|
752
822
|
}
|
|
753
823
|
|
|
754
824
|
type WorkflowRunResultPage {
|
|
@@ -822,6 +892,21 @@ type WorkflowRuntimeMutations {
|
|
|
822
892
|
"""
|
|
823
893
|
rerun(runId: String!): WorkflowRunPayload!
|
|
824
894
|
"""
|
|
895
|
+
Approves or rejects a PENDING WEBHOOK waitpoint, resuming its run. Needs
|
|
896
|
+
write access to the workflow; with approvers listed, the caller's DID must
|
|
897
|
+
be one. reason may be required, per approval.reasonRequiredOn.
|
|
898
|
+
"""
|
|
899
|
+
resolveWaitpoint(
|
|
900
|
+
waitpointId: String!
|
|
901
|
+
approved: Boolean!
|
|
902
|
+
reason: String
|
|
903
|
+
): WaitpointRecord!
|
|
904
|
+
"""
|
|
905
|
+
Cancels a WAITING or PENDING run and its open waitpoints. A RUNNING run
|
|
906
|
+
cannot be cancelled yet.
|
|
907
|
+
"""
|
|
908
|
+
cancelRun(runId: String!): WorkflowRunPayload
|
|
909
|
+
"""
|
|
825
910
|
Mints a managed secret for a connection or workflow document and returns
|
|
826
911
|
its ref; only that document resolves it. The value is stored encrypted
|
|
827
912
|
and is never readable back over any API.
|
|
@@ -915,6 +1000,15 @@ type WorkflowRuntimeQueries {
|
|
|
915
1000
|
): WorkflowRunResultPage!
|
|
916
1001
|
run(id: String!): WorkflowRunRecord
|
|
917
1002
|
"""
|
|
1003
|
+
WAITING runs, each with its waitpoints. Scope them to one
|
|
1004
|
+
workflow, or to every workflow a drive holds; workflowId wins.
|
|
1005
|
+
"""
|
|
1006
|
+
waitingRuns(
|
|
1007
|
+
driveId: String
|
|
1008
|
+
workflowId: String
|
|
1009
|
+
first: Int
|
|
1010
|
+
): [WorkflowRunRecord!]!
|
|
1011
|
+
"""
|
|
918
1012
|
The descriptor (props, auth, ports) of one action or trigger, as the
|
|
919
1013
|
version that runs it describes it; triggers come back under "trigger".
|
|
920
1014
|
"""
|
|
@@ -1307,7 +1401,7 @@ type Workflow_WorkflowPolicy {
|
|
|
1307
1401
|
"""
|
|
1308
1402
|
runTimeoutSeconds: Int!
|
|
1309
1403
|
"""
|
|
1310
|
-
|
|
1404
|
+
Bounds how long a run may stay suspended on a waitpoint: a waitpoint expires this long after it is created, ending the run CANCELLED with WaitpointExpired. A Delay longer than this is refused.
|
|
1311
1405
|
"""
|
|
1312
1406
|
maxSuspensionDays: Int!
|
|
1313
1407
|
"""
|
|
@@ -24,11 +24,20 @@ describe("redactWebhookPath", () => {
|
|
|
24
24
|
);
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
+
it("redacts a waitpoint's resume token and its sub-path", () => {
|
|
28
|
+
expect(
|
|
29
|
+
redactWebhookPath(
|
|
30
|
+
`/api/@powerhousedao/workflow/waitpoints/${TOKEN}/confirm?action=approve`,
|
|
31
|
+
),
|
|
32
|
+
).toBe("/api/@powerhousedao/workflow/waitpoints/[redacted]");
|
|
33
|
+
});
|
|
34
|
+
|
|
27
35
|
it("never returns anything containing the token", () => {
|
|
28
36
|
for (const url of [
|
|
29
37
|
`/webhooks/${TOKEN}`,
|
|
30
38
|
`/reactor/webhooks/${TOKEN}`,
|
|
31
39
|
`/a/b/c/webhooks/${TOKEN}?x=1`,
|
|
40
|
+
`/a/waitpoints/${TOKEN}/sync`,
|
|
32
41
|
]) {
|
|
33
42
|
expect(redactWebhookPath(url)).not.toContain(TOKEN);
|
|
34
43
|
}
|
|
@@ -34,6 +34,41 @@ function runsInfo(query: string) {
|
|
|
34
34
|
const emptyPage = (): Promise<unknown> =>
|
|
35
35
|
Promise.resolve({ records: [], hasNextPage: false, cursor: null });
|
|
36
36
|
|
|
37
|
+
function waitpointRow(id: string, runId: string) {
|
|
38
|
+
return {
|
|
39
|
+
id,
|
|
40
|
+
run_id: runId,
|
|
41
|
+
workflow_id: "wf-1",
|
|
42
|
+
step_id: "step-1",
|
|
43
|
+
step_key: "step_1",
|
|
44
|
+
step_path: "step-1",
|
|
45
|
+
piece_name: "@powerhousedao/piece-approval",
|
|
46
|
+
block_name: "approval",
|
|
47
|
+
type: "WEBHOOK",
|
|
48
|
+
kind: "approval",
|
|
49
|
+
status: "PENDING",
|
|
50
|
+
created_at: "2026-10-28T00:00:00.000Z",
|
|
51
|
+
resume_at: null,
|
|
52
|
+
expires_at: null,
|
|
53
|
+
resolved_at: null,
|
|
54
|
+
decided_by: null,
|
|
55
|
+
approval: '{"title":"Pay?"}',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The run journal's waitpoint read: one pending approval per run.
|
|
60
|
+
function waitpointStore() {
|
|
61
|
+
return {
|
|
62
|
+
listWaitpointsForRuns: vi.fn((runIds: string[]) =>
|
|
63
|
+
Promise.resolve(
|
|
64
|
+
new Map(
|
|
65
|
+
runIds.map((runId) => [runId, [waitpointRow(`wp-${runId}`, runId)]]),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
37
72
|
function fakeRuntime() {
|
|
38
73
|
return {
|
|
39
74
|
webhookEndpoint: vi.fn(() => Promise.resolve(null)),
|
|
@@ -43,6 +78,9 @@ function fakeRuntime() {
|
|
|
43
78
|
run: vi.fn(() => Promise.resolve(null)),
|
|
44
79
|
fire: vi.fn(() => Promise.resolve({})),
|
|
45
80
|
rerun: vi.fn(() => Promise.resolve({})),
|
|
81
|
+
waitingRuns: vi.fn((): Promise<unknown[]> => Promise.resolve([])),
|
|
82
|
+
waitpointsForRun: vi.fn(() => Promise.resolve([{ id: "wp-2" }])),
|
|
83
|
+
store: vi.fn(() => Promise.resolve(waitpointStore())),
|
|
46
84
|
testStep: vi.fn(() => Promise.resolve({})),
|
|
47
85
|
stepOutputTree: vi.fn(() => Promise.resolve({})),
|
|
48
86
|
blockResolutions: vi.fn(() => Promise.resolve([])),
|
|
@@ -78,6 +116,7 @@ function build() {
|
|
|
78
116
|
queries: resolvers.WorkflowRuntimeQueries,
|
|
79
117
|
mutations: resolvers.WorkflowRuntimeMutations,
|
|
80
118
|
secretRecord: resolvers.SecretRecord,
|
|
119
|
+
runRecord: resolvers.WorkflowRunRecord,
|
|
81
120
|
};
|
|
82
121
|
}
|
|
83
122
|
|
|
@@ -367,3 +406,70 @@ describe("piece search", () => {
|
|
|
367
406
|
expect(runtime.searchPieces).not.toHaveBeenCalled();
|
|
368
407
|
});
|
|
369
408
|
});
|
|
409
|
+
|
|
410
|
+
describe("waitpoints", () => {
|
|
411
|
+
it("serves waiting runs with the waitpoints the runtime listed", async () => {
|
|
412
|
+
const { runtime, queries, runRecord } = build();
|
|
413
|
+
runtime.waitingRuns.mockResolvedValueOnce([
|
|
414
|
+
{
|
|
415
|
+
run: {
|
|
416
|
+
row: { id: "run-1", status: "WAITING", trigger_payload: null },
|
|
417
|
+
steps: [],
|
|
418
|
+
},
|
|
419
|
+
waitpoints: [{ id: "wp-1" }],
|
|
420
|
+
},
|
|
421
|
+
]);
|
|
422
|
+
|
|
423
|
+
const runs = (await queries.waitingRuns(
|
|
424
|
+
{},
|
|
425
|
+
{ driveId: "drive-1", first: 10 },
|
|
426
|
+
CTX,
|
|
427
|
+
)) as { id: string }[];
|
|
428
|
+
|
|
429
|
+
expect(runtime.waitingRuns).toHaveBeenCalledWith(
|
|
430
|
+
{ driveId: "drive-1", workflowId: undefined, first: 10 },
|
|
431
|
+
CTX,
|
|
432
|
+
);
|
|
433
|
+
expect(await runRecord.waitpoints(runs[0], {}, CTX)).toEqual([
|
|
434
|
+
{ id: "wp-1" },
|
|
435
|
+
]);
|
|
436
|
+
expect(runtime.waitpointsForRun).not.toHaveBeenCalled();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("reads the waitpoints of every run in a request with one store call", async () => {
|
|
440
|
+
const { runtime, runRecord } = build();
|
|
441
|
+
const store = waitpointStore();
|
|
442
|
+
runtime.store.mockResolvedValueOnce(store);
|
|
443
|
+
const ctx = { ...CTX } as Context;
|
|
444
|
+
const [first, second, again] = (await Promise.all([
|
|
445
|
+
runRecord.waitpoints({ id: "run-1" }, {}, ctx),
|
|
446
|
+
runRecord.waitpoints({ id: "run-2" }, {}, ctx),
|
|
447
|
+
runRecord.waitpoints({ id: "run-1" }, {}, ctx),
|
|
448
|
+
])) as Array<Array<{ id: string }>>;
|
|
449
|
+
|
|
450
|
+
expect(first).toEqual([
|
|
451
|
+
expect.objectContaining({
|
|
452
|
+
id: "wp-run-1",
|
|
453
|
+
runId: "run-1",
|
|
454
|
+
kind: "approval",
|
|
455
|
+
approval: { title: "Pay?" },
|
|
456
|
+
}),
|
|
457
|
+
]);
|
|
458
|
+
expect(second!.map((waitpoint) => waitpoint.id)).toEqual(["wp-run-2"]);
|
|
459
|
+
expect(again).toBe(first);
|
|
460
|
+
expect(runtime.store).toHaveBeenCalledTimes(1);
|
|
461
|
+
expect(store.listWaitpointsForRuns).toHaveBeenCalledWith([
|
|
462
|
+
"run-1",
|
|
463
|
+
"run-2",
|
|
464
|
+
]);
|
|
465
|
+
// The parent resolver served the run; no second run read.
|
|
466
|
+
expect(runtime.waitpointsForRun).not.toHaveBeenCalled();
|
|
467
|
+
expect(runtime.run).not.toHaveBeenCalled();
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("serves no waitpoints when the run journal is unavailable", async () => {
|
|
471
|
+
const { runtime, runRecord } = build();
|
|
472
|
+
runtime.store.mockResolvedValueOnce(undefined as never);
|
|
473
|
+
expect(await runRecord.waitpoints({ id: "run-9" }, {}, CTX)).toEqual([]);
|
|
474
|
+
});
|
|
475
|
+
});
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// The resume URL over a real socket: GET never resumes, the confirm page
|
|
2
|
+
// escapes what it shows, and POST hands the request to the runtime.
|
|
3
|
+
import {
|
|
4
|
+
createHttpAdapter,
|
|
5
|
+
HttpRouteService,
|
|
6
|
+
} from "@powerhousedao/reactor-api";
|
|
7
|
+
import {
|
|
8
|
+
afterAll,
|
|
9
|
+
beforeAll,
|
|
10
|
+
beforeEach,
|
|
11
|
+
describe,
|
|
12
|
+
expect,
|
|
13
|
+
it,
|
|
14
|
+
vi,
|
|
15
|
+
} from "vitest";
|
|
16
|
+
import {
|
|
17
|
+
MAX_REASON_LENGTH,
|
|
18
|
+
registerWaitpointRoutes,
|
|
19
|
+
waitpointBaseUrlOf,
|
|
20
|
+
type WaitpointRouteRuntime,
|
|
21
|
+
} from "../../src/workflow/waitpoint-routes.js";
|
|
22
|
+
|
|
23
|
+
const TOKEN = "tok_abc";
|
|
24
|
+
|
|
25
|
+
describe("the waitpoint resume routes", () => {
|
|
26
|
+
let base: string;
|
|
27
|
+
let close: () => Promise<void>;
|
|
28
|
+
const runtime = {
|
|
29
|
+
describeWaitpointByToken:
|
|
30
|
+
vi.fn<WaitpointRouteRuntime["describeWaitpointByToken"]>(),
|
|
31
|
+
resumeWaitpointByToken:
|
|
32
|
+
vi.fn<WaitpointRouteRuntime["resumeWaitpointByToken"]>(),
|
|
33
|
+
resumeWaitpointByTokenSync:
|
|
34
|
+
vi.fn<WaitpointRouteRuntime["resumeWaitpointByTokenSync"]>(),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
beforeAll(async () => {
|
|
38
|
+
const { adapter } = await createHttpAdapter("express");
|
|
39
|
+
adapter.setupMiddleware({});
|
|
40
|
+
const server = await adapter.listen(0, undefined, "127.0.0.1");
|
|
41
|
+
const { port } = server.address() as { port: number };
|
|
42
|
+
const url = `http://127.0.0.1:${port}`;
|
|
43
|
+
const routes = new HttpRouteService({
|
|
44
|
+
httpAdapter: adapter,
|
|
45
|
+
publicUrl: url,
|
|
46
|
+
});
|
|
47
|
+
const scope = routes.scopeFor("@powerhousedao/workflow");
|
|
48
|
+
registerWaitpointRoutes(scope, runtime);
|
|
49
|
+
base = `${waitpointBaseUrlOf(scope)!}/${TOKEN}`;
|
|
50
|
+
close = () => new Promise((done) => server.close(() => done()));
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
afterAll(() => close());
|
|
54
|
+
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
runtime.describeWaitpointByToken.mockReset().mockResolvedValue({
|
|
57
|
+
workflowName: "Invoice <b>approval</b>",
|
|
58
|
+
stepName: `Ask "finance"`,
|
|
59
|
+
status: "PENDING",
|
|
60
|
+
waitingSince: "2026-10-28T09:30:00.000Z",
|
|
61
|
+
});
|
|
62
|
+
runtime.resumeWaitpointByToken
|
|
63
|
+
.mockReset()
|
|
64
|
+
.mockResolvedValue({ status: "resumed" });
|
|
65
|
+
runtime.resumeWaitpointByTokenSync.mockReset().mockResolvedValue({
|
|
66
|
+
status: "resumed",
|
|
67
|
+
reply: {
|
|
68
|
+
status: 201,
|
|
69
|
+
contentType: "application/json",
|
|
70
|
+
body: '{"ok":true}',
|
|
71
|
+
headers: { "x-next": "later" },
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const form = (action: string, reason = "", accept = "text/html") =>
|
|
77
|
+
fetch(`${base}/confirm?action=${action}`, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: {
|
|
80
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
81
|
+
accept,
|
|
82
|
+
},
|
|
83
|
+
body: new URLSearchParams({ reason }).toString(),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("lives under the workflow package's namespace", () => {
|
|
87
|
+
expect(base).toMatch(
|
|
88
|
+
/\/api\/@powerhousedao\/workflow\/waitpoints\/tok_abc$/,
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("serves no resume URL from a host with no public origin", async () => {
|
|
93
|
+
const { adapter } = await createHttpAdapter("express");
|
|
94
|
+
const routes = new HttpRouteService({ httpAdapter: adapter });
|
|
95
|
+
const scope = routes.scopeFor("@powerhousedao/workflow");
|
|
96
|
+
expect(scope.baseUrl).toMatch(/^\/api\//);
|
|
97
|
+
expect(waitpointBaseUrlOf(scope)).toBeUndefined();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// The bare URL's page posts to /confirm, relative to the token.
|
|
101
|
+
it.each([
|
|
102
|
+
[
|
|
103
|
+
"GET",
|
|
104
|
+
"?action=approve",
|
|
105
|
+
200,
|
|
106
|
+
'formaction="tok_abc/confirm?action=approve"',
|
|
107
|
+
],
|
|
108
|
+
["GET", "/sync", 405, undefined],
|
|
109
|
+
["GET", "/confirm", 200, "<form"],
|
|
110
|
+
["HEAD", "", 200, ""],
|
|
111
|
+
["HEAD", "/sync", 405, ""],
|
|
112
|
+
["HEAD", "/confirm", 200, ""],
|
|
113
|
+
] as const)(
|
|
114
|
+
"never resumes on %s %s, answering %i",
|
|
115
|
+
async (method, path, status, body) => {
|
|
116
|
+
const response = await fetch(`${base}${path}`, { method });
|
|
117
|
+
expect(response.status).toBe(status);
|
|
118
|
+
if (status === 405) expect(response.headers.get("allow")).toBe("POST");
|
|
119
|
+
const text = await response.text();
|
|
120
|
+
if (body === "") expect(text).toBe("");
|
|
121
|
+
else if (body) expect(text).toContain(body);
|
|
122
|
+
expect(runtime.resumeWaitpointByToken).not.toHaveBeenCalled();
|
|
123
|
+
expect(runtime.resumeWaitpointByTokenSync).not.toHaveBeenCalled();
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
it("serves an escaped confirm page under a strict CSP", async () => {
|
|
128
|
+
const response = await fetch(`${base}/confirm?x=1`);
|
|
129
|
+
expect(response.status).toBe(200);
|
|
130
|
+
expect(response.headers.get("content-type")).toContain("text/html");
|
|
131
|
+
const csp = response.headers.get("content-security-policy") ?? "";
|
|
132
|
+
expect(csp).toContain("default-src 'none'");
|
|
133
|
+
expect(csp).toContain("form-action 'self'");
|
|
134
|
+
expect(response.headers.get("referrer-policy")).toBe("no-referrer");
|
|
135
|
+
const body = await response.text();
|
|
136
|
+
expect(runtime.describeWaitpointByToken).toHaveBeenCalledWith(TOKEN);
|
|
137
|
+
expect(body).toContain("Invoice <b>approval</b>");
|
|
138
|
+
expect(body).toContain("Ask "finance"");
|
|
139
|
+
expect(body).not.toContain("<b>");
|
|
140
|
+
expect(body).not.toContain("<script");
|
|
141
|
+
expect(body).toContain('formaction="?x=1&action=approve"');
|
|
142
|
+
expect(body).toContain('formaction="?x=1&action=disapprove"');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("describes the workflow and the wait, times in UTC", async () => {
|
|
146
|
+
runtime.describeWaitpointByToken.mockResolvedValueOnce({
|
|
147
|
+
workflowName: "Vendor sign-off",
|
|
148
|
+
workflowDescription: "Ask <the vendor> to sign off\non the new terms.",
|
|
149
|
+
stepName: "Ask vendor",
|
|
150
|
+
actionName: "Create Approval Links",
|
|
151
|
+
status: "PENDING",
|
|
152
|
+
startedAt: "2026-10-28T09:29:00.000Z",
|
|
153
|
+
triggerName: "Manual Trigger",
|
|
154
|
+
waitingSince: "2026-10-28T09:30:00.000Z",
|
|
155
|
+
expiresAt: "2026-11-04T09:30:00.000Z",
|
|
156
|
+
});
|
|
157
|
+
const body = await (await fetch(`${base}/confirm`)).text();
|
|
158
|
+
expect(body).toContain(
|
|
159
|
+
"Ask <the vendor> to sign off\non the new terms.",
|
|
160
|
+
);
|
|
161
|
+
expect(body).toContain("<dd>Ask vendor · Create Approval Links</dd>");
|
|
162
|
+
expect(body).toContain(
|
|
163
|
+
"<dd>28 Oct 2026, 09:29 UTC, by Manual Trigger</dd>",
|
|
164
|
+
);
|
|
165
|
+
expect(body).toContain(
|
|
166
|
+
"<dt>Waiting since</dt><dd>28 Oct 2026, 09:30 UTC</dd>",
|
|
167
|
+
);
|
|
168
|
+
expect(body).toContain("<dt>Expires</dt><dd>4 Nov 2026, 09:30 UTC</dd>");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("names the action only when the step's own name differs", async () => {
|
|
172
|
+
runtime.describeWaitpointByToken.mockResolvedValueOnce({
|
|
173
|
+
workflowName: "wf",
|
|
174
|
+
stepName: "Create approval links",
|
|
175
|
+
actionName: "Create Approval Links",
|
|
176
|
+
status: "PENDING",
|
|
177
|
+
waitingSince: "2026-10-28T09:30:00.000Z",
|
|
178
|
+
});
|
|
179
|
+
const body = await (await fetch(`${base}/confirm`)).text();
|
|
180
|
+
expect(body).toContain("<dd>Create approval links</dd>");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it.each([
|
|
184
|
+
["COMPLETED", "Already responded"],
|
|
185
|
+
["CONSUMED", "Already responded"],
|
|
186
|
+
["EXPIRED", "Request expired"],
|
|
187
|
+
["CANCELLED", "Run cancelled"],
|
|
188
|
+
] as const)("shows a %s waitpoint without a form", async (status, title) => {
|
|
189
|
+
runtime.describeWaitpointByToken.mockResolvedValueOnce({
|
|
190
|
+
workflowName: "wf",
|
|
191
|
+
stepName: "step",
|
|
192
|
+
status,
|
|
193
|
+
waitingSince: "2026-10-28T09:30:00.000Z",
|
|
194
|
+
});
|
|
195
|
+
const body = await (await fetch(`${base}/confirm`)).text();
|
|
196
|
+
expect(body).toContain(title);
|
|
197
|
+
expect(body).not.toContain("<form");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("answers 404 for a token the runtime does not know", async () => {
|
|
201
|
+
runtime.describeWaitpointByToken.mockResolvedValueOnce(undefined);
|
|
202
|
+
expect((await fetch(`${base}/confirm`)).status).toBe(404);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("resumes on POST with body, headers and query, answering JSON", async () => {
|
|
206
|
+
const response = await fetch(`${base}?a=1`, {
|
|
207
|
+
method: "POST",
|
|
208
|
+
headers: {
|
|
209
|
+
"content-type": "application/json",
|
|
210
|
+
authorization: "Bearer secret",
|
|
211
|
+
},
|
|
212
|
+
body: JSON.stringify({ ok: true }),
|
|
213
|
+
});
|
|
214
|
+
expect(response.status).toBe(200);
|
|
215
|
+
expect(await response.json()).toMatchObject({ status: "resumed" });
|
|
216
|
+
const [token, payload] = runtime.resumeWaitpointByToken.mock.calls[0]!;
|
|
217
|
+
expect(token).toBe(TOKEN);
|
|
218
|
+
expect(payload.body).toEqual({ ok: true });
|
|
219
|
+
expect(payload.queryParams).toEqual({ a: "1" });
|
|
220
|
+
expect(payload.headers?.["content-type"]).toBe("application/json");
|
|
221
|
+
// Credentials are not journaled with the payload.
|
|
222
|
+
expect(payload.headers?.authorization).toBeUndefined();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("answers the sync sub-path with the resumed run's reply", async () => {
|
|
226
|
+
const response = await fetch(`${base}/sync`, { method: "POST" });
|
|
227
|
+
expect(runtime.resumeWaitpointByTokenSync).toHaveBeenCalledOnce();
|
|
228
|
+
expect(runtime.resumeWaitpointByToken).not.toHaveBeenCalled();
|
|
229
|
+
expect(response.status).toBe(201);
|
|
230
|
+
expect(response.headers.get("x-next")).toBe("later");
|
|
231
|
+
expect(await response.json()).toEqual({ ok: true });
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("answers a gone waitpoint on the sync sub-path with 410", async () => {
|
|
235
|
+
runtime.resumeWaitpointByTokenSync.mockResolvedValue({ status: "gone" });
|
|
236
|
+
const response = await fetch(`${base}/sync`, { method: "POST" });
|
|
237
|
+
expect(response.status).toBe(410);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("answers the confirm form with an HTML page", async () => {
|
|
241
|
+
const response = await form("disapprove", "too expensive");
|
|
242
|
+
expect(response.headers.get("content-type")).toContain("text/html");
|
|
243
|
+
expect(await response.text()).toContain("You disapproved this request.");
|
|
244
|
+
const [, payload] = runtime.resumeWaitpointByToken.mock.calls[0]!;
|
|
245
|
+
expect(payload.body).toEqual({ reason: "too expensive" });
|
|
246
|
+
expect(payload.queryParams).toEqual({
|
|
247
|
+
action: "disapprove",
|
|
248
|
+
reason: "too expensive",
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("refuses a confirm POST without an action, recording nothing", async () => {
|
|
253
|
+
expect((await form("maybe")).status).toBe(400);
|
|
254
|
+
expect(runtime.resumeWaitpointByToken).not.toHaveBeenCalled();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("refuses an over-long reason, recording nothing", async () => {
|
|
258
|
+
const response = await form("approve", "x".repeat(MAX_REASON_LENGTH + 1));
|
|
259
|
+
expect(response.status).toBe(400);
|
|
260
|
+
expect(runtime.resumeWaitpointByToken).not.toHaveBeenCalled();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("refuses a body past 1 MiB", async () => {
|
|
264
|
+
const response = await fetch(base, {
|
|
265
|
+
method: "POST",
|
|
266
|
+
headers: { "content-type": "text/plain" },
|
|
267
|
+
body: "x".repeat(1_048_577),
|
|
268
|
+
});
|
|
269
|
+
expect(response.status).toBe(413);
|
|
270
|
+
expect(runtime.resumeWaitpointByToken).not.toHaveBeenCalled();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("answers a repeat as already responded and a gone waitpoint as 410", async () => {
|
|
274
|
+
runtime.resumeWaitpointByToken.mockResolvedValueOnce({ status: "already" });
|
|
275
|
+
const already = await form("approve");
|
|
276
|
+
expect(already.status).toBe(200);
|
|
277
|
+
expect(await already.text()).toContain("Already responded");
|
|
278
|
+
|
|
279
|
+
runtime.resumeWaitpointByToken.mockResolvedValueOnce({ status: "gone" });
|
|
280
|
+
const gone = await fetch(base, { method: "POST" });
|
|
281
|
+
expect(gone.status).toBe(410);
|
|
282
|
+
expect(await gone.json()).toMatchObject({ status: "gone" });
|
|
283
|
+
});
|
|
284
|
+
});
|