@wasit-dev/cli 0.8.0-beta.1 → 0.8.0-beta.2

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/dist/style.d.ts CHANGED
@@ -95,7 +95,8 @@ export declare function spread(left: string, right: string, width: number): stri
95
95
  export declare function truncate(text: string, max: number): string;
96
96
  /**
97
97
  * Plain text wrapped to `width` columns, every line after `indent`. A word
98
- * longer than a line (a URL, a hash) is kept whole and runs over.
98
+ * longer than a line (a URL, a hash) is kept whole and runs over. No line
99
+ * starts with a workflow command (`breakCommandStart`).
99
100
  */
100
101
  export declare function wrap(text: string, width: number, indent: string): string[];
101
102
  /**
package/dist/style.js CHANGED
@@ -160,9 +160,19 @@ export function truncate(text, max) {
160
160
  return "…".slice(0, Math.max(0, max));
161
161
  return `${chars.slice(0, max - 1).join("")}…`;
162
162
  }
163
+ /**
164
+ * A wrapped line that would start with a workflow command once the runner trims
165
+ * its indent (`::name::`, `##[name]`), broken the way the sanitiser breaks one
166
+ * at the start of a text. Wrapping makes new line starts out of words in the
167
+ * middle of a text, after the sanitiser ran, so each one is checked here.
168
+ */
169
+ function breakCommandStart(line) {
170
+ return line.replace(/^(::|##\[)/, (command) => (command === "::" ? ": :" : "# #["));
171
+ }
163
172
  /**
164
173
  * Plain text wrapped to `width` columns, every line after `indent`. A word
165
- * longer than a line (a URL, a hash) is kept whole and runs over.
174
+ * longer than a line (a URL, a hash) is kept whole and runs over. No line
175
+ * starts with a workflow command (`breakCommandStart`).
166
176
  */
167
177
  export function wrap(text, width, indent) {
168
178
  const room = Math.max(20, width - [...indent].length);
@@ -180,7 +190,7 @@ export function wrap(text, width, indent) {
180
190
  }
181
191
  if (current !== "")
182
192
  lines.push(current);
183
- return lines.map((line) => `${indent}${line}`);
193
+ return lines.map((line) => `${indent}${breakCommandStart(line)}`);
184
194
  }
185
195
  /**
186
196
  * The width lines are laid out to: one column less than the terminal's (a line
@@ -0,0 +1,26 @@
1
+ /**
2
+ * `wasit test` for a person at a terminal: the same checks, notes and exit code
3
+ * as the plain output, drawn live (a RUNNING line with a timer while a check is
4
+ * in flight, each result as it arrives, the counts and the duration at the end).
5
+ *
6
+ * Its own module so that a test can drive it with a stream, an environment and
7
+ * stand-in checks, and hold it to what the plain output promises: every text a
8
+ * service chose is sanitised, and no secret of the environment is printed.
9
+ */
10
+ import { runX402PaymentChecks, runX402ReadChecks } from "@wasit-dev/core";
11
+ import { type LiveStream } from "./live.js";
12
+ export interface LiveTestShape {
13
+ method?: string;
14
+ body?: string;
15
+ headers?: Record<string, string>;
16
+ }
17
+ export interface LiveTestDeps {
18
+ /** Where the output goes: `process.stdout` in the CLI. */
19
+ readonly stream: LiveStream;
20
+ /** The environment the payer key and the secrets are read from: `process.env` in the CLI. */
21
+ readonly env: Readonly<Record<string, string | undefined>>;
22
+ /** The checks, replaceable in tests. */
23
+ readonly readChecks?: typeof runX402ReadChecks;
24
+ readonly paymentChecks?: typeof runX402PaymentChecks;
25
+ }
26
+ export declare function liveX402Test(opts: Record<string, unknown>, shape: LiveTestShape, deps: LiveTestDeps): Promise<number>;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * `wasit test` for a person at a terminal: the same checks, notes and exit code
3
+ * as the plain output, drawn live (a RUNNING line with a timer while a check is
4
+ * in flight, each result as it arrives, the counts and the duration at the end).
5
+ *
6
+ * Its own module so that a test can drive it with a stream, an environment and
7
+ * stand-in checks, and hold it to what the plain output promises: every text a
8
+ * service chose is sanitised, and no secret of the environment is printed.
9
+ */
10
+ import { X402_PAYMENT_CHECK_IDS, catalogueEntry, deniedEnvValues, endpointOutcome, networkClass, paymentChainFor, runX402PaymentChecks, runX402ReadChecks, sanitizeText, } from "@wasit-dev/core";
11
+ import { LiveReport } from "./live.js";
12
+ import { exitCodeFor, verdictNotes } from "./output.js";
13
+ import { colorDepth, liveEnabled } from "./style.js";
14
+ import { CLI_VERSION } from "./version.js";
15
+ export async function liveX402Test(opts, shape, deps) {
16
+ const { stream, env } = deps;
17
+ const readChecks = deps.readChecks ?? runX402ReadChecks;
18
+ const paymentChecks = deps.paymentChecks ?? runX402PaymentChecks;
19
+ const secrets = deniedEnvValues(env);
20
+ const live = new LiveReport({
21
+ stream,
22
+ live: liveEnabled(stream, env),
23
+ depth: colorDepth(env),
24
+ clean: (text) => sanitizeText(text, { redact: secrets }),
25
+ });
26
+ const target = opts["target"];
27
+ const network = opts["network"];
28
+ live.header(CLI_VERSION, "x402 conformance", [
29
+ ["target", `${target}${shape.method !== undefined ? ` ${shape.method.toUpperCase()}` : ""}`],
30
+ ["network", network],
31
+ ]);
32
+ live.phase("challenge", [["no payment"]]);
33
+ live.running("X402-01..05", "Challenge checks");
34
+ const results = await readChecks({ target, ...shape, onResult: (result) => live.result(result) });
35
+ live.gap();
36
+ const keyEnv = paymentChainFor(network)?.payerKeyEnv ?? "STELLAR_PRIVATE_KEY";
37
+ const payerKey = opts["payerKey"] ?? env[keyEnv];
38
+ if (opts["readOnly"] === true) {
39
+ live.phase("payment", [["not run"]]);
40
+ live.note("--read-only set: the payment checks were not run.");
41
+ }
42
+ else if (!payerKey) {
43
+ live.phase("payment", [["not run"]]);
44
+ live.note(`No payer key: set ${keyEnv} in .env or pass --payer-key. The payment checks were not run.`);
45
+ }
46
+ else if (networkClass(network) !== "test") {
47
+ // Not a test network: the guard refuses the payment checks before anything
48
+ // is signed (the PREFLIGHT line says why), so no money moves.
49
+ live.phase("payment", [[network], ["not a test network: refused", "danger"]]);
50
+ }
51
+ else {
52
+ live.phase("payment", [[network], ["testnet funds move", "warning"]]);
53
+ live.note(`When the target offers a ${network} option, X402-06 settles a real payment and X402-07..10 send ` +
54
+ "payments it must refuse.");
55
+ }
56
+ if (opts["readOnly"] !== true && payerKey) {
57
+ const order = X402_PAYMENT_CHECK_IDS;
58
+ const startAt = (index) => {
59
+ const id = order[index];
60
+ if (id !== undefined)
61
+ live.running(id, catalogueEntry(id)?.name ?? id);
62
+ };
63
+ startAt(0);
64
+ results.push(...(await paymentChecks({
65
+ target,
66
+ network,
67
+ payerSecretKey: payerKey,
68
+ ...(opts["rpcUrl"] ? { rpcUrl: opts["rpcUrl"] } : {}),
69
+ ...shape,
70
+ onResult: (result) => {
71
+ live.result(result);
72
+ const index = order.indexOf(result.id);
73
+ if (index >= 0)
74
+ startAt(index + 1);
75
+ },
76
+ onProgress: (line) => live.progress(line),
77
+ })));
78
+ }
79
+ live.finish(results, endpointOutcome(results, "x402"), verdictNotes(results, "x402"));
80
+ return exitCodeFor(results, "x402");
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wasit-dev/cli",
3
- "version": "0.8.0-beta.1",
3
+ "version": "0.8.0-beta.2",
4
4
  "description": "CLI for x402/MPP protocol-conformance testing on Stellar — verifies a service's payment flow settles on-chain per spec.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -41,7 +41,7 @@
41
41
  "typecheck": "tsc -p test/tsconfig.json"
42
42
  },
43
43
  "dependencies": {
44
- "@wasit-dev/core": "^0.8.0-beta.1",
44
+ "@wasit-dev/core": "^0.8.0-beta.2",
45
45
  "cfonts": "^3.3.1",
46
46
  "commander": "^15.0.0",
47
47
  "dotenv": "^17.4.2",