@noego/testing 0.1.0 → 0.2.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/README.md +94 -0
- package/dist/index.cjs +610 -641
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +237 -227
- package/dist/index.d.ts +237 -227
- package/dist/index.js +601 -622
- package/dist/index.js.map +1 -1
- package/package.json +9 -6
package/dist/index.cjs
CHANGED
|
@@ -20,702 +20,671 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
runClockContract: () => runClockContract,
|
|
38
|
-
runEventBusContract: () => runEventBusContract,
|
|
39
|
-
runKeyValueStoreContract: () => runKeyValueStoreContract,
|
|
40
|
-
runObjectStoreContract: () => runObjectStoreContract,
|
|
41
|
-
testEvent: () => testEvent
|
|
23
|
+
CallScriptExhaustedError: () => CallScriptExhaustedError,
|
|
24
|
+
ENV_REGISTRY: () => ENV_REGISTRY,
|
|
25
|
+
ExpectationOverflowError: () => ExpectationOverflowError,
|
|
26
|
+
InvalidDescriptorError: () => InvalidDescriptorError,
|
|
27
|
+
MethodNotCallableError: () => MethodNotCallableError,
|
|
28
|
+
MissingIocSeamError: () => MissingIocSeamError,
|
|
29
|
+
NonObjectMethodTargetError: () => NonObjectMethodTargetError,
|
|
30
|
+
TestIocBuilder: () => TestIocBuilder,
|
|
31
|
+
TestingError: () => TestingError,
|
|
32
|
+
UnknownTokenKeyError: () => UnknownTokenKeyError,
|
|
33
|
+
UnwatchedInspectionError: () => UnwatchedInspectionError,
|
|
34
|
+
VerificationError: () => VerificationError,
|
|
35
|
+
test: () => test,
|
|
36
|
+
testIoc: () => testIoc
|
|
42
37
|
});
|
|
43
38
|
module.exports = __toCommonJS(index_exports);
|
|
44
39
|
|
|
45
|
-
// src/
|
|
46
|
-
var
|
|
47
|
-
var ManualClock = class _ManualClock extends import_runtime.Clock {
|
|
48
|
-
static defaultStart = import_runtime.Instant.ofEpochMilliseconds(Date.UTC(2020, 0, 1));
|
|
49
|
-
current;
|
|
50
|
-
constructor(start = _ManualClock.defaultStart) {
|
|
51
|
-
super();
|
|
52
|
-
this.current = start;
|
|
53
|
-
}
|
|
54
|
-
now() {
|
|
55
|
-
return this.current;
|
|
56
|
-
}
|
|
57
|
-
set(instant) {
|
|
58
|
-
if (instant.isBefore(this.current)) {
|
|
59
|
-
throw new RangeError(`ManualClock cannot move backwards: ${this.current.toString()} -> ${instant.toString()}`);
|
|
60
|
-
}
|
|
61
|
-
this.current = instant;
|
|
62
|
-
}
|
|
63
|
-
advanceBy(duration) {
|
|
64
|
-
this.current = this.current.plus(duration);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
40
|
+
// src/builder.ts
|
|
41
|
+
var import_ioc = require("@noego/ioc");
|
|
67
42
|
|
|
68
|
-
// src/
|
|
69
|
-
var
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.clock = clock;
|
|
74
|
-
}
|
|
75
|
-
clock;
|
|
76
|
-
tasks = [];
|
|
77
|
-
nextSequence = 0;
|
|
78
|
-
nextId = 0;
|
|
79
|
-
schedule(input) {
|
|
80
|
-
const id = (0, import_runtime2.brandId)(`task-${++this.nextId}`, "ScheduledTask");
|
|
81
|
-
const internal = {
|
|
82
|
-
id,
|
|
83
|
-
label: input.label,
|
|
84
|
-
deadline: this.clock.now().plus(input.delay),
|
|
85
|
-
sequence: this.nextSequence++,
|
|
86
|
-
task: input.task,
|
|
87
|
-
cancelled: false
|
|
88
|
-
};
|
|
89
|
-
this.tasks.push(internal);
|
|
90
|
-
return {
|
|
91
|
-
id,
|
|
92
|
-
cancel: () => {
|
|
93
|
-
internal.cancelled = true;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
43
|
+
// src/errors.ts
|
|
44
|
+
var TestingError = class extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = new.target.name;
|
|
96
48
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
for (; ; ) {
|
|
104
|
-
const next = this.nextDue(target);
|
|
105
|
-
if (next === null) break;
|
|
106
|
-
if (next.deadline.isAfter(this.clock.now())) {
|
|
107
|
-
this.clock.set(next.deadline);
|
|
108
|
-
}
|
|
109
|
-
await this.execute(next);
|
|
110
|
-
}
|
|
111
|
-
if (target.isAfter(this.clock.now())) {
|
|
112
|
-
this.clock.set(target);
|
|
113
|
-
}
|
|
49
|
+
};
|
|
50
|
+
var MissingIocSeamError = class extends TestingError {
|
|
51
|
+
constructor() {
|
|
52
|
+
super(
|
|
53
|
+
"@noego/testing requires an @noego/ioc version that provides Container.setInstanceDecorator (>= 0.5.x with the instance-decoration seam). Upgrade @noego/ioc."
|
|
54
|
+
);
|
|
114
55
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (next.deadline.isAfter(this.clock.now())) {
|
|
122
|
-
this.clock.set(next.deadline);
|
|
123
|
-
}
|
|
124
|
-
await this.execute(next);
|
|
125
|
-
}
|
|
126
|
-
/** Run tasks (advancing time as needed) until none remain. */
|
|
127
|
-
async runUntilIdle(options) {
|
|
128
|
-
const maxTasks = options?.maxTasks ?? 1e4;
|
|
129
|
-
let executed = 0;
|
|
130
|
-
while (this.live().length > 0) {
|
|
131
|
-
if (executed >= maxTasks) {
|
|
132
|
-
const labels = this.pending().slice(0, 5).map((t) => t.label).join(", ");
|
|
133
|
-
throw new Error(
|
|
134
|
-
`ManualScheduler.runUntilIdle: still not idle after ${maxTasks} tasks \u2014 probable infinite reschedule loop (next: ${labels})`
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
await this.runNext();
|
|
138
|
-
executed += 1;
|
|
139
|
-
}
|
|
56
|
+
};
|
|
57
|
+
var UnwatchedInspectionError = class extends TestingError {
|
|
58
|
+
constructor(token, method) {
|
|
59
|
+
super(
|
|
60
|
+
`Method "${method}" on ${token} is not watched in this environment. Only watched methods are inspectable \u2014 install test.watch() or any test.* behavior/expectation descriptor for it.`
|
|
61
|
+
);
|
|
140
62
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
throw new Error(`ManualScheduler: ${remaining.length} pending task(s) at teardown: ${labels}`);
|
|
148
|
-
}
|
|
63
|
+
};
|
|
64
|
+
var CallScriptExhaustedError = class extends TestingError {
|
|
65
|
+
constructor(token, method, scriptLength, callIndex) {
|
|
66
|
+
super(
|
|
67
|
+
`Call #${callIndex} to ${token}.${method} exceeds its test.calls() script of ${scriptLength} ${scriptLength === 1 ? "entry" : "entries"}.`
|
|
68
|
+
);
|
|
149
69
|
}
|
|
150
|
-
|
|
151
|
-
|
|
70
|
+
};
|
|
71
|
+
var ExpectationOverflowError = class extends TestingError {
|
|
72
|
+
constructor(token, method, expected, attempted) {
|
|
73
|
+
super(
|
|
74
|
+
expected === 0 ? `${token}.${method} was expected never to be called, but it was invoked.` : `${token}.${method} was expected exactly ${expected} ${expected === 1 ? "call" : "calls"}, but call #${attempted} arrived.`
|
|
75
|
+
);
|
|
152
76
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
77
|
+
};
|
|
78
|
+
var VerificationError = class extends TestingError {
|
|
79
|
+
constructor(failures) {
|
|
80
|
+
super(
|
|
81
|
+
"Exact method expectations were not satisfied:\n" + failures.map(
|
|
82
|
+
(f) => ` - ${f.token}.${f.method}: expected exactly ${f.expected} ${f.expected === 1 ? "call" : "calls"}, observed ${f.actual}`
|
|
83
|
+
).join("\n")
|
|
84
|
+
);
|
|
156
85
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
86
|
+
};
|
|
87
|
+
var MethodNotCallableError = class extends TestingError {
|
|
88
|
+
constructor(token, method) {
|
|
89
|
+
super(
|
|
90
|
+
`Cannot install a test.* descriptor on ${token}.${method}: the resolved instance has no callable method with that name.`
|
|
91
|
+
);
|
|
160
92
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
93
|
+
};
|
|
94
|
+
var NonObjectMethodTargetError = class extends TestingError {
|
|
95
|
+
constructor(token) {
|
|
96
|
+
super(
|
|
97
|
+
`.methods() is configured for ${token}, but that token resolved to a non-object value. Method descriptors apply only to IoC-managed instances.`
|
|
98
|
+
);
|
|
164
99
|
}
|
|
165
100
|
};
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// src/identity/sequence_id_generator.ts
|
|
172
|
-
var import_runtime3 = require("@noego/runtime");
|
|
173
|
-
var SequenceIdGenerator = class extends import_runtime3.IdGenerator {
|
|
174
|
-
counters = /* @__PURE__ */ new Map();
|
|
175
|
-
next(brand) {
|
|
176
|
-
const current = (this.counters.get(brand) ?? 0) + 1;
|
|
177
|
-
this.counters.set(brand, current);
|
|
178
|
-
return (0, import_runtime3.brandId)(`${brand.toLowerCase()}-${current}`, brand);
|
|
101
|
+
var UnknownTokenKeyError = class extends TestingError {
|
|
102
|
+
constructor(key, space, known) {
|
|
103
|
+
super(
|
|
104
|
+
`Unknown ${space} key "${key}" \u2014 it does not match any token known to this builder. Pass the class/token itself via a Map, or include the registration through .use(...). Known tokens: ` + (known.length ? known.join(", ") : "(none)")
|
|
105
|
+
);
|
|
179
106
|
}
|
|
180
107
|
};
|
|
108
|
+
var InvalidDescriptorError = class extends TestingError {
|
|
109
|
+
};
|
|
181
110
|
|
|
182
|
-
// src/
|
|
183
|
-
var
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
let t = this.state;
|
|
196
|
-
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
197
|
-
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
198
|
-
return (t ^ t >>> 14) >>> 0;
|
|
199
|
-
}
|
|
200
|
-
bytes(length) {
|
|
201
|
-
if (!Number.isInteger(length) || length < 0) {
|
|
202
|
-
throw new RangeError(`bytes length must be a non-negative integer, got ${length}`);
|
|
203
|
-
}
|
|
204
|
-
const out = new Uint8Array(length);
|
|
205
|
-
for (let i = 0; i < length; i += 1) {
|
|
206
|
-
out[i] = this.nextUint32() & 255;
|
|
207
|
-
}
|
|
208
|
-
return out;
|
|
111
|
+
// src/descriptors.ts
|
|
112
|
+
var DESCRIPTOR = /* @__PURE__ */ Symbol.for("noego:testing:descriptor");
|
|
113
|
+
function isDescriptor(value) {
|
|
114
|
+
return typeof value === "object" && value !== null && value[DESCRIPTOR] === true;
|
|
115
|
+
}
|
|
116
|
+
function frozen(value) {
|
|
117
|
+
return Object.freeze(value);
|
|
118
|
+
}
|
|
119
|
+
function assertBehavior(value, where) {
|
|
120
|
+
if (!isDescriptor(value) || !["returns", "throws", "original", "calls"].includes(value.kind)) {
|
|
121
|
+
throw new InvalidDescriptorError(
|
|
122
|
+
`${where} requires a behavior descriptor (test.returns/throws/original/calls).`
|
|
123
|
+
);
|
|
209
124
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
125
|
+
}
|
|
126
|
+
var test = {
|
|
127
|
+
/** Return the supplied value when the method is called. Auto-watches. */
|
|
128
|
+
returns(value) {
|
|
129
|
+
return frozen({ [DESCRIPTOR]: true, kind: "returns", value });
|
|
130
|
+
},
|
|
131
|
+
/** Throw/reject with the supplied error. Auto-watches. */
|
|
132
|
+
throws(error) {
|
|
133
|
+
return frozen({ [DESCRIPTOR]: true, kind: "throws", error });
|
|
134
|
+
},
|
|
135
|
+
/** Invoke the original effective method. Auto-watches. */
|
|
136
|
+
original() {
|
|
137
|
+
return frozen({ [DESCRIPTOR]: true, kind: "original" });
|
|
138
|
+
},
|
|
139
|
+
/**
|
|
140
|
+
* Per-invocation behavior script: call 1 uses entry 1, and so on. A call
|
|
141
|
+
* after exhaustion fails immediately. Unused entries do not fail verification.
|
|
142
|
+
*/
|
|
143
|
+
calls(script) {
|
|
144
|
+
if (!Array.isArray(script)) {
|
|
145
|
+
throw new InvalidDescriptorError("test.calls() requires an array of behavior descriptors.");
|
|
213
146
|
}
|
|
214
|
-
|
|
215
|
-
return
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
executed = [];
|
|
225
|
-
constructor(script) {
|
|
226
|
-
super();
|
|
227
|
-
this.script = [...script];
|
|
228
|
-
}
|
|
229
|
-
execute(request) {
|
|
230
|
-
const entry = this.script[this.cursor];
|
|
231
|
-
if (entry === void 0) {
|
|
232
|
-
return Promise.reject(
|
|
233
|
-
new Error(`ScriptedFetchClient: unexpected call #${this.cursor + 1} (${request.method} ${request.url}); script is exhausted`)
|
|
234
|
-
);
|
|
147
|
+
script.forEach((entry, i) => assertBehavior(entry, `test.calls() entry #${i + 1}`));
|
|
148
|
+
return frozen({ [DESCRIPTOR]: true, kind: "calls", script: Object.freeze([...script]) });
|
|
149
|
+
},
|
|
150
|
+
/**
|
|
151
|
+
* Keep original behavior and record calls. With a raw wrapper argument, the
|
|
152
|
+
* wrapper's behavior runs and is recorded.
|
|
153
|
+
*/
|
|
154
|
+
watch(wrapper) {
|
|
155
|
+
if (wrapper !== void 0 && typeof wrapper !== "function") {
|
|
156
|
+
throw new InvalidDescriptorError("test.watch() accepts only a raw wrapper function.");
|
|
235
157
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
158
|
+
return frozen({ [DESCRIPTOR]: true, kind: "watch", wrapper });
|
|
159
|
+
},
|
|
160
|
+
/** Require exactly one call; with no behavior, the original runs. */
|
|
161
|
+
once(behavior) {
|
|
162
|
+
if (behavior !== void 0) assertBehavior(behavior, "test.once()");
|
|
163
|
+
return frozen({ [DESCRIPTOR]: true, kind: "expect", expected: 1, behavior });
|
|
164
|
+
},
|
|
165
|
+
/** Require exactly `count` calls; with no behavior, the original runs. */
|
|
166
|
+
times(count, behavior) {
|
|
167
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
168
|
+
throw new InvalidDescriptorError(
|
|
169
|
+
`test.times() requires a non-negative integer count, received ${String(count)}.`
|
|
239
170
|
);
|
|
240
171
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
172
|
+
if (behavior !== void 0) assertBehavior(behavior, "test.times()");
|
|
173
|
+
return frozen({ [DESCRIPTOR]: true, kind: "expect", expected: count, behavior });
|
|
174
|
+
},
|
|
175
|
+
/** Require zero calls; the first invocation fails and skips the original. */
|
|
176
|
+
never() {
|
|
177
|
+
return frozen({ [DESCRIPTOR]: true, kind: "expect", expected: 0 });
|
|
178
|
+
},
|
|
179
|
+
/** Read the recorded history for a watched method in one environment. */
|
|
180
|
+
inspect(environment, token, method) {
|
|
181
|
+
const registry = environment?.[ENV_REGISTRY];
|
|
182
|
+
if (!registry) {
|
|
183
|
+
throw new InvalidDescriptorError(
|
|
184
|
+
"test.inspect() requires a built @noego/testing environment as its first argument."
|
|
185
|
+
);
|
|
251
186
|
}
|
|
187
|
+
return registry.inspect(token, method);
|
|
252
188
|
}
|
|
253
189
|
};
|
|
190
|
+
Object.freeze(test);
|
|
191
|
+
var ENV_REGISTRY = /* @__PURE__ */ Symbol.for("noego:testing:env-registry");
|
|
254
192
|
|
|
255
|
-
// src/
|
|
256
|
-
var
|
|
257
|
-
var
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
193
|
+
// src/method_state.ts
|
|
194
|
+
var CONTEXT_WRAPPED = /* @__PURE__ */ Symbol.for("ioc:context-wrapped");
|
|
195
|
+
var CONTEXT_OWNER = /* @__PURE__ */ Symbol.for("ioc:context-owner");
|
|
196
|
+
function tokenLabel(token) {
|
|
197
|
+
if (typeof token === "function") return token.name || "[anonymous class]";
|
|
198
|
+
if (typeof token === "symbol") return String(token);
|
|
199
|
+
return String(token);
|
|
200
|
+
}
|
|
201
|
+
var MethodState = class {
|
|
202
|
+
constructor(tokenName, method, descriptor) {
|
|
203
|
+
this.tokenName = tokenName;
|
|
204
|
+
this.method = method;
|
|
205
|
+
this.descriptor = descriptor;
|
|
206
|
+
}
|
|
207
|
+
tokenName;
|
|
208
|
+
method;
|
|
209
|
+
descriptor;
|
|
210
|
+
calls = [];
|
|
211
|
+
/** Actual invocation count (includes the call currently executing). */
|
|
212
|
+
actual = 0;
|
|
213
|
+
/** Cursor into a test.calls() script. */
|
|
214
|
+
scriptCursor = 0;
|
|
215
|
+
get expectation() {
|
|
216
|
+
return this.descriptor.kind === "expect" ? this.descriptor : void 0;
|
|
217
|
+
}
|
|
218
|
+
inspection() {
|
|
219
|
+
return {
|
|
220
|
+
count: this.calls.length,
|
|
221
|
+
calls: this.calls.map((c) => ({ ...c, args: [...c.args] }))
|
|
222
|
+
};
|
|
275
223
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
224
|
+
};
|
|
225
|
+
var WatchRegistry = class {
|
|
226
|
+
/** entry-identity → method → state. Entries share states across instances. */
|
|
227
|
+
states = /* @__PURE__ */ new Map();
|
|
228
|
+
nameIndex = /* @__PURE__ */ new Map();
|
|
229
|
+
entries = [];
|
|
230
|
+
addEntry(entry) {
|
|
231
|
+
this.entries.push(entry);
|
|
232
|
+
const byMethod = /* @__PURE__ */ new Map();
|
|
233
|
+
for (const [method, descriptor] of entry.methods) {
|
|
234
|
+
if (typeof descriptor === "function") continue;
|
|
235
|
+
byMethod.set(method, new MethodState(this.entryLabel(entry), method, descriptor));
|
|
282
236
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
this.openHandles.delete(handle);
|
|
288
|
-
});
|
|
289
|
-
this.openHandles.add(handle);
|
|
290
|
-
return handle;
|
|
291
|
-
}
|
|
292
|
-
/** Teardown assertion: no spawned process may remain open. */
|
|
293
|
-
assertNoOpenHandles() {
|
|
294
|
-
if (this.openHandles.size > 0) {
|
|
295
|
-
const labels = [...this.openHandles].map((h) => h.label).join(", ");
|
|
296
|
-
throw new Error(`ScriptedProcessRunner: ${this.openHandles.size} open process handle(s) at teardown: ${labels}`);
|
|
237
|
+
this.states.set(entry.key, byMethod);
|
|
238
|
+
if (entry.byName) this.nameIndex.set(entry.key, entry.key);
|
|
239
|
+
else if (typeof entry.key === "function" && entry.key.name) {
|
|
240
|
+
this.nameIndex.set(entry.key.name, entry.key);
|
|
297
241
|
}
|
|
298
242
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
this.
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
243
|
+
entryLabel(entry) {
|
|
244
|
+
return entry.byName ? String(entry.key) : tokenLabel(entry.key);
|
|
245
|
+
}
|
|
246
|
+
/** Entries applying to a resolving token (exact identity or name match). */
|
|
247
|
+
matchEntries(token) {
|
|
248
|
+
const name = typeof token === "function" ? token.name : typeof token === "string" ? token : void 0;
|
|
249
|
+
return this.entries.filter(
|
|
250
|
+
(entry) => entry.key === token || entry.byName && name !== void 0 && entry.key === name
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
state(entryKey, method) {
|
|
254
|
+
return this.states.get(entryKey)?.get(method);
|
|
255
|
+
}
|
|
256
|
+
inspect(token, method) {
|
|
257
|
+
const label = tokenLabel(token);
|
|
258
|
+
const keys = [token];
|
|
259
|
+
const name = typeof token === "function" ? token.name : typeof token === "string" ? token : void 0;
|
|
260
|
+
if (name !== void 0 && this.nameIndex.has(name)) keys.push(this.nameIndex.get(name));
|
|
261
|
+
for (const key of keys) {
|
|
262
|
+
const state = this.states.get(key)?.get(method);
|
|
263
|
+
if (state) return state.inspection();
|
|
264
|
+
}
|
|
265
|
+
throw new UnwatchedInspectionError(label, method);
|
|
266
|
+
}
|
|
267
|
+
/** Repeatable snapshot check of all exact expectations. */
|
|
268
|
+
verify() {
|
|
269
|
+
const failures = [];
|
|
270
|
+
for (const byMethod of this.states.values()) {
|
|
271
|
+
for (const state of byMethod.values()) {
|
|
272
|
+
const expectation = state.expectation;
|
|
273
|
+
if (!expectation) continue;
|
|
274
|
+
if (state.actual !== expectation.expected) {
|
|
275
|
+
failures.push({
|
|
276
|
+
token: state.tokenName,
|
|
277
|
+
method: state.method,
|
|
278
|
+
expected: expectation.expected,
|
|
279
|
+
actual: state.actual
|
|
280
|
+
});
|
|
281
|
+
}
|
|
329
282
|
}
|
|
330
283
|
}
|
|
331
|
-
|
|
332
|
-
kill(signal = "SIGTERM") {
|
|
333
|
-
if (this.flushed || this.killed) return;
|
|
334
|
-
this.killed = true;
|
|
335
|
-
const exit = { kind: "exit", exitCode: null, signal };
|
|
336
|
-
for (const listener of this.listeners) listener(exit);
|
|
337
|
-
this.resolveResult({ exitCode: null, signal, stdout: "", stderr: "" });
|
|
338
|
-
this.onClosed();
|
|
339
|
-
}
|
|
340
|
-
wait() {
|
|
341
|
-
return this.resultPromise;
|
|
284
|
+
if (failures.length) throw new VerificationError(failures);
|
|
342
285
|
}
|
|
343
286
|
};
|
|
344
|
-
function
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
287
|
+
function isPromiseLike(value) {
|
|
288
|
+
return !!value && typeof value.then === "function";
|
|
289
|
+
}
|
|
290
|
+
function recordOutcome(call, outcome, threw) {
|
|
291
|
+
if (!threw && isPromiseLike(outcome)) {
|
|
292
|
+
call.pending = true;
|
|
293
|
+
outcome.then(
|
|
294
|
+
(value) => {
|
|
295
|
+
call.result = value;
|
|
296
|
+
call.pending = false;
|
|
297
|
+
},
|
|
298
|
+
(error) => {
|
|
299
|
+
call.error = error;
|
|
300
|
+
call.pending = false;
|
|
301
|
+
}
|
|
302
|
+
);
|
|
303
|
+
return outcome;
|
|
356
304
|
}
|
|
357
|
-
|
|
305
|
+
call.pending = false;
|
|
306
|
+
if (threw) call.error = outcome;
|
|
307
|
+
else call.result = outcome;
|
|
308
|
+
return outcome;
|
|
358
309
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
clock;
|
|
368
|
-
entries = /* @__PURE__ */ new Map();
|
|
369
|
-
get(key, codec) {
|
|
370
|
-
const entry = this.entries.get(key);
|
|
371
|
-
if (entry === void 0) return Promise.resolve(null);
|
|
372
|
-
if (entry.expiresAt !== null && !this.clock.now().isBefore(entry.expiresAt)) {
|
|
373
|
-
this.entries.delete(key);
|
|
374
|
-
return Promise.resolve(null);
|
|
310
|
+
function createMethodDecorator(registry) {
|
|
311
|
+
const wrappers = /* @__PURE__ */ new WeakMap();
|
|
312
|
+
return function decorate(instance, token) {
|
|
313
|
+
const entries = registry.matchEntries(token);
|
|
314
|
+
if (entries.length === 0) return instance;
|
|
315
|
+
if (instance === null || typeof instance !== "object" && typeof instance !== "function") {
|
|
316
|
+
throw new NonObjectMethodTargetError(tokenLabel(token));
|
|
375
317
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
delete(key) {
|
|
384
|
-
this.entries.delete(key);
|
|
385
|
-
return Promise.resolve();
|
|
386
|
-
}
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
// src/storage/memory_object_store.ts
|
|
390
|
-
var import_runtime8 = require("@noego/runtime");
|
|
391
|
-
var MemoryObjectStore = class extends import_runtime8.ObjectStore {
|
|
392
|
-
objects = /* @__PURE__ */ new Map();
|
|
393
|
-
nextVersion = 0;
|
|
394
|
-
read(key) {
|
|
395
|
-
return Promise.resolve(this.objects.get(key) ?? null);
|
|
396
|
-
}
|
|
397
|
-
write(key, object) {
|
|
398
|
-
const version = (0, import_runtime8.brandId)(`v${++this.nextVersion}`, "ObjectVersion");
|
|
399
|
-
this.objects.set(key, {
|
|
400
|
-
bytes: object.bytes.slice(),
|
|
401
|
-
contentType: object.contentType,
|
|
402
|
-
metadata: object.metadata !== void 0 ? new Map(object.metadata) : void 0,
|
|
403
|
-
version
|
|
404
|
-
});
|
|
405
|
-
return Promise.resolve(version);
|
|
406
|
-
}
|
|
407
|
-
delete(key) {
|
|
408
|
-
this.objects.delete(key);
|
|
409
|
-
return Promise.resolve();
|
|
410
|
-
}
|
|
411
|
-
};
|
|
412
|
-
|
|
413
|
-
// src/events/recording_event_bus.ts
|
|
414
|
-
var import_runtime9 = require("@noego/runtime");
|
|
415
|
-
var RecordingEventBus = class extends import_runtime9.EventBus {
|
|
416
|
-
constructor(mode = "immediate") {
|
|
417
|
-
super();
|
|
418
|
-
this.mode = mode;
|
|
419
|
-
}
|
|
420
|
-
mode;
|
|
421
|
-
published = [];
|
|
422
|
-
handlers = /* @__PURE__ */ new Set();
|
|
423
|
-
queue = [];
|
|
424
|
-
async publish(event) {
|
|
425
|
-
this.published.push(event);
|
|
426
|
-
if (this.mode === "queued") {
|
|
427
|
-
this.queue.push(event);
|
|
428
|
-
return;
|
|
318
|
+
if (wrappers.has(instance)) return wrappers.get(instance);
|
|
319
|
+
const effective = /* @__PURE__ */ new Map();
|
|
320
|
+
for (const entry of entries) {
|
|
321
|
+
for (const [method, descriptor] of entry.methods) {
|
|
322
|
+
effective.set(method, { entryKey: entry.key, descriptor });
|
|
323
|
+
}
|
|
429
324
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
this.handlers.add(handler);
|
|
434
|
-
const bus = this;
|
|
435
|
-
const subscription = {
|
|
436
|
-
active: true,
|
|
437
|
-
unsubscribe() {
|
|
438
|
-
if (!subscription.active) return;
|
|
439
|
-
subscription.active = false;
|
|
440
|
-
bus.handlers.delete(handler);
|
|
325
|
+
for (const method of effective.keys()) {
|
|
326
|
+
if (typeof instance[method] !== "function") {
|
|
327
|
+
throw new MethodNotCallableError(tokenLabel(token), method);
|
|
441
328
|
}
|
|
442
|
-
};
|
|
443
|
-
return subscription;
|
|
444
|
-
}
|
|
445
|
-
/** Deliver buffered events (queued mode) in publication order. */
|
|
446
|
-
async deliverQueued() {
|
|
447
|
-
while (this.queue.length > 0) {
|
|
448
|
-
const event = this.queue.shift();
|
|
449
|
-
await this.deliver(event);
|
|
450
329
|
}
|
|
330
|
+
const methodCache = /* @__PURE__ */ new Map();
|
|
331
|
+
const proxy = new Proxy(instance, {
|
|
332
|
+
get(target, prop, receiver) {
|
|
333
|
+
if (typeof prop === "string" && effective.has(prop)) {
|
|
334
|
+
let wrapped = methodCache.get(prop);
|
|
335
|
+
if (!wrapped) {
|
|
336
|
+
const { entryKey, descriptor } = effective.get(prop);
|
|
337
|
+
wrapped = buildMethodWrapper(target, prop, descriptor, registry.state(entryKey, prop));
|
|
338
|
+
methodCache.set(prop, wrapped);
|
|
339
|
+
}
|
|
340
|
+
return wrapped;
|
|
341
|
+
}
|
|
342
|
+
if (prop === CONTEXT_WRAPPED || prop === CONTEXT_OWNER) return target[prop];
|
|
343
|
+
return Reflect.get(target, prop, receiver);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
wrappers.set(instance, proxy);
|
|
347
|
+
return proxy;
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function buildMethodWrapper(target, method, descriptor, state) {
|
|
351
|
+
const callOriginal = (self, args) => {
|
|
352
|
+
const fn = target[method];
|
|
353
|
+
return fn.apply(self === void 0 ? target : self, args);
|
|
354
|
+
};
|
|
355
|
+
if (typeof descriptor === "function") {
|
|
356
|
+
const replacement = descriptor((...args) => callOriginal(target, args));
|
|
357
|
+
return function(...args) {
|
|
358
|
+
return replacement.apply(this, args);
|
|
359
|
+
};
|
|
451
360
|
}
|
|
452
|
-
|
|
453
|
-
|
|
361
|
+
if (!isDescriptor(descriptor)) {
|
|
362
|
+
throw new TypeError(`Invalid method descriptor for ${method}`);
|
|
454
363
|
}
|
|
455
|
-
|
|
456
|
-
if (
|
|
457
|
-
|
|
364
|
+
return function(...args) {
|
|
365
|
+
if (!state) throw new TypeError(`Missing method state for ${method}`);
|
|
366
|
+
const expectation = state.expectation;
|
|
367
|
+
const attempted = state.actual + 1;
|
|
368
|
+
if (expectation && attempted > expectation.expected) {
|
|
369
|
+
state.actual = attempted;
|
|
370
|
+
throw new ExpectationOverflowError(state.tokenName, method, expectation.expected, attempted);
|
|
458
371
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
372
|
+
state.actual = attempted;
|
|
373
|
+
const call = {
|
|
374
|
+
index: attempted,
|
|
375
|
+
args: [...args],
|
|
376
|
+
pending: false,
|
|
377
|
+
timestamp: Date.now()
|
|
378
|
+
};
|
|
379
|
+
state.calls.push(call);
|
|
380
|
+
let behavior;
|
|
381
|
+
const base = descriptor.kind === "expect" ? descriptor.behavior ?? "original-effective" : descriptor.kind === "watch" ? descriptor.wrapper ?? "original-effective" : descriptor;
|
|
382
|
+
if (base !== "original-effective" && typeof base !== "function" && base !== void 0 && base.kind === "calls") {
|
|
383
|
+
if (state.scriptCursor >= base.script.length) {
|
|
384
|
+
throw new CallScriptExhaustedError(state.tokenName, method, base.script.length, attempted);
|
|
385
|
+
}
|
|
386
|
+
behavior = base.script[state.scriptCursor];
|
|
387
|
+
state.scriptCursor += 1;
|
|
388
|
+
} else {
|
|
389
|
+
behavior = base;
|
|
463
390
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
var NoopLogSink = class extends import_runtime10.LogSink {
|
|
488
|
-
emit(_envelope) {
|
|
489
|
-
}
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
// src/contracts/clock.contract.ts
|
|
493
|
-
function runClockContract(name, setup) {
|
|
494
|
-
describe(`Clock contract: ${name}`, () => {
|
|
495
|
-
it("returns a monotonically non-decreasing instant across consecutive reads", () => {
|
|
496
|
-
const { clock } = setup();
|
|
497
|
-
const first = clock.now();
|
|
498
|
-
const second = clock.now();
|
|
499
|
-
expect(second.isBefore(first)).toBe(false);
|
|
500
|
-
});
|
|
501
|
-
it("nowMs agrees with now()", () => {
|
|
502
|
-
const { clock } = setup();
|
|
503
|
-
const instant = clock.now();
|
|
504
|
-
const ms = clock.nowMs();
|
|
505
|
-
expect(ms).toBeGreaterThanOrEqual(instant.epochMilliseconds);
|
|
506
|
-
});
|
|
507
|
-
});
|
|
391
|
+
try {
|
|
392
|
+
let outcome;
|
|
393
|
+
if (behavior === "original-effective") {
|
|
394
|
+
outcome = callOriginal(this, args);
|
|
395
|
+
} else if (typeof behavior === "function") {
|
|
396
|
+
outcome = behavior((...inner) => callOriginal(this, inner)).apply(this, args);
|
|
397
|
+
} else if (behavior.kind === "returns") {
|
|
398
|
+
outcome = behavior.value;
|
|
399
|
+
} else if (behavior.kind === "throws") {
|
|
400
|
+
throw behavior.error;
|
|
401
|
+
} else if (behavior.kind === "original") {
|
|
402
|
+
outcome = callOriginal(this, args);
|
|
403
|
+
} else {
|
|
404
|
+
throw new TypeError(`Nested test.calls() scripts are not supported (${method}).`);
|
|
405
|
+
}
|
|
406
|
+
return recordOutcome(call, outcome, false);
|
|
407
|
+
} catch (error) {
|
|
408
|
+
recordOutcome(call, error, true);
|
|
409
|
+
throw error;
|
|
410
|
+
}
|
|
411
|
+
};
|
|
508
412
|
}
|
|
509
413
|
|
|
510
|
-
// src/
|
|
511
|
-
var
|
|
512
|
-
function
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
const { store } = setup();
|
|
521
|
-
const key = (0, import_runtime11.storageKey)("k1");
|
|
522
|
-
await store.put(key, { n: 42 }, codec);
|
|
523
|
-
await expect(store.get(key, codec)).resolves.toEqual({ n: 42 });
|
|
524
|
-
});
|
|
525
|
-
it("overwrites an existing value", async () => {
|
|
526
|
-
const { store } = setup();
|
|
527
|
-
const key = (0, import_runtime11.storageKey)("k1");
|
|
528
|
-
await store.put(key, { n: 1 }, codec);
|
|
529
|
-
await store.put(key, { n: 2 }, codec);
|
|
530
|
-
await expect(store.get(key, codec)).resolves.toEqual({ n: 2 });
|
|
531
|
-
});
|
|
532
|
-
it("deletes a value and tolerates deleting a missing key", async () => {
|
|
533
|
-
const { store } = setup();
|
|
534
|
-
const key = (0, import_runtime11.storageKey)("k1");
|
|
535
|
-
await store.put(key, { n: 1 }, codec);
|
|
536
|
-
await store.delete(key);
|
|
537
|
-
await expect(store.get(key, codec)).resolves.toBeNull();
|
|
538
|
-
await expect(store.delete((0, import_runtime11.storageKey)("missing"))).resolves.toBeUndefined();
|
|
539
|
-
});
|
|
540
|
-
it("expires values after their TTL when TTL is supported", async () => {
|
|
541
|
-
const context = setup();
|
|
542
|
-
if (!context.supportsTtl) return;
|
|
543
|
-
const key = (0, import_runtime11.storageKey)("expiring");
|
|
544
|
-
await context.store.put(key, { n: 7 }, codec, { timeToLive: import_runtime11.Duration.ofSeconds(30) });
|
|
545
|
-
await expect(context.store.get(key, codec)).resolves.toEqual({ n: 7 });
|
|
546
|
-
await context.advanceBy(import_runtime11.Duration.ofSeconds(31));
|
|
547
|
-
await expect(context.store.get(key, codec)).resolves.toBeNull();
|
|
548
|
-
});
|
|
549
|
-
it("keeps values without a TTL alive as time passes", async () => {
|
|
550
|
-
const context = setup();
|
|
551
|
-
const key = (0, import_runtime11.storageKey)("durable");
|
|
552
|
-
await context.store.put(key, { n: 9 }, codec);
|
|
553
|
-
await context.advanceBy(import_runtime11.Duration.ofMinutes(60));
|
|
554
|
-
await expect(context.store.get(key, codec)).resolves.toEqual({ n: 9 });
|
|
555
|
-
});
|
|
556
|
-
});
|
|
414
|
+
// src/builder.ts
|
|
415
|
+
var COMPONENT_OPTIONS_KEY = /* @__PURE__ */ Symbol.for("ioc:component:options");
|
|
416
|
+
function* configEntries(config) {
|
|
417
|
+
if (config instanceof Map) {
|
|
418
|
+
for (const [key, value] of config) yield [key, false, value];
|
|
419
|
+
} else {
|
|
420
|
+
for (const key of [...Object.getOwnPropertyNames(config), ...Object.getOwnPropertySymbols(config)]) {
|
|
421
|
+
yield [key, typeof key === "string", config[key]];
|
|
422
|
+
}
|
|
423
|
+
}
|
|
557
424
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
function runObjectStoreContract(name, setup) {
|
|
562
|
-
const bytes = (...values) => new Uint8Array(values);
|
|
563
|
-
describe(`ObjectStore contract: ${name}`, () => {
|
|
564
|
-
it("returns null for a missing object", async () => {
|
|
565
|
-
const { store } = setup();
|
|
566
|
-
await expect(store.read((0, import_runtime12.objectKey)("missing"))).resolves.toBeNull();
|
|
567
|
-
});
|
|
568
|
-
it("round-trips bytes, content type, and metadata", async () => {
|
|
569
|
-
const { store } = setup();
|
|
570
|
-
const key = (0, import_runtime12.objectKey)("docs/a.bin");
|
|
571
|
-
await store.write(key, {
|
|
572
|
-
bytes: bytes(1, 2, 3),
|
|
573
|
-
contentType: "application/octet-stream",
|
|
574
|
-
metadata: /* @__PURE__ */ new Map([["owner", "alice"]])
|
|
575
|
-
});
|
|
576
|
-
const stored = await store.read(key);
|
|
577
|
-
expect(stored).not.toBeNull();
|
|
578
|
-
expect([...stored.bytes]).toEqual([1, 2, 3]);
|
|
579
|
-
expect(stored.contentType).toBe("application/octet-stream");
|
|
580
|
-
expect(stored.metadata?.get("owner")).toBe("alice");
|
|
581
|
-
});
|
|
582
|
-
it("issues a new version on every write", async () => {
|
|
583
|
-
const { store } = setup();
|
|
584
|
-
const key = (0, import_runtime12.objectKey)("docs/a.bin");
|
|
585
|
-
const v1 = await store.write(key, { bytes: bytes(1), contentType: "text/plain" });
|
|
586
|
-
const v2 = await store.write(key, { bytes: bytes(2), contentType: "text/plain" });
|
|
587
|
-
expect(v1).not.toBe(v2);
|
|
588
|
-
const stored = await store.read(key);
|
|
589
|
-
expect(stored.version).toBe(v2);
|
|
590
|
-
expect([...stored.bytes]).toEqual([2]);
|
|
591
|
-
});
|
|
592
|
-
it("deletes an object and tolerates deleting a missing key", async () => {
|
|
593
|
-
const { store } = setup();
|
|
594
|
-
const key = (0, import_runtime12.objectKey)("docs/a.bin");
|
|
595
|
-
await store.write(key, { bytes: bytes(1), contentType: "text/plain" });
|
|
596
|
-
await store.delete(key);
|
|
597
|
-
await expect(store.read(key)).resolves.toBeNull();
|
|
598
|
-
await expect(store.delete((0, import_runtime12.objectKey)("missing"))).resolves.toBeUndefined();
|
|
599
|
-
});
|
|
600
|
-
it("does not expose caller mutations of written bytes", async () => {
|
|
601
|
-
const { store } = setup();
|
|
602
|
-
const key = (0, import_runtime12.objectKey)("docs/mutable.bin");
|
|
603
|
-
const input = bytes(9);
|
|
604
|
-
await store.write(key, { bytes: input, contentType: "text/plain" });
|
|
605
|
-
input[0] = 0;
|
|
606
|
-
const stored = await store.read(key);
|
|
607
|
-
expect([...stored.bytes]).toEqual([9]);
|
|
608
|
-
});
|
|
609
|
-
});
|
|
425
|
+
function componentScope(cls) {
|
|
426
|
+
const options = typeof Reflect !== "undefined" && Reflect.getMetadata ? Reflect.getMetadata(COMPONENT_OPTIONS_KEY, cls) : void 0;
|
|
427
|
+
return options?.scope;
|
|
610
428
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
describe(`EventBus contract: ${name}`, () => {
|
|
616
|
-
it("delivers published events to subscribers in order", async () => {
|
|
617
|
-
const { bus, deliver } = setup();
|
|
618
|
-
const seen = [];
|
|
619
|
-
const subscription = bus.subscribe((event) => {
|
|
620
|
-
seen.push(event.value);
|
|
621
|
-
});
|
|
622
|
-
await bus.publish(testEvent(1));
|
|
623
|
-
await bus.publish(testEvent(2));
|
|
624
|
-
await deliver();
|
|
625
|
-
expect(seen).toEqual([1, 2]);
|
|
626
|
-
subscription.unsubscribe();
|
|
627
|
-
});
|
|
628
|
-
it("stops delivering after unsubscribe", async () => {
|
|
629
|
-
const { bus, deliver } = setup();
|
|
630
|
-
const seen = [];
|
|
631
|
-
const subscription = bus.subscribe((event) => {
|
|
632
|
-
seen.push(event.value);
|
|
633
|
-
});
|
|
634
|
-
await bus.publish(testEvent(1));
|
|
635
|
-
await deliver();
|
|
636
|
-
subscription.unsubscribe();
|
|
637
|
-
await bus.publish(testEvent(2));
|
|
638
|
-
await deliver();
|
|
639
|
-
expect(seen).toEqual([1]);
|
|
640
|
-
});
|
|
641
|
-
it("supports multiple subscribers", async () => {
|
|
642
|
-
const { bus, deliver } = setup();
|
|
643
|
-
const a = [];
|
|
644
|
-
const b = [];
|
|
645
|
-
const sa = bus.subscribe((event) => {
|
|
646
|
-
a.push(event.value);
|
|
647
|
-
});
|
|
648
|
-
const sb = bus.subscribe((event) => {
|
|
649
|
-
b.push(event.value);
|
|
650
|
-
});
|
|
651
|
-
await bus.publish(testEvent(5));
|
|
652
|
-
await deliver();
|
|
653
|
-
expect(a).toEqual([5]);
|
|
654
|
-
expect(b).toEqual([5]);
|
|
655
|
-
sa.unsubscribe();
|
|
656
|
-
sb.unsubscribe();
|
|
657
|
-
});
|
|
658
|
-
it("marks subscriptions inactive after unsubscribe and tolerates double unsubscribe", async () => {
|
|
659
|
-
const { bus, deliver } = setup();
|
|
660
|
-
const seen = [];
|
|
661
|
-
const subscription = bus.subscribe((event) => {
|
|
662
|
-
seen.push(event.value);
|
|
663
|
-
});
|
|
664
|
-
await bus.publish(testEvent(1));
|
|
665
|
-
await deliver();
|
|
666
|
-
expect(seen).toEqual([1]);
|
|
667
|
-
expect(subscription.active).toBe(true);
|
|
668
|
-
subscription.unsubscribe();
|
|
669
|
-
expect(subscription.active).toBe(false);
|
|
670
|
-
subscription.unsubscribe();
|
|
671
|
-
expect(subscription.active).toBe(false);
|
|
672
|
-
});
|
|
673
|
-
});
|
|
429
|
+
function lifetimeToLoadAs(lifetime) {
|
|
430
|
+
if (lifetime === "singleton") return import_ioc.LoadAs.Singleton;
|
|
431
|
+
if (lifetime === "scoped") return import_ioc.LoadAs.Scoped;
|
|
432
|
+
return import_ioc.LoadAs.Transient;
|
|
674
433
|
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
434
|
+
var TestIocBuilder = class _TestIocBuilder {
|
|
435
|
+
constructor(log) {
|
|
436
|
+
this.log = log;
|
|
437
|
+
Object.freeze(this);
|
|
438
|
+
}
|
|
439
|
+
log;
|
|
440
|
+
/** @internal */
|
|
441
|
+
static create(inputs) {
|
|
442
|
+
return new _TestIocBuilder([]).useAll(inputs);
|
|
443
|
+
}
|
|
444
|
+
/** @internal — read by .use(builderPreset) */
|
|
445
|
+
get writes() {
|
|
446
|
+
return this.log;
|
|
447
|
+
}
|
|
448
|
+
derive(writes) {
|
|
449
|
+
return new _TestIocBuilder([...this.log, ...writes]);
|
|
450
|
+
}
|
|
451
|
+
useAll(inputs) {
|
|
452
|
+
let builder = this;
|
|
453
|
+
for (const input of inputs) builder = builder.use(input);
|
|
454
|
+
return builder;
|
|
455
|
+
}
|
|
456
|
+
/** Apply a reusable composition preset: an ApplicationModule or a builder. */
|
|
457
|
+
use(preset) {
|
|
458
|
+
if (preset instanceof _TestIocBuilder) {
|
|
459
|
+
return this.derive([...preset.writes]);
|
|
460
|
+
}
|
|
461
|
+
return this.derive([{ op: "use", module: preset }]);
|
|
462
|
+
}
|
|
463
|
+
/** Replace the implementation for IoC class tokens in the built environment. */
|
|
464
|
+
classes(config) {
|
|
465
|
+
const writes = [];
|
|
466
|
+
for (const [key, byName, implementation] of configEntries(config)) {
|
|
467
|
+
if (typeof implementation !== "function" || !implementation.prototype) {
|
|
468
|
+
throw new InvalidDescriptorError(
|
|
469
|
+
`.classes() value for "${tokenLabel(key)}" must be a class constructor.`
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
writes.push({ op: "classes", key, byName, implementation });
|
|
473
|
+
}
|
|
474
|
+
return this.derive(writes);
|
|
475
|
+
}
|
|
476
|
+
/** Replace IoC factory/provider registrations. */
|
|
477
|
+
functions(config) {
|
|
478
|
+
const writes = [];
|
|
479
|
+
for (const [key, byName, factory] of configEntries(config)) {
|
|
480
|
+
if (typeof factory !== "function") {
|
|
481
|
+
throw new InvalidDescriptorError(`.functions() value for "${tokenLabel(key)}" must be a function.`);
|
|
687
482
|
}
|
|
483
|
+
writes.push({ op: "functions", key, byName, factory });
|
|
688
484
|
}
|
|
689
|
-
return
|
|
485
|
+
return this.derive(writes);
|
|
690
486
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
487
|
+
/** Provide/replace IoC value registrations. */
|
|
488
|
+
values(config) {
|
|
489
|
+
const writes = [];
|
|
490
|
+
for (const [key, byName, value] of configEntries(config)) {
|
|
491
|
+
writes.push({ op: "values", key, byName, value });
|
|
492
|
+
}
|
|
493
|
+
return this.derive(writes);
|
|
494
|
+
}
|
|
495
|
+
/** Install method behavior/observation descriptors on IoC-managed instances. */
|
|
496
|
+
methods(config) {
|
|
497
|
+
const writes = [];
|
|
498
|
+
for (const [key, byName, methodsInput] of configEntries(config)) {
|
|
499
|
+
const methods = /* @__PURE__ */ new Map();
|
|
500
|
+
const entries = methodsInput instanceof Map ? methodsInput.entries() : Object.entries(methodsInput);
|
|
501
|
+
for (const [name, descriptor] of entries) {
|
|
502
|
+
if (typeof descriptor !== "function" && !isDescriptor(descriptor)) {
|
|
503
|
+
throw new InvalidDescriptorError(
|
|
504
|
+
`.methods() entry ${tokenLabel(key)}.${name} must be a test.* descriptor or a raw wrapper function.`
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
methods.set(name, descriptor);
|
|
508
|
+
}
|
|
509
|
+
writes.push({ op: "methods", key, byName, methods });
|
|
510
|
+
}
|
|
511
|
+
return this.derive(writes);
|
|
512
|
+
}
|
|
513
|
+
/** Materialize a fresh, isolated real-IoC environment. Non-consuming. */
|
|
514
|
+
async build() {
|
|
515
|
+
const root = (0, import_ioc.createContainer)();
|
|
516
|
+
if (typeof root.setInstanceDecorator !== "function") {
|
|
517
|
+
throw new MissingIocSeamError();
|
|
518
|
+
}
|
|
519
|
+
const knownByName = /* @__PURE__ */ new Map();
|
|
520
|
+
const knownLifetimes = /* @__PURE__ */ new Map();
|
|
521
|
+
const note = (token, loadAs) => {
|
|
522
|
+
if (typeof token === "function" && token.name) knownByName.set(token.name, token);
|
|
523
|
+
else if (typeof token === "string") knownByName.set(token, token);
|
|
524
|
+
if (loadAs !== void 0) knownLifetimes.set(token, loadAs);
|
|
525
|
+
};
|
|
526
|
+
const effective = /* @__PURE__ */ new Map();
|
|
527
|
+
const methodWrites = [];
|
|
528
|
+
const resolveKey = (key, byName, space) => {
|
|
529
|
+
if (!byName) return key;
|
|
530
|
+
const known = knownByName.get(key);
|
|
531
|
+
if (known !== void 0) return known;
|
|
532
|
+
if (space === "values" || space === "functions") return key;
|
|
533
|
+
throw new UnknownTokenKeyError(key, space, [...knownByName.keys()]);
|
|
534
|
+
};
|
|
535
|
+
for (const write of this.log) {
|
|
536
|
+
switch (write.op) {
|
|
537
|
+
case "use": {
|
|
538
|
+
for (const reg of (0, import_ioc.flattenModule)(write.module)) {
|
|
539
|
+
const loadAs = lifetimeToLoadAs(reg.lifetime);
|
|
540
|
+
note(reg.token, loadAs);
|
|
541
|
+
if (reg.kind === "class") {
|
|
542
|
+
note(reg.implementation, loadAs);
|
|
543
|
+
effective.set(reg.token, {
|
|
544
|
+
space: "class",
|
|
545
|
+
token: reg.token,
|
|
546
|
+
implementation: reg.implementation,
|
|
547
|
+
loadAs
|
|
548
|
+
});
|
|
549
|
+
knownLifetimes.set(reg.token, loadAs);
|
|
550
|
+
knownLifetimes.set(reg.implementation, loadAs);
|
|
551
|
+
} else if (reg.kind === "factory") {
|
|
552
|
+
effective.set(reg.token, {
|
|
553
|
+
space: "factory",
|
|
554
|
+
token: reg.token,
|
|
555
|
+
factory: reg.implementation,
|
|
556
|
+
loadAs,
|
|
557
|
+
deps: [...reg.dependencies]
|
|
558
|
+
});
|
|
559
|
+
} else {
|
|
560
|
+
effective.set(reg.token, { space: "value", token: reg.token, value: reg.implementation });
|
|
561
|
+
knownLifetimes.set(reg.token, import_ioc.LoadAs.Singleton);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
case "classes": {
|
|
567
|
+
const token = resolveKey(write.key, write.byName, "classes");
|
|
568
|
+
note(write.implementation, void 0);
|
|
569
|
+
if (!write.byName) note(token, void 0);
|
|
570
|
+
effective.set(token, {
|
|
571
|
+
space: "class",
|
|
572
|
+
token,
|
|
573
|
+
implementation: write.implementation,
|
|
574
|
+
// preserve configured lifetime unless the replacement declares its own scope
|
|
575
|
+
loadAs: componentScope(write.implementation) ?? knownLifetimes.get(token)
|
|
576
|
+
});
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
case "functions": {
|
|
580
|
+
const token = resolveKey(write.key, write.byName, "functions");
|
|
581
|
+
const prior = effective.get(token);
|
|
582
|
+
effective.set(token, {
|
|
583
|
+
space: "factory",
|
|
584
|
+
token,
|
|
585
|
+
factory: write.factory,
|
|
586
|
+
// preserve configured lifetime unless the scenario overrides it
|
|
587
|
+
loadAs: prior && prior.space === "factory" ? prior.loadAs : knownLifetimes.get(token),
|
|
588
|
+
deps: prior && prior.space === "factory" ? prior.deps : void 0
|
|
589
|
+
});
|
|
590
|
+
note(token, void 0);
|
|
591
|
+
break;
|
|
592
|
+
}
|
|
593
|
+
case "values": {
|
|
594
|
+
const token = resolveKey(write.key, write.byName, "values");
|
|
595
|
+
effective.set(token, { space: "value", token, value: write.value });
|
|
596
|
+
note(token, import_ioc.LoadAs.Singleton);
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
case "methods": {
|
|
600
|
+
const token = write.byName && knownByName.has(write.key) ? knownByName.get(write.key) : write.key;
|
|
601
|
+
methodWrites.push({
|
|
602
|
+
key: token,
|
|
603
|
+
byName: write.byName && !knownByName.has(write.key),
|
|
604
|
+
methods: new Map(write.methods)
|
|
605
|
+
});
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
for (const entry of effective.values()) {
|
|
611
|
+
if (entry.space === "class") {
|
|
612
|
+
this.registerClassBinding(root, entry.token, entry.implementation, entry.loadAs);
|
|
613
|
+
} else if (entry.space === "factory") {
|
|
614
|
+
root.registerFunction(entry.token, entry.factory, {
|
|
615
|
+
loadAs: entry.loadAs,
|
|
616
|
+
param: entry.deps
|
|
617
|
+
});
|
|
618
|
+
} else {
|
|
619
|
+
const value = entry.value;
|
|
620
|
+
root.registerFunction(entry.token, () => value, { loadAs: import_ioc.LoadAs.Singleton });
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
const registry = new WatchRegistry();
|
|
624
|
+
const mergedMethods = /* @__PURE__ */ new Map();
|
|
625
|
+
for (const write of methodWrites) {
|
|
626
|
+
const existing = mergedMethods.get(write.key);
|
|
627
|
+
if (existing) {
|
|
628
|
+
for (const [name, descriptor] of write.methods) existing.methods.set(name, descriptor);
|
|
629
|
+
} else {
|
|
630
|
+
mergedMethods.set(write.key, { byName: write.byName, methods: new Map(write.methods) });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
for (const [key, { byName, methods }] of mergedMethods) {
|
|
634
|
+
registry.addEntry({ key, byName, methods });
|
|
635
|
+
}
|
|
636
|
+
root.setInstanceDecorator(createMethodDecorator(registry));
|
|
637
|
+
const env = {
|
|
638
|
+
root,
|
|
639
|
+
get: (token, params) => root.get(token, params),
|
|
640
|
+
instance: (cls, params) => root.instance(cls, params),
|
|
641
|
+
extend: () => root.extend(),
|
|
642
|
+
verify: async () => registry.verify(),
|
|
643
|
+
dispose: () => root.dispose(),
|
|
644
|
+
[ENV_REGISTRY]: registry
|
|
645
|
+
};
|
|
646
|
+
return env;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Register a class binding. When token === implementation this is a plain
|
|
650
|
+
* class registration. Otherwise an alias factory resolves the implementation
|
|
651
|
+
* through real IoC, mirroring the implementation's effective lifetime so
|
|
652
|
+
* lifetime validation (captive-lifetime checks) stays honest.
|
|
653
|
+
*/
|
|
654
|
+
registerClassBinding(root, token, implementation, configuredLoadAs) {
|
|
655
|
+
const loadAs = configuredLoadAs ?? componentScope(implementation) ?? import_ioc.LoadAs.Transient;
|
|
656
|
+
root.registerClass(implementation, configuredLoadAs !== void 0 ? { loadAs } : void 0);
|
|
657
|
+
if (token === implementation) return;
|
|
658
|
+
if (loadAs === import_ioc.LoadAs.Singleton) {
|
|
659
|
+
root.registerFunction(token, () => root.get(implementation), {
|
|
660
|
+
loadAs: import_ioc.LoadAs.Singleton
|
|
661
|
+
});
|
|
662
|
+
} else {
|
|
663
|
+
root.registerFunction(token, (scope) => scope.get(implementation), {
|
|
664
|
+
loadAs,
|
|
665
|
+
param: [import_ioc.SCOPED_CONTAINER]
|
|
666
|
+
});
|
|
696
667
|
}
|
|
697
668
|
}
|
|
698
669
|
};
|
|
670
|
+
function testIoc(...inputs) {
|
|
671
|
+
return TestIocBuilder.create(inputs);
|
|
672
|
+
}
|
|
699
673
|
// Annotate the CommonJS export names for ESM import in node:
|
|
700
674
|
0 && (module.exports = {
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
runClockContract,
|
|
716
|
-
runEventBusContract,
|
|
717
|
-
runKeyValueStoreContract,
|
|
718
|
-
runObjectStoreContract,
|
|
719
|
-
testEvent
|
|
675
|
+
CallScriptExhaustedError,
|
|
676
|
+
ENV_REGISTRY,
|
|
677
|
+
ExpectationOverflowError,
|
|
678
|
+
InvalidDescriptorError,
|
|
679
|
+
MethodNotCallableError,
|
|
680
|
+
MissingIocSeamError,
|
|
681
|
+
NonObjectMethodTargetError,
|
|
682
|
+
TestIocBuilder,
|
|
683
|
+
TestingError,
|
|
684
|
+
UnknownTokenKeyError,
|
|
685
|
+
UnwatchedInspectionError,
|
|
686
|
+
VerificationError,
|
|
687
|
+
test,
|
|
688
|
+
testIoc
|
|
720
689
|
});
|
|
721
690
|
//# sourceMappingURL=index.cjs.map
|