@telorun/assert 0.1.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 +17 -0
- package/dist/events.d.ts +17 -0
- package/dist/events.js +123 -0
- package/dist/schema.d.ts +12 -0
- package/dist/schema.js +15 -0
- package/dist/value.js +3 -0
- package/package.json +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SUSTAINABLE USE LICENSE (Fair-code)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DiglyAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, and distribute the Software for any purpose—including commercial purposes—subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
1. ANTI-COMPETITION RESTRICTION: The Software may not be provided to third parties as a managed service, commercial SaaS (Software-as-a-Service), PaaS (Platform-as-a-Service), BaaS (Backend-as-a-Service), or similar offering where the primary value provided to the user is the functionality of the Software itself, without a separate commercial license from the copyright holder.
|
|
8
|
+
|
|
9
|
+
2. PERMITTED COMMERCIAL USE: You are free to use the Software to build, host, and monetize your own commercial applications, products, and services, provided such use does not violate Clause 1.
|
|
10
|
+
|
|
11
|
+
3. ATTRIBUTION: This copyright notice and license must be included in all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
4. CONTRIBUTIONS: Contributions to the Software are welcome and encouraged. By contributing, you agree that your contributions may be incorporated into the Software and distributed under this license.
|
|
14
|
+
|
|
15
|
+
5. DISCLAIMER: The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
|
|
16
|
+
|
|
17
|
+
For commercial licensing, managed hosting exemptions, or enterprise inquiries, please contact DiglyAI.
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Static } from "@sinclair/typebox";
|
|
2
|
+
import { ResourceContext } from "@telorun/sdk";
|
|
3
|
+
export declare const schema: import("@sinclair/typebox").TObject<{
|
|
4
|
+
metadata: import("@sinclair/typebox").TObject<{
|
|
5
|
+
name: import("@sinclair/typebox").TString;
|
|
6
|
+
}>;
|
|
7
|
+
filter: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
8
|
+
type: import("@sinclair/typebox").TString;
|
|
9
|
+
}>>>;
|
|
10
|
+
expect: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
|
|
11
|
+
event: import("@sinclair/typebox").TString;
|
|
12
|
+
payload: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TAny>>;
|
|
13
|
+
}>>;
|
|
14
|
+
}>;
|
|
15
|
+
type AssertManifest = Static<typeof schema>;
|
|
16
|
+
export declare function create(manifest: AssertManifest, ctx: ResourceContext): Promise<{}>;
|
|
17
|
+
export {};
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
const FilterEntry = Type.Object({
|
|
3
|
+
type: Type.String(),
|
|
4
|
+
});
|
|
5
|
+
const ExpectEntry = Type.Object({
|
|
6
|
+
event: Type.String(),
|
|
7
|
+
payload: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
8
|
+
});
|
|
9
|
+
export const schema = Type.Object({
|
|
10
|
+
metadata: Type.Object({
|
|
11
|
+
name: Type.String(),
|
|
12
|
+
}),
|
|
13
|
+
filter: Type.Optional(Type.Array(FilterEntry)),
|
|
14
|
+
expect: Type.Array(ExpectEntry),
|
|
15
|
+
});
|
|
16
|
+
function matchesPattern(pattern, eventName) {
|
|
17
|
+
if (pattern === "*")
|
|
18
|
+
return true;
|
|
19
|
+
if (pattern === eventName)
|
|
20
|
+
return true;
|
|
21
|
+
if (!pattern.includes("*"))
|
|
22
|
+
return false;
|
|
23
|
+
const patternParts = pattern.split(".");
|
|
24
|
+
const eventParts = eventName.split(".");
|
|
25
|
+
if (patternParts.length !== eventParts.length)
|
|
26
|
+
return false;
|
|
27
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
28
|
+
if (patternParts[i] !== "*" && patternParts[i] !== eventParts[i])
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
function matchesPayload(actual, expected) {
|
|
34
|
+
for (const [key, value] of Object.entries(expected)) {
|
|
35
|
+
if (actual == null)
|
|
36
|
+
return false;
|
|
37
|
+
if (typeof value === "object" && value !== null) {
|
|
38
|
+
if (!matchesPayload(actual[key], value))
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
if (actual[key] !== value)
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const useColor = process.stderr.isTTY;
|
|
49
|
+
const c = (code, text) => (useColor ? `\x1b[${code}m${text}\x1b[0m` : text);
|
|
50
|
+
const bold = (t) => c("1", t);
|
|
51
|
+
const red = (t) => c("31", t);
|
|
52
|
+
const green = (t) => c("32", t);
|
|
53
|
+
const yellow = (t) => c("33", t);
|
|
54
|
+
const dim = (t) => c("2", t);
|
|
55
|
+
function buildReport(name, captured, expect) {
|
|
56
|
+
const results = [];
|
|
57
|
+
let pos = 0;
|
|
58
|
+
for (const entry of expect) {
|
|
59
|
+
let found = false;
|
|
60
|
+
while (pos < captured.length) {
|
|
61
|
+
const ev = captured[pos++];
|
|
62
|
+
if (matchesPattern(entry.event, ev.name)) {
|
|
63
|
+
if (!entry.payload || matchesPayload(ev.payload, entry.payload)) {
|
|
64
|
+
results.push({ status: "matched", entry, actual: ev });
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
results.push({ status: "payload-mismatch", entry, actual: ev });
|
|
68
|
+
}
|
|
69
|
+
found = true;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (!found) {
|
|
74
|
+
results.push({ status: "not-found", entry });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const failures = results.filter((r) => r.status !== "matched");
|
|
78
|
+
// if (failures.length === 0) {
|
|
79
|
+
// return null;
|
|
80
|
+
// }
|
|
81
|
+
let report = bold(failures.length > 0
|
|
82
|
+
? red(`Assert.Events.${name}: assertion failed`)
|
|
83
|
+
: green(`Assert.Events.${name}: assertion passed`)) + "\n";
|
|
84
|
+
for (const result of results) {
|
|
85
|
+
if (result.status === "matched") {
|
|
86
|
+
report += ` ${green("✓")} ${dim(result.actual.name)}\n`;
|
|
87
|
+
}
|
|
88
|
+
else if (result.status === "not-found") {
|
|
89
|
+
report += ` ${red("✗")} ${result.entry.event} ${dim("← not found in stream")}\n`;
|
|
90
|
+
if (result.entry.payload) {
|
|
91
|
+
report += ` ${dim("expected payload:")} ${yellow(JSON.stringify(result.entry.payload))}\n`;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (result.status === "payload-mismatch") {
|
|
95
|
+
report += ` ${red("✗")} ${result.actual.name}\n`;
|
|
96
|
+
report += ` ${dim("expected payload:")} ${yellow(JSON.stringify(result.entry.payload))}\n`;
|
|
97
|
+
report += ` ${dim("actual payload: ")} ${red(JSON.stringify(result.actual.payload))}\n`;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { report, passed: failures.length === 0 };
|
|
101
|
+
}
|
|
102
|
+
export async function create(manifest, ctx) {
|
|
103
|
+
const captured = [];
|
|
104
|
+
const filters = manifest.filter ?? [{ type: "*" }];
|
|
105
|
+
ctx.on("*", (event) => {
|
|
106
|
+
if (filters.some((f) => matchesPattern(f.type, event.name))) {
|
|
107
|
+
captured.push({ name: event.name, payload: event.payload });
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
ctx.on("Runtime.Stopping", () => {
|
|
111
|
+
const report = buildReport(manifest.metadata.name, captured, manifest.expect);
|
|
112
|
+
if (report) {
|
|
113
|
+
if (report.passed) {
|
|
114
|
+
process.stdout.write(report.report);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
process.stderr.write(report.report);
|
|
118
|
+
ctx.requestExit(1);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return {};
|
|
123
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ResourceContext } from "@telorun/sdk";
|
|
2
|
+
import { Static } from "@sinclair/typebox";
|
|
3
|
+
export declare const schema: import("@sinclair/typebox").TObject<{
|
|
4
|
+
schema: import("@sinclair/typebox").TObject<{
|
|
5
|
+
type: import("@sinclair/typebox").TString;
|
|
6
|
+
}>;
|
|
7
|
+
}>;
|
|
8
|
+
type AssertManifest = Static<typeof schema>;
|
|
9
|
+
export declare function create(manifest: AssertManifest, ctx: ResourceContext): Promise<{
|
|
10
|
+
invoke: (data: any) => boolean;
|
|
11
|
+
}>;
|
|
12
|
+
export {};
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
export const schema = Type.Object({
|
|
3
|
+
schema: Type.Object({
|
|
4
|
+
type: Type.String(),
|
|
5
|
+
}),
|
|
6
|
+
}, {});
|
|
7
|
+
export async function create(manifest, ctx) {
|
|
8
|
+
const validator = ctx.createSchemaValidator(manifest.schema);
|
|
9
|
+
return {
|
|
10
|
+
invoke: (data) => {
|
|
11
|
+
validator.validate(data);
|
|
12
|
+
return true;
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
package/dist/value.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@telorun/assert",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./schema": "./src/schema.ts",
|
|
7
|
+
"./events": "./src/events.ts"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/**"
|
|
11
|
+
],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@sinclair/typebox": "^0.34.48",
|
|
14
|
+
"@telorun/sdk": "0.2.2"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/node": "^20.0.0",
|
|
18
|
+
"typescript": "^5.0.0"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -p tsconfig.lib.json"
|
|
22
|
+
}
|
|
23
|
+
}
|