@basementuniverse/jsonpad-cli 1.4.0 → 1.6.0

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/CHANGELOG.md CHANGED
@@ -7,6 +7,68 @@ and this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  Dates are npm publish dates.
9
9
 
10
+ ## [Unreleased]
11
+
12
+ Needs `@basementuniverse/jsonpad-sdk` with `runFlow()` (the release after
13
+ 2.2.0), and the JSONPad API release with flows. `package.json` points at the
14
+ SDK's local checkout (`file:../jsonpad-sdk-js`) until that's published.
15
+
16
+ ### Added
17
+
18
+ - **`jsonpad flows`**, which checks and tests flows with the copy of the flows
19
+ engine built into the CLI (`@basementuniverse/jsonpad-flows` 1.0.0), so it
20
+ needs no network and costs no requests:
21
+ - `flows check` compiles a flow file and shows the checker's warnings, with
22
+ `--strict` to fail on them.
23
+ - `flows test` runs a flow's tests (`<name>.tests.json` by default) against
24
+ in-memory items, with `--filter`, `--trace` and `--json`.
25
+ - `flows run` calls an endpoint flow, with `--input`, `--method`, `--public`
26
+ and `--json`.
27
+ - **Flows in schema documents.** `flowFile` and `flowTestsFile` are read
28
+ relative to the document and sent as `document` and `tests`. `sync-schema`
29
+ compiles and tests flows before sending anything (`--skip-flow-tests` turns
30
+ that off), and shows flow changes.
31
+ - `export-schema --split-flows <directory>` writes each flow and its tests to
32
+ their own files and references them from the document.
33
+ - Exit code `10`, for a flow that fails. Exit code `9` now also covers flow
34
+ tests that fail.
35
+
36
+ ### Changed
37
+
38
+ - `@basementuniverse/jsonpad-rules` is now `^1.2.0`, the version the flows
39
+ engine needs, so there's one copy of it.
40
+
41
+ ## [1.5.0] - 2026-09-23
42
+
43
+ Needs `@basementuniverse/jsonpad-sdk` 2.2.0, and the JSONPad API release with
44
+ write rules.
45
+
46
+ ### Added
47
+
48
+ - **`jsonpad rules`**, which uses the copy of the rules engine built into the
49
+ CLI, so it needs no network and costs no requests:
50
+ - `rules check` compiles a rule file and shows the checker's warnings, with
51
+ `--strict` to fail on them.
52
+ - `rules test` runs a rule set against its tests (`<name>.tests.json` by
53
+ default), with `--filter`, `--trace` and `--json`.
54
+ - `rules eval` checks one write and shows what each rule did.
55
+ - `rules test --list` and `rules eval --list` run the same checks through the
56
+ API, and warn when its rules engine isn't the one in this CLI.
57
+ - **`jsonpad lists rules`**: `get` (with `--tests` and `--out`), `set` (from a
58
+ file, with `--tests`, `--remove` and `--skip-check`) and `denials`, which
59
+ lists the writes a list's rules have refused.
60
+ - **Rule files in schema documents.** `rulesFile` and `rulesTestsFile` in a
61
+ list are read relative to the document and sent as `rules` and `rulesTests`,
62
+ so rule text lives in its own file in git. `sync-schema` compiles and tests
63
+ them before sending anything (`--skip-rule-tests` turns that off), and shows
64
+ a rule change as a line diff.
65
+ - `export-schema --split-rules <directory>` writes each list's rules and tests
66
+ to their own files and references them from the document.
67
+ - Exit code `9`, for write rule tests that fail and for a `rules eval` the
68
+ rules refuse.
69
+ - `sync-schema` shows the diagnostics and failing tests the API reports for a
70
+ change, not just its message.
71
+
10
72
  ## [1.4.0] - 2026-09-18
11
73
 
12
74
  Needs `@basementuniverse/jsonpad-sdk` 2.1.0, and the JSONPad API release with
package/README.md CHANGED
@@ -279,6 +279,60 @@ The schema commands are also available as `jsonpad schema sync`,
279
279
  Run `jsonpad --help`, or `jsonpad <command> --help`, for every option. The full
280
280
  command reference is in [REFERENCE.md](REFERENCE.md).
