@mantaq/inspect 0.2.3 → 0.4.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/README.md +7 -9
- package/dist/index.d.mts +39 -3
- package/dist/index.mjs +97 -3
- package/dist/{load-Dw5KOFuM.mjs → load-D0_Jl6Wr.mjs} +2 -0
- package/dist/vite.d.mts +10 -0
- package/dist/vite.mjs +4 -4
- package/package.json +7 -10
- package/dist/cli.d.mts +0 -1
- package/dist/cli.mjs +0 -108
- package/dist/page-rr9IQ-4k.mjs +0 -38
package/README.md
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
# @mantaq/inspect
|
|
2
2
|
|
|
3
|
-
Turns your Mantaq machine into a picture you can walk:
|
|
3
|
+
Turns your Mantaq machine into a picture you can walk: add the Vite plugin, no other config.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
mantaq-inspect src/cart.ts # writes mantaq-viz.html and opens it
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
It finds every exported actor, or function that builds one, from the file's types. It reads handler explanations and sample payloads from the source, fuzzes each machine that runs on a `VirtualClock`, and writes one self-contained HTML page that works offline: commit it, attach it to a PR. The viewer is [`@mantaq/viz`](../viz/README.md).
|
|
5
|
+
It finds every exported actor, or function that builds one, from the file's types. It reads handler explanations and sample payloads from the source, fuzzes each machine that runs on a `VirtualClock`, and shows them in a live page on your Vite dev server. The viewer is [`@mantaq/viz`](../viz/README.md).
|
|
10
6
|
|
|
11
|
-
##
|
|
7
|
+
## Ways in
|
|
12
8
|
|
|
13
|
-
**
|
|
9
|
+
**Gate in tests**: see the docs page "Design gate in vitest" for `expectNoDesignErrors`, `expectReached` and `expectAllStatesReached`.
|
|
14
10
|
|
|
15
11
|
**Vite plugin**: a live page on your dev server that redraws, keeping your walk, when you save.
|
|
16
12
|
|
|
@@ -43,13 +39,14 @@ const { machine, warnings } = inspect({
|
|
|
43
39
|
const json = serializeViz(vizDocument([machine]));
|
|
44
40
|
```
|
|
45
41
|
|
|
46
|
-
`inspectFile(file)` / `inspectModule(file, { load })` do the discovery the
|
|
42
|
+
`inspectFile(file)` / `inspectModule(file, { load })` do the discovery the Vite plugin does. The building blocks (`toVizMachine`, `annotateMachine`, `fuzz`, `fuzzedMachine`, `withTestCoverage`, `guidance`) stay exported for custom pipelines.
|
|
47
43
|
|
|
48
44
|
## What it needs from your file
|
|
49
45
|
|
|
50
46
|
- **Exports.** An actor, or a function that returns one (or `{ actor }`). Functions are called with no arguments, with a `VirtualClock` when the first parameter is a clock, or with `{ clock }` when it is an options object with a `clock`. Functions with other required parameters are skipped. Discovery reads types only; nothing is called to find out.
|
|
51
47
|
- **A fresh actor per call**, to fuzz. A file that only exports one actor instance is drawn, not fuzzed. When a file has factories, its actor instances are skipped.
|
|
52
48
|
- **Exported factories.** Export a factory that takes ports and a clock, not only a built instance. See [Machines in an application](https://anderscan.github.io/mantaq/guides/machines-in-an-app/).
|
|
49
|
+
- **A factory, for the live page.** The page recreates the actor on every step, so it needs an exported factory; a file that only exports an instance stays a static walk, with the usual warning.
|
|
53
50
|
- **A `VirtualClock`**, to fuzz. An actor on the real clock is drawn, with a warning.
|
|
54
51
|
|
|
55
52
|
Every part that cannot run is skipped with a warning, never guessed.
|
|
@@ -58,6 +55,7 @@ Every part that cannot run is skipped with a warning, never guessed.
|
|
|
58
55
|
|
|
59
56
|
The viewer's "Coverage from" picker dims elements with zero hits. What dimmed means depends on the source:
|
|
60
57
|
|
|
58
|
+
- **examples + fuzz** (`reached`): neither an example nor fuzzing reached this. The default when both sources are present, and the same merge `expectReached` uses. Fix a miss by adding an example to `tests`.
|
|
61
59
|
- **example tests** (`tested`): no test exercises this. Collected by replaying the event sequences you pass as `tests`, each on a fresh actor.
|
|
62
60
|
- **fuzz runs** (`coverage`): no fuzz run produced this. Fuzzing reaches almost everything, so little is dimmed. What does stay dimmed is usually a **declared but unreached edge**: the handler lists the target in `to`, but its guards may never allow it. Saga's `compensatingRefund -REFUND_DONE-> failed` is an example. The edge panel calls these out. Fuzzing is not proof, so check the handler.
|
|
63
61
|
|
package/dist/index.d.mts
CHANGED
|
@@ -37,6 +37,8 @@ interface FuzzOptions {
|
|
|
37
37
|
* the run in the corpus as a stepping stone even without new coverage.
|
|
38
38
|
*/
|
|
39
39
|
guide?: (trace: Trace) => boolean;
|
|
40
|
+
/** Known-good sequences (e.g. example tests) the corpus starts from, before any mutation. */
|
|
41
|
+
initial?: ReadonlyArray<ReadonlyArray<VizStep>>;
|
|
40
42
|
}
|
|
41
43
|
interface CorpusEntry {
|
|
42
44
|
steps: VizStep[];
|
|
@@ -148,6 +150,40 @@ interface LoadOptions {
|
|
|
148
150
|
}
|
|
149
151
|
/** Load `file` with Vite (TS, tsconfig paths, extensionless imports) and inspect its machines. */
|
|
150
152
|
export declare function inspectFile(file: string, options?: FileOptions, load?: LoadOptions): Promise<LoadedFile>;
|
|
151
|
-
/**
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
/**
|
|
154
|
+
* States no edge path leads to from the initial state. Empty when an undetermined
|
|
155
|
+
* edge hides the answer. Region states are never reported: the graph does not
|
|
156
|
+
* record where a region starts.
|
|
157
|
+
*/
|
|
158
|
+
export declare function unreachableStates(machine: VizMachine): string[];
|
|
159
|
+
/** Design errors of an inspected machine, one line each. Unreached edges and routing warnings are not errors. */
|
|
160
|
+
export declare function designErrors(machine: VizMachine): string[];
|
|
161
|
+
/**
|
|
162
|
+
* Call from a test: throws when fuzzing finds a handler or effect that throws,
|
|
163
|
+
* an effect sends an event its state does not handle, or a declared state has
|
|
164
|
+
* no path from the initial state. Runs are seeded, so the result is stable.
|
|
165
|
+
*/
|
|
166
|
+
export declare function expectNoDesignErrors(target: InspectTarget): void;
|
|
167
|
+
interface ReachTransition {
|
|
168
|
+
from: string;
|
|
169
|
+
event: string;
|
|
170
|
+
/** Omit to accept any target. */
|
|
171
|
+
to?: string;
|
|
172
|
+
}
|
|
173
|
+
interface ReachExpectation {
|
|
174
|
+
states?: ReadonlyArray<string>;
|
|
175
|
+
transitions?: ReadonlyArray<ReachTransition>;
|
|
176
|
+
}
|
|
177
|
+
/** Ids of the states a fuzz run or the supplied test sequences visited. */
|
|
178
|
+
export declare function reachedStates(machine: VizMachine): string[];
|
|
179
|
+
/** What the expectation asks for and the machine did not reach, one line each. */
|
|
180
|
+
export declare function missedReach(machine: VizMachine, expected: ReachExpectation): string[];
|
|
181
|
+
/**
|
|
182
|
+
* Call from a test: throws unless fuzzing or the target's `tests` visited every
|
|
183
|
+
* listed state and took every listed transition. Reaching is proof; a miss only
|
|
184
|
+
* says nothing got there yet.
|
|
185
|
+
*/
|
|
186
|
+
export declare function expectReached(target: InspectTarget, expected: ReachExpectation): void;
|
|
187
|
+
/** Call from a test: throws unless fuzzing or the target's `tests` visited every declared state. */
|
|
188
|
+
export declare function expectAllStatesReached(target: InspectTarget): void;
|
|
189
|
+
export { type AnalysisSource, type CorpusEntry, type FileOptions, type FuzzOptions, type FuzzResult, type FuzzSettings, type FuzzTarget, type HandlerSite, type InspectTarget, type Inspected, type InspectedModule, type LiteralPool, type MachineExport, type ModuleAnalysis, type ModuleOptions, type Observation, type ReachExpectation, type ReachTransition, type RunError, type Trace, type VizInput, eventCatalog, execute, inspect, inspectModule, machineId, vizDocument };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,98 @@
|
|
|
1
|
-
import { S as toVizMachine, _ as fuzzedMachine, a as inspect, b as shrink, d as analyzeSource, f as annotateMachine, g as literalPool, h as guidance, l as vizDocument, m as branchDistanceGuide, o as inspectModule, p as branchDistance, r as inspectFile, s as machineId, u as analyzeModule, v as withTestCoverage, x as toViz, y as fuzz } from "./load-
|
|
1
|
+
import { S as toVizMachine, _ as fuzzedMachine, a as inspect, b as shrink, d as analyzeSource, f as annotateMachine, g as literalPool, h as guidance, l as vizDocument, m as branchDistanceGuide, o as inspectModule, p as branchDistance, r as inspectFile, s as machineId, u as analyzeModule, v as withTestCoverage, x as toViz, y as fuzz } from "./load-D0_Jl6Wr.mjs";
|
|
2
2
|
import { i as execute, r as eventCatalog } from "./run-B5huYj_r.mjs";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { coveredByAny, unhandledSends } from "@mantaq/viz";
|
|
4
|
+
// src/gate.ts
|
|
5
|
+
/**
|
|
6
|
+
* States no edge path leads to from the initial state. Empty when an undetermined
|
|
7
|
+
* edge hides the answer. Region states are never reported: the graph does not
|
|
8
|
+
* record where a region starts.
|
|
9
|
+
*/
|
|
10
|
+
function unreachableStates(machine) {
|
|
11
|
+
const reached = new Set(machine.states.filter((state) => state.initial || state.region !== null).map((state) => state.id));
|
|
12
|
+
const queue = [...reached];
|
|
13
|
+
let undetermined = false;
|
|
14
|
+
for (let next = queue.shift(); next !== void 0; next = queue.shift()) {
|
|
15
|
+
const from = next;
|
|
16
|
+
for (const edge of machine.edges) {
|
|
17
|
+
if (edge.kind === "effect" || edge.from !== from && !coveredByAny(edge.from, from)) continue;
|
|
18
|
+
if (edge.to === null) {
|
|
19
|
+
undetermined = true;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (reached.has(edge.to)) continue;
|
|
23
|
+
reached.add(edge.to);
|
|
24
|
+
queue.push(edge.to);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (undetermined) return [];
|
|
28
|
+
return machine.states.map((state) => state.id).filter((id) => !reached.has(id));
|
|
29
|
+
}
|
|
30
|
+
/** Design errors of an inspected machine, one line each. Unreached edges and routing warnings are not errors. */
|
|
31
|
+
function designErrors(machine) {
|
|
32
|
+
const thrown = (machine.fuzz?.errors ?? []).map((error) => `${error.event} in ${error.from} throws: ${error.message}`);
|
|
33
|
+
const sends = machine.states.flatMap((state) => unhandledSends(machine, state.id).map(({ effect, event }) => `effect ${effect} in ${state.id} sends ${event}, but ${state.id} has no handler for it`));
|
|
34
|
+
const orphans = unreachableStates(machine).map((id) => `state ${id} is declared but no path leads to it from the initial state`);
|
|
35
|
+
return [
|
|
36
|
+
...thrown,
|
|
37
|
+
...sends,
|
|
38
|
+
...orphans
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Call from a test: throws when fuzzing finds a handler or effect that throws,
|
|
43
|
+
* an effect sends an event its state does not handle, or a declared state has
|
|
44
|
+
* no path from the initial state. Runs are seeded, so the result is stable.
|
|
45
|
+
*/
|
|
46
|
+
function expectNoDesignErrors(target) {
|
|
47
|
+
const { machine } = inspect(target);
|
|
48
|
+
const errors = designErrors(machine);
|
|
49
|
+
if (errors.length === 0) return;
|
|
50
|
+
throw new Error(`${machine.id} has ${errors.length} design error${errors.length === 1 ? "" : "s"}:\n${errors.map((each) => ` - ${each}`).join("\n")}`);
|
|
51
|
+
}
|
|
52
|
+
// src/reach.ts
|
|
53
|
+
function hitCount(coverage) {
|
|
54
|
+
return coverage?.hits ?? 0;
|
|
55
|
+
}
|
|
56
|
+
/** Ids of the states a fuzz run or the supplied test sequences visited. */
|
|
57
|
+
function reachedStates(machine) {
|
|
58
|
+
return machine.states.filter((state) => hitCount(state.coverage) + hitCount(state.tested) > 0).map((state) => state.id);
|
|
59
|
+
}
|
|
60
|
+
function isTaken(edge) {
|
|
61
|
+
return hitCount(edge.coverage) + hitCount(edge.tested) > 0;
|
|
62
|
+
}
|
|
63
|
+
function describeTransition({ from, event, to }) {
|
|
64
|
+
return to === void 0 ? `${from} --${event}-->` : `${from} --${event}--> ${to}`;
|
|
65
|
+
}
|
|
66
|
+
/** What the expectation asks for and the machine did not reach, one line each. */
|
|
67
|
+
function missedReach(machine, expected) {
|
|
68
|
+
const reached = new Set(reachedStates(machine));
|
|
69
|
+
const states = (expected.states ?? []).map((id) => {
|
|
70
|
+
if (!machine.states.some((state) => state.id === id)) return `state ${id} does not exist`;
|
|
71
|
+
return reached.has(id) ? null : `state ${id} was not reached`;
|
|
72
|
+
});
|
|
73
|
+
const transitions = (expected.transitions ?? []).map((wanted) => {
|
|
74
|
+
const matching = machine.edges.filter((edge) => edge.kind !== "effect" && edge.kind !== "initial" && edge.from === wanted.from && edge.event === wanted.event && (wanted.to === void 0 || edge.to === wanted.to));
|
|
75
|
+
if (matching.length === 0) return `transition ${describeTransition(wanted)} does not exist`;
|
|
76
|
+
return matching.some(isTaken) ? null : `transition ${describeTransition(wanted)} was not taken`;
|
|
77
|
+
});
|
|
78
|
+
return [...states, ...transitions].filter((line) => line !== null);
|
|
79
|
+
}
|
|
80
|
+
function failOnMiss(machine, misses) {
|
|
81
|
+
if (misses.length === 0) return;
|
|
82
|
+
throw new Error(`${machine.id} missed ${misses.length} expected ${misses.length === 1 ? "reach" : "reaches"}:\n${misses.map((each) => ` - ${each}`).join("\n")}\nAdd an example run to \`tests\`, or raise \`fuzz.iterations\`, to cover it.`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Call from a test: throws unless fuzzing or the target's `tests` visited every
|
|
86
|
+
* listed state and took every listed transition. Reaching is proof; a miss only
|
|
87
|
+
* says nothing got there yet.
|
|
88
|
+
*/
|
|
89
|
+
function expectReached(target, expected) {
|
|
90
|
+
const { machine } = inspect(target);
|
|
91
|
+
failOnMiss(machine, missedReach(machine, expected));
|
|
92
|
+
}
|
|
93
|
+
/** Call from a test: throws unless fuzzing or the target's `tests` visited every declared state. */
|
|
94
|
+
function expectAllStatesReached(target) {
|
|
95
|
+
const { machine } = inspect(target);
|
|
96
|
+
failOnMiss(machine, missedReach(machine, { states: machine.states.map((state) => state.id) }));
|
|
97
|
+
}
|
|
98
|
+
export { analyzeModule, analyzeSource, annotateMachine, branchDistance, branchDistanceGuide, designErrors, eventCatalog, execute, expectAllStatesReached, expectNoDesignErrors, expectReached, fuzz, fuzzedMachine, guidance, inspect, inspectFile, inspectModule, literalPool, machineId, missedReach, reachedStates, shrink, toViz, toVizMachine, unreachableStates, vizDocument, withTestCoverage };
|
|
@@ -407,6 +407,7 @@ function fuzz(target, options) {
|
|
|
407
407
|
}
|
|
408
408
|
}
|
|
409
409
|
consider([], 0);
|
|
410
|
+
for (const steps of options.initial ?? []) consider([...steps], 0);
|
|
410
411
|
for (let iteration = 1; iteration <= options.iterations; iteration += 1) {
|
|
411
412
|
const at = context.rng.int(corpus.length);
|
|
412
413
|
consider(mutate({
|
|
@@ -1600,6 +1601,7 @@ function driven(target, context) {
|
|
|
1600
1601
|
const fuzzed = settings === false ? structure : fuzzedMachine(run[1], fuzz(run[1], {
|
|
1601
1602
|
seed: settings?.seed ?? DEFAULT_SEED,
|
|
1602
1603
|
iterations: settings?.iterations ?? DEFAULT_ITERATIONS,
|
|
1604
|
+
...target.tests === void 0 ? {} : { initial: target.tests },
|
|
1603
1605
|
...guidance(structure)
|
|
1604
1606
|
}));
|
|
1605
1607
|
return {
|
package/dist/vite.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { t as FuzzSettings } from "./inspect-DvQtAcwv.mjs";
|
|
2
2
|
import { Plugin } from "vite";
|
|
3
|
+
export declare const STYLESHEETS: string[];
|
|
3
4
|
export interface MantaqVizOptions {
|
|
4
5
|
/** Files that export machines: paths or globs, relative to the Vite root. */
|
|
5
6
|
machines: string | string[];
|
|
@@ -8,4 +9,13 @@ export interface MantaqVizOptions {
|
|
|
8
9
|
/** `false` = structure only. Default: 500 iterations, so a save redraws quickly. */
|
|
9
10
|
fuzz?: false | FuzzSettings;
|
|
10
11
|
}
|
|
12
|
+
export declare const TARGETS = "virtual:mantaq/targets";
|
|
13
|
+
export declare const TARGETS_RESOLVED = "\0virtual:mantaq/targets";
|
|
14
|
+
/**
|
|
15
|
+
* Source of `virtual:mantaq/targets`: one factory per exported actor factory,
|
|
16
|
+
* shaped by its type (`call`, `clock`, `options`). A bare actor export cannot
|
|
17
|
+
* be recreated, so it gets none. Like the server-side inspection, plain actors
|
|
18
|
+
* are skipped when a file has factories.
|
|
19
|
+
*/
|
|
20
|
+
export declare function targetsSource(files: ReadonlyArray<string>): string;
|
|
11
21
|
export declare function mantaqViz(options: MantaqVizOptions): Plugin[];
|
package/dist/vite.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as projectRoot, i as runOf, l as vizDocument, n as inspectEntry, s as machineId, t as entryId, u as analyzeModule } from "./load-
|
|
1
|
+
import { c as projectRoot, i as runOf, l as vizDocument, n as inspectEntry, s as machineId, t as entryId, u as analyzeModule } from "./load-D0_Jl6Wr.mjs";
|
|
2
2
|
import { UPDATE_EVENT, livePageHtml, serializeViz } from "@mantaq/viz";
|
|
3
3
|
import { Either } from "@mantaq/utils";
|
|
4
4
|
import { dirname, isAbsolute, resolve } from "node:path";
|
|
@@ -88,8 +88,8 @@ function mantaqViz(options) {
|
|
|
88
88
|
configResolved: (config) => {
|
|
89
89
|
root = config.root;
|
|
90
90
|
},
|
|
91
|
-
resolveId: (id) => id ===
|
|
92
|
-
load: (id) => id ===
|
|
91
|
+
resolveId: (id) => id === "virtual:mantaq/targets" ? TARGETS_RESOLVED : null,
|
|
92
|
+
load: (id) => id === "\0virtual:mantaq/targets" ? targetsSource(machineFiles(root, patterns)) : null
|
|
93
93
|
},
|
|
94
94
|
{
|
|
95
95
|
name: "mantaq-viz",
|
|
@@ -156,4 +156,4 @@ function mantaqViz(options) {
|
|
|
156
156
|
}
|
|
157
157
|
];
|
|
158
158
|
}
|
|
159
|
-
export { mantaqViz };
|
|
159
|
+
export { STYLESHEETS, TARGETS, TARGETS_RESOLVED, mantaqViz, targetsSource };
|
package/package.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mantaq/inspect",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Turn a Mantaq machine into a viz document: structure, handler explanations (AST), fuzz and test coverage.
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Turn a Mantaq machine into a viz document: structure, handler explanations (AST), fuzz and test coverage. Vite plugin included.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/AndersCan/mantaq",
|
|
9
9
|
"directory": "packages/inspect"
|
|
10
10
|
},
|
|
11
|
-
"bin": {
|
|
12
|
-
"mantaq-inspect": "./dist/cli.mjs"
|
|
13
|
-
},
|
|
14
11
|
"files": [
|
|
15
12
|
"dist"
|
|
16
13
|
],
|
|
@@ -27,11 +24,11 @@
|
|
|
27
24
|
},
|
|
28
25
|
"dependencies": {
|
|
29
26
|
"typescript": "6.0.3",
|
|
30
|
-
"@mantaq/core": "0.5.
|
|
31
|
-
"@mantaq/
|
|
27
|
+
"@mantaq/core": "0.5.4",
|
|
28
|
+
"@mantaq/traversal": "0.3.4",
|
|
29
|
+
"@mantaq/viz": "0.3.0",
|
|
32
30
|
"@mantaq/utils": "0.0.5",
|
|
33
|
-
"@mantaq/
|
|
34
|
-
"@mantaq/traversal": "0.3.3"
|
|
31
|
+
"@mantaq/pbt": "0.0.4"
|
|
35
32
|
},
|
|
36
33
|
"devDependencies": {
|
|
37
34
|
"@types/node": "^26.2.0",
|
|
@@ -39,7 +36,7 @@
|
|
|
39
36
|
"happy-dom": "^21.0.4",
|
|
40
37
|
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
41
38
|
"vite-plus": "1.4.0",
|
|
42
|
-
"@mantaq/sugar": "2.0.
|
|
39
|
+
"@mantaq/sugar": "2.0.4"
|
|
43
40
|
},
|
|
44
41
|
"peerDependencies": {
|
|
45
42
|
"vite": "*"
|
package/dist/cli.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/dist/cli.mjs
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { l as vizDocument, r as inspectFile } from "./load-Dw5KOFuM.mjs";
|
|
3
|
-
import { t as htmlPage } from "./page-rr9IQ-4k.mjs";
|
|
4
|
-
import { serializeViz } from "@mantaq/viz";
|
|
5
|
-
import { Either } from "@mantaq/utils";
|
|
6
|
-
import { basename, resolve } from "node:path";
|
|
7
|
-
import { SEED_ENV, parseSeed } from "@mantaq/pbt";
|
|
8
|
-
import { writeFileSync } from "node:fs";
|
|
9
|
-
import { spawn } from "node:child_process";
|
|
10
|
-
import { parseArgs } from "node:util";
|
|
11
|
-
// src/cli.ts
|
|
12
|
-
/**
|
|
13
|
-
* mantaq-inspect: draw the machines a file exports.
|
|
14
|
-
*
|
|
15
|
-
* mantaq-inspect <file.ts>... [--out FILE] [--json] [--no-fuzz] [--iterations N] [--config FILE]
|
|
16
|
-
* [--mode MODE] [--no-open] [--check]
|
|
17
|
-
*
|
|
18
|
-
* Finds every exported actor or actor factory from the file's types, reads
|
|
19
|
-
* handler explanations and sample payloads from the source, fuzzes each
|
|
20
|
-
* machine that runs on a VirtualClock, and writes one self-contained HTML page
|
|
21
|
-
* (default `mantaq-viz.html`) that opens without a server. `--json` writes the
|
|
22
|
-
* viz document instead. `--config` loads the files with that Vite config (env,
|
|
23
|
-
* aliases, plugins) and `--mode` picks its mode. `--check` exits 1 if any warning was printed or
|
|
24
|
-
* fuzzing found a handler error (each printed as `error:`). Seed: MANTAQ_SEED (default from @mantaq/pbt).
|
|
25
|
-
*/
|
|
26
|
-
const USAGE = "usage: mantaq-inspect <file.ts>... [--out FILE] [--json] [--no-fuzz] [--iterations N] [--config FILE] [--mode MODE] [--no-open] [--check]\n";
|
|
27
|
-
function loadOptions(config, mode) {
|
|
28
|
-
return {
|
|
29
|
-
...config === void 0 ? {} : { config: resolve(config) },
|
|
30
|
-
...mode === void 0 ? {} : { mode }
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
function parse() {
|
|
34
|
-
const parsed = Either.from(() => parseArgs({
|
|
35
|
-
allowPositionals: true,
|
|
36
|
-
options: {
|
|
37
|
-
out: { type: "string" },
|
|
38
|
-
json: { type: "boolean" },
|
|
39
|
-
iterations: { type: "string" },
|
|
40
|
-
config: { type: "string" },
|
|
41
|
-
mode: { type: "string" },
|
|
42
|
-
"no-fuzz": { type: "boolean" },
|
|
43
|
-
"no-open": { type: "boolean" },
|
|
44
|
-
check: { type: "boolean" }
|
|
45
|
-
}
|
|
46
|
-
}));
|
|
47
|
-
if (Either.isLeft(parsed)) return Either.left(String(parsed[0]));
|
|
48
|
-
const { positionals, values } = parsed[1];
|
|
49
|
-
if (positionals.length === 0) return Either.left("no file given");
|
|
50
|
-
const iterations = Number(values.iterations ?? 2e3);
|
|
51
|
-
if (!Number.isInteger(iterations) || iterations < 0) return Either.left("bad --iterations");
|
|
52
|
-
const seed = parseSeed(process.env[SEED_ENV]);
|
|
53
|
-
if (Either.isLeft(seed)) return Either.left(seed[0].message);
|
|
54
|
-
const json = values.json === true;
|
|
55
|
-
const fuzz = values["no-fuzz"] === true ? false : {
|
|
56
|
-
seed: seed[1],
|
|
57
|
-
iterations
|
|
58
|
-
};
|
|
59
|
-
return Either.right({
|
|
60
|
-
files: positionals.map((file) => resolve(file)),
|
|
61
|
-
out: resolve(values.out ?? (json ? "mantaq-viz.json" : "mantaq-viz.html")),
|
|
62
|
-
json,
|
|
63
|
-
fuzz,
|
|
64
|
-
check: values.check === true,
|
|
65
|
-
load: loadOptions(values.config, values.mode),
|
|
66
|
-
open: values["no-open"] !== true && !json && process.stdout.isTTY === true
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
function openInBrowser(path) {
|
|
70
|
-
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
|
|
71
|
-
spawn(command, [path], {
|
|
72
|
-
stdio: "ignore",
|
|
73
|
-
detached: true
|
|
74
|
-
}).on("error", () => {}).unref();
|
|
75
|
-
}
|
|
76
|
-
function checkFails(results, machines) {
|
|
77
|
-
const errors = machines.flatMap((machine) => (machine.fuzz?.errors ?? []).map((error) => `${machine.id}: ${error.from} -${error.event}-> ${error.message}`));
|
|
78
|
-
for (const error of errors) process.stderr.write(`error: ${error}\n`);
|
|
79
|
-
return errors.length > 0 || results.some((result) => result.warnings.length > 0);
|
|
80
|
-
}
|
|
81
|
-
async function main() {
|
|
82
|
-
const options = parse();
|
|
83
|
-
if (Either.isLeft(options)) {
|
|
84
|
-
process.stderr.write(`${options[0]}\n${USAGE}`);
|
|
85
|
-
process.exitCode = 1;
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
const { files, out, json, fuzz, open, check, load } = options[1];
|
|
89
|
-
const results = [];
|
|
90
|
-
for (const file of files) results.push(await inspectFile(file, { fuzz }, load));
|
|
91
|
-
for (const warning of results.flatMap((result) => result.warnings)) process.stderr.write(`warning: ${warning}\n`);
|
|
92
|
-
const failed = results.some((result) => result.failed);
|
|
93
|
-
if (failed) process.exitCode = 1;
|
|
94
|
-
const machines = results.flatMap((result) => result.machines);
|
|
95
|
-
if (check && checkFails(results, machines)) process.exitCode = 1;
|
|
96
|
-
if (machines.length === 0) {
|
|
97
|
-
if (!failed) process.stderr.write("no machines found\n");
|
|
98
|
-
process.exitCode = 1;
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const document = vizDocument(machines);
|
|
102
|
-
const title = `${files.map((file) => basename(file)).join(", ")} · mantaq viz`;
|
|
103
|
-
writeFileSync(out, json ? serializeViz(document) : await htmlPage(document, title));
|
|
104
|
-
process.stdout.write(`wrote ${out} (${machines.map((machine) => machine.id).join(", ")})\n`);
|
|
105
|
-
if (open) openInBrowser(out);
|
|
106
|
-
}
|
|
107
|
-
main();
|
|
108
|
-
export {};
|
package/dist/page-rr9IQ-4k.mjs
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { serializeViz, standaloneHtml } from "@mantaq/viz";
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { build } from "vite";
|
|
5
|
-
// src/page.ts
|
|
6
|
-
/** The viewer as one classic script, built from `@mantaq/viz/standalone`, plus its stylesheets. */
|
|
7
|
-
async function viewerBundle() {
|
|
8
|
-
const entry = fileURLToPath(import.meta.resolve("@mantaq/viz/standalone"));
|
|
9
|
-
const output = await build({
|
|
10
|
-
configFile: false,
|
|
11
|
-
logLevel: "silent",
|
|
12
|
-
build: {
|
|
13
|
-
write: false,
|
|
14
|
-
minify: true,
|
|
15
|
-
cssCodeSplit: false,
|
|
16
|
-
lib: {
|
|
17
|
-
entry,
|
|
18
|
-
formats: ["iife"],
|
|
19
|
-
name: "MantaqViz",
|
|
20
|
-
fileName: () => "viz.js"
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return {
|
|
25
|
-
script: (Array.isArray(output) ? output : [output]).flatMap((each) => "output" in each ? each.output : []).flatMap((file) => file.type === "chunk" ? [file.code] : []).join("\n"),
|
|
26
|
-
css: ["@mantaq/viz/viewer.css", "@mantaq/viz/page.css"].map((id) => readFileSync(fileURLToPath(import.meta.resolve(id)), "utf8")).join("\n")
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
/** A self-contained HTML page for `document`: open it from disk, attach it, commit it. */
|
|
30
|
-
async function htmlPage(document, title) {
|
|
31
|
-
const bundle = await viewerBundle();
|
|
32
|
-
return standaloneHtml({
|
|
33
|
-
title,
|
|
34
|
-
...bundle,
|
|
35
|
-
document: serializeViz(document)
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
export { htmlPage as t };
|