📄 src/models/test/reactivity.test.ts
D-OPEN SOVEREIGN
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 *
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 *
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15import { describe, it, expect, beforeEach } from 'vitest';
16import {
17 addReactivitySystem, setReactivitySystem, dbReactivity,
18 type ReactivitySystem,
19} from '../db.ts';
20
21const spySystem = () => {
22 const tracked: any[] = [];
23 const triggered: any[] = [];
24 const sys: ReactivitySystem = {
25 track: o => { tracked.push(o); },
26 trigger: o => { triggered.push(o); },
27 };
28 return { sys, tracked, triggered };
29};
30
31describe('reactivity composition (multi-framework on one page)', () => {
32 beforeEach(() => setReactivitySystem(null)); // reset registry
33
34 it('fans track/trigger out to every registered system', () => {
35 const a = spySystem();
36 const b = spySystem();
37 addReactivitySystem(a.sys);
38 addReactivitySystem(b.sys);
39
40 const rec = { id: 1 };
41 dbReactivity.track(rec);
42 dbReactivity.trigger(rec);
43
44 // Both scopes (e.g. Solid island + vanilla Astro island) react to the same record.
45 expect(a.tracked).toEqual([rec]);
46 expect(b.tracked).toEqual([rec]);
47 expect(a.triggered).toEqual([rec]);
48 expect(b.triggered).toEqual([rec]);
49 });
50
51 it('addReactivitySystem is idempotent for the same system object', () => {
52 const a = spySystem();
53 addReactivitySystem(a.sys);
54 addReactivitySystem(a.sys); // re-install (e.g. per astro:after-swap) — no duplicate
55
56 dbReactivity.trigger({ id: 2 });
57 expect(a.triggered).toHaveLength(1);
58 });
59
60 it('unregister removes only that system, leaving others live', () => {
61 const a = spySystem();
62 const b = spySystem();
63 const offA = addReactivitySystem(a.sys);
64 addReactivitySystem(b.sys);
65
66 offA();
67 dbReactivity.trigger({ id: 3 });
68
69 expect(a.triggered).toHaveLength(0);
70 expect(b.triggered).toHaveLength(1);
71 });
72
73 it('setReactivitySystem replaces all registered systems', () => {
74 const a = spySystem();
75 const b = spySystem();
76 addReactivitySystem(a.sys);
77 setReactivitySystem(b.sys); // single-framework reset
78
79 dbReactivity.trigger({ id: 4 });
80 expect(a.triggered).toHaveLength(0);
81 expect(b.triggered).toHaveLength(1);
82 });
83});
84