@directive-run/core 1.0.0 → 1.1.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/adapter-utils.cjs +1 -1
- package/dist/adapter-utils.d.cts +1 -1
- package/dist/adapter-utils.d.ts +1 -1
- package/dist/adapter-utils.js +1 -1
- package/dist/chunk-B2LVGKNZ.js +3 -0
- package/dist/chunk-B2LVGKNZ.js.map +1 -0
- package/dist/{chunk-MPOV2REO.js → chunk-DB5Z7EOG.js} +3 -3
- package/dist/chunk-DB5Z7EOG.js.map +1 -0
- package/dist/chunk-GSXCAMNZ.js +2 -0
- package/dist/chunk-GSXCAMNZ.js.map +1 -0
- package/dist/chunk-NZ6EK6W5.cjs +3 -0
- package/dist/chunk-NZ6EK6W5.cjs.map +1 -0
- package/dist/{chunk-3H24Z7WM.cjs → chunk-UBI6S2N3.cjs} +3 -3
- package/dist/chunk-UBI6S2N3.cjs.map +1 -0
- package/dist/chunk-WFIJ6OST.cjs +2 -0
- package/dist/chunk-WFIJ6OST.cjs.map +1 -0
- package/dist/{helpers-Bd_P3ZTY.d.ts → helpers-B8RS-sa_.d.ts} +2 -8
- package/dist/{helpers-CU3Il0tz.d.cts → helpers-CroApnZa.d.cts} +2 -8
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internals.cjs +1 -1
- package/dist/internals.d.cts +11 -13
- package/dist/internals.d.ts +11 -13
- package/dist/internals.js +1 -1
- package/dist/plugins/index.d.cts +1 -1
- package/dist/plugins/index.d.ts +1 -1
- package/dist/{plugins-O-3VHejK.d.cts → plugins-day_qfBB.d.cts} +84 -1
- package/dist/{plugins-O-3VHejK.d.ts → plugins-day_qfBB.d.ts} +84 -1
- package/dist/system-6JXMY66X.cjs +2 -0
- package/dist/{system-QQYBNE7A.cjs.map → system-6JXMY66X.cjs.map} +1 -1
- package/dist/system-I534ZO6E.js +2 -0
- package/dist/{system-AGVKMAYF.js.map → system-I534ZO6E.js.map} +1 -1
- package/dist/testing.cjs +1 -1
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +77 -13
- package/dist/testing.d.ts +77 -13
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/worker.cjs +1 -1
- package/dist/worker.d.cts +1 -1
- package/dist/worker.d.ts +1 -1
- package/dist/worker.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-3H24Z7WM.cjs.map +0 -1
- package/dist/chunk-AUSBUCOL.cjs +0 -3
- package/dist/chunk-AUSBUCOL.cjs.map +0 -1
- package/dist/chunk-EFBBEXDA.js +0 -2
- package/dist/chunk-EFBBEXDA.js.map +0 -1
- package/dist/chunk-ER6QUYTL.cjs +0 -2
- package/dist/chunk-ER6QUYTL.cjs.map +0 -1
- package/dist/chunk-MPOV2REO.js.map +0 -1
- package/dist/chunk-YWKFNZQV.js +0 -3
- package/dist/chunk-YWKFNZQV.js.map +0 -1
- package/dist/system-AGVKMAYF.js +0 -2
- package/dist/system-QQYBNE7A.cjs +0 -2
package/dist/testing.d.cts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import { k as ModulesMap, l as CreateSystemOptionsNamed, o as Requirement, M as ModuleSchema, i as CreateSystemOptionsSingle, N as NamespacedSystem, R as RequirementWithId, j as SingleModuleSystem,
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Testing Utilities - Helpers for testing Directive systems
|
|
5
|
-
*
|
|
6
|
-
* Features:
|
|
7
|
-
* - Mock resolvers with manual resolve/reject
|
|
8
|
-
* - Fake timers integration (works with Vitest/Jest fake timers)
|
|
9
|
-
* - Assertion helpers
|
|
10
|
-
* - Facts history tracking
|
|
11
|
-
* - Pending requirements tracking
|
|
12
|
-
*/
|
|
1
|
+
import { k as ModulesMap, l as CreateSystemOptionsNamed, o as Requirement, M as ModuleSchema, i as CreateSystemOptionsSingle, N as NamespacedSystem, R as RequirementWithId, j as SingleModuleSystem, V as ObservationEvent, $ as SystemInspection } from './plugins-day_qfBB.cjs';
|
|
13
2
|
|
|
14
3
|
/**
|
|
15
4
|
* Flush all pending microtasks by awaiting multiple rounds of `Promise.resolve()`.
|
|
@@ -447,5 +436,80 @@ declare function assertNotDynamic(system: {
|
|
|
447
436
|
isDynamic(id: string): boolean;
|
|
448
437
|
};
|
|
449
438
|
}, type: "constraint" | "resolver" | "derivation" | "effect", id: string): void;
|
|
439
|
+
/** Coverage report for a Directive system. */
|
|
440
|
+
interface CoverageReport {
|
|
441
|
+
/** Constraints that evaluated to true at least once. */
|
|
442
|
+
constraintsHit: Set<string>;
|
|
443
|
+
/** Constraints that never evaluated to true. */
|
|
444
|
+
constraintsMissed: Set<string>;
|
|
445
|
+
/** Resolvers that started at least once. */
|
|
446
|
+
resolversRun: Set<string>;
|
|
447
|
+
/** Resolvers that never started. */
|
|
448
|
+
resolversMissed: Set<string>;
|
|
449
|
+
/** Effects that ran at least once. */
|
|
450
|
+
effectsRun: Set<string>;
|
|
451
|
+
/** Derivations that recomputed at least once. */
|
|
452
|
+
derivationsComputed: Set<string>;
|
|
453
|
+
/** Coverage percentage (constraintsHit / total constraints). */
|
|
454
|
+
constraintCoverage: number;
|
|
455
|
+
/** Coverage percentage (resolversRun / total resolvers). */
|
|
456
|
+
resolverCoverage: number;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Track which constraints, resolvers, effects, and derivations are exercised
|
|
460
|
+
* during a test scenario. Returns a coverage report after the scenario runs.
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
* ```typescript
|
|
464
|
+
* const { run, report } = createCoverageTracker(system);
|
|
465
|
+
*
|
|
466
|
+
* await run(async () => {
|
|
467
|
+
* system.facts.userId = 123;
|
|
468
|
+
* await system.settle();
|
|
469
|
+
* system.facts.userId = 0;
|
|
470
|
+
* await system.settle();
|
|
471
|
+
* });
|
|
472
|
+
*
|
|
473
|
+
* const coverage = report();
|
|
474
|
+
* expect(coverage.constraintCoverage).toBe(1); // All constraints hit
|
|
475
|
+
* expect(coverage.constraintsMissed.size).toBe(0);
|
|
476
|
+
* ```
|
|
477
|
+
*/
|
|
478
|
+
declare function createCoverageTracker(system: SingleModuleSystem<any> | NamespacedSystem<any>): {
|
|
479
|
+
/** Run a test scenario while tracking coverage. */
|
|
480
|
+
run: (scenario: () => Promise<void> | void) => Promise<void>;
|
|
481
|
+
/** Get the coverage report. */
|
|
482
|
+
report: () => CoverageReport;
|
|
483
|
+
};
|
|
484
|
+
/**
|
|
485
|
+
* Create a test observer that collects all observation events.
|
|
486
|
+
* Useful for assertion-based testing of system behavior.
|
|
487
|
+
*
|
|
488
|
+
* @example
|
|
489
|
+
* ```typescript
|
|
490
|
+
* const observer = createTestObserver(system);
|
|
491
|
+
*
|
|
492
|
+
* system.facts.count = 5;
|
|
493
|
+
* await system.settle();
|
|
494
|
+
*
|
|
495
|
+
* expect(observer.events.filter(e => e.type === "constraint.evaluate")).toHaveLength(1);
|
|
496
|
+
* expect(observer.ofType("resolver.complete")).toHaveLength(1);
|
|
497
|
+
*
|
|
498
|
+
* observer.clear();
|
|
499
|
+
* observer.dispose();
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
declare function createTestObserver(system: SingleModuleSystem<any> | NamespacedSystem<any>): {
|
|
503
|
+
/** All collected events. */
|
|
504
|
+
events: ObservationEvent[];
|
|
505
|
+
/** Filter events by type. */
|
|
506
|
+
ofType: <T extends ObservationEvent["type"]>(type: T) => Extract<ObservationEvent, {
|
|
507
|
+
type: T;
|
|
508
|
+
}>[];
|
|
509
|
+
/** Clear collected events. */
|
|
510
|
+
clear: () => void;
|
|
511
|
+
/** Stop observing. */
|
|
512
|
+
dispose: () => void;
|
|
513
|
+
};
|
|
450
514
|
|
|
451
|
-
export { type CreateTestSystemOptions, type CreateTestSystemOptionsSingle, type FactChangeRecord, type FakeTimers, type MockResolver, type MockResolverContext, type MockResolverOptions, type TestSystem, type TestSystemBase, type TestSystemSingle, assertDynamic, assertNotDynamic, createFakeTimers, createMockResolver, createTestSystem, flushMicrotasks, mockResolver, settleWithFakeTimers };
|
|
515
|
+
export { type CoverageReport, type CreateTestSystemOptions, type CreateTestSystemOptionsSingle, type FactChangeRecord, type FakeTimers, type MockResolver, type MockResolverContext, type MockResolverOptions, type TestSystem, type TestSystemBase, type TestSystemSingle, assertDynamic, assertNotDynamic, createCoverageTracker, createFakeTimers, createMockResolver, createTestObserver, createTestSystem, flushMicrotasks, mockResolver, settleWithFakeTimers };
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import { k as ModulesMap, l as CreateSystemOptionsNamed, o as Requirement, M as ModuleSchema, i as CreateSystemOptionsSingle, N as NamespacedSystem, R as RequirementWithId, j as SingleModuleSystem,
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Testing Utilities - Helpers for testing Directive systems
|
|
5
|
-
*
|
|
6
|
-
* Features:
|
|
7
|
-
* - Mock resolvers with manual resolve/reject
|
|
8
|
-
* - Fake timers integration (works with Vitest/Jest fake timers)
|
|
9
|
-
* - Assertion helpers
|
|
10
|
-
* - Facts history tracking
|
|
11
|
-
* - Pending requirements tracking
|
|
12
|
-
*/
|
|
1
|
+
import { k as ModulesMap, l as CreateSystemOptionsNamed, o as Requirement, M as ModuleSchema, i as CreateSystemOptionsSingle, N as NamespacedSystem, R as RequirementWithId, j as SingleModuleSystem, V as ObservationEvent, $ as SystemInspection } from './plugins-day_qfBB.js';
|
|
13
2
|
|
|
14
3
|
/**
|
|
15
4
|
* Flush all pending microtasks by awaiting multiple rounds of `Promise.resolve()`.
|
|
@@ -447,5 +436,80 @@ declare function assertNotDynamic(system: {
|
|
|
447
436
|
isDynamic(id: string): boolean;
|
|
448
437
|
};
|
|
449
438
|
}, type: "constraint" | "resolver" | "derivation" | "effect", id: string): void;
|
|
439
|
+
/** Coverage report for a Directive system. */
|
|
440
|
+
interface CoverageReport {
|
|
441
|
+
/** Constraints that evaluated to true at least once. */
|
|
442
|
+
constraintsHit: Set<string>;
|
|
443
|
+
/** Constraints that never evaluated to true. */
|
|
444
|
+
constraintsMissed: Set<string>;
|
|
445
|
+
/** Resolvers that started at least once. */
|
|
446
|
+
resolversRun: Set<string>;
|
|
447
|
+
/** Resolvers that never started. */
|
|
448
|
+
resolversMissed: Set<string>;
|
|
449
|
+
/** Effects that ran at least once. */
|
|
450
|
+
effectsRun: Set<string>;
|
|
451
|
+
/** Derivations that recomputed at least once. */
|
|
452
|
+
derivationsComputed: Set<string>;
|
|
453
|
+
/** Coverage percentage (constraintsHit / total constraints). */
|
|
454
|
+
constraintCoverage: number;
|
|
455
|
+
/** Coverage percentage (resolversRun / total resolvers). */
|
|
456
|
+
resolverCoverage: number;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Track which constraints, resolvers, effects, and derivations are exercised
|
|
460
|
+
* during a test scenario. Returns a coverage report after the scenario runs.
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
* ```typescript
|
|
464
|
+
* const { run, report } = createCoverageTracker(system);
|
|
465
|
+
*
|
|
466
|
+
* await run(async () => {
|
|
467
|
+
* system.facts.userId = 123;
|
|
468
|
+
* await system.settle();
|
|
469
|
+
* system.facts.userId = 0;
|
|
470
|
+
* await system.settle();
|
|
471
|
+
* });
|
|
472
|
+
*
|
|
473
|
+
* const coverage = report();
|
|
474
|
+
* expect(coverage.constraintCoverage).toBe(1); // All constraints hit
|
|
475
|
+
* expect(coverage.constraintsMissed.size).toBe(0);
|
|
476
|
+
* ```
|
|
477
|
+
*/
|
|
478
|
+
declare function createCoverageTracker(system: SingleModuleSystem<any> | NamespacedSystem<any>): {
|
|
479
|
+
/** Run a test scenario while tracking coverage. */
|
|
480
|
+
run: (scenario: () => Promise<void> | void) => Promise<void>;
|
|
481
|
+
/** Get the coverage report. */
|
|
482
|
+
report: () => CoverageReport;
|
|
483
|
+
};
|
|
484
|
+
/**
|
|
485
|
+
* Create a test observer that collects all observation events.
|
|
486
|
+
* Useful for assertion-based testing of system behavior.
|
|
487
|
+
*
|
|
488
|
+
* @example
|
|
489
|
+
* ```typescript
|
|
490
|
+
* const observer = createTestObserver(system);
|
|
491
|
+
*
|
|
492
|
+
* system.facts.count = 5;
|
|
493
|
+
* await system.settle();
|
|
494
|
+
*
|
|
495
|
+
* expect(observer.events.filter(e => e.type === "constraint.evaluate")).toHaveLength(1);
|
|
496
|
+
* expect(observer.ofType("resolver.complete")).toHaveLength(1);
|
|
497
|
+
*
|
|
498
|
+
* observer.clear();
|
|
499
|
+
* observer.dispose();
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
declare function createTestObserver(system: SingleModuleSystem<any> | NamespacedSystem<any>): {
|
|
503
|
+
/** All collected events. */
|
|
504
|
+
events: ObservationEvent[];
|
|
505
|
+
/** Filter events by type. */
|
|
506
|
+
ofType: <T extends ObservationEvent["type"]>(type: T) => Extract<ObservationEvent, {
|
|
507
|
+
type: T;
|
|
508
|
+
}>[];
|
|
509
|
+
/** Clear collected events. */
|
|
510
|
+
clear: () => void;
|
|
511
|
+
/** Stop observing. */
|
|
512
|
+
dispose: () => void;
|
|
513
|
+
};
|
|
450
514
|
|
|
451
|
-
export { type CreateTestSystemOptions, type CreateTestSystemOptionsSingle, type FactChangeRecord, type FakeTimers, type MockResolver, type MockResolverContext, type MockResolverOptions, type TestSystem, type TestSystemBase, type TestSystemSingle, assertDynamic, assertNotDynamic, createFakeTimers, createMockResolver, createTestSystem, flushMicrotasks, mockResolver, settleWithFakeTimers };
|
|
515
|
+
export { type CoverageReport, type CreateTestSystemOptions, type CreateTestSystemOptionsSingle, type FactChangeRecord, type FakeTimers, type MockResolver, type MockResolverContext, type MockResolverOptions, type TestSystem, type TestSystemBase, type TestSystemSingle, assertDynamic, assertNotDynamic, createCoverageTracker, createFakeTimers, createMockResolver, createTestObserver, createTestSystem, flushMicrotasks, mockResolver, settleWithFakeTimers };
|
package/dist/testing.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {a}from'./chunk-
|
|
1
|
+
import {a}from'./chunk-DB5Z7EOG.js';import'./chunk-B2LVGKNZ.js';import'./chunk-GSXCAMNZ.js';import'./chunk-4CMO5OVZ.js';async function R(){for(let t=0;t<10;t++)await Promise.resolve();}async function T(t,s,r={}){let{totalTime:o=5e3,stepSize:c=10,maxIterations:l=1e3}=r,m=0,u=0;for(;m<o&&u<l;){if(await R(),t.inspect().inflight.length===0){await R();return}s(c),m+=c,u++;}let v=t.inspect();if(v.inflight.length>0){let f=v.inflight.map(p=>p.resolverId).join(", ");throw new Error(`[Directive] settleWithFakeTimers did not settle after ${o}ms. ${v.inflight.length} resolvers still inflight: ${f}`)}}function D(){let t=0,s=[];return {async advance(r){let o=t+r;for(;s.length>0&&s[0].time<=o;){let c=s.shift();t=c.time,c.callback(),await Promise.resolve();}t=o;},async next(){if(s.length===0)return;let r=s.shift();t=r.time,r.callback(),await Promise.resolve();},async runAll(){for(;s.length>0;)await this.next();},now(){return t},reset(){t=0,s.length=0;}}}function b(t){let s=typeof t=="string"?{requirement:(o=>o.type===t)}:t,r=s.calls??[];return {requirement:s.requirement??(o=>true),async resolve(o,c){if(r.push(o),s.delay&&await new Promise(l=>setTimeout(l,s.delay)),s.error)throw typeof s.error=="string"?new Error(s.error):s.error;s.resolve&&await s.resolve(o,c);}}}function C(t){let s=[],r=[];return {...{get calls(){return s},get pending(){return r},resolve(l){let m=r.shift();m&&m.resolve(l);},reject(l){let m=r.shift();m&&m.reject(l);},resolveAll(l){for(;r.length>0;)this.resolve(l);},rejectAll(l){for(;r.length>0;)this.reject(l);},reset(){s.length=0,r.length=0;}},handler:(l,m)=>(s.push(l),new Promise((u,v)=>{r.push({requirement:l,resolve:f=>u(f),reject:v});}))}}function E(t){return "module"in t?x(t):M(t)}function x(t){let s=[],r=new Map,o=[],c=[],l={};if(t.mocks?.resolvers)for(let[i,e]of Object.entries(t.mocks.resolvers)){let n=[];r.set(i,n),l[i]=b({...e,calls:n});}let m={...t.module,resolvers:{...t.module.resolvers,...l}},v=a({...t,module:m,plugins:[{name:"__test-tracking__",onFactSet:(i,e,n)=>{c.push({key:i,fullKey:i,namespace:void 0,previousValue:n,newValue:e,timestamp:Date.now()});},onRequirementCreated:i=>{o.push(i);}},...t.plugins??[]]}),f=v.dispatch.bind(v);return v.dispatch=i=>{s.push(i),f(i);},{...v,eventHistory:s,resolverCalls:r,get allRequirements(){return o},getFactsHistory(){return [...c]},resetFactsHistory(){c.length=0;},async waitForIdle(i=5e3){let e=Date.now(),n=async()=>{await new Promise(d=>setTimeout(d,0));let a=v.inspect();if(a.inflight.length>0){if(Date.now()-e>i){let d=a.inflight.map(g=>g.id).join(", ");throw new Error(`[Directive] waitForIdle timed out after ${i}ms. ${a.inflight.length} resolvers still inflight: ${d}`)}return await new Promise(d=>setTimeout(d,10)),n()}};return n()},assertRequirement(i){if(!o.some(n=>n.requirement.type===i))throw new Error(`[Directive] Expected requirement of type "${i}" but none found`)},assertResolverCalled(i,e){let n=r.get(i)??[];if(e!==void 0){if(n.length!==e)throw new Error(`[Directive] Expected resolver "${i}" to be called ${e} times but was called ${n.length} times`)}else if(n.length===0)throw new Error(`[Directive] Expected resolver "${i}" to be called but it was not`)},assertFactSet(i,e){let n=c.filter(a=>a.key===i);if(n.length===0)throw new Error(`[Directive] Expected fact "${i}" to be set but it was not`);if(e!==void 0&&!n.some(d=>d.newValue===e)){let d=n.map(g=>JSON.stringify(g.newValue)).join(", ");throw new Error(`[Directive] Expected fact "${i}" to be set to ${JSON.stringify(e)} but got: ${d}`)}},assertFactChanges(i,e){let n=c.filter(a=>a.key===i);if(n.length!==e)throw new Error(`[Directive] Expected fact "${i}" to change ${e} times but it changed ${n.length} times`)}}}function M(t){let s=[],r=new Map,o=[],c=[],l={};if(t.mocks?.resolvers)for(let[e,n]of Object.entries(t.mocks.resolvers)){let a=[];r.set(e,a),l[e]=b({...n,calls:a});}let m={};for(let[e,n]of Object.entries(t.modules))m[e]={...n,resolvers:{...n.resolvers,...l}};let u=new Set(Object.keys(t.modules)),f=a({...t,modules:m,plugins:[{name:"__test-tracking__",onFactSet:(e,n,a)=>{let g=e.indexOf("::"),y,h;if(g>0){let w=e.substring(0,g);u.has(w)?(y=w,h=e.substring(g+2)):h=e;}else h=e;c.push({key:h,fullKey:e,namespace:y,previousValue:a,newValue:n,timestamp:Date.now()});},onRequirementCreated:e=>{o.push(e);}},...t.plugins??[]]}),p=f.dispatch.bind(f);return f.dispatch=e=>{s.push(e),p(e);},{...f,eventHistory:s,resolverCalls:r,get allRequirements(){return o},getFactsHistory(){return [...c]},resetFactsHistory(){c.length=0;},async waitForIdle(e=5e3){let n=Date.now(),a=async()=>{await new Promise(g=>setTimeout(g,0));let d=f.inspect();if(d.inflight.length>0){if(Date.now()-n>e){let g=d.inflight.map(y=>y.id).join(", ");throw new Error(`[Directive] waitForIdle timed out after ${e}ms. ${d.inflight.length} resolvers still inflight: ${g}`)}return await new Promise(g=>setTimeout(g,10)),a()}};return a()},assertRequirement(e){if(!o.some(a=>a.requirement.type===e))throw new Error(`[Directive] Expected requirement of type "${e}" but none found`)},assertResolverCalled(e,n){let a=r.get(e)??[];if(n!==void 0){if(a.length!==n)throw new Error(`[Directive] Expected resolver "${e}" to be called ${n} times but was called ${a.length} times`)}else if(a.length===0)throw new Error(`[Directive] Expected resolver "${e}" to be called but it was not`)},assertFactSet(e,n){let a=c.filter(d=>d.key===e);if(a.length===0)throw new Error(`[Directive] Expected fact "${e}" to be set but it was not`);if(n!==void 0&&!a.some(g=>g.newValue===n)){let g=a.map(y=>JSON.stringify(y.newValue)).join(", ");throw new Error(`[Directive] Expected fact "${e}" to be set to ${JSON.stringify(n)} but got: ${g}`)}},assertFactChanges(e,n){let a=c.filter(d=>d.key===e);if(a.length!==n)throw new Error(`[Directive] Expected fact "${e}" to change ${n} times but it changed ${a.length} times`)}}}function O(t,s,r){if(!k(t,s,r))throw new Error(`[Directive] Expected ${s} "${r}" to be dynamic, but it is not.`)}function $(t,s,r){if(k(t,s,r))throw new Error(`[Directive] Expected ${s} "${r}" to NOT be dynamic, but it is.`)}function k(t,s,r){switch(s){case "constraint":return t.constraints.isDynamic(r);case "resolver":return t.resolvers.isDynamic(r);case "derivation":return t.derive.isDynamic(r);case "effect":return t.effects.isDynamic(r)}}function P(t){let s=new Set,r=new Set,o=new Set,c=new Set,l=null;return {async run(m){l=t.observe(u=>{switch(u.type){case "constraint.evaluate":u.active&&s.add(u.id);break;case "resolver.start":r.add(u.resolver);break;case "effect.run":o.add(u.id);break;case "derivation.compute":c.add(u.id);break}});try{await m();}finally{l?.(),l=null;}},report(){let m=t.inspect(),u=new Set(m.constraints.map(i=>i.id)),v=new Set(m.resolverDefs.map(i=>i.id)),f=new Set;for(let i of u)s.has(i)||f.add(i);let p=new Set;for(let i of v)r.has(i)||p.add(i);return {constraintsHit:s,constraintsMissed:f,resolversRun:r,resolversMissed:p,effectsRun:o,derivationsComputed:c,constraintCoverage:u.size===0?1:s.size/u.size,resolverCoverage:v.size===0?1:r.size/v.size}}}}function F(t){let s=[],r=t.observe(o=>s.push(o));return {events:s,ofType(o){return s.filter(c=>c.type===o)},clear(){s.length=0;},dispose(){r();}}}export{O as assertDynamic,$ as assertNotDynamic,P as createCoverageTracker,D as createFakeTimers,b as createMockResolver,F as createTestObserver,E as createTestSystem,R as flushMicrotasks,C as mockResolver,T as settleWithFakeTimers};//# sourceMappingURL=testing.js.map
|
|
2
2
|
//# sourceMappingURL=testing.js.map
|
package/dist/testing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/testing.ts"],"names":["flushMicrotasks","i","settleWithFakeTimers","system","advanceTime","options","totalTime","stepSize","maxIterations","elapsed","iterations","finalInspection","resolverIds","r","createFakeTimers","currentTime","timers","ms","targetTime","timer","createMockResolver","typeOrOptions","req","calls","_req","ctx","resolve","mockResolver","_requirementType","pending","result","item","error","_ctx","reject","val","createTestSystem","createTestSystemSingle","createTestSystemNamed","eventHistory","resolverCalls","allRequirements","factsHistory","mockResolvers","type","mockOptions","moduleWithMocks","createSystem","fullKey","value","previousValue","requirement","originalDispatch","event","maxWait","startTime","checkIdle","inspection","times","key","changes","c","actualValues","modulesWithMocks","name","module","moduleNamespaces","sepIndex","namespace","possibleNamespace","assertDynamic","id","getDynamicCheck","assertNotDynamic"],"mappings":"wHAiDA,eAAsBA,GAAiC,CAErD,IAAA,IAASC,CAAAA,CAAI,CAAA,CAAGA,EAAI,EAAA,CAAIA,CAAAA,EAAAA,CACtB,MAAM,OAAA,CAAQ,UAElB,CAgCA,eAAsBC,CAAAA,CACpBC,CAAAA,CACAC,EACAC,CAAAA,CAOI,EAAC,CACU,CACf,GAAM,CAAE,SAAA,CAAAC,EAAY,GAAA,CAAM,QAAA,CAAAC,EAAW,EAAA,CAAI,aAAA,CAAAC,CAAAA,CAAgB,GAAK,EAAIH,CAAAA,CAE9DI,CAAAA,CAAU,EACVC,CAAAA,CAAa,CAAA,CAEjB,KAAOD,CAAAA,CAAUH,CAAAA,EAAaI,CAAAA,CAAaF,CAAAA,EAAe,CAMxD,GAJA,MAAMR,GAAgB,CAGHG,CAAAA,CAAO,SAAQ,CACnB,QAAA,CAAS,MAAA,GAAW,CAAA,CAAG,CAEpC,MAAMH,CAAAA,GACN,MACF,CAGAI,EAAYG,CAAQ,CAAA,CACpBE,CAAAA,EAAWF,CAAAA,CACXG,IACF,CAGA,IAAMC,EAAkBR,CAAAA,CAAO,OAAA,GAC/B,GAAIQ,CAAAA,CAAgB,QAAA,CAAS,MAAA,CAAS,EAAG,CACvC,IAAMC,EAAcD,CAAAA,CAAgB,QAAA,CACjC,IAAKE,CAAAA,EAAMA,CAAAA,CAAE,UAAU,CAAA,CACvB,KAAK,IAAI,CAAA,CACZ,MAAM,IAAI,KAAA,CACR,yDAAyDP,CAAS,CAAA,IAAA,EAAOK,CAAAA,CAAgB,Q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ACtC,KAAK,aACH,OAAOpE,CAAAA,CAAO,OAAO,SAAA,CAAUoE,CAAE,CAAA,CACnC,KAAK,SACH,OAAOpE,CAAAA,CAAO,QAAQ,SAAA,CAAUoE,CAAE,CACtC,CACF","file":"testing.js","sourcesContent":["/**\n * Testing Utilities - Helpers for testing Directive systems\n *\n * Features:\n * - Mock resolvers with manual resolve/reject\n * - Fake timers integration (works with Vitest/Jest fake timers)\n * - Assertion helpers\n * - Facts history tracking\n * - Pending requirements tracking\n */\n\nimport { createSystem } from \"../core/system.js\";\nimport type {\n CreateSystemOptionsNamed,\n CreateSystemOptionsSingle,\n ModuleDef,\n ModuleSchema,\n ModulesMap,\n NamespacedSystem,\n Requirement,\n RequirementWithId,\n SingleModuleSystem,\n SystemInspection,\n} from \"../core/types.js\";\n\n// ============================================================================\n// Fake Timers Integration\n// ============================================================================\n\n/**\n * Flush all pending microtasks by awaiting multiple rounds of `Promise.resolve()`.\n *\n * Call this after advancing fake timers to ensure all Promise callbacks\n * (including nested microtasks) have run before making assertions.\n *\n * @returns A promise that resolves after all pending microtasks have been drained.\n *\n * @example\n * ```typescript\n * vi.useFakeTimers();\n * system.start();\n * system.facts.userId = 1; // Triggers constraint\n * await flushMicrotasks(); // Let reconciliation start\n * vi.advanceTimersByTime(100); // Advance resolver delay\n * await flushMicrotasks(); // Let resolver complete\n * ```\n *\n * @public\n */\nexport async function flushMicrotasks(): Promise<void> {\n // Multiple rounds to catch nested microtasks\n for (let i = 0; i < 10; i++) {\n await Promise.resolve();\n }\n}\n\n/**\n * Wait for the system to settle with fake timers enabled.\n *\n * Repeatedly advances fake timers in discrete steps while flushing microtasks,\n * until no resolvers remain inflight or the time budget is exhausted.\n *\n * @param system - The Directive system to wait on (must expose {@link SystemInspection} via `inspect()`).\n * @param advanceTime - Function that advances fake timers by a given number of milliseconds (e.g., `vi.advanceTimersByTime`).\n * @param options - Configuration for total time budget, step size, and iteration limit.\n * @returns A promise that resolves once the system is idle.\n *\n * @throws Error if the system does not settle within the configured time budget.\n *\n * @example\n * ```typescript\n * vi.useFakeTimers();\n * const system = createSystem({ modules: [myModule] });\n * system.start();\n * system.dispatch({ type: \"triggerAsync\" });\n *\n * await settleWithFakeTimers(system, vi.advanceTimersByTime, {\n * totalTime: 1000,\n * stepSize: 10,\n * });\n *\n * expect(system.facts.result).toBe(\"done\");\n * ```\n *\n * @public\n */\nexport async function settleWithFakeTimers(\n system: { inspect(): SystemInspection },\n advanceTime: (ms: number) => void,\n options: {\n /** Total time to advance (default: 5000ms) */\n totalTime?: number;\n /** Time to advance each step (default: 10ms) */\n stepSize?: number;\n /** Maximum iterations before giving up (default: 1000) */\n maxIterations?: number;\n } = {},\n): Promise<void> {\n const { totalTime = 5000, stepSize = 10, maxIterations = 1000 } = options;\n\n let elapsed = 0;\n let iterations = 0;\n\n while (elapsed < totalTime && iterations < maxIterations) {\n // Flush microtasks first (handles queueMicrotask, Promise.resolve)\n await flushMicrotasks();\n\n // Check if settled\n const inspection = system.inspect();\n if (inspection.inflight.length === 0) {\n // One more flush to be safe\n await flushMicrotasks();\n return;\n }\n\n // Advance fake timers\n advanceTime(stepSize);\n elapsed += stepSize;\n iterations++;\n }\n\n // Final check\n const finalInspection = system.inspect();\n if (finalInspection.inflight.length > 0) {\n const resolverIds = finalInspection.inflight\n .map((r) => r.resolverId)\n .join(\", \");\n throw new Error(\n `[Directive] settleWithFakeTimers did not settle after ${totalTime}ms. ${finalInspection.inflight.length} resolvers still inflight: ${resolverIds}`,\n );\n }\n}\n\n// ============================================================================\n// Fake Timers (for standalone use without vi.useFakeTimers)\n// ============================================================================\n\n/**\n * Standalone fake timer controller for tests that do not use Vitest/Jest fake timers.\n *\n * @remarks\n * For most tests, prefer Vitest's `vi.useFakeTimers()` paired with\n * {@link settleWithFakeTimers} for better integration. Use this interface\n * only when you need a lightweight, framework-independent timer mock.\n *\n * @public\n */\nexport interface FakeTimers {\n /** Advance time by a number of milliseconds, firing any timers that fall within the window. */\n advance(ms: number): Promise<void>;\n /** Advance to the next scheduled timer and fire its callback. */\n next(): Promise<void>;\n /** Run all pending timers in chronological order. */\n runAll(): Promise<void>;\n /** Get the current fake time in milliseconds. */\n now(): number;\n /** Reset the clock to time 0 and discard all scheduled timers. */\n reset(): void;\n}\n\n/**\n * Create standalone fake timers for testing without a framework timer mock.\n *\n * @remarks\n * For most tests, prefer Vitest's `vi.useFakeTimers()` paired with\n * {@link settleWithFakeTimers}. This factory is useful when you need an\n * isolated timer that does not interfere with global timer state.\n *\n * @returns A {@link FakeTimers} controller with `advance`, `next`, `runAll`, `now`, and `reset` methods.\n *\n * @example\n * ```typescript\n * const timers = createFakeTimers();\n * // schedule work, then:\n * await timers.advance(500);\n * expect(timers.now()).toBe(500);\n * ```\n *\n * @public\n */\nexport function createFakeTimers(): FakeTimers {\n let currentTime = 0;\n const timers: Array<{ time: number; callback: () => void }> = [];\n\n return {\n async advance(ms: number): Promise<void> {\n const targetTime = currentTime + ms;\n\n // Run all timers that would fire during this advance\n while (timers.length > 0 && timers[0]!.time <= targetTime) {\n const timer = timers.shift()!;\n currentTime = timer.time;\n timer.callback();\n await Promise.resolve(); // Allow microtasks\n }\n\n currentTime = targetTime;\n },\n\n async next(): Promise<void> {\n if (timers.length === 0) return;\n\n const timer = timers.shift()!;\n currentTime = timer.time;\n timer.callback();\n await Promise.resolve();\n },\n\n async runAll(): Promise<void> {\n while (timers.length > 0) {\n await this.next();\n }\n },\n\n now(): number {\n return currentTime;\n },\n\n reset(): void {\n currentTime = 0;\n timers.length = 0;\n },\n };\n}\n\n// ============================================================================\n// Mock Resolvers\n// ============================================================================\n\n/**\n * Context passed to mock resolver resolve functions.\n *\n * @public\n */\nexport interface MockResolverContext {\n /** Facts object (use type assertion for specific facts) */\n // biome-ignore lint/suspicious/noExplicitAny: Facts type varies by system\n facts: any;\n /** Abort signal for cancellation */\n signal: AbortSignal;\n}\n\n/**\n * Configuration for a simple mock resolver created via {@link createMockResolver}.\n *\n * @typeParam R - The requirement type this resolver handles.\n *\n * @public\n */\nexport interface MockResolverOptions<R extends Requirement = Requirement> {\n /** Predicate to check if this resolver handles a given requirement. */\n requirement?: (req: Requirement) => req is R;\n /** Mock implementation invoked when the resolver runs. */\n resolve?: (req: R, ctx: MockResolverContext) => void | Promise<void>;\n /** Artificial delay in milliseconds before the resolver completes. */\n delay?: number;\n /** Error (or message string) to throw, simulating a resolver failure. */\n error?: Error | string;\n /** Array that receives every requirement passed to this resolver. */\n calls?: R[];\n}\n\n/** Internal resolver definition type for mock resolvers */\ninterface MockResolverDef {\n requirement: (req: Requirement) => boolean;\n resolve: (req: Requirement, ctx: MockResolverContext) => Promise<void>;\n}\n\n/**\n * Create a simple mock resolver that matches requirements by type and optionally\n * records calls, injects delays, or throws errors.\n *\n * @param typeOrOptions - A requirement type string, or a full {@link MockResolverOptions} object.\n * @returns A resolver definition that can be spread into a module's `resolvers` map.\n *\n * @example\n * ```typescript\n * const calls: Requirement[] = [];\n * const mock = createMockResolver({ requirement: (r): r is MyReq => r.type === \"LOAD\", calls });\n * ```\n *\n * @public\n */\nexport function createMockResolver<R extends Requirement = Requirement>(\n typeOrOptions: string | MockResolverOptions<R>,\n): MockResolverDef {\n const options: MockResolverOptions<R> =\n typeof typeOrOptions === \"string\"\n ? {\n requirement: ((req: Requirement) => req.type === typeOrOptions) as (\n req: Requirement,\n ) => req is R,\n }\n : typeOrOptions;\n\n const calls: R[] = options.calls ?? [];\n\n return {\n requirement:\n options.requirement ?? ((_req: Requirement): _req is R => true),\n async resolve(req: Requirement, ctx: MockResolverContext): Promise<void> {\n calls.push(req as R);\n\n if (options.delay) {\n await new Promise((resolve) => setTimeout(resolve, options.delay));\n }\n\n if (options.error) {\n throw typeof options.error === \"string\"\n ? new Error(options.error)\n : options.error;\n }\n\n if (options.resolve) {\n await options.resolve(req as R, ctx);\n }\n },\n };\n}\n\n// ============================================================================\n// Mock Resolver (Advanced)\n// ============================================================================\n\n/**\n * A mock resolver that captures requirements for manual resolution.\n *\n * @remarks\n * Use this when you need fine-grained control over when and how requirements\n * resolve. Requirements are queued in `pending` and stay unresolved until you\n * explicitly call `resolve()` or `reject()`.\n *\n * @typeParam R - The requirement type this resolver handles.\n *\n * @public\n */\nexport interface MockResolver<R extends Requirement = Requirement> {\n /** All requirements received by this resolver */\n readonly calls: R[];\n /** Pending requirements waiting to be resolved/rejected */\n readonly pending: Array<{\n requirement: R;\n resolve: (result?: unknown) => void;\n reject: (error: Error) => void;\n }>;\n /** Resolve the next pending requirement */\n resolve(result?: unknown): void;\n /** Reject the next pending requirement */\n reject(error: Error): void;\n /** Resolve all pending requirements */\n resolveAll(result?: unknown): void;\n /** Reject all pending requirements */\n rejectAll(error: Error): void;\n /** Clear call history and pending queue */\n reset(): void;\n}\n\n/**\n * Create a mock resolver that captures requirements instead of resolving them,\n * giving you manual control over when and how each requirement completes.\n *\n * @param _requirementType - The requirement `type` string this mock handles (used for documentation; matching is done by the test harness).\n * @returns A {@link MockResolver} with a `handler` function suitable for passing to {@link createTestSystem} mocks.\n *\n * @example\n * ```typescript\n * const fetchMock = mockResolver<{ type: \"FETCH_USER\"; id: string }>(\"FETCH_USER\");\n *\n * const system = createTestSystem({\n * modules: [userModule],\n * mocks: {\n * resolvers: {\n * FETCH_USER: { resolve: fetchMock.handler },\n * },\n * },\n * });\n *\n * system.facts.userId = \"123\";\n * await flushMicrotasks();\n *\n * // Requirement is pending\n * expect(fetchMock.calls).toHaveLength(1);\n * expect(fetchMock.calls[0].id).toBe(\"123\");\n *\n * // Manually resolve it\n * fetchMock.resolve({ name: \"John\" });\n * await flushMicrotasks();\n *\n * expect(system.facts.user).toEqual({ name: \"John\" });\n * ```\n *\n * @public\n */\nexport function mockResolver<R extends Requirement = Requirement>(\n _requirementType: string,\n): MockResolver<R> & {\n /** Handler that can be passed to createTestSystem mocks */\n handler: (req: Requirement, ctx: MockResolverContext) => Promise<void>;\n} {\n const calls: R[] = [];\n const pending: Array<{\n requirement: R;\n resolve: (result?: unknown) => void;\n reject: (error: Error) => void;\n }> = [];\n\n const mock: MockResolver<R> = {\n get calls() {\n return calls;\n },\n get pending() {\n return pending;\n },\n resolve(result?: unknown) {\n const item = pending.shift();\n if (item) {\n item.resolve(result);\n }\n },\n reject(error: Error) {\n const item = pending.shift();\n if (item) {\n item.reject(error);\n }\n },\n resolveAll(result?: unknown) {\n while (pending.length > 0) {\n this.resolve(result);\n }\n },\n rejectAll(error: Error) {\n while (pending.length > 0) {\n this.reject(error);\n }\n },\n reset() {\n calls.length = 0;\n pending.length = 0;\n },\n };\n\n const handler = (\n req: Requirement,\n _ctx: MockResolverContext,\n ): Promise<void> => {\n calls.push(req as R);\n return new Promise<void>((resolve, reject) => {\n pending.push({\n requirement: req as R,\n resolve: (val?: unknown) => resolve(val as void),\n reject,\n });\n });\n };\n\n return {\n ...mock,\n handler,\n };\n}\n\n// ============================================================================\n// Fact Change Tracking\n// ============================================================================\n\n/**\n * Record of a single fact change captured by the test tracking plugin.\n *\n * @public\n */\nexport interface FactChangeRecord {\n /** The fact key that changed (without namespace prefix for namespaced systems) */\n key: string;\n /** The full key including namespace prefix (e.g., \"test::value\") */\n fullKey: string;\n /** The namespace (e.g., \"test\") - undefined for single-module systems */\n namespace?: string;\n /** The previous value */\n previousValue: unknown;\n /** The new value */\n newValue: unknown;\n /** Timestamp of the change */\n timestamp: number;\n}\n\n// ============================================================================\n// Test System\n// ============================================================================\n\n/** Common testing utilities shared by both single-module and namespaced test systems. */\nexport interface TestSystemBase {\n /**\n * Wait for all pending operations to complete.\n * @param maxWait - Maximum time to wait in ms (default: 5000)\n * @throws Error if timeout is exceeded with resolvers still inflight\n */\n waitForIdle(maxWait?: number): Promise<void>;\n /** Get the history of dispatched events */\n eventHistory: Array<{ type: string; [key: string]: unknown }>;\n /** Get resolver call history */\n resolverCalls: Map<string, Requirement[]>;\n /**\n * Get all requirements that have been generated (both resolved and pending).\n * Unlike `inspect().unmet`, this includes requirements that have already been handled.\n */\n readonly allRequirements: RequirementWithId[];\n /**\n * Get all fact changes since system start or last reset.\n */\n getFactsHistory(): FactChangeRecord[];\n /**\n * Reset the facts history tracking.\n */\n resetFactsHistory(): void;\n /** Assert that a requirement was created */\n assertRequirement(type: string): void;\n /** Assert that a resolver was called */\n assertResolverCalled(type: string, times?: number): void;\n /**\n * Assert that a fact was set to a specific value.\n */\n assertFactSet(key: string, value?: unknown): void;\n /**\n * Assert the number of times a fact was changed.\n */\n assertFactChanges(key: string, times: number): void;\n}\n\n/**\n * A single-module Directive system augmented with testing utilities.\n *\n * @remarks\n * Extends {@link SingleModuleSystem} with event/resolver/fact tracking, idle\n * waiting, and assertion helpers. Created via {@link createTestSystem}.\n *\n * @typeParam S - The module schema.\n *\n * @public\n */\nexport interface TestSystemSingle<S extends ModuleSchema>\n extends SingleModuleSystem<S>,\n TestSystemBase {}\n\n/**\n * A Directive system augmented with testing utilities.\n *\n * @remarks\n * Extends {@link NamespacedSystem} with event/resolver/fact tracking, idle\n * waiting, and assertion helpers. Created via {@link createTestSystem}.\n *\n * @typeParam Modules - The modules map that defines the system's schema.\n *\n * @public\n */\nexport interface TestSystem<Modules extends ModulesMap>\n extends NamespacedSystem<Modules>,\n TestSystemBase {}\n\n/**\n * Options for {@link createTestSystem}, extending the standard system options\n * with mock resolver injection and automatic tracking.\n *\n * @typeParam Modules - The modules map that defines the system's schema.\n *\n * @public\n */\nexport interface CreateTestSystemOptions<Modules extends ModulesMap>\n extends Omit<CreateSystemOptionsNamed<Modules>, \"plugins\"> {\n /** Mock resolvers by type */\n mocks?: {\n resolvers?: Record<string, MockResolverOptions>;\n };\n /** Additional plugins (tracking plugin is added automatically) */\n // biome-ignore lint/suspicious/noExplicitAny: Plugins are schema-agnostic\n plugins?: any[];\n}\n\n/**\n * Options for {@link createTestSystem} with a single module (no namespacing).\n *\n * @typeParam S - The module schema.\n *\n * @public\n */\nexport interface CreateTestSystemOptionsSingle<S extends ModuleSchema>\n extends Omit<CreateSystemOptionsSingle<S>, \"plugins\"> {\n /** Mock resolvers by type */\n mocks?: {\n resolvers?: Record<string, MockResolverOptions>;\n };\n /** Additional plugins (tracking plugin is added automatically) */\n // biome-ignore lint/suspicious/noExplicitAny: Plugins are schema-agnostic\n plugins?: any[];\n}\n\n/**\n * Create a Directive system instrumented for testing.\n *\n * Wraps {@link createSystem} with an automatic tracking plugin that records\n * dispatched events, resolver calls, fact changes, and generated requirements.\n * Mock resolvers can be injected via `options.mocks.resolvers` to replace\n * real resolvers by requirement type.\n *\n * @param options - System configuration with optional mock resolvers and additional plugins.\n * @returns A {@link TestSystem} or {@link TestSystemSingle} with assertion helpers, idle waiting, and history tracking.\n *\n * @example\n * ```typescript\n * // Namespaced (multiple modules)\n * const system = createTestSystem({\n * modules: { counter: counterModule },\n * mocks: { resolvers: { INCREMENT: { resolve: (req, context) => { context.facts.count++; } } } },\n * });\n *\n * // Single module\n * const system = createTestSystem({\n * module: counterModule,\n * });\n * system.start();\n * ```\n *\n * @public\n */\nexport function createTestSystem<S extends ModuleSchema>(\n options: CreateTestSystemOptionsSingle<S>,\n): TestSystemSingle<S>;\nexport function createTestSystem<Modules extends ModulesMap>(\n options: CreateTestSystemOptions<Modules>,\n): TestSystem<Modules>;\nexport function createTestSystem<\n S extends ModuleSchema,\n Modules extends ModulesMap,\n>(\n options:\n | CreateTestSystemOptionsSingle<S>\n | CreateTestSystemOptions<Modules>,\n): TestSystemSingle<S> | TestSystem<Modules> {\n // Single module mode: wrap into namespaced and delegate\n if (\"module\" in options) {\n return createTestSystemSingle(\n options as CreateTestSystemOptionsSingle<S>,\n );\n }\n\n return createTestSystemNamed(options as CreateTestSystemOptions<Modules>);\n}\n\nfunction createTestSystemSingle<S extends ModuleSchema>(\n options: CreateTestSystemOptionsSingle<S>,\n): TestSystemSingle<S> {\n const eventHistory: Array<{ type: string; [key: string]: unknown }> = [];\n const resolverCalls = new Map<string, Requirement[]>();\n const allRequirements: RequirementWithId[] = [];\n const factsHistory: FactChangeRecord[] = [];\n\n // Create mock resolvers\n const mockResolvers: Record<string, MockResolverDef> = {};\n if (options.mocks?.resolvers) {\n for (const [type, mockOptions] of Object.entries(options.mocks.resolvers)) {\n const calls: Requirement[] = [];\n resolverCalls.set(type, calls);\n mockResolvers[type] = createMockResolver({ ...mockOptions, calls });\n }\n }\n\n // Create module with mock resolvers\n // biome-ignore lint/suspicious/noExplicitAny: Mock resolvers have simplified types\n const moduleWithMocks: ModuleDef<S> = {\n ...options.module,\n resolvers: {\n ...options.module.resolvers,\n ...mockResolvers,\n } as any,\n };\n\n // Create tracking plugin\n const trackingPlugin = {\n name: \"__test-tracking__\",\n onFactSet: (fullKey: string, value: unknown, previousValue: unknown) => {\n factsHistory.push({\n key: fullKey,\n fullKey,\n namespace: undefined,\n previousValue,\n newValue: value,\n timestamp: Date.now(),\n });\n },\n onRequirementCreated: (requirement: RequirementWithId) => {\n allRequirements.push(requirement);\n },\n };\n\n // Create the underlying single-module system\n const system = createSystem({\n ...options,\n module: moduleWithMocks,\n plugins: [trackingPlugin, ...(options.plugins ?? [])],\n // biome-ignore lint/suspicious/noExplicitAny: Internal overload compatibility\n } as any) as SingleModuleSystem<S>;\n\n // Wrap dispatch to track events\n const originalDispatch = system.dispatch.bind(system);\n // biome-ignore lint/suspicious/noExplicitAny: Event type varies\n (system as any).dispatch = (event: any) => {\n eventHistory.push(event);\n originalDispatch(event);\n };\n\n const testSystem: TestSystemSingle<S> = {\n ...system,\n eventHistory,\n resolverCalls,\n\n get allRequirements() {\n return allRequirements;\n },\n\n getFactsHistory(): FactChangeRecord[] {\n return [...factsHistory];\n },\n\n resetFactsHistory(): void {\n factsHistory.length = 0;\n },\n\n async waitForIdle(maxWait = 5000): Promise<void> {\n const startTime = Date.now();\n\n const checkIdle = async (): Promise<void> => {\n await new Promise((resolve) => setTimeout(resolve, 0));\n const inspection = system.inspect();\n if (inspection.inflight.length > 0) {\n if (Date.now() - startTime > maxWait) {\n const resolverIds = inspection.inflight\n .map((r) => r.id)\n .join(\", \");\n throw new Error(\n `[Directive] waitForIdle timed out after ${maxWait}ms. ${inspection.inflight.length} resolvers still inflight: ${resolverIds}`,\n );\n }\n await new Promise((resolve) => setTimeout(resolve, 10));\n\n return checkIdle();\n }\n };\n\n return checkIdle();\n },\n\n assertRequirement(type: string): void {\n const hasRequirement = allRequirements.some(\n (r) => r.requirement.type === type,\n );\n if (!hasRequirement) {\n throw new Error(\n `[Directive] Expected requirement of type \"${type}\" but none found`,\n );\n }\n },\n\n assertResolverCalled(type: string, times?: number): void {\n const calls = resolverCalls.get(type) ?? [];\n if (times !== undefined) {\n if (calls.length !== times) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called ${times} times but was called ${calls.length} times`,\n );\n }\n } else if (calls.length === 0) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called but it was not`,\n );\n }\n },\n\n assertFactSet(key: string, value?: unknown): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length === 0) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set but it was not`,\n );\n }\n if (value !== undefined) {\n const hasValue = changes.some((c) => c.newValue === value);\n if (!hasValue) {\n const actualValues = changes\n .map((c) => JSON.stringify(c.newValue))\n .join(\", \");\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set to ${JSON.stringify(value)} but got: ${actualValues}`,\n );\n }\n }\n },\n\n assertFactChanges(key: string, times: number): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length !== times) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to change ${times} times but it changed ${changes.length} times`,\n );\n }\n },\n };\n\n return testSystem;\n}\n\nfunction createTestSystemNamed<Modules extends ModulesMap>(\n options: CreateTestSystemOptions<Modules>,\n): TestSystem<Modules> {\n const eventHistory: Array<{ type: string; [key: string]: unknown }> = [];\n const resolverCalls = new Map<string, Requirement[]>();\n const allRequirements: RequirementWithId[] = [];\n const factsHistory: FactChangeRecord[] = [];\n\n // Create mock resolvers\n const mockResolvers: Record<string, MockResolverDef> = {};\n if (options.mocks?.resolvers) {\n for (const [type, mockOptions] of Object.entries(options.mocks.resolvers)) {\n const calls: Requirement[] = [];\n resolverCalls.set(type, calls);\n mockResolvers[type] = createMockResolver({ ...mockOptions, calls });\n }\n }\n\n // Create modules with mock resolvers\n const modulesWithMocks: Modules = {} as Modules;\n for (const [name, module] of Object.entries(options.modules)) {\n // biome-ignore lint/suspicious/noExplicitAny: Module types are complex\n (modulesWithMocks as any)[name] = {\n ...module,\n resolvers: {\n ...module.resolvers,\n ...mockResolvers,\n },\n };\n }\n\n // Get module namespaces for key parsing\n const moduleNamespaces = new Set(Object.keys(options.modules));\n\n // Create tracking plugin\n const trackingPlugin = {\n name: \"__test-tracking__\",\n onFactSet: (fullKey: string, value: unknown, previousValue: unknown) => {\n // Parse namespaced key (e.g., \"test::value\" -> namespace: \"test\", key: \"value\")\n const SEPARATOR = \"::\";\n const sepIndex = fullKey.indexOf(SEPARATOR);\n let namespace: string | undefined;\n let key: string;\n\n if (sepIndex > 0) {\n const possibleNamespace = fullKey.substring(0, sepIndex);\n if (moduleNamespaces.has(possibleNamespace)) {\n namespace = possibleNamespace;\n key = fullKey.substring(sepIndex + SEPARATOR.length);\n } else {\n key = fullKey;\n }\n } else {\n key = fullKey;\n }\n\n factsHistory.push({\n key,\n fullKey,\n namespace,\n previousValue,\n newValue: value,\n timestamp: Date.now(),\n });\n },\n onRequirementCreated: (requirement: RequirementWithId) => {\n allRequirements.push(requirement);\n },\n };\n\n // Create the underlying system\n const system = createSystem({\n ...options,\n modules: modulesWithMocks,\n plugins: [trackingPlugin, ...(options.plugins ?? [])],\n }) as NamespacedSystem<Modules>;\n\n // Wrap dispatch to track events\n const originalDispatch = system.dispatch.bind(system);\n // biome-ignore lint/suspicious/noExplicitAny: Event type varies\n (system as any).dispatch = (event: any) => {\n eventHistory.push(event);\n originalDispatch(event);\n };\n\n const testSystem: TestSystem<Modules> = {\n ...system,\n eventHistory,\n resolverCalls,\n\n get allRequirements() {\n return allRequirements;\n },\n\n getFactsHistory(): FactChangeRecord[] {\n return [...factsHistory];\n },\n\n resetFactsHistory(): void {\n factsHistory.length = 0;\n },\n\n async waitForIdle(maxWait = 5000): Promise<void> {\n const startTime = Date.now();\n\n const checkIdle = async (): Promise<void> => {\n // Wait for microtasks\n await new Promise((resolve) => setTimeout(resolve, 0));\n\n // Check if there are inflight resolvers\n const inspection = system.inspect();\n if (inspection.inflight.length > 0) {\n // Check timeout\n if (Date.now() - startTime > maxWait) {\n const resolverIds = inspection.inflight.map((r) => r.id).join(\", \");\n throw new Error(\n `[Directive] waitForIdle timed out after ${maxWait}ms. ${inspection.inflight.length} resolvers still inflight: ${resolverIds}`,\n );\n }\n // Wait a bit more and check again\n await new Promise((resolve) => setTimeout(resolve, 10));\n return checkIdle();\n }\n };\n\n return checkIdle();\n },\n\n assertRequirement(type: string): void {\n const hasRequirement = allRequirements.some(\n (r) => r.requirement.type === type,\n );\n if (!hasRequirement) {\n throw new Error(\n `[Directive] Expected requirement of type \"${type}\" but none found`,\n );\n }\n },\n\n assertResolverCalled(type: string, times?: number): void {\n const calls = resolverCalls.get(type) ?? [];\n if (times !== undefined) {\n if (calls.length !== times) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called ${times} times but was called ${calls.length} times`,\n );\n }\n } else if (calls.length === 0) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called but it was not`,\n );\n }\n },\n\n assertFactSet(key: string, value?: unknown): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length === 0) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set but it was not`,\n );\n }\n if (value !== undefined) {\n const hasValue = changes.some((c) => c.newValue === value);\n if (!hasValue) {\n const actualValues = changes\n .map((c) => JSON.stringify(c.newValue))\n .join(\", \");\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set to ${JSON.stringify(value)} but got: ${actualValues}`,\n );\n }\n }\n },\n\n assertFactChanges(key: string, times: number): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length !== times) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to change ${times} times but it changed ${changes.length} times`,\n );\n }\n },\n };\n\n return testSystem;\n}\n\n// ============================================================================\n// Dynamic Definition Assertions\n// ============================================================================\n\n/**\n * Assert that a definition was dynamically registered on the system.\n *\n * @param system - The Directive system to check.\n * @param type - The definition type: \"constraint\", \"resolver\", \"derivation\", or \"effect\".\n * @param id - The definition ID.\n * @throws Error if the definition is not dynamic.\n *\n * @example\n * ```typescript\n * system.constraints.register(\"myRule\", { when: () => true, require: { type: \"DO\" } });\n * assertDynamic(system, \"constraint\", \"myRule\"); // passes\n * assertDynamic(system, \"constraint\", \"staticRule\"); // throws\n * ```\n *\n * @public\n */\nexport function assertDynamic(\n system: {\n constraints: { isDynamic(id: string): boolean };\n effects: { isDynamic(id: string): boolean };\n resolvers: { isDynamic(id: string): boolean };\n derive: { isDynamic(id: string): boolean };\n },\n type: \"constraint\" | \"resolver\" | \"derivation\" | \"effect\",\n id: string,\n): void {\n const isDynamic = getDynamicCheck(system, type, id);\n if (!isDynamic) {\n throw new Error(\n `[Directive] Expected ${type} \"${id}\" to be dynamic, but it is not.`,\n );\n }\n}\n\n/**\n * Assert that a definition is NOT dynamically registered (i.e., is static or does not exist).\n *\n * @param system - The Directive system to check.\n * @param type - The definition type: \"constraint\", \"resolver\", \"derivation\", or \"effect\".\n * @param id - The definition ID.\n * @throws Error if the definition is dynamic.\n *\n * @example\n * ```typescript\n * assertNotDynamic(system, \"constraint\", \"staticRule\"); // passes\n * system.constraints.register(\"myRule\", def);\n * assertNotDynamic(system, \"constraint\", \"myRule\"); // throws\n * ```\n *\n * @public\n */\nexport function assertNotDynamic(\n system: {\n constraints: { isDynamic(id: string): boolean };\n effects: { isDynamic(id: string): boolean };\n resolvers: { isDynamic(id: string): boolean };\n derive: { isDynamic(id: string): boolean };\n },\n type: \"constraint\" | \"resolver\" | \"derivation\" | \"effect\",\n id: string,\n): void {\n const isDynamic = getDynamicCheck(system, type, id);\n if (isDynamic) {\n throw new Error(\n `[Directive] Expected ${type} \"${id}\" to NOT be dynamic, but it is.`,\n );\n }\n}\n\n/** @internal */\nfunction getDynamicCheck(\n system: {\n constraints: { isDynamic(id: string): boolean };\n effects: { isDynamic(id: string): boolean };\n resolvers: { isDynamic(id: string): boolean };\n derive: { isDynamic(id: string): boolean };\n },\n type: \"constraint\" | \"resolver\" | \"derivation\" | \"effect\",\n id: string,\n): boolean {\n switch (type) {\n case \"constraint\":\n return system.constraints.isDynamic(id);\n case \"resolver\":\n return system.resolvers.isDynamic(id);\n case \"derivation\":\n return system.derive.isDynamic(id);\n case \"effect\":\n return system.effects.isDynamic(id);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/utils/testing.ts"],"names":["flushMicrotasks","i","settleWithFakeTimers","system","advanceTime","options","totalTime","stepSize","maxIterations","elapsed","iterations","finalInspection","resolverIds","r","createFakeTimers","currentTime","timers","ms","targetTime","timer","createMockResolver","typeOrOptions","req","calls","_req","ctx","resolve","mockResolver","_requirementType","pending","result","item","error","_ctx","reject","val","createTestSystem","createTestSystemSingle","createTestSystemNamed","eventHistory","resolverCalls","allRequirements","factsHistory","mockResolvers","type","mockOptions","moduleWithMocks","createSystem","fullKey","value","previousValue","requirement","originalDispatch","event","maxWait","startTime","checkIdle","inspection","times","key","changes","c","actualValues","modulesWithMocks","name","module","moduleNamespaces","sepIndex","namespace","possibleNamespace","assertDynamic","id","getDynamicCheck","assertNotDynamic","createCoverageTracker","constraintsHit","resolversRun","effectsRun","derivationsComputed","unsub","scenario","allConstraints","allResolvers","constraintsMissed","resolversMissed","createTestObserver","events","e"],"mappings":"wHAiDA,eAAsBA,CAAAA,EAAiC,CAErD,IAAA,IAASC,CAAAA,CAAI,EAAGA,CAAAA,CAAI,EAAA,CAAIA,IACtB,MAAM,OAAA,CAAQ,UAElB,CAgCA,eAAsBC,CAAAA,CACpBC,CAAAA,CACAC,EACAC,CAAAA,CAOI,EAAC,CACU,CACf,GAAM,CAAE,UAAAC,CAAAA,CAAY,GAA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CAAAA,CAAa,IACzC,CACF,CACF,CACF,CAwBO,SAASG,EAEdlF,CAAAA,CAYA,CACA,IAAMmF,CAAAA,CAA+D,GAC/DP,CAAAA,CAAQ5E,CAAAA,CAAO,QAASkD,CAAAA,EAAUiC,CAAAA,CAAO,KAAKjC,CAAK,CAAC,CAAA,CAE1D,OAAO,CACL,MAAA,CAAAiC,EACA,MAAA,CAAO1C,CAAAA,CAAM,CACX,OAAO0C,CAAAA,CAAO,OAAQC,CAAAA,EAAMA,CAAAA,CAAE,IAAA,GAAS3C,CAAI,CAC7C,CAAA,CACA,OAAQ,CACN0C,CAAAA,CAAO,OAAS,EAClB,CAAA,CACA,SAAU,CACRP,CAAAA,GACF,CACF,CACF","file":"testing.js","sourcesContent":["/**\n * Testing Utilities - Helpers for testing Directive systems\n *\n * Features:\n * - Mock resolvers with manual resolve/reject\n * - Fake timers integration (works with Vitest/Jest fake timers)\n * - Assertion helpers\n * - Facts history tracking\n * - Pending requirements tracking\n */\n\nimport { createSystem } from \"../core/system.js\";\nimport type {\n CreateSystemOptionsNamed,\n CreateSystemOptionsSingle,\n ModuleDef,\n ModuleSchema,\n ModulesMap,\n NamespacedSystem,\n Requirement,\n RequirementWithId,\n SingleModuleSystem,\n SystemInspection,\n} from \"../core/types.js\";\n\n// ============================================================================\n// Fake Timers Integration\n// ============================================================================\n\n/**\n * Flush all pending microtasks by awaiting multiple rounds of `Promise.resolve()`.\n *\n * Call this after advancing fake timers to ensure all Promise callbacks\n * (including nested microtasks) have run before making assertions.\n *\n * @returns A promise that resolves after all pending microtasks have been drained.\n *\n * @example\n * ```typescript\n * vi.useFakeTimers();\n * system.start();\n * system.facts.userId = 1; // Triggers constraint\n * await flushMicrotasks(); // Let reconciliation start\n * vi.advanceTimersByTime(100); // Advance resolver delay\n * await flushMicrotasks(); // Let resolver complete\n * ```\n *\n * @public\n */\nexport async function flushMicrotasks(): Promise<void> {\n // Multiple rounds to catch nested microtasks\n for (let i = 0; i < 10; i++) {\n await Promise.resolve();\n }\n}\n\n/**\n * Wait for the system to settle with fake timers enabled.\n *\n * Repeatedly advances fake timers in discrete steps while flushing microtasks,\n * until no resolvers remain inflight or the time budget is exhausted.\n *\n * @param system - The Directive system to wait on (must expose {@link SystemInspection} via `inspect()`).\n * @param advanceTime - Function that advances fake timers by a given number of milliseconds (e.g., `vi.advanceTimersByTime`).\n * @param options - Configuration for total time budget, step size, and iteration limit.\n * @returns A promise that resolves once the system is idle.\n *\n * @throws Error if the system does not settle within the configured time budget.\n *\n * @example\n * ```typescript\n * vi.useFakeTimers();\n * const system = createSystem({ modules: [myModule] });\n * system.start();\n * system.dispatch({ type: \"triggerAsync\" });\n *\n * await settleWithFakeTimers(system, vi.advanceTimersByTime, {\n * totalTime: 1000,\n * stepSize: 10,\n * });\n *\n * expect(system.facts.result).toBe(\"done\");\n * ```\n *\n * @public\n */\nexport async function settleWithFakeTimers(\n system: { inspect(): SystemInspection },\n advanceTime: (ms: number) => void,\n options: {\n /** Total time to advance (default: 5000ms) */\n totalTime?: number;\n /** Time to advance each step (default: 10ms) */\n stepSize?: number;\n /** Maximum iterations before giving up (default: 1000) */\n maxIterations?: number;\n } = {},\n): Promise<void> {\n const { totalTime = 5000, stepSize = 10, maxIterations = 1000 } = options;\n\n let elapsed = 0;\n let iterations = 0;\n\n while (elapsed < totalTime && iterations < maxIterations) {\n // Flush microtasks first (handles queueMicrotask, Promise.resolve)\n await flushMicrotasks();\n\n // Check if settled\n const inspection = system.inspect();\n if (inspection.inflight.length === 0) {\n // One more flush to be safe\n await flushMicrotasks();\n return;\n }\n\n // Advance fake timers\n advanceTime(stepSize);\n elapsed += stepSize;\n iterations++;\n }\n\n // Final check\n const finalInspection = system.inspect();\n if (finalInspection.inflight.length > 0) {\n const resolverIds = finalInspection.inflight\n .map((r) => r.resolverId)\n .join(\", \");\n throw new Error(\n `[Directive] settleWithFakeTimers did not settle after ${totalTime}ms. ${finalInspection.inflight.length} resolvers still inflight: ${resolverIds}`,\n );\n }\n}\n\n// ============================================================================\n// Fake Timers (for standalone use without vi.useFakeTimers)\n// ============================================================================\n\n/**\n * Standalone fake timer controller for tests that do not use Vitest/Jest fake timers.\n *\n * @remarks\n * For most tests, prefer Vitest's `vi.useFakeTimers()` paired with\n * {@link settleWithFakeTimers} for better integration. Use this interface\n * only when you need a lightweight, framework-independent timer mock.\n *\n * @public\n */\nexport interface FakeTimers {\n /** Advance time by a number of milliseconds, firing any timers that fall within the window. */\n advance(ms: number): Promise<void>;\n /** Advance to the next scheduled timer and fire its callback. */\n next(): Promise<void>;\n /** Run all pending timers in chronological order. */\n runAll(): Promise<void>;\n /** Get the current fake time in milliseconds. */\n now(): number;\n /** Reset the clock to time 0 and discard all scheduled timers. */\n reset(): void;\n}\n\n/**\n * Create standalone fake timers for testing without a framework timer mock.\n *\n * @remarks\n * For most tests, prefer Vitest's `vi.useFakeTimers()` paired with\n * {@link settleWithFakeTimers}. This factory is useful when you need an\n * isolated timer that does not interfere with global timer state.\n *\n * @returns A {@link FakeTimers} controller with `advance`, `next`, `runAll`, `now`, and `reset` methods.\n *\n * @example\n * ```typescript\n * const timers = createFakeTimers();\n * // schedule work, then:\n * await timers.advance(500);\n * expect(timers.now()).toBe(500);\n * ```\n *\n * @public\n */\nexport function createFakeTimers(): FakeTimers {\n let currentTime = 0;\n const timers: Array<{ time: number; callback: () => void }> = [];\n\n return {\n async advance(ms: number): Promise<void> {\n const targetTime = currentTime + ms;\n\n // Run all timers that would fire during this advance\n while (timers.length > 0 && timers[0]!.time <= targetTime) {\n const timer = timers.shift()!;\n currentTime = timer.time;\n timer.callback();\n await Promise.resolve(); // Allow microtasks\n }\n\n currentTime = targetTime;\n },\n\n async next(): Promise<void> {\n if (timers.length === 0) return;\n\n const timer = timers.shift()!;\n currentTime = timer.time;\n timer.callback();\n await Promise.resolve();\n },\n\n async runAll(): Promise<void> {\n while (timers.length > 0) {\n await this.next();\n }\n },\n\n now(): number {\n return currentTime;\n },\n\n reset(): void {\n currentTime = 0;\n timers.length = 0;\n },\n };\n}\n\n// ============================================================================\n// Mock Resolvers\n// ============================================================================\n\n/**\n * Context passed to mock resolver resolve functions.\n *\n * @public\n */\nexport interface MockResolverContext {\n /** Facts object (use type assertion for specific facts) */\n // biome-ignore lint/suspicious/noExplicitAny: Facts type varies by system\n facts: any;\n /** Abort signal for cancellation */\n signal: AbortSignal;\n}\n\n/**\n * Configuration for a simple mock resolver created via {@link createMockResolver}.\n *\n * @typeParam R - The requirement type this resolver handles.\n *\n * @public\n */\nexport interface MockResolverOptions<R extends Requirement = Requirement> {\n /** Predicate to check if this resolver handles a given requirement. */\n requirement?: (req: Requirement) => req is R;\n /** Mock implementation invoked when the resolver runs. */\n resolve?: (req: R, ctx: MockResolverContext) => void | Promise<void>;\n /** Artificial delay in milliseconds before the resolver completes. */\n delay?: number;\n /** Error (or message string) to throw, simulating a resolver failure. */\n error?: Error | string;\n /** Array that receives every requirement passed to this resolver. */\n calls?: R[];\n}\n\n/** Internal resolver definition type for mock resolvers */\ninterface MockResolverDef {\n requirement: (req: Requirement) => boolean;\n resolve: (req: Requirement, ctx: MockResolverContext) => Promise<void>;\n}\n\n/**\n * Create a simple mock resolver that matches requirements by type and optionally\n * records calls, injects delays, or throws errors.\n *\n * @param typeOrOptions - A requirement type string, or a full {@link MockResolverOptions} object.\n * @returns A resolver definition that can be spread into a module's `resolvers` map.\n *\n * @example\n * ```typescript\n * const calls: Requirement[] = [];\n * const mock = createMockResolver({ requirement: (r): r is MyReq => r.type === \"LOAD\", calls });\n * ```\n *\n * @public\n */\nexport function createMockResolver<R extends Requirement = Requirement>(\n typeOrOptions: string | MockResolverOptions<R>,\n): MockResolverDef {\n const options: MockResolverOptions<R> =\n typeof typeOrOptions === \"string\"\n ? {\n requirement: ((req: Requirement) => req.type === typeOrOptions) as (\n req: Requirement,\n ) => req is R,\n }\n : typeOrOptions;\n\n const calls: R[] = options.calls ?? [];\n\n return {\n requirement:\n options.requirement ?? ((_req: Requirement): _req is R => true),\n async resolve(req: Requirement, ctx: MockResolverContext): Promise<void> {\n calls.push(req as R);\n\n if (options.delay) {\n await new Promise((resolve) => setTimeout(resolve, options.delay));\n }\n\n if (options.error) {\n throw typeof options.error === \"string\"\n ? new Error(options.error)\n : options.error;\n }\n\n if (options.resolve) {\n await options.resolve(req as R, ctx);\n }\n },\n };\n}\n\n// ============================================================================\n// Mock Resolver (Advanced)\n// ============================================================================\n\n/**\n * A mock resolver that captures requirements for manual resolution.\n *\n * @remarks\n * Use this when you need fine-grained control over when and how requirements\n * resolve. Requirements are queued in `pending` and stay unresolved until you\n * explicitly call `resolve()` or `reject()`.\n *\n * @typeParam R - The requirement type this resolver handles.\n *\n * @public\n */\nexport interface MockResolver<R extends Requirement = Requirement> {\n /** All requirements received by this resolver */\n readonly calls: R[];\n /** Pending requirements waiting to be resolved/rejected */\n readonly pending: Array<{\n requirement: R;\n resolve: (result?: unknown) => void;\n reject: (error: Error) => void;\n }>;\n /** Resolve the next pending requirement */\n resolve(result?: unknown): void;\n /** Reject the next pending requirement */\n reject(error: Error): void;\n /** Resolve all pending requirements */\n resolveAll(result?: unknown): void;\n /** Reject all pending requirements */\n rejectAll(error: Error): void;\n /** Clear call history and pending queue */\n reset(): void;\n}\n\n/**\n * Create a mock resolver that captures requirements instead of resolving them,\n * giving you manual control over when and how each requirement completes.\n *\n * @param _requirementType - The requirement `type` string this mock handles (used for documentation; matching is done by the test harness).\n * @returns A {@link MockResolver} with a `handler` function suitable for passing to {@link createTestSystem} mocks.\n *\n * @example\n * ```typescript\n * const fetchMock = mockResolver<{ type: \"FETCH_USER\"; id: string }>(\"FETCH_USER\");\n *\n * const system = createTestSystem({\n * modules: [userModule],\n * mocks: {\n * resolvers: {\n * FETCH_USER: { resolve: fetchMock.handler },\n * },\n * },\n * });\n *\n * system.facts.userId = \"123\";\n * await flushMicrotasks();\n *\n * // Requirement is pending\n * expect(fetchMock.calls).toHaveLength(1);\n * expect(fetchMock.calls[0].id).toBe(\"123\");\n *\n * // Manually resolve it\n * fetchMock.resolve({ name: \"John\" });\n * await flushMicrotasks();\n *\n * expect(system.facts.user).toEqual({ name: \"John\" });\n * ```\n *\n * @public\n */\nexport function mockResolver<R extends Requirement = Requirement>(\n _requirementType: string,\n): MockResolver<R> & {\n /** Handler that can be passed to createTestSystem mocks */\n handler: (req: Requirement, ctx: MockResolverContext) => Promise<void>;\n} {\n const calls: R[] = [];\n const pending: Array<{\n requirement: R;\n resolve: (result?: unknown) => void;\n reject: (error: Error) => void;\n }> = [];\n\n const mock: MockResolver<R> = {\n get calls() {\n return calls;\n },\n get pending() {\n return pending;\n },\n resolve(result?: unknown) {\n const item = pending.shift();\n if (item) {\n item.resolve(result);\n }\n },\n reject(error: Error) {\n const item = pending.shift();\n if (item) {\n item.reject(error);\n }\n },\n resolveAll(result?: unknown) {\n while (pending.length > 0) {\n this.resolve(result);\n }\n },\n rejectAll(error: Error) {\n while (pending.length > 0) {\n this.reject(error);\n }\n },\n reset() {\n calls.length = 0;\n pending.length = 0;\n },\n };\n\n const handler = (\n req: Requirement,\n _ctx: MockResolverContext,\n ): Promise<void> => {\n calls.push(req as R);\n return new Promise<void>((resolve, reject) => {\n pending.push({\n requirement: req as R,\n resolve: (val?: unknown) => resolve(val as void),\n reject,\n });\n });\n };\n\n return {\n ...mock,\n handler,\n };\n}\n\n// ============================================================================\n// Fact Change Tracking\n// ============================================================================\n\n/**\n * Record of a single fact change captured by the test tracking plugin.\n *\n * @public\n */\nexport interface FactChangeRecord {\n /** The fact key that changed (without namespace prefix for namespaced systems) */\n key: string;\n /** The full key including namespace prefix (e.g., \"test::value\") */\n fullKey: string;\n /** The namespace (e.g., \"test\") - undefined for single-module systems */\n namespace?: string;\n /** The previous value */\n previousValue: unknown;\n /** The new value */\n newValue: unknown;\n /** Timestamp of the change */\n timestamp: number;\n}\n\n// ============================================================================\n// Test System\n// ============================================================================\n\n/** Common testing utilities shared by both single-module and namespaced test systems. */\nexport interface TestSystemBase {\n /**\n * Wait for all pending operations to complete.\n * @param maxWait - Maximum time to wait in ms (default: 5000)\n * @throws Error if timeout is exceeded with resolvers still inflight\n */\n waitForIdle(maxWait?: number): Promise<void>;\n /** Get the history of dispatched events */\n eventHistory: Array<{ type: string; [key: string]: unknown }>;\n /** Get resolver call history */\n resolverCalls: Map<string, Requirement[]>;\n /**\n * Get all requirements that have been generated (both resolved and pending).\n * Unlike `inspect().unmet`, this includes requirements that have already been handled.\n */\n readonly allRequirements: RequirementWithId[];\n /**\n * Get all fact changes since system start or last reset.\n */\n getFactsHistory(): FactChangeRecord[];\n /**\n * Reset the facts history tracking.\n */\n resetFactsHistory(): void;\n /** Assert that a requirement was created */\n assertRequirement(type: string): void;\n /** Assert that a resolver was called */\n assertResolverCalled(type: string, times?: number): void;\n /**\n * Assert that a fact was set to a specific value.\n */\n assertFactSet(key: string, value?: unknown): void;\n /**\n * Assert the number of times a fact was changed.\n */\n assertFactChanges(key: string, times: number): void;\n}\n\n/**\n * A single-module Directive system augmented with testing utilities.\n *\n * @remarks\n * Extends {@link SingleModuleSystem} with event/resolver/fact tracking, idle\n * waiting, and assertion helpers. Created via {@link createTestSystem}.\n *\n * @typeParam S - The module schema.\n *\n * @public\n */\nexport interface TestSystemSingle<S extends ModuleSchema>\n extends SingleModuleSystem<S>,\n TestSystemBase {}\n\n/**\n * A Directive system augmented with testing utilities.\n *\n * @remarks\n * Extends {@link NamespacedSystem} with event/resolver/fact tracking, idle\n * waiting, and assertion helpers. Created via {@link createTestSystem}.\n *\n * @typeParam Modules - The modules map that defines the system's schema.\n *\n * @public\n */\nexport interface TestSystem<Modules extends ModulesMap>\n extends NamespacedSystem<Modules>,\n TestSystemBase {}\n\n/**\n * Options for {@link createTestSystem}, extending the standard system options\n * with mock resolver injection and automatic tracking.\n *\n * @typeParam Modules - The modules map that defines the system's schema.\n *\n * @public\n */\nexport interface CreateTestSystemOptions<Modules extends ModulesMap>\n extends Omit<CreateSystemOptionsNamed<Modules>, \"plugins\"> {\n /** Mock resolvers by type */\n mocks?: {\n resolvers?: Record<string, MockResolverOptions>;\n };\n /** Additional plugins (tracking plugin is added automatically) */\n // biome-ignore lint/suspicious/noExplicitAny: Plugins are schema-agnostic\n plugins?: any[];\n}\n\n/**\n * Options for {@link createTestSystem} with a single module (no namespacing).\n *\n * @typeParam S - The module schema.\n *\n * @public\n */\nexport interface CreateTestSystemOptionsSingle<S extends ModuleSchema>\n extends Omit<CreateSystemOptionsSingle<S>, \"plugins\"> {\n /** Mock resolvers by type */\n mocks?: {\n resolvers?: Record<string, MockResolverOptions>;\n };\n /** Additional plugins (tracking plugin is added automatically) */\n // biome-ignore lint/suspicious/noExplicitAny: Plugins are schema-agnostic\n plugins?: any[];\n}\n\n/**\n * Create a Directive system instrumented for testing.\n *\n * Wraps {@link createSystem} with an automatic tracking plugin that records\n * dispatched events, resolver calls, fact changes, and generated requirements.\n * Mock resolvers can be injected via `options.mocks.resolvers` to replace\n * real resolvers by requirement type.\n *\n * @param options - System configuration with optional mock resolvers and additional plugins.\n * @returns A {@link TestSystem} or {@link TestSystemSingle} with assertion helpers, idle waiting, and history tracking.\n *\n * @example\n * ```typescript\n * // Namespaced (multiple modules)\n * const system = createTestSystem({\n * modules: { counter: counterModule },\n * mocks: { resolvers: { INCREMENT: { resolve: (req, context) => { context.facts.count++; } } } },\n * });\n *\n * // Single module\n * const system = createTestSystem({\n * module: counterModule,\n * });\n * system.start();\n * ```\n *\n * @public\n */\nexport function createTestSystem<S extends ModuleSchema>(\n options: CreateTestSystemOptionsSingle<S>,\n): TestSystemSingle<S>;\nexport function createTestSystem<Modules extends ModulesMap>(\n options: CreateTestSystemOptions<Modules>,\n): TestSystem<Modules>;\nexport function createTestSystem<\n S extends ModuleSchema,\n Modules extends ModulesMap,\n>(\n options:\n | CreateTestSystemOptionsSingle<S>\n | CreateTestSystemOptions<Modules>,\n): TestSystemSingle<S> | TestSystem<Modules> {\n // Single module mode: wrap into namespaced and delegate\n if (\"module\" in options) {\n return createTestSystemSingle(\n options as CreateTestSystemOptionsSingle<S>,\n );\n }\n\n return createTestSystemNamed(options as CreateTestSystemOptions<Modules>);\n}\n\nfunction createTestSystemSingle<S extends ModuleSchema>(\n options: CreateTestSystemOptionsSingle<S>,\n): TestSystemSingle<S> {\n const eventHistory: Array<{ type: string; [key: string]: unknown }> = [];\n const resolverCalls = new Map<string, Requirement[]>();\n const allRequirements: RequirementWithId[] = [];\n const factsHistory: FactChangeRecord[] = [];\n\n // Create mock resolvers\n const mockResolvers: Record<string, MockResolverDef> = {};\n if (options.mocks?.resolvers) {\n for (const [type, mockOptions] of Object.entries(options.mocks.resolvers)) {\n const calls: Requirement[] = [];\n resolverCalls.set(type, calls);\n mockResolvers[type] = createMockResolver({ ...mockOptions, calls });\n }\n }\n\n // Create module with mock resolvers\n // biome-ignore lint/suspicious/noExplicitAny: Mock resolvers have simplified types\n const moduleWithMocks: ModuleDef<S> = {\n ...options.module,\n resolvers: {\n ...options.module.resolvers,\n ...mockResolvers,\n } as any,\n };\n\n // Create tracking plugin\n const trackingPlugin = {\n name: \"__test-tracking__\",\n onFactSet: (fullKey: string, value: unknown, previousValue: unknown) => {\n factsHistory.push({\n key: fullKey,\n fullKey,\n namespace: undefined,\n previousValue,\n newValue: value,\n timestamp: Date.now(),\n });\n },\n onRequirementCreated: (requirement: RequirementWithId) => {\n allRequirements.push(requirement);\n },\n };\n\n // Create the underlying single-module system\n const system = createSystem({\n ...options,\n module: moduleWithMocks,\n plugins: [trackingPlugin, ...(options.plugins ?? [])],\n // biome-ignore lint/suspicious/noExplicitAny: Internal overload compatibility\n } as any) as SingleModuleSystem<S>;\n\n // Wrap dispatch to track events\n const originalDispatch = system.dispatch.bind(system);\n // biome-ignore lint/suspicious/noExplicitAny: Event type varies\n (system as any).dispatch = (event: any) => {\n eventHistory.push(event);\n originalDispatch(event);\n };\n\n const testSystem: TestSystemSingle<S> = {\n ...system,\n eventHistory,\n resolverCalls,\n\n get allRequirements() {\n return allRequirements;\n },\n\n getFactsHistory(): FactChangeRecord[] {\n return [...factsHistory];\n },\n\n resetFactsHistory(): void {\n factsHistory.length = 0;\n },\n\n async waitForIdle(maxWait = 5000): Promise<void> {\n const startTime = Date.now();\n\n const checkIdle = async (): Promise<void> => {\n await new Promise((resolve) => setTimeout(resolve, 0));\n const inspection = system.inspect();\n if (inspection.inflight.length > 0) {\n if (Date.now() - startTime > maxWait) {\n const resolverIds = inspection.inflight\n .map((r) => r.id)\n .join(\", \");\n throw new Error(\n `[Directive] waitForIdle timed out after ${maxWait}ms. ${inspection.inflight.length} resolvers still inflight: ${resolverIds}`,\n );\n }\n await new Promise((resolve) => setTimeout(resolve, 10));\n\n return checkIdle();\n }\n };\n\n return checkIdle();\n },\n\n assertRequirement(type: string): void {\n const hasRequirement = allRequirements.some(\n (r) => r.requirement.type === type,\n );\n if (!hasRequirement) {\n throw new Error(\n `[Directive] Expected requirement of type \"${type}\" but none found`,\n );\n }\n },\n\n assertResolverCalled(type: string, times?: number): void {\n const calls = resolverCalls.get(type) ?? [];\n if (times !== undefined) {\n if (calls.length !== times) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called ${times} times but was called ${calls.length} times`,\n );\n }\n } else if (calls.length === 0) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called but it was not`,\n );\n }\n },\n\n assertFactSet(key: string, value?: unknown): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length === 0) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set but it was not`,\n );\n }\n if (value !== undefined) {\n const hasValue = changes.some((c) => c.newValue === value);\n if (!hasValue) {\n const actualValues = changes\n .map((c) => JSON.stringify(c.newValue))\n .join(\", \");\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set to ${JSON.stringify(value)} but got: ${actualValues}`,\n );\n }\n }\n },\n\n assertFactChanges(key: string, times: number): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length !== times) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to change ${times} times but it changed ${changes.length} times`,\n );\n }\n },\n };\n\n return testSystem;\n}\n\nfunction createTestSystemNamed<Modules extends ModulesMap>(\n options: CreateTestSystemOptions<Modules>,\n): TestSystem<Modules> {\n const eventHistory: Array<{ type: string; [key: string]: unknown }> = [];\n const resolverCalls = new Map<string, Requirement[]>();\n const allRequirements: RequirementWithId[] = [];\n const factsHistory: FactChangeRecord[] = [];\n\n // Create mock resolvers\n const mockResolvers: Record<string, MockResolverDef> = {};\n if (options.mocks?.resolvers) {\n for (const [type, mockOptions] of Object.entries(options.mocks.resolvers)) {\n const calls: Requirement[] = [];\n resolverCalls.set(type, calls);\n mockResolvers[type] = createMockResolver({ ...mockOptions, calls });\n }\n }\n\n // Create modules with mock resolvers\n const modulesWithMocks: Modules = {} as Modules;\n for (const [name, module] of Object.entries(options.modules)) {\n // biome-ignore lint/suspicious/noExplicitAny: Module types are complex\n (modulesWithMocks as any)[name] = {\n ...module,\n resolvers: {\n ...module.resolvers,\n ...mockResolvers,\n },\n };\n }\n\n // Get module namespaces for key parsing\n const moduleNamespaces = new Set(Object.keys(options.modules));\n\n // Create tracking plugin\n const trackingPlugin = {\n name: \"__test-tracking__\",\n onFactSet: (fullKey: string, value: unknown, previousValue: unknown) => {\n // Parse namespaced key (e.g., \"test::value\" -> namespace: \"test\", key: \"value\")\n const SEPARATOR = \"::\";\n const sepIndex = fullKey.indexOf(SEPARATOR);\n let namespace: string | undefined;\n let key: string;\n\n if (sepIndex > 0) {\n const possibleNamespace = fullKey.substring(0, sepIndex);\n if (moduleNamespaces.has(possibleNamespace)) {\n namespace = possibleNamespace;\n key = fullKey.substring(sepIndex + SEPARATOR.length);\n } else {\n key = fullKey;\n }\n } else {\n key = fullKey;\n }\n\n factsHistory.push({\n key,\n fullKey,\n namespace,\n previousValue,\n newValue: value,\n timestamp: Date.now(),\n });\n },\n onRequirementCreated: (requirement: RequirementWithId) => {\n allRequirements.push(requirement);\n },\n };\n\n // Create the underlying system\n const system = createSystem({\n ...options,\n modules: modulesWithMocks,\n plugins: [trackingPlugin, ...(options.plugins ?? [])],\n }) as NamespacedSystem<Modules>;\n\n // Wrap dispatch to track events\n const originalDispatch = system.dispatch.bind(system);\n // biome-ignore lint/suspicious/noExplicitAny: Event type varies\n (system as any).dispatch = (event: any) => {\n eventHistory.push(event);\n originalDispatch(event);\n };\n\n const testSystem: TestSystem<Modules> = {\n ...system,\n eventHistory,\n resolverCalls,\n\n get allRequirements() {\n return allRequirements;\n },\n\n getFactsHistory(): FactChangeRecord[] {\n return [...factsHistory];\n },\n\n resetFactsHistory(): void {\n factsHistory.length = 0;\n },\n\n async waitForIdle(maxWait = 5000): Promise<void> {\n const startTime = Date.now();\n\n const checkIdle = async (): Promise<void> => {\n // Wait for microtasks\n await new Promise((resolve) => setTimeout(resolve, 0));\n\n // Check if there are inflight resolvers\n const inspection = system.inspect();\n if (inspection.inflight.length > 0) {\n // Check timeout\n if (Date.now() - startTime > maxWait) {\n const resolverIds = inspection.inflight.map((r) => r.id).join(\", \");\n throw new Error(\n `[Directive] waitForIdle timed out after ${maxWait}ms. ${inspection.inflight.length} resolvers still inflight: ${resolverIds}`,\n );\n }\n // Wait a bit more and check again\n await new Promise((resolve) => setTimeout(resolve, 10));\n return checkIdle();\n }\n };\n\n return checkIdle();\n },\n\n assertRequirement(type: string): void {\n const hasRequirement = allRequirements.some(\n (r) => r.requirement.type === type,\n );\n if (!hasRequirement) {\n throw new Error(\n `[Directive] Expected requirement of type \"${type}\" but none found`,\n );\n }\n },\n\n assertResolverCalled(type: string, times?: number): void {\n const calls = resolverCalls.get(type) ?? [];\n if (times !== undefined) {\n if (calls.length !== times) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called ${times} times but was called ${calls.length} times`,\n );\n }\n } else if (calls.length === 0) {\n throw new Error(\n `[Directive] Expected resolver \"${type}\" to be called but it was not`,\n );\n }\n },\n\n assertFactSet(key: string, value?: unknown): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length === 0) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set but it was not`,\n );\n }\n if (value !== undefined) {\n const hasValue = changes.some((c) => c.newValue === value);\n if (!hasValue) {\n const actualValues = changes\n .map((c) => JSON.stringify(c.newValue))\n .join(\", \");\n throw new Error(\n `[Directive] Expected fact \"${key}\" to be set to ${JSON.stringify(value)} but got: ${actualValues}`,\n );\n }\n }\n },\n\n assertFactChanges(key: string, times: number): void {\n const changes = factsHistory.filter((c) => c.key === key);\n if (changes.length !== times) {\n throw new Error(\n `[Directive] Expected fact \"${key}\" to change ${times} times but it changed ${changes.length} times`,\n );\n }\n },\n };\n\n return testSystem;\n}\n\n// ============================================================================\n// Dynamic Definition Assertions\n// ============================================================================\n\n/**\n * Assert that a definition was dynamically registered on the system.\n *\n * @param system - The Directive system to check.\n * @param type - The definition type: \"constraint\", \"resolver\", \"derivation\", or \"effect\".\n * @param id - The definition ID.\n * @throws Error if the definition is not dynamic.\n *\n * @example\n * ```typescript\n * system.constraints.register(\"myRule\", { when: () => true, require: { type: \"DO\" } });\n * assertDynamic(system, \"constraint\", \"myRule\"); // passes\n * assertDynamic(system, \"constraint\", \"staticRule\"); // throws\n * ```\n *\n * @public\n */\nexport function assertDynamic(\n system: {\n constraints: { isDynamic(id: string): boolean };\n effects: { isDynamic(id: string): boolean };\n resolvers: { isDynamic(id: string): boolean };\n derive: { isDynamic(id: string): boolean };\n },\n type: \"constraint\" | \"resolver\" | \"derivation\" | \"effect\",\n id: string,\n): void {\n const isDynamic = getDynamicCheck(system, type, id);\n if (!isDynamic) {\n throw new Error(\n `[Directive] Expected ${type} \"${id}\" to be dynamic, but it is not.`,\n );\n }\n}\n\n/**\n * Assert that a definition is NOT dynamically registered (i.e., is static or does not exist).\n *\n * @param system - The Directive system to check.\n * @param type - The definition type: \"constraint\", \"resolver\", \"derivation\", or \"effect\".\n * @param id - The definition ID.\n * @throws Error if the definition is dynamic.\n *\n * @example\n * ```typescript\n * assertNotDynamic(system, \"constraint\", \"staticRule\"); // passes\n * system.constraints.register(\"myRule\", def);\n * assertNotDynamic(system, \"constraint\", \"myRule\"); // throws\n * ```\n *\n * @public\n */\nexport function assertNotDynamic(\n system: {\n constraints: { isDynamic(id: string): boolean };\n effects: { isDynamic(id: string): boolean };\n resolvers: { isDynamic(id: string): boolean };\n derive: { isDynamic(id: string): boolean };\n },\n type: \"constraint\" | \"resolver\" | \"derivation\" | \"effect\",\n id: string,\n): void {\n const isDynamic = getDynamicCheck(system, type, id);\n if (isDynamic) {\n throw new Error(\n `[Directive] Expected ${type} \"${id}\" to NOT be dynamic, but it is.`,\n );\n }\n}\n\n/** @internal */\nfunction getDynamicCheck(\n system: {\n constraints: { isDynamic(id: string): boolean };\n effects: { isDynamic(id: string): boolean };\n resolvers: { isDynamic(id: string): boolean };\n derive: { isDynamic(id: string): boolean };\n },\n type: \"constraint\" | \"resolver\" | \"derivation\" | \"effect\",\n id: string,\n): boolean {\n switch (type) {\n case \"constraint\":\n return system.constraints.isDynamic(id);\n case \"resolver\":\n return system.resolvers.isDynamic(id);\n case \"derivation\":\n return system.derive.isDynamic(id);\n case \"effect\":\n return system.effects.isDynamic(id);\n }\n}\n\n// ============================================================================\n// Constraint Coverage\n// ============================================================================\n\n/** Coverage report for a Directive system. */\nexport interface CoverageReport {\n /** Constraints that evaluated to true at least once. */\n constraintsHit: Set<string>;\n /** Constraints that never evaluated to true. */\n constraintsMissed: Set<string>;\n /** Resolvers that started at least once. */\n resolversRun: Set<string>;\n /** Resolvers that never started. */\n resolversMissed: Set<string>;\n /** Effects that ran at least once. */\n effectsRun: Set<string>;\n /** Derivations that recomputed at least once. */\n derivationsComputed: Set<string>;\n /** Coverage percentage (constraintsHit / total constraints). */\n constraintCoverage: number;\n /** Coverage percentage (resolversRun / total resolvers). */\n resolverCoverage: number;\n}\n\n/**\n * Track which constraints, resolvers, effects, and derivations are exercised\n * during a test scenario. Returns a coverage report after the scenario runs.\n *\n * @example\n * ```typescript\n * const { run, report } = createCoverageTracker(system);\n *\n * await run(async () => {\n * system.facts.userId = 123;\n * await system.settle();\n * system.facts.userId = 0;\n * await system.settle();\n * });\n *\n * const coverage = report();\n * expect(coverage.constraintCoverage).toBe(1); // All constraints hit\n * expect(coverage.constraintsMissed.size).toBe(0);\n * ```\n */\nexport function createCoverageTracker(\n // biome-ignore lint/suspicious/noExplicitAny: Works with any system type\n system: SingleModuleSystem<any> | NamespacedSystem<any>,\n): {\n /** Run a test scenario while tracking coverage. */\n run: (scenario: () => Promise<void> | void) => Promise<void>;\n /** Get the coverage report. */\n report: () => CoverageReport;\n} {\n const constraintsHit = new Set<string>();\n const resolversRun = new Set<string>();\n const effectsRun = new Set<string>();\n const derivationsComputed = new Set<string>();\n\n let unsub: (() => void) | null = null;\n\n return {\n async run(scenario) {\n unsub = system.observe((event) => {\n switch (event.type) {\n case \"constraint.evaluate\":\n if (event.active) constraintsHit.add(event.id);\n break;\n case \"resolver.start\":\n resolversRun.add(event.resolver);\n break;\n case \"effect.run\":\n effectsRun.add(event.id);\n break;\n case \"derivation.compute\":\n derivationsComputed.add(event.id);\n break;\n }\n });\n\n try {\n await scenario();\n } finally {\n unsub?.();\n unsub = null;\n }\n },\n\n report(): CoverageReport {\n const inspection = system.inspect();\n\n const allConstraints = new Set(inspection.constraints.map((c) => c.id));\n const allResolvers = new Set(\n inspection.resolverDefs.map((r) => r.id),\n );\n\n const constraintsMissed = new Set<string>();\n for (const id of allConstraints) {\n if (!constraintsHit.has(id)) constraintsMissed.add(id);\n }\n\n const resolversMissed = new Set<string>();\n for (const id of allResolvers) {\n if (!resolversRun.has(id)) resolversMissed.add(id);\n }\n\n return {\n constraintsHit,\n constraintsMissed,\n resolversRun,\n resolversMissed,\n effectsRun,\n derivationsComputed,\n constraintCoverage:\n allConstraints.size === 0\n ? 1\n : constraintsHit.size / allConstraints.size,\n resolverCoverage:\n allResolvers.size === 0\n ? 1\n : resolversRun.size / allResolvers.size,\n };\n },\n };\n}\n\n// ============================================================================\n// Test Observer\n// ============================================================================\n\n/**\n * Create a test observer that collects all observation events.\n * Useful for assertion-based testing of system behavior.\n *\n * @example\n * ```typescript\n * const observer = createTestObserver(system);\n *\n * system.facts.count = 5;\n * await system.settle();\n *\n * expect(observer.events.filter(e => e.type === \"constraint.evaluate\")).toHaveLength(1);\n * expect(observer.ofType(\"resolver.complete\")).toHaveLength(1);\n *\n * observer.clear();\n * observer.dispose();\n * ```\n */\nexport function createTestObserver(\n // biome-ignore lint/suspicious/noExplicitAny: Works with any system type\n system: SingleModuleSystem<any> | NamespacedSystem<any>,\n): {\n /** All collected events. */\n events: import(\"../core/types/system.js\").ObservationEvent[];\n /** Filter events by type. */\n ofType: <T extends import(\"../core/types/system.js\").ObservationEvent[\"type\"]>(\n type: T,\n ) => Extract<import(\"../core/types/system.js\").ObservationEvent, { type: T }>[];\n /** Clear collected events. */\n clear: () => void;\n /** Stop observing. */\n dispose: () => void;\n} {\n const events: import(\"../core/types/system.js\").ObservationEvent[] = [];\n const unsub = system.observe((event) => events.push(event));\n\n return {\n events,\n ofType(type) {\n return events.filter((e) => e.type === type) as any;\n },\n clear() {\n events.length = 0;\n },\n dispose() {\n unsub();\n },\n };\n}\n"]}
|
package/dist/worker.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';function _(o){let{worker:c,onFactChange:l,onDerivationChange:y,onRequirementCreated:E,onRequirementMet:a,onError:r}=o,s=new Map,u=0,t=null,i=null,d=null,T=null;function g(e){return e?.(),null}function R(e,n){let S=s.get(e);S&&(S.resolve(n),s.delete(e));}function M(){t=g(t);}function m(){i=g(i);}function I(){d=g(d);}function b(){T=g(T);}function h(e){l?.(e.key,e.value,e.prev);}function O(e){y?.(e.key,e.value);}function W(e){E?.(e.requirement);}function C(e){a?.(e.requirementId,e.resolverId);}function P(e){r?.(e.error,e.source);}function D(e){R(e.requestId,e.snapshot);}function w(e){R(e.requestId,e.inspection);}function q(e){let n=s.get(e.requestId);n&&(e.success?n.resolve(void 0):n.reject(new Error(e.error||"Settle failed")),s.delete(e.requestId));}c.onmessage=e=>{let n=e.data;switch(n.type){case "READY":return M();case "STARTED":return m();case "STOPPED":return I();case "DESTROYED":return b();case "FACT_CHANGED":return h(n);case "DERIVATION_CHANGED":return O(n);case "REQUIREMENT_CREATED":return W(n);case "REQUIREMENT_MET":return C(n);case "ERROR":return P(n);case "SNAPSHOT_RESULT":return D(n);case "INSPECT_RESULT":return w(n);case "SETTLE_RESULT":return q(n)}},c.onerror=e=>{r?.(e.message,"worker");};function p(e){c.postMessage(e);}function k(e){return new Promise((n,S)=>{s.set(e.requestId,{resolve:n,reject:S}),p(e);})}return {init(e){return new Promise(n=>{t=n,p({type:"INIT",config:e});})},start(){return new Promise(e=>{i=e,p({type:"START"});})},stop(){return new Promise(e=>{d=e,p({type:"STOP"});})},destroy(){return new Promise(e=>{T=e,p({type:"DESTROY"});})},setFact(e,n){p({type:"SET_FACT",key:e,value:n});},setFacts(e){p({type:"SET_FACTS",facts:e});},dispatch(e){p({type:"DISPATCH",event:e});},getSnapshot(e){let n=`snapshot-${++u}`;return k({type:"GET_SNAPSHOT",options:e,requestId:n})},inspect(){let e=`inspect-${++u}`;return k({type:"INSPECT",requestId:e})},settle(e){let n=`settle-${++u}`;return k({type:"SETTLE",timeout:e,requestId:n})},terminate(){c.terminate();}}}var v=null;function f(){return v||(v=new Map),v}function N(o,c){f().set(o,c);}function x(){let o=null;async function c(t){let i=await A(t.config);return postMessage({type:"READY"}),i}function l(t){t.start(),postMessage({type:"STARTED"});}function y(t){t.stop(),postMessage({type:"STOPPED"});}function E(t){t.destroy(),postMessage({type:"DESTROYED"});}function a(t,i){let d=t.getSnapshot(i.options);postMessage({type:"SNAPSHOT_RESULT",requestId:i.requestId,snapshot:d});}function r(t,i){let d=t.inspect();postMessage({type:"INSPECT_RESULT",requestId:i.requestId,inspection:d});}async function s(t,i){try{await t.settle(i.timeout),postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:!0});}catch(d){postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:false,error:d instanceof Error?d.message:String(d)});}}async function u(t){if(t.type==="INIT"){o=await c(t);return}if(o)switch(t.type){case "START":l(o);break;case "STOP":y(o);break;case "DESTROY":E(o),o=null;break;case "SET_FACT":o.setFact(t.key,t.value);break;case "SET_FACTS":o.setFacts(t.facts);break;case "DISPATCH":o.dispatch(t.event);break;case "GET_SNAPSHOT":a(o,t);break;case "INSPECT":r(o,t);break;case "SETTLE":await s(o,t);break}}self.onmessage=async t=>{try{await u(t.data);}catch(i){postMessage({type:"ERROR",error:i instanceof Error?i.message:String(i),source:t.data.type});}};}async function A(o){let{createSystem:c}=await import('./system-
|
|
1
|
+
'use strict';function _(o){let{worker:c,onFactChange:l,onDerivationChange:y,onRequirementCreated:E,onRequirementMet:a,onError:r}=o,s=new Map,u=0,t=null,i=null,d=null,T=null;function g(e){return e?.(),null}function R(e,n){let S=s.get(e);S&&(S.resolve(n),s.delete(e));}function M(){t=g(t);}function m(){i=g(i);}function I(){d=g(d);}function b(){T=g(T);}function h(e){l?.(e.key,e.value,e.prev);}function O(e){y?.(e.key,e.value);}function W(e){E?.(e.requirement);}function C(e){a?.(e.requirementId,e.resolverId);}function P(e){r?.(e.error,e.source);}function D(e){R(e.requestId,e.snapshot);}function w(e){R(e.requestId,e.inspection);}function q(e){let n=s.get(e.requestId);n&&(e.success?n.resolve(void 0):n.reject(new Error(e.error||"Settle failed")),s.delete(e.requestId));}c.onmessage=e=>{let n=e.data;switch(n.type){case "READY":return M();case "STARTED":return m();case "STOPPED":return I();case "DESTROYED":return b();case "FACT_CHANGED":return h(n);case "DERIVATION_CHANGED":return O(n);case "REQUIREMENT_CREATED":return W(n);case "REQUIREMENT_MET":return C(n);case "ERROR":return P(n);case "SNAPSHOT_RESULT":return D(n);case "INSPECT_RESULT":return w(n);case "SETTLE_RESULT":return q(n)}},c.onerror=e=>{r?.(e.message,"worker");};function p(e){c.postMessage(e);}function k(e){return new Promise((n,S)=>{s.set(e.requestId,{resolve:n,reject:S}),p(e);})}return {init(e){return new Promise(n=>{t=n,p({type:"INIT",config:e});})},start(){return new Promise(e=>{i=e,p({type:"START"});})},stop(){return new Promise(e=>{d=e,p({type:"STOP"});})},destroy(){return new Promise(e=>{T=e,p({type:"DESTROY"});})},setFact(e,n){p({type:"SET_FACT",key:e,value:n});},setFacts(e){p({type:"SET_FACTS",facts:e});},dispatch(e){p({type:"DISPATCH",event:e});},getSnapshot(e){let n=`snapshot-${++u}`;return k({type:"GET_SNAPSHOT",options:e,requestId:n})},inspect(){let e=`inspect-${++u}`;return k({type:"INSPECT",requestId:e})},settle(e){let n=`settle-${++u}`;return k({type:"SETTLE",timeout:e,requestId:n})},terminate(){c.terminate();}}}var v=null;function f(){return v||(v=new Map),v}function N(o,c){f().set(o,c);}function x(){let o=null;async function c(t){let i=await A(t.config);return postMessage({type:"READY"}),i}function l(t){t.start(),postMessage({type:"STARTED"});}function y(t){t.stop(),postMessage({type:"STOPPED"});}function E(t){t.destroy(),postMessage({type:"DESTROYED"});}function a(t,i){let d=t.getSnapshot(i.options);postMessage({type:"SNAPSHOT_RESULT",requestId:i.requestId,snapshot:d});}function r(t,i){let d=t.inspect();postMessage({type:"INSPECT_RESULT",requestId:i.requestId,inspection:d});}async function s(t,i){try{await t.settle(i.timeout),postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:!0});}catch(d){postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:false,error:d instanceof Error?d.message:String(d)});}}async function u(t){if(t.type==="INIT"){o=await c(t);return}if(o)switch(t.type){case "START":l(o);break;case "STOP":y(o);break;case "DESTROY":E(o),o=null;break;case "SET_FACT":o.setFact(t.key,t.value);break;case "SET_FACTS":o.setFacts(t.facts);break;case "DISPATCH":o.dispatch(t.event);break;case "GET_SNAPSHOT":a(o,t);break;case "INSPECT":r(o,t);break;case "SETTLE":await s(o,t);break}}self.onmessage=async t=>{try{await u(t.data);}catch(i){postMessage({type:"ERROR",error:i instanceof Error?i.message:String(i),source:t.data.type});}};}async function A(o){let{createSystem:c}=await import('./system-6JXMY66X.cjs'),l=f(),y={};for(let r of o.moduleNames){let s=l.get(r);if(!s)throw new Error(`[Directive Worker] Module "${r}" not registered. Call registerWorkerModule('${r}', module) before handling messages.`);y[r]=s;}let a=c({modules:y,plugins:[{name:"__worker-tracking__",onFactSet:(r,s,u)=>{postMessage({type:"FACT_CHANGED",key:r,value:s,prev:u});},onDerivationCompute:(r,s)=>{postMessage({type:"DERIVATION_CHANGED",key:r,value:s});},onRequirementCreated:r=>{postMessage({type:"REQUIREMENT_CREATED",requirement:{...r.requirement,id:r.id}});},onRequirementMet:(r,s)=>{postMessage({type:"REQUIREMENT_MET",requirementId:r.id,resolverId:s});}}],history:o.history});return {start:()=>a.start(),stop:()=>a.stop(),destroy:()=>a.destroy(),setFact:(r,s)=>{a.facts[r]=s;},setFacts:r=>{let s=a.facts;if(s.$store?.batch)s.$store.batch(()=>{for(let[u,t]of Object.entries(r))s[u]=t;});else for(let[u,t]of Object.entries(r))s[u]=t;},dispatch:r=>{a.dispatch(r);},getSnapshot:r=>a.getDistributableSnapshot(r),inspect:()=>a.inspect(),settle:r=>a.settle(r)}}exports.createWorkerClient=_;exports.getWorkerModuleRegistry=f;exports.handleWorkerMessages=x;exports.registerWorkerModule=N;//# sourceMappingURL=worker.cjs.map
|
|
2
2
|
//# sourceMappingURL=worker.cjs.map
|
package/dist/worker.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { M as ModuleSchema, s as DistributableSnapshotOptions, r as DistributableSnapshot,
|
|
1
|
+
import { M as ModuleSchema, s as DistributableSnapshotOptions, r as DistributableSnapshot, $ as SystemInspection, o as Requirement } from './plugins-day_qfBB.cjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Web Worker Adapter - Run Directive engine off the main thread
|
package/dist/worker.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { M as ModuleSchema, s as DistributableSnapshotOptions, r as DistributableSnapshot,
|
|
1
|
+
import { M as ModuleSchema, s as DistributableSnapshotOptions, r as DistributableSnapshot, $ as SystemInspection, o as Requirement } from './plugins-day_qfBB.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Web Worker Adapter - Run Directive engine off the main thread
|
package/dist/worker.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function _(o){let{worker:c,onFactChange:l,onDerivationChange:y,onRequirementCreated:E,onRequirementMet:a,onError:r}=o,s=new Map,u=0,t=null,i=null,d=null,T=null;function g(e){return e?.(),null}function R(e,n){let S=s.get(e);S&&(S.resolve(n),s.delete(e));}function M(){t=g(t);}function m(){i=g(i);}function I(){d=g(d);}function b(){T=g(T);}function h(e){l?.(e.key,e.value,e.prev);}function O(e){y?.(e.key,e.value);}function W(e){E?.(e.requirement);}function C(e){a?.(e.requirementId,e.resolverId);}function P(e){r?.(e.error,e.source);}function D(e){R(e.requestId,e.snapshot);}function w(e){R(e.requestId,e.inspection);}function q(e){let n=s.get(e.requestId);n&&(e.success?n.resolve(void 0):n.reject(new Error(e.error||"Settle failed")),s.delete(e.requestId));}c.onmessage=e=>{let n=e.data;switch(n.type){case "READY":return M();case "STARTED":return m();case "STOPPED":return I();case "DESTROYED":return b();case "FACT_CHANGED":return h(n);case "DERIVATION_CHANGED":return O(n);case "REQUIREMENT_CREATED":return W(n);case "REQUIREMENT_MET":return C(n);case "ERROR":return P(n);case "SNAPSHOT_RESULT":return D(n);case "INSPECT_RESULT":return w(n);case "SETTLE_RESULT":return q(n)}},c.onerror=e=>{r?.(e.message,"worker");};function p(e){c.postMessage(e);}function k(e){return new Promise((n,S)=>{s.set(e.requestId,{resolve:n,reject:S}),p(e);})}return {init(e){return new Promise(n=>{t=n,p({type:"INIT",config:e});})},start(){return new Promise(e=>{i=e,p({type:"START"});})},stop(){return new Promise(e=>{d=e,p({type:"STOP"});})},destroy(){return new Promise(e=>{T=e,p({type:"DESTROY"});})},setFact(e,n){p({type:"SET_FACT",key:e,value:n});},setFacts(e){p({type:"SET_FACTS",facts:e});},dispatch(e){p({type:"DISPATCH",event:e});},getSnapshot(e){let n=`snapshot-${++u}`;return k({type:"GET_SNAPSHOT",options:e,requestId:n})},inspect(){let e=`inspect-${++u}`;return k({type:"INSPECT",requestId:e})},settle(e){let n=`settle-${++u}`;return k({type:"SETTLE",timeout:e,requestId:n})},terminate(){c.terminate();}}}var v=null;function f(){return v||(v=new Map),v}function N(o,c){f().set(o,c);}function x(){let o=null;async function c(t){let i=await A(t.config);return postMessage({type:"READY"}),i}function l(t){t.start(),postMessage({type:"STARTED"});}function y(t){t.stop(),postMessage({type:"STOPPED"});}function E(t){t.destroy(),postMessage({type:"DESTROYED"});}function a(t,i){let d=t.getSnapshot(i.options);postMessage({type:"SNAPSHOT_RESULT",requestId:i.requestId,snapshot:d});}function r(t,i){let d=t.inspect();postMessage({type:"INSPECT_RESULT",requestId:i.requestId,inspection:d});}async function s(t,i){try{await t.settle(i.timeout),postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:!0});}catch(d){postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:false,error:d instanceof Error?d.message:String(d)});}}async function u(t){if(t.type==="INIT"){o=await c(t);return}if(o)switch(t.type){case "START":l(o);break;case "STOP":y(o);break;case "DESTROY":E(o),o=null;break;case "SET_FACT":o.setFact(t.key,t.value);break;case "SET_FACTS":o.setFacts(t.facts);break;case "DISPATCH":o.dispatch(t.event);break;case "GET_SNAPSHOT":a(o,t);break;case "INSPECT":r(o,t);break;case "SETTLE":await s(o,t);break}}self.onmessage=async t=>{try{await u(t.data);}catch(i){postMessage({type:"ERROR",error:i instanceof Error?i.message:String(i),source:t.data.type});}};}async function A(o){let{createSystem:c}=await import('./system-
|
|
1
|
+
function _(o){let{worker:c,onFactChange:l,onDerivationChange:y,onRequirementCreated:E,onRequirementMet:a,onError:r}=o,s=new Map,u=0,t=null,i=null,d=null,T=null;function g(e){return e?.(),null}function R(e,n){let S=s.get(e);S&&(S.resolve(n),s.delete(e));}function M(){t=g(t);}function m(){i=g(i);}function I(){d=g(d);}function b(){T=g(T);}function h(e){l?.(e.key,e.value,e.prev);}function O(e){y?.(e.key,e.value);}function W(e){E?.(e.requirement);}function C(e){a?.(e.requirementId,e.resolverId);}function P(e){r?.(e.error,e.source);}function D(e){R(e.requestId,e.snapshot);}function w(e){R(e.requestId,e.inspection);}function q(e){let n=s.get(e.requestId);n&&(e.success?n.resolve(void 0):n.reject(new Error(e.error||"Settle failed")),s.delete(e.requestId));}c.onmessage=e=>{let n=e.data;switch(n.type){case "READY":return M();case "STARTED":return m();case "STOPPED":return I();case "DESTROYED":return b();case "FACT_CHANGED":return h(n);case "DERIVATION_CHANGED":return O(n);case "REQUIREMENT_CREATED":return W(n);case "REQUIREMENT_MET":return C(n);case "ERROR":return P(n);case "SNAPSHOT_RESULT":return D(n);case "INSPECT_RESULT":return w(n);case "SETTLE_RESULT":return q(n)}},c.onerror=e=>{r?.(e.message,"worker");};function p(e){c.postMessage(e);}function k(e){return new Promise((n,S)=>{s.set(e.requestId,{resolve:n,reject:S}),p(e);})}return {init(e){return new Promise(n=>{t=n,p({type:"INIT",config:e});})},start(){return new Promise(e=>{i=e,p({type:"START"});})},stop(){return new Promise(e=>{d=e,p({type:"STOP"});})},destroy(){return new Promise(e=>{T=e,p({type:"DESTROY"});})},setFact(e,n){p({type:"SET_FACT",key:e,value:n});},setFacts(e){p({type:"SET_FACTS",facts:e});},dispatch(e){p({type:"DISPATCH",event:e});},getSnapshot(e){let n=`snapshot-${++u}`;return k({type:"GET_SNAPSHOT",options:e,requestId:n})},inspect(){let e=`inspect-${++u}`;return k({type:"INSPECT",requestId:e})},settle(e){let n=`settle-${++u}`;return k({type:"SETTLE",timeout:e,requestId:n})},terminate(){c.terminate();}}}var v=null;function f(){return v||(v=new Map),v}function N(o,c){f().set(o,c);}function x(){let o=null;async function c(t){let i=await A(t.config);return postMessage({type:"READY"}),i}function l(t){t.start(),postMessage({type:"STARTED"});}function y(t){t.stop(),postMessage({type:"STOPPED"});}function E(t){t.destroy(),postMessage({type:"DESTROYED"});}function a(t,i){let d=t.getSnapshot(i.options);postMessage({type:"SNAPSHOT_RESULT",requestId:i.requestId,snapshot:d});}function r(t,i){let d=t.inspect();postMessage({type:"INSPECT_RESULT",requestId:i.requestId,inspection:d});}async function s(t,i){try{await t.settle(i.timeout),postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:!0});}catch(d){postMessage({type:"SETTLE_RESULT",requestId:i.requestId,success:false,error:d instanceof Error?d.message:String(d)});}}async function u(t){if(t.type==="INIT"){o=await c(t);return}if(o)switch(t.type){case "START":l(o);break;case "STOP":y(o);break;case "DESTROY":E(o),o=null;break;case "SET_FACT":o.setFact(t.key,t.value);break;case "SET_FACTS":o.setFacts(t.facts);break;case "DISPATCH":o.dispatch(t.event);break;case "GET_SNAPSHOT":a(o,t);break;case "INSPECT":r(o,t);break;case "SETTLE":await s(o,t);break}}self.onmessage=async t=>{try{await u(t.data);}catch(i){postMessage({type:"ERROR",error:i instanceof Error?i.message:String(i),source:t.data.type});}};}async function A(o){let{createSystem:c}=await import('./system-I534ZO6E.js'),l=f(),y={};for(let r of o.moduleNames){let s=l.get(r);if(!s)throw new Error(`[Directive Worker] Module "${r}" not registered. Call registerWorkerModule('${r}', module) before handling messages.`);y[r]=s;}let a=c({modules:y,plugins:[{name:"__worker-tracking__",onFactSet:(r,s,u)=>{postMessage({type:"FACT_CHANGED",key:r,value:s,prev:u});},onDerivationCompute:(r,s)=>{postMessage({type:"DERIVATION_CHANGED",key:r,value:s});},onRequirementCreated:r=>{postMessage({type:"REQUIREMENT_CREATED",requirement:{...r.requirement,id:r.id}});},onRequirementMet:(r,s)=>{postMessage({type:"REQUIREMENT_MET",requirementId:r.id,resolverId:s});}}],history:o.history});return {start:()=>a.start(),stop:()=>a.stop(),destroy:()=>a.destroy(),setFact:(r,s)=>{a.facts[r]=s;},setFacts:r=>{let s=a.facts;if(s.$store?.batch)s.$store.batch(()=>{for(let[u,t]of Object.entries(r))s[u]=t;});else for(let[u,t]of Object.entries(r))s[u]=t;},dispatch:r=>{a.dispatch(r);},getSnapshot:r=>a.getDistributableSnapshot(r),inspect:()=>a.inspect(),settle:r=>a.settle(r)}}export{_ as createWorkerClient,f as getWorkerModuleRegistry,x as handleWorkerMessages,N as registerWorkerModule};//# sourceMappingURL=worker.js.map
|
|
2
2
|
//# sourceMappingURL=worker.js.map
|
package/package.json
CHANGED