@mantaq/test 0.1.2 → 1.0.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/dist/index.d.mts +68 -24
- package/dist/index.mjs +101 -76
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -40,18 +40,40 @@ interface TestHarness<C = Record<string, unknown>> {
|
|
|
40
40
|
context: C | undefined;
|
|
41
41
|
assertAllStatesVisited(): void;
|
|
42
42
|
assertAllTransitionsVisited(): void;
|
|
43
|
-
assertStateVisited(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
43
|
+
assertStateVisited(target: {
|
|
44
|
+
stateName: string;
|
|
45
|
+
}): void;
|
|
46
|
+
assertStateNeverVisited(banned: {
|
|
47
|
+
stateName: string;
|
|
48
|
+
}): void;
|
|
49
|
+
assertTransitionVisited(expected: {
|
|
50
|
+
from: string;
|
|
51
|
+
event: string;
|
|
52
|
+
}): void;
|
|
53
|
+
assertTransitionNeverVisited(banned: {
|
|
54
|
+
from: string;
|
|
55
|
+
event: string;
|
|
56
|
+
}): void;
|
|
57
|
+
assertContextNever(check: {
|
|
58
|
+
predicate: (context: unknown) => boolean;
|
|
59
|
+
}): void;
|
|
60
|
+
assertEffectRan(expected: {
|
|
61
|
+
stateName: string;
|
|
62
|
+
effectName: string;
|
|
63
|
+
}): void;
|
|
64
|
+
assertEffectNeverRan(banned: {
|
|
65
|
+
stateName: string;
|
|
66
|
+
effectName: string;
|
|
67
|
+
}): void;
|
|
52
68
|
wasStateVisited(stateName: string): boolean;
|
|
53
|
-
wasTransitionVisited(
|
|
54
|
-
|
|
69
|
+
wasTransitionVisited(expected: {
|
|
70
|
+
from: string;
|
|
71
|
+
event: string;
|
|
72
|
+
}): boolean;
|
|
73
|
+
wasEffectRun(queried: {
|
|
74
|
+
stateName: string;
|
|
75
|
+
effectName: string;
|
|
76
|
+
}): boolean;
|
|
55
77
|
reset(): void;
|
|
56
78
|
}
|
|
57
79
|
//#endregion
|
|
@@ -59,19 +81,41 @@ interface TestHarness<C = Record<string, unknown>> {
|
|
|
59
81
|
declare function createTestHarness<C>(actor: AnyActor<C>): TestHarness<C>;
|
|
60
82
|
//#endregion
|
|
61
83
|
//#region src/coverage.d.ts
|
|
62
|
-
declare function computeCoverage(graph: ActorGraph,
|
|
84
|
+
declare function computeCoverage(graph: ActorGraph, tracked: {
|
|
85
|
+
history: History;
|
|
86
|
+
}): CoverageReport;
|
|
63
87
|
//#endregion
|
|
64
88
|
//#region src/assertions.d.ts
|
|
65
|
-
declare function assertAllStatesVisited(graph: ActorGraph,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
declare function
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
declare function
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
declare function
|
|
75
|
-
|
|
89
|
+
declare function assertAllStatesVisited(graph: ActorGraph, checked: {
|
|
90
|
+
history: History;
|
|
91
|
+
}): void;
|
|
92
|
+
declare function assertAllTransitionsVisited(graph: ActorGraph, checked: {
|
|
93
|
+
history: History;
|
|
94
|
+
}): void;
|
|
95
|
+
declare function assertStateVisited(history: History, target: {
|
|
96
|
+
stateName: string;
|
|
97
|
+
}): void;
|
|
98
|
+
declare function assertStateNeverVisited(history: History, banned: {
|
|
99
|
+
stateName: string;
|
|
100
|
+
}): void;
|
|
101
|
+
declare function assertTransitionVisited(history: History, expected: {
|
|
102
|
+
from: string;
|
|
103
|
+
event: string;
|
|
104
|
+
}): void;
|
|
105
|
+
declare function assertTransitionNeverVisited(history: History, banned: {
|
|
106
|
+
from: string;
|
|
107
|
+
event: string;
|
|
108
|
+
}): void;
|
|
109
|
+
declare function assertContextNever<C>(actor: AnyActor<C>, check: {
|
|
110
|
+
predicate: (context: unknown) => boolean;
|
|
111
|
+
}): void;
|
|
112
|
+
declare function assertEffectRan(history: History, expected: {
|
|
113
|
+
stateName: string;
|
|
114
|
+
effectName: string;
|
|
115
|
+
}): void;
|
|
116
|
+
declare function assertEffectNeverRan(history: History, banned: {
|
|
117
|
+
stateName: string;
|
|
118
|
+
effectName: string;
|
|
119
|
+
}): void;
|
|
76
120
|
//#endregion
|
|
77
|
-
export { type CoverageReport, type TestHarness, assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan,
|
|
121
|
+
export { type CoverageReport, type TestHarness, assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan, assertStateNeverVisited, assertStateVisited, assertTransitionNeverVisited, assertTransitionVisited, computeCoverage, createTestHarness };
|
package/dist/index.mjs
CHANGED
|
@@ -1,24 +1,93 @@
|
|
|
1
|
-
import { buildGraph, instrument } from "@mantaq/traversal";
|
|
1
|
+
import { INITIAL_NODE_ID, buildGraph, instrument, parseInternalEventIds } from "@mantaq/traversal";
|
|
2
|
+
//#region src/assertions.ts
|
|
3
|
+
/**
|
|
4
|
+
* Assertion-style precondition. A violated assertion is a programmer bug in
|
|
5
|
+
* the test (or the code under test), so it cannot be returned as a value —
|
|
6
|
+
* it explodes the test run, like core's isErrorBomb sites.
|
|
7
|
+
*/
|
|
8
|
+
function isFailed(check) {
|
|
9
|
+
if (!check.holds) throw new Error(check.detail);
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
function assertAllStatesVisited(graph, checked) {
|
|
13
|
+
const visited = checked.history.visitedStates();
|
|
14
|
+
const missing = graph.nodes.filter((node) => node.id !== INITIAL_NODE_ID).filter((node) => !visited.has(node.id)).map((node) => node.id);
|
|
15
|
+
isFailed({
|
|
16
|
+
holds: missing.length === 0,
|
|
17
|
+
detail: `assertAllStatesVisited failed: states not visited: ${missing.join(", ")}`
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function assertAllTransitionsVisited(graph, checked) {
|
|
21
|
+
const fired = checked.history.firedTransitions();
|
|
22
|
+
const missing = graph.edges.filter((edge) => edge.source !== INITIAL_NODE_ID && edge.target !== INITIAL_NODE_ID && !edge.isUndetermined && !edge.isInternal).filter((edge) => !fired.has(`${edge.source}:${edge.label}`));
|
|
23
|
+
isFailed({
|
|
24
|
+
holds: missing.length === 0,
|
|
25
|
+
detail: `assertAllTransitionsVisited failed: transitions not visited: ${missing.map((edge) => `${edge.source}:${edge.label}`).join(", ")}`
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function assertStateVisited(history, target) {
|
|
29
|
+
isFailed({
|
|
30
|
+
holds: history.visitedStates().has(target.stateName),
|
|
31
|
+
detail: `assertStateVisited failed: state "${target.stateName}" was not visited`
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function assertStateNeverVisited(history, banned) {
|
|
35
|
+
isFailed({
|
|
36
|
+
holds: !history.visitedStates().has(banned.stateName),
|
|
37
|
+
detail: `assertStateNeverVisited failed: state "${banned.stateName}" was visited`
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function assertTransitionVisited(history, expected) {
|
|
41
|
+
isFailed({
|
|
42
|
+
holds: history.firedTransitions().has(`${expected.from}:${expected.event}`),
|
|
43
|
+
detail: `assertTransitionVisited failed: transition "${expected.from}:${expected.event}" was not visited`
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function assertTransitionNeverVisited(history, banned) {
|
|
47
|
+
isFailed({
|
|
48
|
+
holds: !history.firedTransitions().has(`${banned.from}:${banned.event}`),
|
|
49
|
+
detail: `assertTransitionNeverVisited failed: transition "${banned.from}:${banned.event}" was visited`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function assertContextNever(actor, check) {
|
|
53
|
+
isFailed({
|
|
54
|
+
holds: !check.predicate(actor.context),
|
|
55
|
+
detail: "assertContextNever failed: predicate matched current context"
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function assertEffectRan(history, expected) {
|
|
59
|
+
isFailed({
|
|
60
|
+
holds: history.effects().some((effect) => effect.stateName === expected.stateName && effect.effectName === expected.effectName),
|
|
61
|
+
detail: `assertEffectRan failed: effect "${expected.effectName}" for state "${expected.stateName}" did not run`
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function assertEffectNeverRan(history, banned) {
|
|
65
|
+
isFailed({
|
|
66
|
+
holds: !history.effects().some((effect) => effect.stateName === banned.stateName && effect.effectName === banned.effectName),
|
|
67
|
+
detail: `assertEffectNeverRan failed: effect "${banned.effectName}" for state "${banned.stateName}" ran`
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
2
71
|
//#region src/coverage.ts
|
|
3
|
-
function computeCoverage(graph,
|
|
4
|
-
const visitedStates = history.visitedStates();
|
|
5
|
-
const firedTransitions = history.firedTransitions();
|
|
6
|
-
const effects = history.effects();
|
|
7
|
-
const graphNodes = graph.nodes.filter((
|
|
8
|
-
const graphEdges = graph.edges.filter((
|
|
72
|
+
function computeCoverage(graph, tracked) {
|
|
73
|
+
const visitedStates = tracked.history.visitedStates();
|
|
74
|
+
const firedTransitions = tracked.history.firedTransitions();
|
|
75
|
+
const effects = tracked.history.effects();
|
|
76
|
+
const graphNodes = graph.nodes.filter((node) => node.id !== INITIAL_NODE_ID);
|
|
77
|
+
const graphEdges = graph.edges.filter((edge) => edge.source !== INITIAL_NODE_ID && edge.target !== INITIAL_NODE_ID && !edge.isUndetermined && !edge.isInternal);
|
|
9
78
|
const statesTotal = graphNodes.length;
|
|
10
|
-
const statesVisited = graphNodes.filter((
|
|
11
|
-
const statesUncovered = graphNodes.filter((
|
|
79
|
+
const statesVisited = graphNodes.filter((node) => visitedStates.has(node.id)).length;
|
|
80
|
+
const statesUncovered = graphNodes.filter((node) => !visitedStates.has(node.id)).map((node) => node.id);
|
|
12
81
|
const transitionsTotal = graphEdges.length;
|
|
13
|
-
const transitionsVisited = graphEdges.filter((
|
|
14
|
-
const transitionsUncovered = graphEdges.filter((
|
|
15
|
-
from:
|
|
16
|
-
event:
|
|
82
|
+
const transitionsVisited = graphEdges.filter((edge) => firedTransitions.has(`${edge.source}:${edge.label}`)).length;
|
|
83
|
+
const transitionsUncovered = graphEdges.filter((edge) => !firedTransitions.has(`${edge.source}:${edge.label}`)).map((edge) => ({
|
|
84
|
+
from: edge.source,
|
|
85
|
+
event: edge.label
|
|
17
86
|
}));
|
|
18
|
-
const effectEdges = graph.edges.filter((
|
|
19
|
-
const effectStatesFromGraph = new Set(effectEdges.map((
|
|
20
|
-
const effectsRan = new Set(effects.map((
|
|
21
|
-
const effectsUnexecuted = [...effectStatesFromGraph].filter((
|
|
87
|
+
const effectEdges = graph.edges.filter((edge) => edge.isInternal && edge.source === edge.target && edge.label.startsWith("effect:"));
|
|
88
|
+
const effectStatesFromGraph = new Set(effectEdges.map((edge) => edge.source));
|
|
89
|
+
const effectsRan = new Set(effects.map((effect) => effect.stateName));
|
|
90
|
+
const effectsUnexecuted = [...effectStatesFromGraph].filter((stateName) => !effectsRan.has(stateName));
|
|
22
91
|
return {
|
|
23
92
|
states: {
|
|
24
93
|
total: statesTotal,
|
|
@@ -43,58 +112,16 @@ function computeCoverage(graph, history) {
|
|
|
43
112
|
};
|
|
44
113
|
}
|
|
45
114
|
//#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
115
|
//#region src/harness.ts
|
|
89
116
|
function createTestHarness(actor) {
|
|
90
117
|
const instrumented = instrument(actor);
|
|
91
|
-
const internalTypes =
|
|
118
|
+
const internalTypes = parseInternalEventIds(actor.options);
|
|
92
119
|
const graph = buildGraph(actor, { internalIds: internalTypes });
|
|
93
120
|
return {
|
|
94
121
|
actor: instrumented,
|
|
95
122
|
graph,
|
|
96
123
|
history: instrumented.history,
|
|
97
|
-
coverage: () => computeCoverage(graph, instrumented.history),
|
|
124
|
+
coverage: () => computeCoverage(graph, { history: instrumented.history }),
|
|
98
125
|
send(event) {
|
|
99
126
|
instrumented.send(event);
|
|
100
127
|
},
|
|
@@ -107,22 +134,20 @@ function createTestHarness(actor) {
|
|
|
107
134
|
get context() {
|
|
108
135
|
return instrumented.context;
|
|
109
136
|
},
|
|
110
|
-
assertAllStatesVisited: () => assertAllStatesVisited(graph, instrumented.history),
|
|
111
|
-
assertAllTransitionsVisited: () => assertAllTransitionsVisited(graph, instrumented.history),
|
|
112
|
-
assertStateVisited: (
|
|
113
|
-
assertStateNeverVisited: (
|
|
114
|
-
assertTransitionVisited: (
|
|
115
|
-
assertTransitionNeverVisited: (
|
|
116
|
-
assertContextNever: (
|
|
117
|
-
assertEffectRan: (
|
|
118
|
-
assertEffectNeverRan: (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
wasTransitionVisited: (from, event) => instrumented.history.firedTransitions().has(`${from}:${event}`),
|
|
123
|
-
wasEffectRun: (name) => instrumented.history.effects().some((e) => e.stateName === name),
|
|
137
|
+
assertAllStatesVisited: () => assertAllStatesVisited(graph, { history: instrumented.history }),
|
|
138
|
+
assertAllTransitionsVisited: () => assertAllTransitionsVisited(graph, { history: instrumented.history }),
|
|
139
|
+
assertStateVisited: (target) => assertStateVisited(instrumented.history, target),
|
|
140
|
+
assertStateNeverVisited: (banned) => assertStateNeverVisited(instrumented.history, banned),
|
|
141
|
+
assertTransitionVisited: (expected) => assertTransitionVisited(instrumented.history, expected),
|
|
142
|
+
assertTransitionNeverVisited: (banned) => assertTransitionNeverVisited(instrumented.history, banned),
|
|
143
|
+
assertContextNever: (check) => assertContextNever(instrumented, check),
|
|
144
|
+
assertEffectRan: (expected) => assertEffectRan(instrumented.history, expected),
|
|
145
|
+
assertEffectNeverRan: (banned) => assertEffectNeverRan(instrumented.history, banned),
|
|
146
|
+
wasStateVisited: (stateName) => instrumented.history.visitedStates().has(stateName),
|
|
147
|
+
wasTransitionVisited: (expected) => instrumented.history.firedTransitions().has(`${expected.from}:${expected.event}`),
|
|
148
|
+
wasEffectRun: (queried) => instrumented.history.effects().some((effect) => effect.stateName === queried.stateName && effect.effectName === queried.effectName),
|
|
124
149
|
reset: () => instrumented.history.reset()
|
|
125
150
|
};
|
|
126
151
|
}
|
|
127
152
|
//#endregion
|
|
128
|
-
export { assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan,
|
|
153
|
+
export { assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan, assertStateNeverVisited, assertStateVisited, assertTransitionNeverVisited, assertTransitionVisited, computeCoverage, createTestHarness };
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mantaq/test",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Test harness and assertion utilities for verifying mantaq actor behavior.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/AndersCan/mantaq",
|
|
9
|
-
"directory": "packages/
|
|
9
|
+
"directory": "packages/testkit"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist"
|
|
@@ -20,14 +20,14 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@mantaq/core": "0.
|
|
24
|
-
"@mantaq/traversal": "0.
|
|
23
|
+
"@mantaq/core": "0.4.0",
|
|
24
|
+
"@mantaq/traversal": "0.2.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^26.2.0",
|
|
28
28
|
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
29
29
|
"@vitest/coverage-v8": "4.1.10",
|
|
30
|
-
"typescript": "
|
|
30
|
+
"typescript": "6.0.3",
|
|
31
31
|
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.8",
|
|
32
32
|
"vite-plus": "0.2.8"
|
|
33
33
|
},
|