@cmodjs/core 8.3.0 → 8.3.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/testing.js CHANGED
@@ -245,26 +245,48 @@ export function testMod(definition, options = {}) {
245
245
  after,
246
246
  report,
247
247
  });
248
- const providedInSession = given.map(({ name: named, mod, program, methods }) => {
249
- if (program !== undefined)
250
- return undefined;
248
+ const inSession = (index) => {
249
+ const { name: named, mod, methods } = given[index];
251
250
  const made = sessionMods.get(mod) ?? createProviders({ name: mod, call: (_to, method, input) => served.answer(method, input, mod) ?? Promise.reject(new Error(`${mod} reaches no ${method} in the test kit.`)), changed: () => undefined, after, report });
252
251
  sessionMods.set(mod, made);
253
252
  return made.provide(named, methods);
254
- });
253
+ };
254
+ const asProgram = (index) => {
255
+ const { name: named, mod, program, device, methods } = given[index];
256
+ return { name: named, mod, program: program, ...(device === undefined ? {} : { device }), methods };
257
+ };
258
+ const isFirst = (index) => given[index]?.isStarted !== false;
259
+ const providedInSession = given.map((each, index) => (each.program === undefined && isFirst(index) ? inSession(index) : undefined));
260
+ const late = new Map();
261
+ const startLate = (index) => {
262
+ const known = late.get(index);
263
+ if (known !== undefined)
264
+ return known;
265
+ const started = fakeProviders().then(async () => (given[index]?.program === undefined ? inSession(index) : (await fake.serve.provide([asProgram(index)], { after, report }))[0]));
266
+ late.set(index, started);
267
+ return started;
268
+ };
255
269
  let fakesReady;
256
270
  const fakeProviders = () => (fakesReady ??= (async () => {
257
- const programs = await fake.serve.provide(given.flatMap(({ name: named, mod, program, device, methods }) => (program === undefined ? [] : [{ name: named, mod, program, ...(device === undefined ? {} : { device }), methods }])), { after, report });
258
- let next = 0;
259
- return providedInSession.map((inSession) => inSession ?? programs[next++]);
271
+ const first = given.flatMap((each, index) => (each.program !== undefined && isFirst(index) ? [index] : []));
272
+ const programs = await fake.serve.provide(first.map(asProgram), { after, report });
273
+ return given.map((_each, index) => providedInSession[index] ?? (first.includes(index) ? programs[first.indexOf(index)] : undefined));
260
274
  })());
261
275
  const fakeProvider = (index) => {
262
276
  const reached = async () => {
263
- const each = (await fakeProviders())[index];
277
+ const started = late.get(index);
278
+ const each = (await fakeProviders())[index] ?? (started === undefined ? undefined : await started);
279
+ if (each === undefined)
280
+ throw new Error(`The test kit's fake provider ${index} has isStarted: false, so start() it before it tells, asks, or leaves.`);
264
281
  await fake.settle();
265
282
  return each;
266
283
  };
267
284
  return {
285
+ start: () => {
286
+ if (isFirst(index))
287
+ throw new Error(`The test kit's fake provider ${index} runs from the start. Give it isStarted: false to start it later.`);
288
+ void modeling(startLate(index).then(() => fake.settle()));
289
+ },
268
290
  remove: () => void modeling(reached().then((each) => each.remove())),
269
291
  tell: (event, payload) => void modeling(reached().then((each) => each.tell(event, payload))),
270
292
  ask: (event, payload) => modeling(reached().then((each) => each.ask(event, payload))),