281
281
 
282
+ ### Write rules
283
+
284
+ A list's [write rules](https://jsonpad.io/docs/write-rules) check every item
285
+ write made with an API token. The CLI has its own copy of the rules engine, so
286
+ checking and testing rules needs no network and doesn't count against your
287
+ request allowance.
288
+
289
+ ```bash
290
+ # Check that a rule file compiles, and show the checker's warnings
291
+ jsonpad rules check rules/games.rules
292
+
293
+ # Run its tests (rules/games.tests.json unless another file is given)
294
+ jsonpad rules test rules/games.rules
295
+ jsonpad rules test rules/games.rules --filter timeout --trace
296
+
297
+ # Try one write, and see what each rule did
298
+ jsonpad rules eval rules/games.rules --action update \
299
+ --old @before.json --merge '{"status":"sent"}' \
300
+ --token '{"id":"t","tags":["writer"]}'
301
+
302
+ # The same checks through the API, which always has the final say
303
+ jsonpad rules test --list games
304
+ jsonpad rules eval --list games --action delete --item 098e58bc --identity-id 3e3ce22b
305
+
306
+ # Read and write a list's rules
307
+ jsonpad lists rules get games --out rules/games.rules
308
+ jsonpad lists rules set games rules/games.rules --tests rules/games.tests.json
309
+ jsonpad lists rules set games --remove
310
+
311
+ # See the writes the rules have refused, and why
312
+ jsonpad lists rules denials games
313
+ ```
314
+
315
+ A rule set's rules and tests live in git alongside the schema document, which
316
+ points at them:
317
+
318
+ ```json
319
+ {
320
+ "lists": {
321
+ "games": {
322
+ "name": "Games",
323
+ "indexable": true,
324
+ "rulesFile": "rules/games.rules",
325
+ "rulesTestsFile": "rules/games.tests.json"
326
+ }
327
+ }
328
+ }
329
+ ```
330
+
331
+ `jsonpad sync-schema` reads those files, compiles the rules and runs their
332
+ tests before it sends anything, so a mistake costs no requests
333
+ (`--skip-rule-tests` turns that off). `jsonpad export-schema --split-rules
334
+ rules --out jsonpad-schema.json` writes them back out the same way.
335
+
282
336
  ### Output
283
337
 
284
338
  Commands that output records print a table in a terminal, and JSON when their
@@ -337,12 +391,64 @@ press Tab.
337
391
  ## Continuous integration
338
392
 
339
393
  ```yaml
394
+ # Write rules are checked and tested without touching the API, so this needs
395
+ # no token and costs no requests
396
+ - name: Test the write rules
397
+ run: npx @basementuniverse/jsonpad-cli rules test rules/games.rules
398
+
340
399
  - name: Sync the JSONPad schema
341
400
  run: npx @basementuniverse/jsonpad-cli sync-schema --wait
342
401
  env:
343
402
  JSONPAD_TOKEN: ${{ secrets.JSONPAD_TOKEN }}
344
403
  ```
345
404
 
