@xplane/devtools 0.9.2 → 0.11.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/assertions/index.d.mts +178 -0
- package/dist/assertions/index.d.mts.map +1 -0
- package/dist/assertions/index.mjs +487 -0
- package/dist/assertions/index.mjs.map +1 -0
- package/package.json +7 -7
- package/dist/assertions/index.d.ts +0 -175
- package/dist/assertions/index.js +0 -562
- package/dist/assertions/index.js.map +0 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Composition, KubernetesResource, KubernetesResource as KubernetesResource$1 } from "@xplane/core";
|
|
2
|
+
|
|
3
|
+
//#region src/assertions/match.d.ts
|
|
4
|
+
/** Symbol used to identify Matcher instances. */
|
|
5
|
+
declare const MATCHER: unique symbol;
|
|
6
|
+
/** Result of a match operation. */
|
|
7
|
+
interface MatchResult {
|
|
8
|
+
/** Whether the match succeeded. */
|
|
9
|
+
pass: boolean;
|
|
10
|
+
/** Human-readable failure messages (empty if pass is true). */
|
|
11
|
+
failures: string[];
|
|
12
|
+
}
|
|
13
|
+
/** Interface for custom matchers. */
|
|
14
|
+
interface Matcher {
|
|
15
|
+
readonly [MATCHER]: true;
|
|
16
|
+
test(actual: unknown): MatchResult;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Factory for composable matchers used in assertions.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* template.hasResourceSpec('v1', 'ConfigMap', {
|
|
24
|
+
* data: Match.objectLike({ key: 'value' }),
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare class Match {
|
|
29
|
+
private constructor();
|
|
30
|
+
/** Deep-partial object match — actual may be a superset of pattern. */
|
|
31
|
+
static objectLike(pattern: object): Matcher;
|
|
32
|
+
/** Exact object match — actual must equal pattern exactly (same keys, same values). */
|
|
33
|
+
static objectEquals(pattern: object): Matcher;
|
|
34
|
+
/** Array subset match — items must appear in actual in order. */
|
|
35
|
+
static arrayWith(items: unknown[]): Matcher;
|
|
36
|
+
/** Exact array match — actual must equal items exactly. */
|
|
37
|
+
static arrayEquals(items: unknown[]): Matcher;
|
|
38
|
+
/** String regex match. */
|
|
39
|
+
static stringLikeRegexp(pattern: string | RegExp): Matcher;
|
|
40
|
+
/** Asserts the value is absent (undefined). */
|
|
41
|
+
static absent(): Matcher;
|
|
42
|
+
/** Asserts any non-null/non-undefined value is present. */
|
|
43
|
+
static anyValue(): Matcher;
|
|
44
|
+
/** Inverts a match — asserts the value does NOT match the given pattern. */
|
|
45
|
+
static not(pattern: unknown): Matcher;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/assertions/template.d.ts
|
|
49
|
+
/** Options for `Template.synthesize()`. */
|
|
50
|
+
interface SynthesizeOptions {
|
|
51
|
+
/** XR (composite resource) data to inject before instantiation. */
|
|
52
|
+
xr?: Record<string, unknown>;
|
|
53
|
+
/** Environment data to inject before instantiation. */
|
|
54
|
+
environment?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A snapshot of rendered resources from a Composition, providing
|
|
58
|
+
* assertion methods for unit testing.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const template = Template.synthesize(MyComposition, {
|
|
63
|
+
* xr: { spec: { region: 'us-east-1' } },
|
|
64
|
+
* });
|
|
65
|
+
* template.hasResourceSpec('ec2.aws.crossplane.io/v1beta1', 'VPC', {
|
|
66
|
+
* forProvider: { region: 'us-east-1' },
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare class Template {
|
|
71
|
+
private readonly _resources;
|
|
72
|
+
private constructor();
|
|
73
|
+
/**
|
|
74
|
+
* Ergonomic factory: injects XR/environment data and instantiates
|
|
75
|
+
* the Composition class, then builds a Template from the rendered resources.
|
|
76
|
+
*
|
|
77
|
+
* Users never need to touch `Composition._pendingXR` directly.
|
|
78
|
+
*/
|
|
79
|
+
static synthesize(Ctor: new () => Composition, options?: SynthesizeOptions): Template;
|
|
80
|
+
/**
|
|
81
|
+
* Build a Template from an already-instantiated Composition.
|
|
82
|
+
*/
|
|
83
|
+
static fromComposition(composition: Composition): Template;
|
|
84
|
+
/**
|
|
85
|
+
* Build a Template from a pre-built array of KubernetesResource objects.
|
|
86
|
+
* Used internally by Simulator.
|
|
87
|
+
*/
|
|
88
|
+
static fromResources(resources: KubernetesResource$1[]): Template;
|
|
89
|
+
/** Get all resources matching apiVersion + kind. */
|
|
90
|
+
private _filterByGVK;
|
|
91
|
+
/**
|
|
92
|
+
* Assert the number of resources with the given apiVersion and kind.
|
|
93
|
+
*/
|
|
94
|
+
resourceCountIs(apiVersion: string, kind: string, count: number): void;
|
|
95
|
+
/**
|
|
96
|
+
* Assert that at least one resource of the given type matches the expected properties.
|
|
97
|
+
* Uses deep-partial matching by default (actual can be a superset of expected).
|
|
98
|
+
*/
|
|
99
|
+
hasResource(apiVersion: string, kind: string, props?: object): void;
|
|
100
|
+
/**
|
|
101
|
+
* Assert that at least one resource of the given type has a spec matching the expected properties.
|
|
102
|
+
* Shorthand for matching against the `spec` field only.
|
|
103
|
+
*/
|
|
104
|
+
hasResourceSpec(apiVersion: string, kind: string, specProps: object): void;
|
|
105
|
+
/**
|
|
106
|
+
* Assert that at least one resource of the given type has metadata matching the expected properties.
|
|
107
|
+
*/
|
|
108
|
+
hasResourceMetadata(apiVersion: string, kind: string, metaProps: object): void;
|
|
109
|
+
/**
|
|
110
|
+
* Assert that ALL resources of the given type match the expected properties.
|
|
111
|
+
*/
|
|
112
|
+
allResources(apiVersion: string, kind: string, props: object): void;
|
|
113
|
+
/**
|
|
114
|
+
* Find all resources of the given type that match the expected properties.
|
|
115
|
+
* Returns matches — never throws.
|
|
116
|
+
*/
|
|
117
|
+
findResources(apiVersion: string, kind: string, props?: object): KubernetesResource$1[];
|
|
118
|
+
/**
|
|
119
|
+
* Serialize all resources to a JSON-compatible array for snapshot testing.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* expect(template.toJSON()).toMatchSnapshot();
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
toJSON(): KubernetesResource$1[];
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/assertions/simulator.d.ts
|
|
130
|
+
/** Result of a simulation run. */
|
|
131
|
+
interface SimulationResult {
|
|
132
|
+
/** Resources that are ready to emit (all dependencies satisfied). */
|
|
133
|
+
emitted: Template;
|
|
134
|
+
/** Resources that are blocked on unresolved dependencies. */
|
|
135
|
+
blocked: Template;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Simulates the full rendering pipeline including observed state injection,
|
|
139
|
+
* edge resolution, and sequencing — mimicking what `@xplane/function` does at runtime.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* const result = Simulator.synthesize(MyComposition, { xr: { ... } })
|
|
144
|
+
* .withObserved([{ apiVersion: '...', kind: 'VPC', metadata: { name: 'vpc-abc' }, status: { atProvider: { vpcId: 'vpc-123' } } }])
|
|
145
|
+
* .run();
|
|
146
|
+
*
|
|
147
|
+
* result.emitted.hasResourceSpec('ec2.aws.crossplane.io/v1beta1', 'Subnet', {
|
|
148
|
+
* forProvider: { vpcId: 'vpc-123' },
|
|
149
|
+
* });
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
declare class Simulator {
|
|
153
|
+
private readonly _composition;
|
|
154
|
+
private _observed;
|
|
155
|
+
private constructor();
|
|
156
|
+
/**
|
|
157
|
+
* Ergonomic factory: injects XR/environment data, instantiates the
|
|
158
|
+
* Composition class, and returns a Simulator ready for `.withObserved().run()`.
|
|
159
|
+
*/
|
|
160
|
+
static synthesize(Ctor: new () => Composition, options?: SynthesizeOptions): Simulator;
|
|
161
|
+
/**
|
|
162
|
+
* Build a Simulator from an already-instantiated Composition.
|
|
163
|
+
*/
|
|
164
|
+
static fromComposition(composition: Composition): Simulator;
|
|
165
|
+
/**
|
|
166
|
+
* Provide observed (cluster) state for resources.
|
|
167
|
+
* Each resource is matched to a declared resource by its construct path
|
|
168
|
+
* (i.e., `metadata.name` in observed state maps to `resource.path` in the composition).
|
|
169
|
+
*/
|
|
170
|
+
withObserved(resources: KubernetesResource$1[]): this;
|
|
171
|
+
/**
|
|
172
|
+
* Run the simulation: inject observed state, resolve edges, determine sequencing.
|
|
173
|
+
*/
|
|
174
|
+
run(): SimulationResult;
|
|
175
|
+
}
|
|
176
|
+
//#endregion
|
|
177
|
+
export { type KubernetesResource, Match, type MatchResult, type Matcher, type SimulationResult, Simulator, type SynthesizeOptions, Template };
|
|
178
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/assertions/match.ts","../../src/assertions/template.ts","../../src/assertions/simulator.ts"],"mappings":";;;;cACa,OAAA;;UAGI,WAAA;EAH2C;EAK1D,IAAA;EAL0D;EAO1D,QAAQ;AAAA;;UAIO,OAAA;EAAA,UACL,OAAA;EACV,IAAA,CAAK,MAAA,YAAkB,WAAW;AAAA;;;;;;;;;;;cAwQvB,KAAA;EAAA,QACJ,WAAA,CAAA;EAG6B;EAAA,OAA7B,UAAA,CAAW,OAAA,WAAkB,OAAA;EAKhB;EAAA,OAAb,YAAA,CAAa,OAAA,WAAkB,OAAA;EAK/B;EAAA,OAAA,SAAA,CAAU,KAAA,cAAmB,OAAA;EAAA;EAAA,OAK7B,WAAA,CAAY,KAAA,cAAmB,OAAA;EAAnB;EAAA,OAKZ,gBAAA,CAAiB,OAAA,WAAkB,MAAA,GAAS,OAAA;EAA5C;EAAA,OAKA,MAAA,CAAA,GAAU,OAAA;EALO;EAAA,OAUjB,QAAA,CAAA,GAAY,OAAA;EALZ;EAAA,OAUA,GAAA,CAAI,OAAA,YAAmB,OAAA;AAAA;;;;UCzTf,iBAAA;EDHJ;ECKX,EAAA,GAAK,MAAA;;EAEL,WAAA,GAAc,MAAM;AAAA;ADJtB;;;;AAIU;AAIV;;;;;;;;;AARA,cCqBa,QAAA;EAAA,iBACM,UAAA;EAAA,QAEV,WAAA,CAAA;;;;;;;SAUA,UAAA,CAAW,IAAA,YAAgB,WAAA,EAAa,OAAA,GAAS,iBAAA,GAAyB,QAAA;ED6QhE;;;EAAA,OChPV,eAAA,CAAgB,WAAA,EAAa,WAAA,GAAc,QAAA;ED0Pb;;;;EAAA,OC3O9B,aAAA,CAAc,SAAA,EAAW,oBAAA,KAAuB,QAAA;ED6MhD;EAAA,QCxMC,YAAA;EDwM8B;;;ECjMtC,eAAA,CAAgB,UAAA,UAAoB,IAAA,UAAc,KAAA;ED2M3C;;;;EC9LP,WAAA,CAAY,UAAA,UAAoB,IAAA,UAAc,KAAA;EDmMtB;;;;EC1KxB,eAAA,CAAgB,UAAA,UAAoB,IAAA,UAAc,SAAA;EDoL/B;;;EC9JnB,mBAAA,CAAoB,UAAA,UAAoB,IAAA,UAAc,SAAA;EDmKjB;AAAA;;EC7IrC,YAAA,CAAa,UAAA,UAAoB,IAAA,UAAc,KAAA;;AA5KjD;;;EAsME,aAAA,CAAc,UAAA,UAAoB,IAAA,UAAc,KAAA,YAAiB,oBAAA;EApMjE;;;;;AAEoB;AAiBtB;;EAmME,MAAA,CAAA,GAAU,oBAAA;AAAA;;;;UClNK,gBAAA;EFT2C;EEW1D,OAAA,EAAS,QAAA;EFXiD;EEa1D,OAAA,EAAS,QAAQ;AAAA;;;;AFNT;AAIV;;;;;;;;;AAEoC;AAwQpC;cEzNa,SAAA;EAAA,iBACM,YAAA;EAAA,QACT,SAAA;EAAA,QAED,WAAA,CAAA;EFmO6B;;;;EAAA,OE3N7B,UAAA,CAAW,IAAA,YAAgB,WAAA,EAAa,OAAA,GAAS,iBAAA,GAAyB,SAAA;EF+O9D;;;EAAA,OElNZ,eAAA,CAAgB,WAAA,EAAa,WAAA,GAAc,SAAA;EFiL3C;;;;;EExKP,YAAA,CAAa,SAAA,EAAW,oBAAA;EFgLc;;;EExKtC,GAAA,CAAA,GAAO,gBAAA;AAAA"}
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { resolveSequencing } from "@xplane/core";
|
|
2
|
+
//#region src/assertions/match.ts
|
|
3
|
+
/** Symbol used to identify Matcher instances. */
|
|
4
|
+
const MATCHER = Symbol.for("xplane.devtools.matcher");
|
|
5
|
+
/** Check whether a value is a Matcher instance. */
|
|
6
|
+
function isMatcher(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && MATCHER in value && value[MATCHER] === true;
|
|
8
|
+
}
|
|
9
|
+
function pass() {
|
|
10
|
+
return {
|
|
11
|
+
pass: true,
|
|
12
|
+
failures: []
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function fail(message) {
|
|
16
|
+
return {
|
|
17
|
+
pass: false,
|
|
18
|
+
failures: [message]
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function merge(results) {
|
|
22
|
+
const failures = results.flatMap((r) => r.failures);
|
|
23
|
+
return {
|
|
24
|
+
pass: failures.length === 0,
|
|
25
|
+
failures
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Deep-match `actual` against `expected`.
|
|
30
|
+
* - If `expected` is a Matcher, delegates to its `.test()`.
|
|
31
|
+
* - Literal objects are matched recursively (deep-partial by default via objectLike semantics at the top level is handled by the caller).
|
|
32
|
+
* - This function performs EXACT matching — partial matching is handled by ObjectLikeMatcher.
|
|
33
|
+
*/
|
|
34
|
+
function deepMatch(actual, expected, path = "") {
|
|
35
|
+
if (isMatcher(expected)) {
|
|
36
|
+
const result = expected.test(actual);
|
|
37
|
+
if (!result.pass) return {
|
|
38
|
+
pass: false,
|
|
39
|
+
failures: result.failures.map((f) => path ? `${path}: ${f}` : f)
|
|
40
|
+
};
|
|
41
|
+
return pass();
|
|
42
|
+
}
|
|
43
|
+
if (expected === null || expected === void 0 || typeof expected !== "object") {
|
|
44
|
+
if (actual === expected) return pass();
|
|
45
|
+
return fail(path ? `${path}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}` : `expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
|
|
46
|
+
}
|
|
47
|
+
if (Array.isArray(expected)) {
|
|
48
|
+
if (!Array.isArray(actual)) return fail(`${path || "value"}: expected an array, got ${typeof actual}`);
|
|
49
|
+
if (actual.length !== expected.length) return fail(`${path || "value"}: expected array of length ${expected.length}, got length ${actual.length}`);
|
|
50
|
+
const results = [];
|
|
51
|
+
for (let i = 0; i < expected.length; i++) results.push(deepMatch(actual[i], expected[i], `${path}[${i}]`));
|
|
52
|
+
return merge(results);
|
|
53
|
+
}
|
|
54
|
+
if (typeof actual !== "object" || actual === null || Array.isArray(actual)) return fail(`${path || "value"}: expected an object, got ${JSON.stringify(actual)}`);
|
|
55
|
+
const expectedObj = expected;
|
|
56
|
+
const actualObj = actual;
|
|
57
|
+
const results = [];
|
|
58
|
+
for (const key of Object.keys(expectedObj)) results.push(deepMatch(actualObj[key], expectedObj[key], path ? `${path}.${key}` : key));
|
|
59
|
+
for (const key of Object.keys(actualObj)) if (!(key in expectedObj)) results.push(fail(`${path ? `${path}.${key}` : key}: unexpected key`));
|
|
60
|
+
return merge(results);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Deep-partial match: all keys in `expected` must match in `actual`, but
|
|
64
|
+
* `actual` may have additional keys at any level.
|
|
65
|
+
*/
|
|
66
|
+
function deepPartialMatch(actual, expected, path = "") {
|
|
67
|
+
if (isMatcher(expected)) {
|
|
68
|
+
const result = expected.test(actual);
|
|
69
|
+
if (!result.pass) return {
|
|
70
|
+
pass: false,
|
|
71
|
+
failures: result.failures.map((f) => path ? `${path}: ${f}` : f)
|
|
72
|
+
};
|
|
73
|
+
return pass();
|
|
74
|
+
}
|
|
75
|
+
if (expected === null || expected === void 0 || typeof expected !== "object") {
|
|
76
|
+
if (actual === expected) return pass();
|
|
77
|
+
return fail(path ? `${path}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}` : `expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(expected)) {
|
|
80
|
+
if (!Array.isArray(actual)) return fail(`${path || "value"}: expected an array, got ${typeof actual}`);
|
|
81
|
+
if (actual.length !== expected.length) return fail(`${path || "value"}: expected array of length ${expected.length}, got length ${actual.length}`);
|
|
82
|
+
const results = [];
|
|
83
|
+
for (let i = 0; i < expected.length; i++) results.push(deepPartialMatch(actual[i], expected[i], `${path}[${i}]`));
|
|
84
|
+
return merge(results);
|
|
85
|
+
}
|
|
86
|
+
if (typeof actual !== "object" || actual === null || Array.isArray(actual)) return fail(`${path || "value"}: expected an object, got ${JSON.stringify(actual)}`);
|
|
87
|
+
const expectedObj = expected;
|
|
88
|
+
const actualObj = actual;
|
|
89
|
+
const results = [];
|
|
90
|
+
for (const key of Object.keys(expectedObj)) if (!(key in actualObj)) results.push(fail(`${path ? `${path}.${key}` : key}: key not found in actual`));
|
|
91
|
+
else results.push(deepPartialMatch(actualObj[key], expectedObj[key], path ? `${path}.${key}` : key));
|
|
92
|
+
return merge(results);
|
|
93
|
+
}
|
|
94
|
+
var ObjectLikeMatcher = class {
|
|
95
|
+
pattern;
|
|
96
|
+
[MATCHER] = true;
|
|
97
|
+
constructor(pattern) {
|
|
98
|
+
this.pattern = pattern;
|
|
99
|
+
}
|
|
100
|
+
test(actual) {
|
|
101
|
+
return deepPartialMatch(actual, this.pattern);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var ObjectEqualsMatcher = class {
|
|
105
|
+
pattern;
|
|
106
|
+
[MATCHER] = true;
|
|
107
|
+
constructor(pattern) {
|
|
108
|
+
this.pattern = pattern;
|
|
109
|
+
}
|
|
110
|
+
test(actual) {
|
|
111
|
+
return deepMatch(actual, this.pattern);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var ArrayWithMatcher = class {
|
|
115
|
+
items;
|
|
116
|
+
[MATCHER] = true;
|
|
117
|
+
constructor(items) {
|
|
118
|
+
this.items = items;
|
|
119
|
+
}
|
|
120
|
+
test(actual) {
|
|
121
|
+
if (!Array.isArray(actual)) return fail(`expected an array, got ${typeof actual}`);
|
|
122
|
+
let searchStart = 0;
|
|
123
|
+
for (let i = 0; i < this.items.length; i++) {
|
|
124
|
+
let found = false;
|
|
125
|
+
for (let j = searchStart; j < actual.length; j++) if (deepPartialMatch(actual[j], this.items[i]).pass) {
|
|
126
|
+
searchStart = j + 1;
|
|
127
|
+
found = true;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
if (!found) return fail(`arrayWith: could not find match for item at index ${i}: ${JSON.stringify(this.items[i])}`);
|
|
131
|
+
}
|
|
132
|
+
return pass();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var ArrayEqualsMatcher = class {
|
|
136
|
+
items;
|
|
137
|
+
[MATCHER] = true;
|
|
138
|
+
constructor(items) {
|
|
139
|
+
this.items = items;
|
|
140
|
+
}
|
|
141
|
+
test(actual) {
|
|
142
|
+
return deepMatch(actual, this.items);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
var StringLikeRegexpMatcher = class {
|
|
146
|
+
[MATCHER] = true;
|
|
147
|
+
regex;
|
|
148
|
+
constructor(pattern) {
|
|
149
|
+
this.regex = typeof pattern === "string" ? new RegExp(pattern) : pattern;
|
|
150
|
+
}
|
|
151
|
+
test(actual) {
|
|
152
|
+
if (typeof actual !== "string") return fail(`expected a string, got ${typeof actual}`);
|
|
153
|
+
if (!this.regex.test(actual)) return fail(`expected string matching ${this.regex}, got "${actual}"`);
|
|
154
|
+
return pass();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
var AbsentMatcher = class {
|
|
158
|
+
[MATCHER] = true;
|
|
159
|
+
test(actual) {
|
|
160
|
+
if (actual !== void 0) return fail(`expected absent (undefined), got ${JSON.stringify(actual)}`);
|
|
161
|
+
return pass();
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var AnyValueMatcher = class {
|
|
165
|
+
[MATCHER] = true;
|
|
166
|
+
test(actual) {
|
|
167
|
+
if (actual === null || actual === void 0) return fail(`expected any value, got ${actual}`);
|
|
168
|
+
return pass();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
var NotMatcher = class {
|
|
172
|
+
pattern;
|
|
173
|
+
[MATCHER] = true;
|
|
174
|
+
constructor(pattern) {
|
|
175
|
+
this.pattern = pattern;
|
|
176
|
+
}
|
|
177
|
+
test(actual) {
|
|
178
|
+
if ((isMatcher(this.pattern) ? this.pattern.test(actual) : deepPartialMatch(actual, this.pattern)).pass) return fail(`expected NOT to match, but matched: ${JSON.stringify(actual)}`);
|
|
179
|
+
return pass();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* Factory for composable matchers used in assertions.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* template.hasResourceSpec('v1', 'ConfigMap', {
|
|
188
|
+
* data: Match.objectLike({ key: 'value' }),
|
|
189
|
+
* });
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
var Match = class {
|
|
193
|
+
constructor() {}
|
|
194
|
+
/** Deep-partial object match — actual may be a superset of pattern. */
|
|
195
|
+
static objectLike(pattern) {
|
|
196
|
+
return new ObjectLikeMatcher(pattern);
|
|
197
|
+
}
|
|
198
|
+
/** Exact object match — actual must equal pattern exactly (same keys, same values). */
|
|
199
|
+
static objectEquals(pattern) {
|
|
200
|
+
return new ObjectEqualsMatcher(pattern);
|
|
201
|
+
}
|
|
202
|
+
/** Array subset match — items must appear in actual in order. */
|
|
203
|
+
static arrayWith(items) {
|
|
204
|
+
return new ArrayWithMatcher(items);
|
|
205
|
+
}
|
|
206
|
+
/** Exact array match — actual must equal items exactly. */
|
|
207
|
+
static arrayEquals(items) {
|
|
208
|
+
return new ArrayEqualsMatcher(items);
|
|
209
|
+
}
|
|
210
|
+
/** String regex match. */
|
|
211
|
+
static stringLikeRegexp(pattern) {
|
|
212
|
+
return new StringLikeRegexpMatcher(pattern);
|
|
213
|
+
}
|
|
214
|
+
/** Asserts the value is absent (undefined). */
|
|
215
|
+
static absent() {
|
|
216
|
+
return new AbsentMatcher();
|
|
217
|
+
}
|
|
218
|
+
/** Asserts any non-null/non-undefined value is present. */
|
|
219
|
+
static anyValue() {
|
|
220
|
+
return new AnyValueMatcher();
|
|
221
|
+
}
|
|
222
|
+
/** Inverts a match — asserts the value does NOT match the given pattern. */
|
|
223
|
+
static not(pattern) {
|
|
224
|
+
return new NotMatcher(pattern);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/assertions/template.ts
|
|
229
|
+
/**
|
|
230
|
+
* A snapshot of rendered resources from a Composition, providing
|
|
231
|
+
* assertion methods for unit testing.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* const template = Template.synthesize(MyComposition, {
|
|
236
|
+
* xr: { spec: { region: 'us-east-1' } },
|
|
237
|
+
* });
|
|
238
|
+
* template.hasResourceSpec('ec2.aws.crossplane.io/v1beta1', 'VPC', {
|
|
239
|
+
* forProvider: { region: 'us-east-1' },
|
|
240
|
+
* });
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
var Template = class Template {
|
|
244
|
+
_resources;
|
|
245
|
+
constructor(resources) {
|
|
246
|
+
this._resources = resources;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Ergonomic factory: injects XR/environment data and instantiates
|
|
250
|
+
* the Composition class, then builds a Template from the rendered resources.
|
|
251
|
+
*
|
|
252
|
+
* Users never need to touch `Composition._pendingXR` directly.
|
|
253
|
+
*/
|
|
254
|
+
static synthesize(Ctor, options = {}) {
|
|
255
|
+
let base = Ctor;
|
|
256
|
+
while (base && !Object.hasOwn(base, "_pendingXR")) base = Object.getPrototypeOf(base);
|
|
257
|
+
if (!base) throw new Error("Could not find Composition base class with _pendingXR");
|
|
258
|
+
const BaseComposition = base;
|
|
259
|
+
BaseComposition._pendingXR = options.xr;
|
|
260
|
+
BaseComposition._pendingEnvironment = options.environment;
|
|
261
|
+
try {
|
|
262
|
+
const instance = new Ctor();
|
|
263
|
+
return Template.fromComposition(instance);
|
|
264
|
+
} finally {
|
|
265
|
+
BaseComposition._pendingXR = void 0;
|
|
266
|
+
BaseComposition._pendingEnvironment = void 0;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Build a Template from an already-instantiated Composition.
|
|
271
|
+
*/
|
|
272
|
+
static fromComposition(composition) {
|
|
273
|
+
return new Template([...composition.resources.values()].map((r) => r.toDesired()));
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Build a Template from a pre-built array of KubernetesResource objects.
|
|
277
|
+
* Used internally by Simulator.
|
|
278
|
+
*/
|
|
279
|
+
static fromResources(resources) {
|
|
280
|
+
return new Template(resources);
|
|
281
|
+
}
|
|
282
|
+
/** Get all resources matching apiVersion + kind. */
|
|
283
|
+
_filterByGVK(apiVersion, kind) {
|
|
284
|
+
return this._resources.filter((r) => r.apiVersion === apiVersion && r.kind === kind);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Assert the number of resources with the given apiVersion and kind.
|
|
288
|
+
*/
|
|
289
|
+
resourceCountIs(apiVersion, kind, count) {
|
|
290
|
+
const matched = this._filterByGVK(apiVersion, kind);
|
|
291
|
+
if (matched.length !== count) throw new Error(`Expected ${count} resource(s) of type ${apiVersion}/${kind}, found ${matched.length}`);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Assert that at least one resource of the given type matches the expected properties.
|
|
295
|
+
* Uses deep-partial matching by default (actual can be a superset of expected).
|
|
296
|
+
*/
|
|
297
|
+
hasResource(apiVersion, kind, props) {
|
|
298
|
+
const matched = this._filterByGVK(apiVersion, kind);
|
|
299
|
+
if (matched.length === 0) throw new Error(`No resources found with type ${apiVersion}/${kind}`);
|
|
300
|
+
if (!props) return;
|
|
301
|
+
const allFailures = [];
|
|
302
|
+
for (const resource of matched) {
|
|
303
|
+
const result = deepPartialMatch(resource, props);
|
|
304
|
+
if (result.pass) return;
|
|
305
|
+
allFailures.push(` Resource: ${JSON.stringify(resource.metadata?.name ?? "(unnamed)")}\n ${result.failures.join("\n ")}`);
|
|
306
|
+
}
|
|
307
|
+
throw new Error(`No resource of type ${apiVersion}/${kind} matches the expected properties:\n${allFailures.join("\n")}`);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Assert that at least one resource of the given type has a spec matching the expected properties.
|
|
311
|
+
* Shorthand for matching against the `spec` field only.
|
|
312
|
+
*/
|
|
313
|
+
hasResourceSpec(apiVersion, kind, specProps) {
|
|
314
|
+
const matched = this._filterByGVK(apiVersion, kind);
|
|
315
|
+
if (matched.length === 0) throw new Error(`No resources found with type ${apiVersion}/${kind}`);
|
|
316
|
+
const allFailures = [];
|
|
317
|
+
for (const resource of matched) {
|
|
318
|
+
const result = deepPartialMatch(resource.spec ?? {}, specProps);
|
|
319
|
+
if (result.pass) return;
|
|
320
|
+
allFailures.push(` Resource: ${JSON.stringify(resource.metadata?.name ?? "(unnamed)")}\n ${result.failures.join("\n ")}`);
|
|
321
|
+
}
|
|
322
|
+
throw new Error(`No resource of type ${apiVersion}/${kind} has spec matching the expected properties:\n${allFailures.join("\n")}`);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Assert that at least one resource of the given type has metadata matching the expected properties.
|
|
326
|
+
*/
|
|
327
|
+
hasResourceMetadata(apiVersion, kind, metaProps) {
|
|
328
|
+
const matched = this._filterByGVK(apiVersion, kind);
|
|
329
|
+
if (matched.length === 0) throw new Error(`No resources found with type ${apiVersion}/${kind}`);
|
|
330
|
+
const allFailures = [];
|
|
331
|
+
for (const resource of matched) {
|
|
332
|
+
const result = deepPartialMatch(resource.metadata ?? {}, metaProps);
|
|
333
|
+
if (result.pass) return;
|
|
334
|
+
allFailures.push(` Resource: ${JSON.stringify(resource.metadata?.name ?? "(unnamed)")}\n ${result.failures.join("\n ")}`);
|
|
335
|
+
}
|
|
336
|
+
throw new Error(`No resource of type ${apiVersion}/${kind} has metadata matching the expected properties:\n${allFailures.join("\n")}`);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Assert that ALL resources of the given type match the expected properties.
|
|
340
|
+
*/
|
|
341
|
+
allResources(apiVersion, kind, props) {
|
|
342
|
+
const matched = this._filterByGVK(apiVersion, kind);
|
|
343
|
+
if (matched.length === 0) throw new Error(`No resources found with type ${apiVersion}/${kind}`);
|
|
344
|
+
const failures = [];
|
|
345
|
+
for (const resource of matched) {
|
|
346
|
+
const result = deepPartialMatch(resource, props);
|
|
347
|
+
if (!result.pass) failures.push(` Resource: ${JSON.stringify(resource.metadata?.name ?? "(unnamed)")}\n ${result.failures.join("\n ")}`);
|
|
348
|
+
}
|
|
349
|
+
if (failures.length > 0) throw new Error(`Not all resources of type ${apiVersion}/${kind} match:\n${failures.join("\n")}`);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Find all resources of the given type that match the expected properties.
|
|
353
|
+
* Returns matches — never throws.
|
|
354
|
+
*/
|
|
355
|
+
findResources(apiVersion, kind, props) {
|
|
356
|
+
const matched = this._filterByGVK(apiVersion, kind);
|
|
357
|
+
if (!props) return matched;
|
|
358
|
+
return matched.filter((resource) => {
|
|
359
|
+
return deepPartialMatch(resource, props).pass;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Serialize all resources to a JSON-compatible array for snapshot testing.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* expect(template.toJSON()).toMatchSnapshot();
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
toJSON() {
|
|
371
|
+
return structuredClone(this._resources);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
//#endregion
|
|
375
|
+
//#region src/assertions/simulator.ts
|
|
376
|
+
function getNestedValue(obj, path) {
|
|
377
|
+
let current = obj;
|
|
378
|
+
for (const segment of path.split(".")) {
|
|
379
|
+
if (current === null || current === void 0 || typeof current !== "object") return;
|
|
380
|
+
current = current[segment];
|
|
381
|
+
}
|
|
382
|
+
return current;
|
|
383
|
+
}
|
|
384
|
+
function setNestedValue(obj, path, value) {
|
|
385
|
+
const segments = path.split(".");
|
|
386
|
+
let current = obj;
|
|
387
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
388
|
+
const seg = segments[i];
|
|
389
|
+
if (!(seg in current) || typeof current[seg] !== "object" || current[seg] === null) current[seg] = {};
|
|
390
|
+
current = current[seg];
|
|
391
|
+
}
|
|
392
|
+
const lastSeg = segments[segments.length - 1];
|
|
393
|
+
if (lastSeg !== void 0) current[lastSeg] = value;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Simulates the full rendering pipeline including observed state injection,
|
|
397
|
+
* edge resolution, and sequencing — mimicking what `@xplane/function` does at runtime.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* const result = Simulator.synthesize(MyComposition, { xr: { ... } })
|
|
402
|
+
* .withObserved([{ apiVersion: '...', kind: 'VPC', metadata: { name: 'vpc-abc' }, status: { atProvider: { vpcId: 'vpc-123' } } }])
|
|
403
|
+
* .run();
|
|
404
|
+
*
|
|
405
|
+
* result.emitted.hasResourceSpec('ec2.aws.crossplane.io/v1beta1', 'Subnet', {
|
|
406
|
+
* forProvider: { vpcId: 'vpc-123' },
|
|
407
|
+
* });
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
var Simulator = class Simulator {
|
|
411
|
+
_composition;
|
|
412
|
+
_observed = [];
|
|
413
|
+
constructor(composition) {
|
|
414
|
+
this._composition = composition;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Ergonomic factory: injects XR/environment data, instantiates the
|
|
418
|
+
* Composition class, and returns a Simulator ready for `.withObserved().run()`.
|
|
419
|
+
*/
|
|
420
|
+
static synthesize(Ctor, options = {}) {
|
|
421
|
+
let base = Ctor;
|
|
422
|
+
while (base && !Object.hasOwn(base, "_pendingXR")) base = Object.getPrototypeOf(base);
|
|
423
|
+
if (!base) throw new Error("Could not find Composition base class with _pendingXR");
|
|
424
|
+
const BaseComposition = base;
|
|
425
|
+
BaseComposition._pendingXR = options.xr;
|
|
426
|
+
BaseComposition._pendingEnvironment = options.environment;
|
|
427
|
+
try {
|
|
428
|
+
return new Simulator(new Ctor());
|
|
429
|
+
} finally {
|
|
430
|
+
BaseComposition._pendingXR = void 0;
|
|
431
|
+
BaseComposition._pendingEnvironment = void 0;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Build a Simulator from an already-instantiated Composition.
|
|
436
|
+
*/
|
|
437
|
+
static fromComposition(composition) {
|
|
438
|
+
return new Simulator(composition);
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Provide observed (cluster) state for resources.
|
|
442
|
+
* Each resource is matched to a declared resource by its construct path
|
|
443
|
+
* (i.e., `metadata.name` in observed state maps to `resource.path` in the composition).
|
|
444
|
+
*/
|
|
445
|
+
withObserved(resources) {
|
|
446
|
+
this._observed = resources;
|
|
447
|
+
return this;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Run the simulation: inject observed state, resolve edges, determine sequencing.
|
|
451
|
+
*/
|
|
452
|
+
run() {
|
|
453
|
+
const composition = this._composition;
|
|
454
|
+
const resources = composition.resources;
|
|
455
|
+
const collector = composition.collector;
|
|
456
|
+
const graph = composition.graph;
|
|
457
|
+
const observedMap = /* @__PURE__ */ new Map();
|
|
458
|
+
for (const obs of this._observed) {
|
|
459
|
+
const name = obs.metadata?.name;
|
|
460
|
+
if (name) observedMap.set(name, obs);
|
|
461
|
+
}
|
|
462
|
+
graph.addEdges(collector.edges);
|
|
463
|
+
for (const [path, resource] of resources) {
|
|
464
|
+
const observed = observedMap.get(path);
|
|
465
|
+
if (observed) resource.setObserved(observed);
|
|
466
|
+
}
|
|
467
|
+
for (const edge of collector.edges) {
|
|
468
|
+
const observed = observedMap.get(edge.from.id);
|
|
469
|
+
if (!observed) continue;
|
|
470
|
+
const value = getNestedValue(observed, edge.fromPath);
|
|
471
|
+
if (value === void 0 || value === null) continue;
|
|
472
|
+
const targetResource = resources.get(edge.to.id);
|
|
473
|
+
if (!targetResource) continue;
|
|
474
|
+
const toPath = edge.toPath;
|
|
475
|
+
if (toPath.startsWith("spec.")) setNestedValue(targetResource.spec, toPath.slice(5), value);
|
|
476
|
+
}
|
|
477
|
+
const sequencing = resolveSequencing(resources, graph, observedMap);
|
|
478
|
+
return {
|
|
479
|
+
emitted: Template.fromResources(sequencing.emit.map((r) => r.toDesired())),
|
|
480
|
+
blocked: Template.fromResources(sequencing.blocked.map((r) => r.toDesired()))
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
//#endregion
|
|
485
|
+
export { Match, Simulator, Template };
|
|
486
|
+
|
|
487
|
+
//# sourceMappingURL=index.mjs.map
|