@crustjs/testing 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chenxin Yan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @crustjs/testing
2
+
3
+ Typed terminal testing helpers for Crust CLI applications.
4
+
5
+ Use core `app.run(path, input)` for quiet captured output and typed completed/finished/failed outcomes. `captureExecute(app, argv)` tests terminal parsing, error presentation, and exit codes. `runInteractive(app, path, input)` from `@crustjs/testing/interactive` drives fake-terminal prompts (requires `@crustjs/prompts`) and propagates failed outcomes through `done` and `waitFor`.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ bun add -d @crustjs/testing
11
+ ```
12
+
13
+ ## Documentation
14
+
15
+ Full docs: [crustjs.com/docs/modules/testing](https://crustjs.com/docs/modules/testing)
@@ -0,0 +1,24 @@
1
+ import { InvocationIO } from "@crustjs/core";
2
+ //#region src/index.d.ts
3
+ /** Structural io shape accepted by {@link captureExecute}. */
4
+ export type CaptureIO = Partial<InvocationIO>;
5
+ /** Minimal structural surface of `execute()` invoked by {@link captureExecute}. */
6
+ export interface ExecutableApp {
7
+ execute(options?: {
8
+ argv?: string[];
9
+ io?: CaptureIO;
10
+ }): Promise<number>;
11
+ }
12
+ export interface CapturedExecute {
13
+ readonly stdout: string;
14
+ readonly stderr: string;
15
+ /** The exit code `execute()` established (`0`, `1`, or `130` for cancellation). */
16
+ readonly exitCode: number;
17
+ }
18
+ /**
19
+ * Drive the terminal `execute()` path in-process: exit-code protocol,
20
+ * Extension `onError` rendering, and cancellation (130) are all observable
21
+ * without spawning a subprocess.
22
+ */
23
+ export declare function captureExecute(app: ExecutableApp, argv: readonly string[]): Promise<CapturedExecute>;
24
+ //#endregion
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ //#region src/index.ts
2
+ let activeExecuteCaptures = 0;
3
+ let exitCodeBeforeExecuteCaptures;
4
+ /**
5
+ * Drive the terminal `execute()` path in-process: exit-code protocol,
6
+ * Extension `onError` rendering, and cancellation (130) are all observable
7
+ * without spawning a subprocess.
8
+ */
9
+ async function captureExecute(app, argv) {
10
+ const stdoutLines = [];
11
+ const stderrLines = [];
12
+ if (activeExecuteCaptures === 0) exitCodeBeforeExecuteCaptures = process.exitCode;
13
+ activeExecuteCaptures++;
14
+ try {
15
+ const exitCode = await app.execute({
16
+ argv: [...argv],
17
+ io: {
18
+ stdout: (text) => {
19
+ stdoutLines.push(text);
20
+ },
21
+ stderr: (text) => {
22
+ stderrLines.push(text);
23
+ }
24
+ }
25
+ });
26
+ return {
27
+ stdout: stdoutLines.join("\n"),
28
+ stderr: stderrLines.join("\n"),
29
+ exitCode
30
+ };
31
+ } finally {
32
+ activeExecuteCaptures--;
33
+ if (activeExecuteCaptures === 0) process.exitCode = exitCodeBeforeExecuteCaptures ?? 0;
34
+ }
35
+ }
36
+ //#endregion
37
+ export { captureExecute };
@@ -0,0 +1,13 @@
1
+ import { Key } from "@crustjs/prompts/testing";
2
+ import { AnyCrust, CommandPath, CommandShapeAt, RunInputArguments } from "@crustjs/core";
3
+ //#region src/interactive.d.ts
4
+ export interface InteractiveRun {
5
+ waitFor(pattern: RegExp, timeoutMs?: number): Promise<void>;
6
+ type(text: string): void;
7
+ keys(...namedKeys: Key[]): void;
8
+ screen(): string;
9
+ readonly done: Promise<void>;
10
+ }
11
+ /** Run an application with fake terminal streams for its prompts and stderr output. */
12
+ export declare function runInteractive<App extends AnyCrust, const Path extends CommandPath<App["_types"]["tree"]>>(app: App, path: Path, ...args: RunInputArguments<CommandShapeAt<App["_types"]["shape"], Path>>): InteractiveRun;
13
+ //#endregion
@@ -0,0 +1,49 @@
1
+ import { setTimeout } from "node:timers/promises";
2
+ import { withTerminalIO } from "@crustjs/prompts";
3
+ import { createPromptIO } from "@crustjs/prompts/testing";
4
+ //#region src/interactive.ts
5
+ /** Run an application with fake terminal streams for its prompts and stderr output. */
6
+ function runInteractive(app, path, ...args) {
7
+ const harness = createPromptIO();
8
+ const output = harness.io.output;
9
+ const [input] = args;
10
+ const done = withTerminalIO(harness.io, () => app.run(path, input, {
11
+ stdout: () => {},
12
+ stderr: (text) => {
13
+ output.write(`${text}\n`);
14
+ }
15
+ }).then((outcome) => {
16
+ if (outcome.status === "failed") throw outcome.error;
17
+ }));
18
+ let settled = false;
19
+ let failed = false;
20
+ let failure;
21
+ done.then(() => {
22
+ settled = true;
23
+ }, (cause) => {
24
+ settled = true;
25
+ failed = true;
26
+ failure = cause;
27
+ });
28
+ return {
29
+ waitFor: async (pattern, timeoutMs = 5e3) => {
30
+ const matcher = new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ""));
31
+ const deadline = Date.now() + timeoutMs;
32
+ while (!matcher.test(harness.screen())) {
33
+ if (settled) {
34
+ if (matcher.test(harness.screen())) return;
35
+ if (failed) throw failure;
36
+ throw new Error(`waitFor(${matcher}) never matched; the application already completed. Screen:\n${harness.screen()}`);
37
+ }
38
+ if (Date.now() > deadline) throw new Error(`waitFor(${matcher}) timed out after ${timeoutMs}ms. Screen:\n${harness.screen()}`);
39
+ await setTimeout(1);
40
+ }
41
+ },
42
+ type: (text) => harness.type(text),
43
+ keys: (...namedKeys) => harness.keys(...namedKeys),
44
+ screen: () => harness.screen(),
45
+ done
46
+ };
47
+ }
48
+ //#endregion
49
+ export { runInteractive };
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@crustjs/testing",
3
+ "version": "0.1.0",
4
+ "description": "Testing helpers for Crust CLI applications",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "license": "MIT",
8
+ "author": "chenxin-yan",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/chenxin-yan/crust.git",
12
+ "directory": "packages/testing"
13
+ },
14
+ "homepage": "https://crustjs.com",
15
+ "bugs": {
16
+ "url": "https://github.com/chenxin-yan/crust/issues"
17
+ },
18
+ "keywords": [
19
+ "cli",
20
+ "testing",
21
+ "crust",
22
+ "bun",
23
+ "typescript"
24
+ ],
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ },
33
+ "./interactive": {
34
+ "types": "./dist/interactive.d.ts",
35
+ "import": "./dist/interactive.js"
36
+ }
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "scripts": {
42
+ "build": "tsdown",
43
+ "dev": "tsdown --watch",
44
+ "check:types": "tsc --noEmit",
45
+ "test": "bun test",
46
+ "prepack": "cp ../../LICENSE LICENSE",
47
+ "postpack": "rm -f LICENSE"
48
+ },
49
+ "devDependencies": {
50
+ "@crustjs/config": "0.0.0",
51
+ "@crustjs/core": "0.2.0",
52
+ "@crustjs/progress": "0.1.0",
53
+ "@crustjs/prompts": "0.2.0",
54
+ "tsdown": "^0.23.0"
55
+ },
56
+ "peerDependencies": {
57
+ "@crustjs/core": "^0.2.0",
58
+ "@crustjs/prompts": "0.x",
59
+ "typescript": "^7.0.0"
60
+ },
61
+ "peerDependenciesMeta": {
62
+ "@crustjs/prompts": {
63
+ "optional": true
64
+ },
65
+ "typescript": {
66
+ "optional": true
67
+ }
68
+ },
69
+ "engines": {
70
+ "bun": ">=1.3.14",
71
+ "node": ">=22",
72
+ "deno": ">=2.8"
73
+ }
74
+ }