slicejs-web-framework 3.4.3 → 3.5.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/.opencode/opencode.json +14 -14
- package/Slice/Components/Structural/ContextManager/ContextManager.js +422 -422
- package/Slice/Components/Structural/ContextManager/ContextManagerDebugger.js +468 -468
- package/Slice/Components/Structural/Controller/Controller.js +1406 -1198
- package/Slice/Components/Structural/Debugger/Debugger.js +1503 -1503
- package/Slice/Components/Structural/EventManager/EventManager.js +651 -366
- package/Slice/Components/Structural/EventManager/EventManagerDebugger.js +368 -30
- package/Slice/Components/Structural/LeakInspector/LeakInspector.js +296 -0
- package/Slice/Components/Structural/Logger/Logger.js +225 -225
- package/Slice/Components/Structural/Router/Router.js +775 -775
- package/Slice/Components/Structural/StylesManager/StylesManager.js +82 -82
- package/Slice/Components/Structural/StylesManager/ThemeManager/ThemeManager.js +106 -106
- package/Slice/Slice.js +663 -627
- package/Slice/tests/build-singleton.test.js +244 -244
- package/Slice/tests/bundle-v2-runtime-contract.test.js +738 -738
- package/Slice/tests/context-patch-use.test.js +95 -95
- package/Slice/tests/event-emitters.test.js +79 -0
- package/Slice/tests/event-registry.test.js +141 -0
- package/Slice/tests/fixtures/real-runtime-loader.mjs +21 -21
- package/Slice/tests/leak-orphans.test.js +131 -0
- package/Slice/tests/public-env-typed-accessors.test.js +66 -66
- package/api/index.js +308 -281
- package/api/utils/publicEnvResolver.js +123 -123
- package/package.json +44 -44
- package/sliceConfig.schema.json +10 -1
- package/src/sliceConfig.json +5 -1
- package/types/index.d.ts +12 -0
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
|
|
4
|
-
globalThis.alert = () => {};
|
|
5
|
-
|
|
6
|
-
// Minimal global `slice` so ContextManager's slice.logger / slice.events work in
|
|
7
|
-
// isolation (a tiny in-memory event bus drives the watch notifications).
|
|
8
|
-
const subscribers = new Map(); // eventName -> Set<fn>
|
|
9
|
-
let subId = 0;
|
|
10
|
-
globalThis.slice = {
|
|
11
|
-
logger: { logError() {}, logInfo() {}, logWarning() {} },
|
|
12
|
-
events: {
|
|
13
|
-
subscribe(name, cb) {
|
|
14
|
-
if (!subscribers.has(name)) subscribers.set(name, new Set());
|
|
15
|
-
subscribers.get(name).add(cb);
|
|
16
|
-
return `sub-${++subId}`;
|
|
17
|
-
},
|
|
18
|
-
emit(name, ...args) {
|
|
19
|
-
for (const cb of subscribers.get(name) ?? []) cb(...args);
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const { default: ContextManager } = await import(
|
|
25
|
-
'../Components/Structural/ContextManager/ContextManager.js'
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
function freshContext(name, initial) {
|
|
29
|
-
const ctx = new ContextManager();
|
|
30
|
-
ctx.create(name, initial);
|
|
31
|
-
return ctx;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
test('patch merges into existing state (does not replace)', () => {
|
|
35
|
-
const ctx = freshContext('cart', { items: 2, total: 47, discount: 0 });
|
|
36
|
-
ctx.patch('cart', { discount: 0.1 });
|
|
37
|
-
assert.deepEqual(ctx.getState('cart'), { items: 2, total: 47, discount: 0.1 });
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('setState replaces, patch keeps the rest', () => {
|
|
41
|
-
const ctx = freshContext('s', { a: 1, b: 2 });
|
|
42
|
-
ctx.setState('s', { a: 9 }); // replace → drops b
|
|
43
|
-
assert.deepEqual(ctx.getState('s'), { a: 9 });
|
|
44
|
-
ctx.patch('s', { b: 5 }); // merge onto { a: 9 }
|
|
45
|
-
assert.deepEqual(ctx.getState('s'), { a: 9, b: 5 });
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test('patch rejects non-object partials and missing contexts (no throw)', () => {
|
|
49
|
-
const ctx = freshContext('s', { a: 1 });
|
|
50
|
-
ctx.patch('s', null);
|
|
51
|
-
ctx.patch('s', [1, 2]);
|
|
52
|
-
ctx.patch('nope', { x: 1 });
|
|
53
|
-
assert.deepEqual(ctx.getState('s'), { a: 1 });
|
|
54
|
-
assert.equal(ctx.has('nope'), false);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('use(name) returns a handle bound to that context', () => {
|
|
58
|
-
const ctx = freshContext('settings', { model: 'a' });
|
|
59
|
-
const settings = ctx.use('settings');
|
|
60
|
-
|
|
61
|
-
assert.equal(settings.get().model, 'a');
|
|
62
|
-
settings.set({ model: 'b' });
|
|
63
|
-
assert.equal(ctx.getState('settings').model, 'b');
|
|
64
|
-
settings.patch({ theme: 'dark' });
|
|
65
|
-
assert.deepEqual(ctx.getState('settings'), { model: 'b', theme: 'dark' });
|
|
66
|
-
assert.equal(settings.has(), true);
|
|
67
|
-
settings.destroy();
|
|
68
|
-
assert.equal(ctx.has('settings'), false);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('use(name).bind calls back immediately and on every change', () => {
|
|
72
|
-
const ctx = freshContext('cart', { items: [], n: 0 });
|
|
73
|
-
const calls = [];
|
|
74
|
-
ctx.use('cart').bind({ sliceId: 'c1' }, (state) => calls.push(state.n));
|
|
75
|
-
|
|
76
|
-
assert.deepEqual(calls, [0]); // immediate initial call
|
|
77
|
-
ctx.patch('cart', { n: 1 });
|
|
78
|
-
assert.deepEqual(calls, [0, 1]); // fired on change
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('use(name).bind with a selector only fires when the selected value changes', () => {
|
|
82
|
-
const ctx = freshContext('cart', { items: ['x'], note: 'a' });
|
|
83
|
-
const counts = [];
|
|
84
|
-
ctx.use('cart').bind(
|
|
85
|
-
{ sliceId: 'c2' },
|
|
86
|
-
(count) => counts.push(count),
|
|
87
|
-
(s) => s.items.length
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
assert.deepEqual(counts, [1]); // immediate, selected value = length
|
|
91
|
-
ctx.patch('cart', { note: 'b' }); // length unchanged → no fire
|
|
92
|
-
assert.deepEqual(counts, [1]);
|
|
93
|
-
ctx.patch('cart', { items: ['x', 'y'] }); // length changed → fire
|
|
94
|
-
assert.deepEqual(counts, [1, 2]);
|
|
95
|
-
});
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
|
|
4
|
+
globalThis.alert = () => {};
|
|
5
|
+
|
|
6
|
+
// Minimal global `slice` so ContextManager's slice.logger / slice.events work in
|
|
7
|
+
// isolation (a tiny in-memory event bus drives the watch notifications).
|
|
8
|
+
const subscribers = new Map(); // eventName -> Set<fn>
|
|
9
|
+
let subId = 0;
|
|
10
|
+
globalThis.slice = {
|
|
11
|
+
logger: { logError() {}, logInfo() {}, logWarning() {} },
|
|
12
|
+
events: {
|
|
13
|
+
subscribe(name, cb) {
|
|
14
|
+
if (!subscribers.has(name)) subscribers.set(name, new Set());
|
|
15
|
+
subscribers.get(name).add(cb);
|
|
16
|
+
return `sub-${++subId}`;
|
|
17
|
+
},
|
|
18
|
+
emit(name, ...args) {
|
|
19
|
+
for (const cb of subscribers.get(name) ?? []) cb(...args);
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const { default: ContextManager } = await import(
|
|
25
|
+
'../Components/Structural/ContextManager/ContextManager.js'
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
function freshContext(name, initial) {
|
|
29
|
+
const ctx = new ContextManager();
|
|
30
|
+
ctx.create(name, initial);
|
|
31
|
+
return ctx;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
test('patch merges into existing state (does not replace)', () => {
|
|
35
|
+
const ctx = freshContext('cart', { items: 2, total: 47, discount: 0 });
|
|
36
|
+
ctx.patch('cart', { discount: 0.1 });
|
|
37
|
+
assert.deepEqual(ctx.getState('cart'), { items: 2, total: 47, discount: 0.1 });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('setState replaces, patch keeps the rest', () => {
|
|
41
|
+
const ctx = freshContext('s', { a: 1, b: 2 });
|
|
42
|
+
ctx.setState('s', { a: 9 }); // replace → drops b
|
|
43
|
+
assert.deepEqual(ctx.getState('s'), { a: 9 });
|
|
44
|
+
ctx.patch('s', { b: 5 }); // merge onto { a: 9 }
|
|
45
|
+
assert.deepEqual(ctx.getState('s'), { a: 9, b: 5 });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('patch rejects non-object partials and missing contexts (no throw)', () => {
|
|
49
|
+
const ctx = freshContext('s', { a: 1 });
|
|
50
|
+
ctx.patch('s', null);
|
|
51
|
+
ctx.patch('s', [1, 2]);
|
|
52
|
+
ctx.patch('nope', { x: 1 });
|
|
53
|
+
assert.deepEqual(ctx.getState('s'), { a: 1 });
|
|
54
|
+
assert.equal(ctx.has('nope'), false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('use(name) returns a handle bound to that context', () => {
|
|
58
|
+
const ctx = freshContext('settings', { model: 'a' });
|
|
59
|
+
const settings = ctx.use('settings');
|
|
60
|
+
|
|
61
|
+
assert.equal(settings.get().model, 'a');
|
|
62
|
+
settings.set({ model: 'b' });
|
|
63
|
+
assert.equal(ctx.getState('settings').model, 'b');
|
|
64
|
+
settings.patch({ theme: 'dark' });
|
|
65
|
+
assert.deepEqual(ctx.getState('settings'), { model: 'b', theme: 'dark' });
|
|
66
|
+
assert.equal(settings.has(), true);
|
|
67
|
+
settings.destroy();
|
|
68
|
+
assert.equal(ctx.has('settings'), false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('use(name).bind calls back immediately and on every change', () => {
|
|
72
|
+
const ctx = freshContext('cart', { items: [], n: 0 });
|
|
73
|
+
const calls = [];
|
|
74
|
+
ctx.use('cart').bind({ sliceId: 'c1' }, (state) => calls.push(state.n));
|
|
75
|
+
|
|
76
|
+
assert.deepEqual(calls, [0]); // immediate initial call
|
|
77
|
+
ctx.patch('cart', { n: 1 });
|
|
78
|
+
assert.deepEqual(calls, [0, 1]); // fired on change
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('use(name).bind with a selector only fires when the selected value changes', () => {
|
|
82
|
+
const ctx = freshContext('cart', { items: ['x'], note: 'a' });
|
|
83
|
+
const counts = [];
|
|
84
|
+
ctx.use('cart').bind(
|
|
85
|
+
{ sliceId: 'c2' },
|
|
86
|
+
(count) => counts.push(count),
|
|
87
|
+
(s) => s.items.length
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
assert.deepEqual(counts, [1]); // immediate, selected value = length
|
|
91
|
+
ctx.patch('cart', { note: 'b' }); // length unchanged → no fire
|
|
92
|
+
assert.deepEqual(counts, [1]);
|
|
93
|
+
ctx.patch('cart', { items: ['x', 'y'] }); // length changed → fire
|
|
94
|
+
assert.deepEqual(counts, [1, 2]);
|
|
95
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Tests for emitter tracing: who emits each event (bind attribution + dev
|
|
2
|
+
// stack-trace fallback), recorded only while a debugger is "open" (recording).
|
|
3
|
+
import test from 'node:test';
|
|
4
|
+
import assert from 'node:assert/strict';
|
|
5
|
+
|
|
6
|
+
globalThis.slice = {
|
|
7
|
+
isProduction: () => false,
|
|
8
|
+
controller: { activeComponents: new Map() },
|
|
9
|
+
logger: { logError() {}, logInfo() {}, logWarning() {}, info() {}, error() {} },
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const { default: EventManager } = await import('../Components/Structural/EventManager/EventManager.js');
|
|
13
|
+
|
|
14
|
+
test('bound emit is attributed to its component', () => {
|
|
15
|
+
const em = new EventManager();
|
|
16
|
+
em.startRecording();
|
|
17
|
+
const comp = { sliceId: 'card-1', constructor: { name: 'Card' } };
|
|
18
|
+
em.bind(comp).emit('user:login', { id: 1 });
|
|
19
|
+
|
|
20
|
+
const bucket = em.emitters.get('user:login');
|
|
21
|
+
assert.ok(bucket, 'event has emitters bucket');
|
|
22
|
+
const entry = bucket.get('card-1');
|
|
23
|
+
assert.equal(entry.name, 'Card');
|
|
24
|
+
assert.equal(entry.sliceId, 'card-1');
|
|
25
|
+
assert.equal(entry.count, 1);
|
|
26
|
+
|
|
27
|
+
const hist = em.emitHistory.find((e) => e.eventName === 'user:login');
|
|
28
|
+
assert.equal(hist.emitter.sliceId, 'card-1', 'history entry carries the emitter');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('global emit falls back to a dev stack-trace source', () => {
|
|
32
|
+
const em = new EventManager();
|
|
33
|
+
em.startRecording();
|
|
34
|
+
em.emit('router:change');
|
|
35
|
+
|
|
36
|
+
const hist = em.emitHistory.find((e) => e.eventName === 'router:change');
|
|
37
|
+
assert.ok(hist.emitter, 'emitter captured for global emit');
|
|
38
|
+
assert.ok(hist.emitter.source && hist.emitter.source.includes('.js'), 'source is file:line');
|
|
39
|
+
assert.ok(!hist.emitter.source.includes('EventManager.js'), 'attributes the caller, not the manager');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('emitter counts accumulate per distinct source', () => {
|
|
43
|
+
const em = new EventManager();
|
|
44
|
+
em.startRecording();
|
|
45
|
+
em.bind({ sliceId: 'a-1', constructor: { name: 'A' } }).emit('x:y');
|
|
46
|
+
em.bind({ sliceId: 'a-1', constructor: { name: 'A' } }).emit('x:y');
|
|
47
|
+
em.bind({ sliceId: 'b-2', constructor: { name: 'B' } }).emit('x:y');
|
|
48
|
+
|
|
49
|
+
const bucket = em.emitters.get('x:y');
|
|
50
|
+
assert.equal(bucket.get('a-1').count, 2);
|
|
51
|
+
assert.equal(bucket.get('b-2').count, 1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('no recording (debugger closed) => no emitter tracking, zero overhead', () => {
|
|
55
|
+
const em = new EventManager();
|
|
56
|
+
em.emit('a:b');
|
|
57
|
+
em.bind({ sliceId: 'z-1', constructor: { name: 'Z' } }).emit('a:b');
|
|
58
|
+
assert.equal(em.emitters.size, 0);
|
|
59
|
+
assert.equal(em.emitHistory.length, 0);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('production: no stack-trace source captured', () => {
|
|
63
|
+
globalThis.slice.isProduction = () => true;
|
|
64
|
+
const em = new EventManager();
|
|
65
|
+
em.startRecording();
|
|
66
|
+
em.emit('p:q');
|
|
67
|
+
const hist = em.emitHistory.find((e) => e.eventName === 'p:q');
|
|
68
|
+
assert.equal(hist.emitter, null, 'no source attribution in production');
|
|
69
|
+
globalThis.slice.isProduction = () => false;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('startRecording resets the emitters map', () => {
|
|
73
|
+
const em = new EventManager();
|
|
74
|
+
em.startRecording();
|
|
75
|
+
em.bind({ sliceId: 'a-1', constructor: { name: 'A' } }).emit('x:y');
|
|
76
|
+
assert.equal(em.emitters.size, 1);
|
|
77
|
+
em.startRecording();
|
|
78
|
+
assert.equal(em.emitters.size, 0, 'fresh session clears prior emitters');
|
|
79
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Unit tests for the EventManager typed-event registry (register/validate/warn).
|
|
2
|
+
// EventManager only depends on the `slice` global for logging + isProduction +
|
|
3
|
+
// controller.activeComponents, so we stub a minimal one and drive the real class.
|
|
4
|
+
import test from 'node:test';
|
|
5
|
+
import assert from 'node:assert/strict';
|
|
6
|
+
|
|
7
|
+
const warnings = [];
|
|
8
|
+
globalThis.slice = {
|
|
9
|
+
isProduction: () => false,
|
|
10
|
+
controller: { activeComponents: new Map() },
|
|
11
|
+
logger: {
|
|
12
|
+
logError() {},
|
|
13
|
+
logInfo() {},
|
|
14
|
+
logWarning(_c, message) { warnings.push(message); },
|
|
15
|
+
info() {}, error() {}, warn() {},
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const { default: EventManager } = await import('../Components/Structural/EventManager/EventManager.js');
|
|
20
|
+
|
|
21
|
+
const undeclaredWarns = (name) => warnings.filter((w) => w.includes('Undeclared') && (!name || w.includes(name)));
|
|
22
|
+
|
|
23
|
+
test('loose mode: with no registry, nothing warns', () => {
|
|
24
|
+
warnings.length = 0;
|
|
25
|
+
const em = new EventManager();
|
|
26
|
+
em.emit('anything:happens');
|
|
27
|
+
em.subscribe('whatever:event', () => {});
|
|
28
|
+
assert.equal(undeclaredWarns().length, 0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('declared event emits and subscribes without warning', () => {
|
|
32
|
+
warnings.length = 0;
|
|
33
|
+
const em = new EventManager();
|
|
34
|
+
em.register({ 'user:login': { payload: { id: 'number' } } });
|
|
35
|
+
em.subscribe('user:login', () => {});
|
|
36
|
+
em.emit('user:login', { id: 1 });
|
|
37
|
+
assert.equal(undeclaredWarns().length, 0);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('undeclared event warns exactly once but still delivers', () => {
|
|
41
|
+
warnings.length = 0;
|
|
42
|
+
const em = new EventManager();
|
|
43
|
+
em.register({ 'user:login': { payload: null } });
|
|
44
|
+
|
|
45
|
+
let calls = 0;
|
|
46
|
+
em.subscribe('ghost:event', () => { calls += 1; }); // subscribe (undeclared) -> warns
|
|
47
|
+
em.emit('ghost:event'); // emit (undeclared, already warned)
|
|
48
|
+
em.emit('ghost:event');
|
|
49
|
+
|
|
50
|
+
assert.equal(undeclaredWarns('ghost:event').length, 1, 'warned exactly once');
|
|
51
|
+
assert.equal(calls, 2, 'delivery still happened both emits');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('framework events (router:*/context:*) never warn', () => {
|
|
55
|
+
warnings.length = 0;
|
|
56
|
+
const em = new EventManager();
|
|
57
|
+
em.register({ 'user:login': { payload: null } });
|
|
58
|
+
em.emit('router:change');
|
|
59
|
+
em.emit('context:user', {});
|
|
60
|
+
em.subscribe('context:cart', () => {});
|
|
61
|
+
assert.equal(undeclaredWarns().length, 0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('production: undeclared events do not warn', () => {
|
|
65
|
+
warnings.length = 0;
|
|
66
|
+
globalThis.slice.isProduction = () => true;
|
|
67
|
+
const em = new EventManager();
|
|
68
|
+
em.register({ 'a:b': { payload: null } });
|
|
69
|
+
em.emit('zzz:undeclared');
|
|
70
|
+
assert.equal(undeclaredWarns().length, 0);
|
|
71
|
+
globalThis.slice.isProduction = () => false;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('register() merges catalogs and is chainable', () => {
|
|
75
|
+
const em = new EventManager();
|
|
76
|
+
const ret = em.register({ 'a:1': { payload: null } }).register({ 'b:2': { payload: { x: 'string' } } });
|
|
77
|
+
assert.equal(ret, em, 'returns the manager for chaining');
|
|
78
|
+
assert.ok(em.registry.has('a:1') && em.registry.has('b:2'), 'both catalogs merged');
|
|
79
|
+
assert.equal(em.isDeclared('a:1'), true);
|
|
80
|
+
assert.equal(em.isDeclared('nope:nope'), false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('register(namespace, catalog) prefixes keys', () => {
|
|
84
|
+
const em = new EventManager();
|
|
85
|
+
em.register('user', { login: { payload: { id: 'number' } }, logout: { payload: null } });
|
|
86
|
+
assert.ok(em.registry.has('user:login'), 'user:login declared');
|
|
87
|
+
assert.ok(em.registry.has('user:logout'), 'user:logout declared');
|
|
88
|
+
assert.equal(em.namespaceOf('user:login'), 'user');
|
|
89
|
+
assert.equal(em.namespaceOf('ready'), null);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('event without namespace warns once in dev', () => {
|
|
93
|
+
warnings.length = 0;
|
|
94
|
+
const em = new EventManager();
|
|
95
|
+
em.register({ 'ready': { payload: null } }); // declared but no namespace -> warns
|
|
96
|
+
em.emit('ready');
|
|
97
|
+
em.emit('ready');
|
|
98
|
+
const nsWarns = warnings.filter((w) => w.includes('no namespace'));
|
|
99
|
+
assert.equal(nsWarns.length, 1, 'namespace warning fired exactly once');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('no-namespace warning is silent in loose mode (no registry)', () => {
|
|
103
|
+
warnings.length = 0;
|
|
104
|
+
const em = new EventManager();
|
|
105
|
+
em.emit('ready'); // nobody registered -> loose mode, no warnings at all
|
|
106
|
+
assert.equal(warnings.length, 0);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('loadGraph stores the static graph and exposes emitters/listeners', () => {
|
|
110
|
+
const em = new EventManager();
|
|
111
|
+
assert.deepEqual(em.staticEmittersOf('user:login'), [], 'empty before loadGraph');
|
|
112
|
+
em.loadGraph({
|
|
113
|
+
events: {
|
|
114
|
+
'user:login': {
|
|
115
|
+
payload: { id: 'number' },
|
|
116
|
+
emitters: [{ file: 'src/Login.js', line: 12, component: 'Login' }],
|
|
117
|
+
listeners: [{ file: 'src/Header.js', line: 5, component: 'Header' }],
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
dynamic: { emitters: [], listeners: [] },
|
|
121
|
+
});
|
|
122
|
+
assert.equal(em.staticEmittersOf('user:login')[0].component, 'Login');
|
|
123
|
+
assert.equal(em.staticListenersOf('user:login')[0].component, 'Header');
|
|
124
|
+
assert.deepEqual(em.staticEmittersOf('unknown:event'), []);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('loadGraph ignores malformed input', () => {
|
|
128
|
+
const em = new EventManager();
|
|
129
|
+
em.loadGraph(null);
|
|
130
|
+
em.loadGraph({ nope: true });
|
|
131
|
+
assert.equal(em.graph, null);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('undeclared usage is tracked for the debugger drift view', () => {
|
|
135
|
+
warnings.length = 0;
|
|
136
|
+
const em = new EventManager();
|
|
137
|
+
em.register({ 'declared:ok': { payload: null } });
|
|
138
|
+
em.emit('drifted:event');
|
|
139
|
+
assert.ok(em.undeclared.has('drifted:event'), 'drift set records undeclared usage');
|
|
140
|
+
assert.ok(!em.undeclared.has('declared:ok'), 'declared events are not drift');
|
|
141
|
+
});
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
// Node ESM resolve hook so the REAL Controller can load under `node --test`.
|
|
2
|
-
//
|
|
3
|
-
// Controller.js does `import components from '/Components/components.js'` — a
|
|
4
|
-
// browser-absolute path that the dev server serves but node can't resolve. We
|
|
5
|
-
// map only that exact specifier to a small fixture components map (just data,
|
|
6
|
-
// the same shape the real components.js exports). Everything else resolves
|
|
7
|
-
// normally, so the tests exercise the real Slice + Controller code, not mocks.
|
|
8
|
-
// AppProbe lives in a CUSTOM category ('AppServices') whose type is 'Service' —
|
|
9
|
-
// it must be allowed as a singleton (the type, not the category name, decides).
|
|
10
|
-
const COMPONENTS = { Probe: 'Service', ModalProbe: 'Visual', AppProbe: 'AppServices' };
|
|
11
|
-
const STUB = `export default ${JSON.stringify(COMPONENTS)};`;
|
|
12
|
-
|
|
13
|
-
export async function resolve(specifier, context, nextResolve) {
|
|
14
|
-
if (specifier === '/Components/components.js') {
|
|
15
|
-
return {
|
|
16
|
-
url: `data:text/javascript,${encodeURIComponent(STUB)}`,
|
|
17
|
-
shortCircuit: true
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
return nextResolve(specifier, context);
|
|
21
|
-
}
|
|
1
|
+
// Node ESM resolve hook so the REAL Controller can load under `node --test`.
|
|
2
|
+
//
|
|
3
|
+
// Controller.js does `import components from '/Components/components.js'` — a
|
|
4
|
+
// browser-absolute path that the dev server serves but node can't resolve. We
|
|
5
|
+
// map only that exact specifier to a small fixture components map (just data,
|
|
6
|
+
// the same shape the real components.js exports). Everything else resolves
|
|
7
|
+
// normally, so the tests exercise the real Slice + Controller code, not mocks.
|
|
8
|
+
// AppProbe lives in a CUSTOM category ('AppServices') whose type is 'Service' —
|
|
9
|
+
// it must be allowed as a singleton (the type, not the category name, decides).
|
|
10
|
+
const COMPONENTS = { Probe: 'Service', ModalProbe: 'Visual', AppProbe: 'AppServices' };
|
|
11
|
+
const STUB = `export default ${JSON.stringify(COMPONENTS)};`;
|
|
12
|
+
|
|
13
|
+
export async function resolve(specifier, context, nextResolve) {
|
|
14
|
+
if (specifier === '/Components/components.js') {
|
|
15
|
+
return {
|
|
16
|
+
url: `data:text/javascript,${encodeURIComponent(STUB)}`,
|
|
17
|
+
shortCircuit: true
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return nextResolve(specifier, context);
|
|
21
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Real-Controller tests for the dev-mode leak detector (findOrphans + criteria).
|
|
2
|
+
// Loads the REAL Controller via the resolve hook (same pattern as
|
|
3
|
+
// destroy-cascade.test.js) and drives it with a minimal fake DOM. Fake nodes are
|
|
4
|
+
// made `instanceof HTMLElement` because findOrphans filters on that.
|
|
5
|
+
import { register } from 'node:module';
|
|
6
|
+
import test from 'node:test';
|
|
7
|
+
import assert from 'node:assert/strict';
|
|
8
|
+
|
|
9
|
+
globalThis.alert = () => {};
|
|
10
|
+
register(new URL('./fixtures/real-runtime-loader.mjs', import.meta.url));
|
|
11
|
+
|
|
12
|
+
globalThis.HTMLElement = class HTMLElement {};
|
|
13
|
+
globalThis.slice = {
|
|
14
|
+
logger: { logError() {}, logWarning() {}, logInfo() {} },
|
|
15
|
+
events: null,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const { default: Controller } = await import('../Components/Structural/Controller/Controller.js');
|
|
19
|
+
|
|
20
|
+
function mkEl(sliceId, children = []) {
|
|
21
|
+
return Object.assign(Object.create(HTMLElement.prototype), {
|
|
22
|
+
sliceId,
|
|
23
|
+
tagName: 'SLICE-X',
|
|
24
|
+
isConnected: true,
|
|
25
|
+
parentComponent: null,
|
|
26
|
+
_children: children,
|
|
27
|
+
querySelectorAll() {
|
|
28
|
+
const out = [];
|
|
29
|
+
const walk = (n) => { for (const c of n._children) { out.push(c); walk(c); } };
|
|
30
|
+
walk(this);
|
|
31
|
+
return out;
|
|
32
|
+
},
|
|
33
|
+
setAttribute() {},
|
|
34
|
+
remove() { this.isConnected = false; },
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function flatten(root) {
|
|
39
|
+
const out = [root];
|
|
40
|
+
for (const c of root._children) out.push(...flatten(c));
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function buildTree(controller, root) {
|
|
45
|
+
for (const node of flatten(root)) controller.registerComponent(node);
|
|
46
|
+
controller.registerComponentsRecursively(root);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test('a built-then-detached component (no destroy) IS flagged', () => {
|
|
50
|
+
const c = new Controller();
|
|
51
|
+
const comp = mkEl('leaky');
|
|
52
|
+
buildTree(c, comp);
|
|
53
|
+
comp.isConnected = false; // simulate innerHTML='' / remove() without destroyComponent
|
|
54
|
+
|
|
55
|
+
const orphans = c.findOrphans();
|
|
56
|
+
assert.equal(orphans.length, 1, 'one orphan');
|
|
57
|
+
assert.equal(orphans[0].sliceId, 'leaky');
|
|
58
|
+
assert.ok(orphans[0].retainPath.includes('Object(leaky)') || orphans[0].retainPath.length >= 1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('a connected component is NOT flagged', () => {
|
|
62
|
+
const c = new Controller();
|
|
63
|
+
const comp = mkEl('alive');
|
|
64
|
+
buildTree(c, comp);
|
|
65
|
+
assert.equal(c.findOrphans().length, 0);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('__sliceCached components are excluded (Route/MultiRoute cache)', () => {
|
|
69
|
+
const c = new Controller();
|
|
70
|
+
const comp = mkEl('cachedView');
|
|
71
|
+
buildTree(c, comp);
|
|
72
|
+
comp.isConnected = false;
|
|
73
|
+
comp.__sliceCached = true;
|
|
74
|
+
assert.equal(c.findOrphans().length, 0);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('router-managed route-* sliceIds are excluded', () => {
|
|
78
|
+
const c = new Controller();
|
|
79
|
+
const comp = mkEl('route-Home');
|
|
80
|
+
buildTree(c, comp);
|
|
81
|
+
comp.isConnected = false;
|
|
82
|
+
assert.equal(c.findOrphans().length, 0);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('ancestor de-dup: only the detached root is reported', () => {
|
|
86
|
+
const c = new Controller();
|
|
87
|
+
const child = mkEl('child');
|
|
88
|
+
const parent = mkEl('parent', [child]);
|
|
89
|
+
buildTree(c, parent);
|
|
90
|
+
parent.isConnected = false;
|
|
91
|
+
child.isConnected = false;
|
|
92
|
+
|
|
93
|
+
const orphans = c.findOrphans();
|
|
94
|
+
assert.equal(orphans.length, 1, 'only the root, not the descendant');
|
|
95
|
+
assert.equal(orphans[0].sliceId, 'parent');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('descendants of a cached subtree are NOT flagged', () => {
|
|
99
|
+
const c = new Controller();
|
|
100
|
+
const child = mkEl('child');
|
|
101
|
+
const parent = mkEl('parent', [child]);
|
|
102
|
+
buildTree(c, parent);
|
|
103
|
+
parent.__sliceCached = true; // cached route root
|
|
104
|
+
parent.isConnected = false;
|
|
105
|
+
child.isConnected = false;
|
|
106
|
+
assert.equal(c.findOrphans().length, 0);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('app-registered exclusion predicate is honored', () => {
|
|
110
|
+
const c = new Controller();
|
|
111
|
+
const comp = mkEl('special');
|
|
112
|
+
buildTree(c, comp);
|
|
113
|
+
comp.isConnected = false;
|
|
114
|
+
c.registerLeakExclusion((node) => node.sliceId === 'special');
|
|
115
|
+
assert.equal(c.findOrphans().length, 0);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('growth heuristic trips on monotonic increase and resets on shrink', () => {
|
|
119
|
+
const c = new Controller();
|
|
120
|
+
assert.equal(c.isGrowthMonotonic(), false, 'needs >= 5 samples');
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i < 5; i++) {
|
|
123
|
+
c.registerComponent(mkEl(`g${i}`));
|
|
124
|
+
c.sampleComponentGrowth();
|
|
125
|
+
}
|
|
126
|
+
assert.equal(c.isGrowthMonotonic(), true, 'sizes 1..5 are monotonic growth');
|
|
127
|
+
|
|
128
|
+
c.destroyComponent('g4');
|
|
129
|
+
c.sampleComponentGrowth(); // size dips
|
|
130
|
+
assert.equal(c.isGrowthMonotonic(), false, 'a shrink breaks the monotonic signal');
|
|
131
|
+
});
|