@mantaq/test 0.0.1
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 +21 -0
- package/README.md +8 -0
- package/dist/index.d.mts +76 -0
- package/dist/index.mjs +128 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anders Can
|
|
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,8 @@
|
|
|
1
|
+
# @mantaq/test
|
|
2
|
+
|
|
3
|
+
Test harness and assertion utilities for verifying
|
|
4
|
+
[@mantaq/core](https://www.npmjs.com/package/@mantaq/core) actor behavior.
|
|
5
|
+
|
|
6
|
+
Drive actors through event sequences, advance the virtual clock
|
|
7
|
+
deterministically, and assert on coverage of states and transitions
|
|
8
|
+
(see [@mantaq/traversal](https://www.npmjs.com/package/@mantaq/traversal)).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ActorGraph, History } from "@mantaq/traversal";
|
|
2
|
+
import { AnyActor, Snapshot } from "@mantaq/core";
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
interface CoverageReport {
|
|
5
|
+
states: {
|
|
6
|
+
total: number;
|
|
7
|
+
visited: number;
|
|
8
|
+
uncovered: string[];
|
|
9
|
+
};
|
|
10
|
+
transitions: {
|
|
11
|
+
total: number;
|
|
12
|
+
visited: number;
|
|
13
|
+
uncovered: Array<{
|
|
14
|
+
from: string;
|
|
15
|
+
event: string;
|
|
16
|
+
}>;
|
|
17
|
+
};
|
|
18
|
+
effects: {
|
|
19
|
+
total: number;
|
|
20
|
+
ran: number;
|
|
21
|
+
unexecuted: string[];
|
|
22
|
+
};
|
|
23
|
+
percent: {
|
|
24
|
+
states: number;
|
|
25
|
+
transitions: number;
|
|
26
|
+
effects: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
interface TestHarness {
|
|
30
|
+
actor: AnyActor;
|
|
31
|
+
graph: ActorGraph;
|
|
32
|
+
history: History;
|
|
33
|
+
coverage: () => CoverageReport;
|
|
34
|
+
send(event: {
|
|
35
|
+
id: string;
|
|
36
|
+
} & Record<string, unknown>): void;
|
|
37
|
+
state: AnyActor["state"];
|
|
38
|
+
snapshot(): Snapshot;
|
|
39
|
+
context: Record<string, unknown> | undefined;
|
|
40
|
+
assertAllStatesVisited(): void;
|
|
41
|
+
assertAllTransitionsVisited(): void;
|
|
42
|
+
assertStateVisited(stateName: string): void;
|
|
43
|
+
assertStateNeverVisited(stateName: string): void;
|
|
44
|
+
assertTransitionVisited(from: string, event: string): void;
|
|
45
|
+
assertTransitionNeverVisited(from: string, event: string): void;
|
|
46
|
+
assertContextNever(predicate: (ctx: unknown) => boolean): void;
|
|
47
|
+
assertEffectRan(stateName: string): void;
|
|
48
|
+
assertEffectNeverRan(stateName: string): void;
|
|
49
|
+
assertReachedState(stateName: string): void;
|
|
50
|
+
assertNeverReachedState(stateName: string): void;
|
|
51
|
+
wasStateVisited(stateName: string): boolean;
|
|
52
|
+
wasTransitionVisited(from: string, event: string): boolean;
|
|
53
|
+
wasEffectRun(stateName: string): boolean;
|
|
54
|
+
reset(): void;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/harness.d.ts
|
|
58
|
+
declare function createTestHarness(actor: AnyActor): TestHarness;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/coverage.d.ts
|
|
61
|
+
declare function computeCoverage(graph: ActorGraph, history: History): CoverageReport;
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/assertions.d.ts
|
|
64
|
+
declare function assertAllStatesVisited(graph: ActorGraph, history: History): void;
|
|
65
|
+
declare function assertAllTransitionsVisited(graph: ActorGraph, history: History): void;
|
|
66
|
+
declare function assertStateVisited(history: History, stateName: string): void;
|
|
67
|
+
declare function assertStateNeverVisited(history: History, stateName: string): void;
|
|
68
|
+
declare function assertTransitionVisited(history: History, from: string, event: string): void;
|
|
69
|
+
declare function assertTransitionNeverVisited(history: History, from: string, event: string): void;
|
|
70
|
+
declare function assertContextNever(actor: AnyActor, predicate: (ctx: unknown) => boolean): void;
|
|
71
|
+
declare function assertEffectRan(history: History, stateName: string): void;
|
|
72
|
+
declare function assertEffectNeverRan(history: History, stateName: string): void;
|
|
73
|
+
declare function assertReachedState(history: History, stateName: string): void;
|
|
74
|
+
declare function assertNeverReachedState(history: History, stateName: string): void;
|
|
75
|
+
//#endregion
|
|
76
|
+
export { type CoverageReport, type TestHarness, assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan, assertNeverReachedState, assertReachedState, assertStateNeverVisited, assertStateVisited, assertTransitionNeverVisited, assertTransitionVisited, computeCoverage, createTestHarness };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { buildGraph, instrument } from "@mantaq/traversal";
|
|
2
|
+
//#region src/coverage.ts
|
|
3
|
+
function computeCoverage(graph, history) {
|
|
4
|
+
const visitedStates = history.visitedStates();
|
|
5
|
+
const firedTransitions = history.firedTransitions();
|
|
6
|
+
const effects = history.effects();
|
|
7
|
+
const graphNodes = graph.nodes.filter((n) => n.id !== "__initial__");
|
|
8
|
+
const graphEdges = graph.edges.filter((e) => e.source !== "__initial__" && e.target !== "__initial__" && !e.isUndetermined && !e.isInternal);
|
|
9
|
+
const statesTotal = graphNodes.length;
|
|
10
|
+
const statesVisited = graphNodes.filter((n) => visitedStates.has(n.label)).length;
|
|
11
|
+
const statesUncovered = graphNodes.filter((n) => !visitedStates.has(n.label)).map((n) => n.label);
|
|
12
|
+
const transitionsTotal = graphEdges.length;
|
|
13
|
+
const transitionsVisited = graphEdges.filter((e) => firedTransitions.has(`${e.source}:${e.label}`)).length;
|
|
14
|
+
const transitionsUncovered = graphEdges.filter((e) => !firedTransitions.has(`${e.source}:${e.label}`)).map((e) => ({
|
|
15
|
+
from: e.source,
|
|
16
|
+
event: e.label
|
|
17
|
+
}));
|
|
18
|
+
const effectEdges = graph.edges.filter((e) => e.isInternal && e.source === e.target && e.label.startsWith("effect:"));
|
|
19
|
+
const effectStatesFromGraph = new Set(effectEdges.map((e) => e.source));
|
|
20
|
+
const effectsRan = new Set(effects.map((e) => e.stateName));
|
|
21
|
+
const effectsUnexecuted = [...effectStatesFromGraph].filter((s) => !effectsRan.has(s));
|
|
22
|
+
return {
|
|
23
|
+
states: {
|
|
24
|
+
total: statesTotal,
|
|
25
|
+
visited: statesVisited,
|
|
26
|
+
uncovered: statesUncovered
|
|
27
|
+
},
|
|
28
|
+
transitions: {
|
|
29
|
+
total: transitionsTotal,
|
|
30
|
+
visited: transitionsVisited,
|
|
31
|
+
uncovered: transitionsUncovered
|
|
32
|
+
},
|
|
33
|
+
effects: {
|
|
34
|
+
total: effectStatesFromGraph.size,
|
|
35
|
+
ran: effectsRan.size,
|
|
36
|
+
unexecuted: effectsUnexecuted
|
|
37
|
+
},
|
|
38
|
+
percent: {
|
|
39
|
+
states: statesTotal === 0 ? 0 : statesVisited / statesTotal * 100,
|
|
40
|
+
transitions: transitionsTotal === 0 ? 0 : transitionsVisited / transitionsTotal * 100,
|
|
41
|
+
effects: effectStatesFromGraph.size === 0 ? 0 : effectsRan.size / effectStatesFromGraph.size * 100
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/assertions.ts
|
|
47
|
+
function assertAllStatesVisited(graph, history) {
|
|
48
|
+
const visited = history.visitedStates();
|
|
49
|
+
const missing = graph.nodes.filter((n) => n.id !== "__initial__").filter((n) => !visited.has(n.label)).map((n) => n.label);
|
|
50
|
+
if (missing.length > 0) throw new Error(`assertAllStatesVisited failed: states not visited: ${missing.join(", ")}`);
|
|
51
|
+
}
|
|
52
|
+
function assertAllTransitionsVisited(graph, history) {
|
|
53
|
+
const fired = history.firedTransitions();
|
|
54
|
+
const missing = graph.edges.filter((e) => e.source !== "__initial__" && e.target !== "__initial__" && !e.isUndetermined && !e.isInternal).filter((e) => !fired.has(`${e.source}:${e.label}`));
|
|
55
|
+
if (missing.length > 0) {
|
|
56
|
+
const list = missing.map((e) => `${e.source}:${e.label}`).join(", ");
|
|
57
|
+
throw new Error(`assertAllTransitionsVisited failed: transitions not visited: ${list}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function assertStateVisited(history, stateName) {
|
|
61
|
+
if (!history.visitedStates().has(stateName)) throw new Error(`assertStateVisited failed: state "${stateName}" was not visited`);
|
|
62
|
+
}
|
|
63
|
+
function assertStateNeverVisited(history, stateName) {
|
|
64
|
+
if (history.visitedStates().has(stateName)) throw new Error(`assertStateNeverVisited failed: state "${stateName}" was visited`);
|
|
65
|
+
}
|
|
66
|
+
function assertTransitionVisited(history, from, event) {
|
|
67
|
+
if (!history.firedTransitions().has(`${from}:${event}`)) throw new Error(`assertTransitionVisited failed: transition "${from}:${event}" was not visited`);
|
|
68
|
+
}
|
|
69
|
+
function assertTransitionNeverVisited(history, from, event) {
|
|
70
|
+
if (history.firedTransitions().has(`${from}:${event}`)) throw new Error(`assertTransitionNeverVisited failed: transition "${from}:${event}" was visited`);
|
|
71
|
+
}
|
|
72
|
+
function assertContextNever(actor, predicate) {
|
|
73
|
+
if (predicate(actor.context)) throw new Error("assertContextNever failed: predicate matched current context");
|
|
74
|
+
}
|
|
75
|
+
function assertEffectRan(history, stateName) {
|
|
76
|
+
if (!history.effects().some((e) => e.stateName === stateName)) throw new Error(`assertEffectRan failed: effect for state "${stateName}" did not run`);
|
|
77
|
+
}
|
|
78
|
+
function assertEffectNeverRan(history, stateName) {
|
|
79
|
+
if (history.effects().some((e) => e.stateName === stateName)) throw new Error(`assertEffectNeverRan failed: effect for state "${stateName}" ran`);
|
|
80
|
+
}
|
|
81
|
+
function assertReachedState(history, stateName) {
|
|
82
|
+
assertStateVisited(history, stateName);
|
|
83
|
+
}
|
|
84
|
+
function assertNeverReachedState(history, stateName) {
|
|
85
|
+
assertStateNeverVisited(history, stateName);
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/harness.ts
|
|
89
|
+
function createTestHarness(actor) {
|
|
90
|
+
const instrumented = instrument(actor);
|
|
91
|
+
const internalIds = new Set((actor.options?.internal ?? []).map((e) => e.id));
|
|
92
|
+
const graph = buildGraph(actor, { internalIds });
|
|
93
|
+
return {
|
|
94
|
+
actor: instrumented,
|
|
95
|
+
graph,
|
|
96
|
+
history: instrumented.history,
|
|
97
|
+
coverage: () => computeCoverage(graph, instrumented.history),
|
|
98
|
+
send(event) {
|
|
99
|
+
instrumented.send(event);
|
|
100
|
+
},
|
|
101
|
+
get state() {
|
|
102
|
+
return instrumented.state;
|
|
103
|
+
},
|
|
104
|
+
snapshot() {
|
|
105
|
+
return instrumented.snapshot();
|
|
106
|
+
},
|
|
107
|
+
get context() {
|
|
108
|
+
return instrumented.context;
|
|
109
|
+
},
|
|
110
|
+
assertAllStatesVisited: () => assertAllStatesVisited(graph, instrumented.history),
|
|
111
|
+
assertAllTransitionsVisited: () => assertAllTransitionsVisited(graph, instrumented.history),
|
|
112
|
+
assertStateVisited: (name) => assertStateVisited(instrumented.history, name),
|
|
113
|
+
assertStateNeverVisited: (name) => assertStateNeverVisited(instrumented.history, name),
|
|
114
|
+
assertTransitionVisited: (from, event) => assertTransitionVisited(instrumented.history, from, event),
|
|
115
|
+
assertTransitionNeverVisited: (from, event) => assertTransitionNeverVisited(instrumented.history, from, event),
|
|
116
|
+
assertContextNever: (predicate) => assertContextNever(instrumented, predicate),
|
|
117
|
+
assertEffectRan: (name) => assertEffectRan(instrumented.history, name),
|
|
118
|
+
assertEffectNeverRan: (name) => assertEffectNeverRan(instrumented.history, name),
|
|
119
|
+
assertReachedState: (name) => assertReachedState(instrumented.history, name),
|
|
120
|
+
assertNeverReachedState: (name) => assertNeverReachedState(instrumented.history, name),
|
|
121
|
+
wasStateVisited: (name) => instrumented.history.visitedStates().has(name),
|
|
122
|
+
wasTransitionVisited: (from, event) => instrumented.history.firedTransitions().has(`${from}:${event}`),
|
|
123
|
+
wasEffectRun: (name) => instrumented.history.effects().some((e) => e.stateName === name),
|
|
124
|
+
reset: () => instrumented.history.reset()
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
export { assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan, assertNeverReachedState, assertReachedState, assertStateNeverVisited, assertStateVisited, assertTransitionNeverVisited, assertTransitionVisited, computeCoverage, createTestHarness };
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mantaq/test",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Test harness and assertion utilities for verifying mantaq actor behavior.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/AndersCan/mantaq",
|
|
9
|
+
"directory": "packages/test"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./dist/index.mjs",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@mantaq/core": "0.0.1",
|
|
24
|
+
"@mantaq/traversal": "0.0.1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^26.2.0",
|
|
28
|
+
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
29
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
30
|
+
"typescript": "^7.0.2",
|
|
31
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.8",
|
|
32
|
+
"vite-plus": "0.2.8"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "vp pack",
|
|
36
|
+
"dev": "vp pack --watch",
|
|
37
|
+
"test": "vp test",
|
|
38
|
+
"check": "vp check",
|
|
39
|
+
"coverage": "vp test run --coverage"
|
|
40
|
+
}
|
|
41
|
+
}
|