@intent-framework/testing 0.1.0-alpha.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 +21 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +75 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mahyar
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ScreenDefinition, DefaultScreenServices } from "@intent-framework/core";
|
|
2
|
+
export type ScreenHandle<TServices extends object = DefaultScreenServices> = {
|
|
3
|
+
act(label: string): {
|
|
4
|
+
toBeEnabled(): void;
|
|
5
|
+
toBeBlocked(): void;
|
|
6
|
+
toBeBlockedBy(...reasons: string[]): void;
|
|
7
|
+
run(): Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
answer(label: string, value: string): Promise<void>;
|
|
10
|
+
feedback(): string | null;
|
|
11
|
+
state(): ScreenDefinition<TServices>;
|
|
12
|
+
resource(name: string): {
|
|
13
|
+
status(): string;
|
|
14
|
+
load(): Promise<void>;
|
|
15
|
+
reload(): Promise<void>;
|
|
16
|
+
invalidate(): void;
|
|
17
|
+
stale(): boolean;
|
|
18
|
+
};
|
|
19
|
+
start(): Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
export type TestScreenOptions<TServices extends object = DefaultScreenServices> = {
|
|
22
|
+
services?: TServices;
|
|
23
|
+
};
|
|
24
|
+
export declare function testScreen<TServices extends object = DefaultScreenServices>(name: string | ScreenDefinition<TServices>, fn: (screen: ScreenHandle<TServices>) => Promise<void>, options?: TestScreenOptions<TServices>): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createScreenRuntime } from "@intent-framework/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
async function testScreen(name, fn, options) {
|
|
5
|
+
const screenDef = typeof name === "string" ? {
|
|
6
|
+
name,
|
|
7
|
+
asks: [],
|
|
8
|
+
acts: [],
|
|
9
|
+
flows: [],
|
|
10
|
+
surfaces: [],
|
|
11
|
+
resources: []
|
|
12
|
+
} : name;
|
|
13
|
+
const runtime = createScreenRuntime(screenDef, { services: options?.services });
|
|
14
|
+
const handle = {
|
|
15
|
+
act(label) {
|
|
16
|
+
const actNode = screenDef.acts.find((a) => a.label.toLowerCase() === label.toLowerCase());
|
|
17
|
+
if (!actNode) throw new Error(`Act "${label}" not found. Available acts: ${screenDef.acts.map((a) => a.label).join(", ")}`);
|
|
18
|
+
return {
|
|
19
|
+
toBeEnabled() {
|
|
20
|
+
if (!actNode.enabled.current) {
|
|
21
|
+
const reasons = actNode.blockedReasons.length > 0 ? actNode.blockedReasons : actNode.conditions.filter((c) => !c.check()).map((_) => "condition not met");
|
|
22
|
+
throw new Error(`Expected act "${label}" to be enabled but it was blocked.\n Reasons: ${reasons.join(", ")}`);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
toBeBlocked() {
|
|
26
|
+
if (actNode.enabled.current) throw new Error(`Expected act "${label}" to be blocked but it was enabled.`);
|
|
27
|
+
},
|
|
28
|
+
toBeBlockedBy(...reasons) {
|
|
29
|
+
if (actNode.enabled.current) throw new Error(`Expected act "${label}" to be blocked by "${reasons.join(", ")}" but it was enabled.`);
|
|
30
|
+
const actualReasons = actNode.blockedReasons;
|
|
31
|
+
for (const reason of reasons) if (!actualReasons.includes(reason)) throw new Error(`Expected act "${label}" to be blocked by "${reason}" but blocked reasons were: ${JSON.stringify(actualReasons)}`);
|
|
32
|
+
},
|
|
33
|
+
async run() {
|
|
34
|
+
await runtime.executeAct(actNode);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
async answer(label, value) {
|
|
39
|
+
const askNode = screenDef.asks.find((a) => a.label.toLowerCase() === label.toLowerCase());
|
|
40
|
+
if (!askNode) throw new Error(`Ask "${label}" not found. Available asks: ${screenDef.asks.map((a) => a.label).join(", ")}`);
|
|
41
|
+
const stateObj = askNode.state;
|
|
42
|
+
if (typeof stateObj.set === "function") stateObj.set(value);
|
|
43
|
+
},
|
|
44
|
+
resource(name$1) {
|
|
45
|
+
const resourceNode = runtime.resources.find((r) => r.name.toLowerCase() === name$1.toLowerCase());
|
|
46
|
+
if (!resourceNode) throw new Error(`Resource "${name$1}" not found. Available resources: ${runtime.resources.map((r) => r.name).join(", ")}`);
|
|
47
|
+
return {
|
|
48
|
+
status: () => resourceNode.status,
|
|
49
|
+
load: () => resourceNode.load(runtime.getExecutionContext()),
|
|
50
|
+
reload: () => resourceNode.reload(runtime.getExecutionContext()),
|
|
51
|
+
invalidate: () => resourceNode.invalidate(),
|
|
52
|
+
stale: () => resourceNode.stale.current
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
feedback() {
|
|
56
|
+
const actWithFeedback = screenDef.acts.find((a) => a.statusMessage !== null);
|
|
57
|
+
return actWithFeedback?.statusMessage ?? null;
|
|
58
|
+
},
|
|
59
|
+
state() {
|
|
60
|
+
return screenDef;
|
|
61
|
+
},
|
|
62
|
+
start() {
|
|
63
|
+
return runtime.start();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
await runtime.start();
|
|
67
|
+
try {
|
|
68
|
+
await fn(handle);
|
|
69
|
+
} finally {
|
|
70
|
+
runtime.dispose();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
export { testScreen };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@intent-framework/testing",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.1.0-alpha.0",
|
|
7
|
+
"description": "Semantic test harness for Intent screens",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/intent-framework/intent.git",
|
|
12
|
+
"directory": "packages/testing"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@intent-framework/core": "0.1.0-alpha.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsdown": "^0.3.0",
|
|
33
|
+
"typescript": "^5.7.0",
|
|
34
|
+
"vitest": "^3.0.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsdown",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"typecheck": "tsc -b",
|
|
40
|
+
"lint": "tsc -b"
|
|
41
|
+
}
|
|
42
|
+
}
|