405
+ ### Flows
406
+
407
+ [Flows](https://jsonpad.io/docs/flows) are small programs drawn in the
408
+ dashboard: endpoints your app calls, and steps that run when items change. The
409
+ CLI has its own copy of the flows engine, so checking and testing a flow needs
410
+ no network and doesn't count against your request allowance. Tests run against
411
+ an in-memory copy of the items they declare.
412
+
413
+ ```bash
414
+ # Check that a flow file compiles, and show the checker's warnings
415
+ jsonpad flows check flows/deal-card.flow.json
416
+
417
+ # Run its tests (flows/deal-card.tests.json unless another file is given)
418
+ jsonpad flows test flows/deal-card.flow.json --trace
419
+
420
+ # Call an endpoint flow (the token needs the run permission on it)
421
+ jsonpad flows run deal-card --input '{"gameId":"g1"}'
422
+ jsonpad flows run leaderboard --method GET --input '{"top":10}'
423
+
424
+ # Call a public flow by its id, without a token
425
+ jsonpad flows run 5b9d2c1e-7f0a-4c55-9d1e-3a6b8f2e4c10 --public
426
+ ```
427
+
428
+ A flow that fails exits with `10`, and says why and at which node. Flows are
429
+ created and updated with schema sync: declare them in the schema document, next
430
+ to the lists they use.
431
+
432
+ ```json
433
+ {
434
+ "scope": "cards",
435
+ "lists": { "games": {}, "decks": {}, "hands": {} },
436
+ "flows": {
437
+ "deal-card": {
438
+ "flowFile": "flows/deal-card.flow.json",
439
+ "flowTestsFile": "flows/deal-card.tests.json"
440
+ }
441
+ }
442
+ }
443
+ ```
444
+
445
+ `sync-schema` compiles and tests every flow before sending anything
446
+ (`--skip-flow-tests` turns that off). Where the nodes sit in the dashboard is
447
+ kept when a flow is updated, so moving them around there doesn't make the file
448
+ out of date. `export-schema --split-flows flows` writes each flow and its tests
449
+ to their own files. Syncing flows needs a token that is allowed everything,
450
+ because a flow runs with your full privileges.
451
+
346
452
  ## Exit codes
347
453
 
348
454
  | Code | Meaning |
@@ -356,6 +462,8 @@ press Tab.
356
462
  | `6` | Not found |
357
463
  | `7` | The token isn't allowed to do this, or isn't valid |
358
464
  | `8` | Rate limited (after retrying), or a plan limit or the quota was reached |
465
+ | `9` | Write rule or flow tests failed, or a rules check refused a write |
466
+ | `10` | A flow failed (`flows run`) |
359
467
 
360
468
  A dry run exits the same way the real sync would.
361
469
 
package/REFERENCE.md CHANGED
@@ -6,11 +6,11 @@ Run `jsonpad <command> --help` for the same information in a terminal.
6
6
 
7
7
  ## Commands
8
8
 
9
- - [`jsonpad sync-schema`](#jsonpad-sync-schema): Create and update lists and indexes to match a schema document
9
+ - [`jsonpad sync-schema`](#jsonpad-sync-schema): Create and update lists, indexes and flows to match a schema document
10
10
  - [`jsonpad export-schema`](#jsonpad-export-schema): Write a schema document for existing lists
11
11
  - [`jsonpad move-lists`](#jsonpad-move-lists): Move lists (by id or path name) to a scope, or release them from their scope
12
12
  - [`jsonpad rebuild-index`](#jsonpad-rebuild-index): Rebuild an index whose last build failed, once the problem has been fixed
13
- - [`jsonpad schema sync`](#jsonpad-schema-sync): Create and update lists and indexes to match a schema document
13
+ - [`jsonpad schema sync`](#jsonpad-schema-sync): Create and update lists, indexes and flows to match a schema document
14
14
  - [`jsonpad schema export`](#jsonpad-schema-export): Write a schema document for existing lists
15
15
  - [`jsonpad schema move`](#jsonpad-schema-move): Move lists (by id or path name) to a scope, or release them from their scope
16
16
  - [`jsonpad lists list`](#jsonpad-lists-list): List lists (the default when no command is given)
@@ -19,6 +19,9 @@ Run `jsonpad <command> --help` for the same information in a terminal.
19
19
  - [`jsonpad lists update`](#jsonpad-lists-update): Change a list. Fields that aren't given are left as they are
20
20
  - [`jsonpad lists delete`](#jsonpad-lists-delete): Delete a list. Its items and indexes are deleted in the background
21
21
  - [`jsonpad lists search`](#jsonpad-lists-search): Search a list's items, using the indexes that allow searching
22
+ - [`jsonpad lists rules get`](#jsonpad-lists-rules-get): Show a list's write rules
23
+ - [`jsonpad lists rules set`](#jsonpad-lists-rules-set): Set a list's write rules from a file
24
+ - [`jsonpad lists rules denials`](#jsonpad-lists-rules-denials): The most recent writes the list's rules refused, newest first
22
25
  - [`jsonpad lists stats`](#jsonpad-lists-stats): Show a list's stats: its items, indexes and events, by day
23
26
  - [`jsonpad lists events`](#jsonpad-lists-events): List a list's events, newest first by default
24
27
  - [`jsonpad lists event`](#jsonpad-lists-event): Show one of a list's events
@@ -69,6 +72,12 @@ Run `jsonpad <command> --help` for the same information in a terminal.
69
72
  - [`jsonpad identities stats`](#jsonpad-identities-stats): Show an identity's events, by day
70
73
  - [`jsonpad identities events`](#jsonpad-identities-events): List an identity's events, newest first by default
71
74
  - [`jsonpad identities event`](#jsonpad-identities-event): Show one of an identity's events
75
+ - [`jsonpad rules check`](#jsonpad-rules-check): Check that a rule file compiles, and show the checker's warnings
76
+ - [`jsonpad rules test`](#jsonpad-rules-test): Run a rule set against its tests
77
+ - [`jsonpad rules eval`](#jsonpad-rules-eval): Check one write against a rule set, and show what each rule did
78
+ - [`jsonpad flows check`](#jsonpad-flows-check): Check that a flow file compiles, and show the checker's warnings
79
+ - [`jsonpad flows test`](#jsonpad-flows-test): Run a flow's tests, against an in-memory copy of your data
80
+ - [`jsonpad flows run`](#jsonpad-flows-run): Call an endpoint flow, e.g. jsonpad flows run create-order --input '{"productId":"p1"}'
72
81
  - [`jsonpad whoami`](#jsonpad-whoami): Show the token you're using, its plan's limits and usage
73
82
  - [`jsonpad config set-profile`](#jsonpad-config-set-profile): Add or update a profile
74
83
  - [`jsonpad config use`](#jsonpad-config-use): Choose the default profile
@@ -80,7 +89,7 @@ Run `jsonpad <command> --help` for the same information in a terminal.
80
89
 
81
90
  ### `jsonpad sync-schema`
82
91
 
83
- Create and update lists and indexes to match a schema document
92
+ Create and update lists, indexes and flows to match a schema document
84
93
 
85
94
  ```
86
95
  jsonpad sync-schema [options] [file]
@@ -99,6 +108,8 @@ jsonpad sync-schema [options] [file]
99
108
  | `--wait` | Wait for index builds to finish |
100
109
  | `--timeout <seconds>` | How long --wait waits for each index (default 600) |
101
110
  | `--show-unchanged` | Also list resources that don't change |
111
+ | `--skip-rule-tests` | Don't compile and test write rules here first (the API always checks them) |
112
+ | `--skip-flow-tests` | Don't compile and test flows here first (the API always checks them) |
102
113
  | `--json` | Print the API's response as JSON |
103
114
 
104
115
  ### `jsonpad export-schema`
@@ -115,6 +126,8 @@ jsonpad export-schema [options]
115
126
  | `--tagged <tags>` | Only lists with one of these comma-separated tags (repeat the option to require every group) |
116
127
  | `--lists <path names>` | Only these comma-separated lists |
117
128
  | `--out <file>` | Write the document to a file, not stdout |
129
+ | `--split-rules <directory>` | Write each list's write rules and rule tests to their own files in this directory, and reference them from the document (needs --out) |
130
+ | `--split-flows <directory>` | Write each flow and its tests to their own files in this directory (name.flow.json and name.tests.json), and reference them from the document (needs --out) |
118
131
 
119
132
  ### `jsonpad move-lists`
120
133
 
@@ -329,6 +342,60 @@ jsonpad lists search [options] <list> <query>
329
342
  | `--json` | Output JSON (the same as --output json) |
330
343
  | `-q, --quiet` | Only output ids (the same as --output id) |
331
344
 
345
+ ### `jsonpad lists rules get`
346
+
347
+ Show a list's write rules
348
+
349
+ ```
350
+ jsonpad lists rules get [options] <list>
351
+ ```
352
+
353
+ | Argument | Description |
354
+ | --- | --- |
355
+ | `list` | The list (id or path name) |
356
+
357
+ | Option | Description |
358
+ | --- | --- |
359
+ | `--tests` | Show the rule tests instead of the rules |
360
+ | `-o, --out <file>` | Write to a file, not stdout |
361
+
362
+ ### `jsonpad lists rules set`
363
+
364
+ Set a list's write rules from a file
365
+
366
+ ```
367
+ jsonpad lists rules set [options] <list> [file]
368
+ ```
369
+
370
+ | Argument | Description |
371
+ | --- | --- |
372
+ | `list` | The list (id or path name) |
373
+ | `file` (optional) | The rule file, or - for stdin |
374
+
375
+ | Option | Description |
376
+ | --- | --- |
377
+ | `--tests <file>` | Also set the rule tests from this file |
378
+ | `--remove` | Remove the list's rules |
379
+ | `--skip-check` | Don't compile and test the rules here first (the API always checks them) |
380
+
381
+ ### `jsonpad lists rules denials`
382
+
383
+ The most recent writes the list's rules refused, newest first
384
+
385
+ ```
386
+ jsonpad lists rules denials [options] <list>
387
+ ```
388
+
389
+ | Argument | Description |
390
+ | --- | --- |
391
+ | `list` | The list (id or path name) |
392
+
393
+ | Option | Description |
394
+ | --- | --- |
395
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
396
+ | `--json` | Output JSON (the same as --output json) |
397
+ | `-q, --quiet` | Only output ids (the same as --output id) |
398
+
332
399
  ### `jsonpad lists stats`
333
400
 
334
401
  Show a list's stats: its items, indexes and events, by day
@@ -1387,6 +1454,126 @@ jsonpad identities event [options] <identity> <event>
1387
1454
  | `--json` | Output JSON (the same as --output json) |
1388
1455
  | `-q, --quiet` | Only output ids (the same as --output id) |
1389
1456
 
1457
+ ### `jsonpad rules check`
1458
+
1459
+ Check that a rule file compiles, and show the checker's warnings
1460
+
1461
+ ```
1462
+ jsonpad rules check [options] <file>
1463
+ ```
1464
+
1465
+ | Argument | Description |
1466
+ | --- | --- |
1467
+ | `file` | The rule file |
1468
+
1469
+ | Option | Description |
1470
+ | --- | --- |
1471
+ | `--strict` | Fail if there are any warnings |
1472
+
1473
+ ### `jsonpad rules test`
1474
+
1475
+ Run a rule set against its tests
1476
+
1477
+ ```
1478
+ jsonpad rules test [options] [rules] [tests]
1479
+ ```
1480
+
1481
+ | Argument | Description |
1482
+ | --- | --- |
1483
+ | `rules` (optional) | The rule file (not needed with --list) |
1484
+ | `tests` (optional) | The test file (default: the rule file with .tests.json, or the list's stored tests with --list) |
1485
+
1486
+ | Option | Description |
1487
+ | --- | --- |
1488
+ | `--rules <file>` | The rule file to test against --list |
1489
+ | `--list <list>` | Test against the API, using the list's stored rules and tests unless they're given |
1490
+ | `--filter <text>` | Only tests whose name contains this text |
1491
+ | `--trace` | Show how each rule was evaluated for a failing test |
1492
+ | `--json` | Print the results as JSON |
1493
+
1494
+ ### `jsonpad rules eval`
1495
+
1496
+ Check one write against a rule set, and show what each rule did
1497
+
1498
+ ```
1499
+ jsonpad rules eval [options] [rules]
1500
+ ```
1501
+
1502
+ | Argument | Description |
1503
+ | --- | --- |
1504
+ | `rules` (optional) | The rule file (not needed with --list) |
1505
+
1506
+ | Option | Description |
1507
+ | --- | --- |
1508
+ | `--action <action>` | What the write does: create, update, delete, restore |
1509
+ | `--list <list>` | Use the list's stored rules, and check with the API |
1510
+ | `--item <item>` | Take the old data from this item (needs --list) |
1511
+ | `--old <json>` | The data before the write (@file, - for stdin) |
1512
+ | `--new <json>` | The data after the write (@file, - for stdin) |
1513
+ | `--patch <json>` | A JSON Patch applied to the old data |
1514
+ | `--merge <json>` | A JSON merge patch applied to the old data |
1515
+ | `--identity <json>` | The identity making the write |
1516
+ | `--identity-id <id>` | An existing identity making the write (needs --list) |
1517
+ | `--token <json>` | The token making the write, e.g. '{"tags":["writer"]}' |
1518
+ | `--now <date>` | The time of the write (ISO 8601) |
1519
+ | `--pointer <pointer>` | The JSON pointer, for writes to part of an item |
1520
+ | `--trace` | Show every expression that was evaluated |
1521
+ | `--json` | Print the result as JSON |
1522
+
1523
+ ### `jsonpad flows check`
1524
+
1525
+ Check that a flow file compiles, and show the checker's warnings
1526
+
1527
+ ```
1528
+ jsonpad flows check [options] <file>
1529
+ ```
1530
+
1531
+ | Argument | Description |
1532
+ | --- | --- |
1533
+ | `file` | The flow file (a JSON flow document) |
1534
+
1535
+ | Option | Description |
1536
+ | --- | --- |
1537
+ | `--strict` | Fail if there are any warnings |
1538
+
1539
+ ### `jsonpad flows test`
1540
+
1541
+ Run a flow's tests, against an in-memory copy of your data
1542
+
1543
+ ```
1544
+ jsonpad flows test [options] <file> [tests]
1545
+ ```
1546
+
1547
+ | Argument | Description |
1548
+ | --- | --- |
1549
+ | `file` | The flow file |
1550
+ | `tests` (optional) | The test file (default: the flow file with .tests.json, e.g. deal-card.flow.json and deal-card.tests.json) |
1551
+
1552
+ | Option | Description |
1553
+ | --- | --- |
1554
+ | `--filter <text>` | Only tests whose name contains this text |
1555
+ | `--trace` | Show every node of a failing test |
1556
+ | `--json` | Print the results as JSON |
1557
+
1558
+ ### `jsonpad flows run`
1559
+
1560
+ Call an endpoint flow, e.g. jsonpad flows run create-order --input '{"productId":"p1"}'
1561
+
1562
+ ```
1563
+ jsonpad flows run [options] <path>
1564
+ ```
1565
+
1566
+ | Argument | Description |
1567
+ | --- | --- |
1568
+ | `path` | The flow's endpoint path, or its id with --public |
1569
+
1570
+ | Option | Description |
1571
+ | --- | --- |
1572
+ | `--input <json>` | The input (the request body, or the query parameters for GET), as JSON (@file, - for stdin) |
1573
+ | `--method <method>` | GET, POST, PUT, PATCH, DELETE (default POST) |
1574
+ | `--public` | Call a public flow by its id, without a token |
1575
+ | `--json` | Print the status, body and run id as JSON |
1576
+
1390
1577
  ### `jsonpad whoami`
1391
1578
 
1392
1579
  Show the token you're using: what it can do, its plan's limits, and the account's usage this month
@@ -1546,6 +1733,8 @@ JSONPAD_TOKEN, then the default profile.
1546
1733
  7 The token isn't allowed to do this, or isn't valid
1547
1734
  8 Rate limited (after retrying), or a plan limit or the monthly quota was
1548
1735
  reached
1736
+ 9 Write rule or flow tests failed, or a rules check refused a write
1737
+ 10 A flow failed (flows run)
1549
1738
 
1550
1739
  The schema commands and rebuild-index (and indexes rebuild) exit with 1 for
1551
1740
  every API error, as they did in @basementuniverse/jsonpad-sdk.
@@ -0,0 +1,100 @@
1
+ import { CliError, describeApiError, EXIT_FLOW_FAILED } from '../errors.js';
2
+ import { compileFlow, defaultFlowTestsFile, engineVersion, runFlowTests, } from '../flows.js';
3
+ import { readJsonInput } from '../input.js';
4
+ import { readJsonFile } from '../rules.js';
5
+ import { FlowError } from '../sdk.js';
6
+ const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
7
+ export function defineFlows(command, context) {
8
+ command
9
+ .description('Check, test and run flows')
10
+ .addHelpText('after', `\nFlows are checked and tested by the copy of the flows engine built into this\nCLI (version ${engineVersion}), so check and test need no network and don't count\nagainst your request allowance. The API always has the final say.\n\nTo create and update flows, declare them in your schema document (see\nsync-schema) with flowFile and flowTestsFile.\n`);
11
+ command
12
+ .command('check')
13
+ .description("Check that a flow file compiles, and show the checker's warnings")
14
+ .argument('<file>', 'The flow file (a JSON flow document)')
15
+ .option('--strict', 'Fail if there are any warnings')
16
+ .action((file, options) => checkFlow(context, file, options));
17
+ command
18
+ .command('test')
19
+ .description("Run a flow's tests, against an in-memory copy of your data")
20
+ .argument('<file>', 'The flow file')
21
+ .argument('[tests]', 'The test file (default: the flow file with .tests.json, e.g. deal-card.flow.json and deal-card.tests.json)')
22
+ .option('--filter <text>', 'Only tests whose name contains this text')
23
+ .option('--trace', 'Show every node of a failing test')
24
+ .option('--json', 'Print the results as JSON')
25
+ .action((file, tests, options) => testFlow(context, file, tests, options));
26
+ command
27
+ .command('run')
28
+ .description('Call an endpoint flow, e.g. jsonpad flows run create-order --input \'{"productId":"p1"}\'')
29
+ .argument('<path>', "The flow's endpoint path, or its id with --public")
30
+ .option('--input <json>', 'The input (the request body, or the query parameters for GET), as JSON (@file, - for stdin)')
31
+ .option('--method <method>', `${METHODS.join(', ')} (default POST)`)
32
+ .option('--public', 'Call a public flow by its id, without a token')
33
+ .option('--json', 'Print the status, body and run id as JSON')
34
+ .action((flowPath, options) => runFlow(context, flowPath, options));
35
+ return command;
36
+ }
37
+ async function checkFlow(context, file, options) {
38
+ compileFlow(context, readJsonFile(context, file, 'the flow file'), file, {
39
+ strict: options.strict,
40
+ });
41
+ context.log(`${context.colours.green('✓')} ${file} compiles`);
42
+ }
43
+ async function testFlow(context, file, testsFile, options) {
44
+ const flow = compileFlow(context, readJsonFile(context, file, 'the flow file'), file, { quiet: options.json });
45
+ const tests = testsFile ?? defaultFlowTestsFile(file);
46
+ await runFlowTests(context, flow, readJsonFile(context, tests, 'the test file'), tests, options);
47
+ }
48
+ async function runFlow(context, flowPath, options) {
49
+ const method = (options.method ?? 'POST').toUpperCase();
50
+ if (!METHODS.includes(method)) {
51
+ throw new CliError(`--method must be one of ${METHODS.join(', ')}`);
52
+ }
53
+ const input = options.input
54
+ ? await readJsonInput(context, '--input', options.input)
55
+ : undefined;
56
+ if (input !== undefined &&
57
+ (typeof input !== 'object' || input === null || Array.isArray(input))) {
58
+ throw new CliError('--input must be a JSON object');
59
+ }
60
+ const jsonpad = context.createClient();
61
+ let response;
62
+ try {
63
+ response = options.public
64
+ ? await jsonpad.runPublicFlow(flowPath, input, {
65
+ method: method,
66
+ })
67
+ : await jsonpad.runFlow(flowPath, input, {
68
+ method: method,
69
+ });
70
+ }
71
+ catch (error) {
72
+ if (error instanceof FlowError) {
73
+ if (options.json) {
74
+ context.log(JSON.stringify({
75
+ status: error.status,
76
+ error: {
77
+ code: error.flowCode,
78
+ message: error.flowMessage,
79
+ node: error.node,
80
+ },
81
+ runId: error.runId,
82
+ }, null, 2));
83
+ }
84
+ throw new CliError(`The flow failed (${error.status}): ${error.flowMessage}${error.node ? ` at node "${error.node}"` : ''}${error.flowCode ? ` [${error.flowCode}]` : ''}${error.runId ? `\nRun ${error.runId}` : ''}`, EXIT_FLOW_FAILED);
85
+ }
86
+ throw new CliError(describeApiError(error));
87
+ }
88
+ if (options.json) {
89
+ context.log(JSON.stringify({
90
+ status: response.status,
91
+ body: response.body,
92
+ runId: response.runId,
93
+ }, null, 2));
94
+ return;
95
+ }
96
+ context.error(context.colours.dim(`${response.status}${response.runId ? ` (run ${response.runId})` : ''}`));
97
+ if (response.body !== null) {
98
+ context.log(JSON.stringify(response.body, null, 2));
99
+ }
100
+ }