@mantaq/test 0.1.2 → 2.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 +69 -33
- package/dist/index.mjs +107 -79
- package/package.json +8 -8
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ActorGraph, History } from "@mantaq/traversal";
|
|
2
2
|
import { AnyActor, Snapshot } from "@mantaq/core";
|
|
3
|
-
//#region src/types.d.ts
|
|
4
3
|
interface CoverageReport {
|
|
5
4
|
states: {
|
|
6
5
|
total: number;
|
|
@@ -40,38 +39,75 @@ interface TestHarness<C = Record<string, unknown>> {
|
|
|
40
39
|
context: C | undefined;
|
|
41
40
|
assertAllStatesVisited(): void;
|
|
42
41
|
assertAllTransitionsVisited(): void;
|
|
43
|
-
assertStateVisited(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
42
|
+
assertStateVisited(target: {
|
|
43
|
+
stateName: string;
|
|
44
|
+
}): void;
|
|
45
|
+
assertStateNeverVisited(banned: {
|
|
46
|
+
stateName: string;
|
|
47
|
+
}): void;
|
|
48
|
+
assertTransitionVisited(expected: {
|
|
49
|
+
from: string;
|
|
50
|
+
event: string;
|
|
51
|
+
}): void;
|
|
52
|
+
assertTransitionNeverVisited(banned: {
|
|
53
|
+
from: string;
|
|
54
|
+
event: string;
|
|
55
|
+
}): void;
|
|
56
|
+
assertContextNever(check: {
|
|
57
|
+
predicate: (context: unknown) => boolean;
|
|
58
|
+
}): void;
|
|
59
|
+
assertEffectRan(expected: {
|
|
60
|
+
stateName: string;
|
|
61
|
+
effectName: string;
|
|
62
|
+
}): void;
|
|
63
|
+
assertEffectNeverRan(banned: {
|
|
64
|
+
stateName: string;
|
|
65
|
+
effectName: string;
|
|
66
|
+
}): void;
|
|
52
67
|
wasStateVisited(stateName: string): boolean;
|
|
53
|
-
wasTransitionVisited(
|
|
54
|
-
|
|
68
|
+
wasTransitionVisited(expected: {
|
|
69
|
+
from: string;
|
|
70
|
+
event: string;
|
|
71
|
+
}): boolean;
|
|
72
|
+
wasEffectRun(queried: {
|
|
73
|
+
stateName: string;
|
|
74
|
+
effectName: string;
|
|
75
|
+
}): boolean;
|
|
55
76
|
reset(): void;
|
|
56
77
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
declare function assertStateVisited(history: History,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
declare function
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
declare function
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
export
|
|
78
|
+
export declare function createTestHarness<C>(actor: AnyActor<C>): TestHarness<C>;
|
|
79
|
+
export declare function computeCoverage(graph: ActorGraph, tracked: {
|
|
80
|
+
history: History;
|
|
81
|
+
}): CoverageReport;
|
|
82
|
+
export declare function assertAllStatesVisited(graph: ActorGraph, checked: {
|
|
83
|
+
history: History;
|
|
84
|
+
}): void;
|
|
85
|
+
export declare function assertAllTransitionsVisited(graph: ActorGraph, checked: {
|
|
86
|
+
history: History;
|
|
87
|
+
}): void;
|
|
88
|
+
export declare function assertStateVisited(history: History, target: {
|
|
89
|
+
stateName: string;
|
|
90
|
+
}): void;
|
|
91
|
+
export declare function assertStateNeverVisited(history: History, banned: {
|
|
92
|
+
stateName: string;
|
|
93
|
+
}): void;
|
|
94
|
+
export declare function assertTransitionVisited(history: History, expected: {
|
|
95
|
+
from: string;
|
|
96
|
+
event: string;
|
|
97
|
+
}): void;
|
|
98
|
+
export declare function assertTransitionNeverVisited(history: History, banned: {
|
|
99
|
+
from: string;
|
|
100
|
+
event: string;
|
|
101
|
+
}): void;
|
|
102
|
+
export declare function assertContextNever<C>(actor: AnyActor<C>, check: {
|
|
103
|
+
predicate: (context: unknown) => boolean;
|
|
104
|
+
}): void;
|
|
105
|
+
export declare function assertEffectRan(history: History, expected: {
|
|
106
|
+
stateName: string;
|
|
107
|
+
effectName: string;
|
|
108
|
+
}): void;
|
|
109
|
+
export declare function assertEffectNeverRan(history: History, banned: {
|
|
110
|
+
stateName: string;
|
|
111
|
+
effectName: string;
|
|
112
|
+
}): void;
|
|
113
|
+
export type { CoverageReport, TestHarness };
|
package/dist/index.mjs
CHANGED
|
@@ -1,24 +1,94 @@
|
|
|
1
|
-
import { buildGraph, instrument } from "@mantaq/traversal";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { INITIAL_NODE_ID, buildGraph, instrument, isAnyNode, parseInternalEventIds } from "@mantaq/traversal";
|
|
2
|
+
// 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
|
+
// src/coverage.ts
|
|
71
|
+
function computeCoverage(graph, tracked) {
|
|
72
|
+
const visitedStates = tracked.history.visitedStates();
|
|
73
|
+
const firedTransitions = tracked.history.firedTransitions();
|
|
74
|
+
const effects = tracked.history.effects();
|
|
75
|
+
const firedRecords = tracked.history.transitions();
|
|
76
|
+
const isFired = (edge) => isAnyNode(edge.source) ? firedRecords.some((record) => record.event === edge.label && isInAnyScope(record.from, edge.source)) : firedTransitions.has(`${edge.source}:${edge.label}`);
|
|
77
|
+
const graphNodes = graph.nodes.filter((node) => node.id !== INITIAL_NODE_ID && !isAnyNode(node.id));
|
|
78
|
+
const graphEdges = graph.edges.filter((edge) => edge.source !== INITIAL_NODE_ID && edge.target !== INITIAL_NODE_ID && !edge.isUndetermined && !edge.isInternal);
|
|
9
79
|
const statesTotal = graphNodes.length;
|
|
10
|
-
const statesVisited = graphNodes.filter((
|
|
11
|
-
const statesUncovered = graphNodes.filter((
|
|
80
|
+
const statesVisited = graphNodes.filter((node) => visitedStates.has(node.id)).length;
|
|
81
|
+
const statesUncovered = graphNodes.filter((node) => !visitedStates.has(node.id)).map((node) => node.id);
|
|
12
82
|
const transitionsTotal = graphEdges.length;
|
|
13
|
-
const transitionsVisited = graphEdges.filter(
|
|
14
|
-
const transitionsUncovered = graphEdges.filter((
|
|
15
|
-
from:
|
|
16
|
-
event:
|
|
83
|
+
const transitionsVisited = graphEdges.filter(isFired).length;
|
|
84
|
+
const transitionsUncovered = graphEdges.filter((edge) => !isFired(edge)).map((edge) => ({
|
|
85
|
+
from: edge.source,
|
|
86
|
+
event: edge.label
|
|
17
87
|
}));
|
|
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((
|
|
88
|
+
const effectEdges = graph.edges.filter((edge) => edge.isInternal && edge.source === edge.target && edge.label.startsWith("effect:"));
|
|
89
|
+
const effectStatesFromGraph = new Set(effectEdges.map((edge) => edge.source));
|
|
90
|
+
const effectsRan = new Set(effects.map((effect) => effect.stateName));
|
|
91
|
+
const effectsUnexecuted = [...effectStatesFromGraph].filter((stateName) => !effectsRan.has(stateName));
|
|
22
92
|
return {
|
|
23
93
|
states: {
|
|
24
94
|
total: statesTotal,
|
|
@@ -42,59 +112,20 @@ function computeCoverage(graph, history) {
|
|
|
42
112
|
}
|
|
43
113
|
};
|
|
44
114
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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);
|
|
115
|
+
function isInAnyScope(from, anyNodeId) {
|
|
116
|
+
const prefix = anyNodeId.slice(0, -1);
|
|
117
|
+
return from.startsWith(prefix) && !from.slice(prefix.length).includes(".");
|
|
86
118
|
}
|
|
87
|
-
|
|
88
|
-
//#region src/harness.ts
|
|
119
|
+
// src/harness.ts
|
|
89
120
|
function createTestHarness(actor) {
|
|
90
121
|
const instrumented = instrument(actor);
|
|
91
|
-
const internalTypes =
|
|
122
|
+
const internalTypes = parseInternalEventIds(actor.options);
|
|
92
123
|
const graph = buildGraph(actor, { internalIds: internalTypes });
|
|
93
124
|
return {
|
|
94
125
|
actor: instrumented,
|
|
95
126
|
graph,
|
|
96
127
|
history: instrumented.history,
|
|
97
|
-
coverage: () => computeCoverage(graph, instrumented.history),
|
|
128
|
+
coverage: () => computeCoverage(graph, { history: instrumented.history }),
|
|
98
129
|
send(event) {
|
|
99
130
|
instrumented.send(event);
|
|
100
131
|
},
|
|
@@ -107,22 +138,19 @@ function createTestHarness(actor) {
|
|
|
107
138
|
get context() {
|
|
108
139
|
return instrumented.context;
|
|
109
140
|
},
|
|
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),
|
|
141
|
+
assertAllStatesVisited: () => assertAllStatesVisited(graph, { history: instrumented.history }),
|
|
142
|
+
assertAllTransitionsVisited: () => assertAllTransitionsVisited(graph, { history: instrumented.history }),
|
|
143
|
+
assertStateVisited: (target) => assertStateVisited(instrumented.history, target),
|
|
144
|
+
assertStateNeverVisited: (banned) => assertStateNeverVisited(instrumented.history, banned),
|
|
145
|
+
assertTransitionVisited: (expected) => assertTransitionVisited(instrumented.history, expected),
|
|
146
|
+
assertTransitionNeverVisited: (banned) => assertTransitionNeverVisited(instrumented.history, banned),
|
|
147
|
+
assertContextNever: (check) => assertContextNever(instrumented, check),
|
|
148
|
+
assertEffectRan: (expected) => assertEffectRan(instrumented.history, expected),
|
|
149
|
+
assertEffectNeverRan: (banned) => assertEffectNeverRan(instrumented.history, banned),
|
|
150
|
+
wasStateVisited: (stateName) => instrumented.history.visitedStates().has(stateName),
|
|
151
|
+
wasTransitionVisited: (expected) => instrumented.history.firedTransitions().has(`${expected.from}:${expected.event}`),
|
|
152
|
+
wasEffectRun: (queried) => instrumented.history.effects().some((effect) => effect.stateName === queried.stateName && effect.effectName === queried.effectName),
|
|
124
153
|
reset: () => instrumented.history.reset()
|
|
125
154
|
};
|
|
126
155
|
}
|
|
127
|
-
|
|
128
|
-
export { assertAllStatesVisited, assertAllTransitionsVisited, assertContextNever, assertEffectNeverRan, assertEffectRan, assertNeverReachedState, assertReachedState, assertStateNeverVisited, assertStateVisited, assertTransitionNeverVisited, assertTransitionVisited, computeCoverage, createTestHarness };
|
|
156
|
+
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": "2.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,16 +20,16 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@mantaq/core": "0.
|
|
24
|
-
"@mantaq/traversal": "0.
|
|
23
|
+
"@mantaq/core": "0.5.0",
|
|
24
|
+
"@mantaq/traversal": "0.3.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
|
-
"@vitest/coverage-v8": "
|
|
30
|
-
"typescript": "
|
|
31
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@
|
|
32
|
-
"vite-plus": "
|
|
29
|
+
"@vitest/coverage-v8": "5.0.5",
|
|
30
|
+
"typescript": "6.0.3",
|
|
31
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
32
|
+
"vite-plus": "1.4.0"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "vp pack",
|