@ibgib/core-gib 0.1.13 → 0.1.14

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.
Files changed (100) hide show
  1. package/dist/keystone/keystone-helpers.mjs +3 -3
  2. package/dist/keystone/keystone-helpers.mjs.map +1 -1
  3. package/dist/sync/sync-constants.d.mts +4 -1
  4. package/dist/sync/sync-constants.d.mts.map +1 -1
  5. package/dist/sync/sync-constants.mjs +3 -0
  6. package/dist/sync/sync-constants.mjs.map +1 -1
  7. package/dist/sync/sync-helpers.d.mts +18 -2
  8. package/dist/sync/sync-helpers.d.mts.map +1 -1
  9. package/dist/sync/sync-helpers.mjs +84 -3
  10. package/dist/sync/sync-helpers.mjs.map +1 -1
  11. package/dist/sync/sync-innerspace.respec.d.mts +0 -6
  12. package/dist/sync/sync-innerspace.respec.d.mts.map +1 -1
  13. package/dist/sync/sync-innerspace.respec.mjs +395 -404
  14. package/dist/sync/sync-innerspace.respec.mjs.map +1 -1
  15. package/dist/sync/sync-peer/sync-peer-types.d.mts +31 -0
  16. package/dist/sync/sync-peer/sync-peer-types.d.mts.map +1 -0
  17. package/dist/sync/sync-peer/sync-peer-types.mjs +5 -0
  18. package/dist/sync/sync-peer/sync-peer-types.mjs.map +1 -0
  19. package/dist/sync/sync-peer/sync-peer-v1.d.mts +22 -0
  20. package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -0
  21. package/dist/sync/sync-peer/sync-peer-v1.mjs +13 -0
  22. package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -0
  23. package/dist/sync/sync-saga-context/sync-saga-context-constants.d.mts +8 -0
  24. package/dist/sync/sync-saga-context/sync-saga-context-constants.d.mts.map +1 -0
  25. package/dist/sync/sync-saga-context/sync-saga-context-constants.mjs +8 -0
  26. package/dist/sync/sync-saga-context/sync-saga-context-constants.mjs.map +1 -0
  27. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +54 -0
  28. package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -0
  29. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +87 -0
  30. package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -0
  31. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +66 -0
  32. package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -0
  33. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs +12 -0
  34. package/dist/sync/sync-saga-context/sync-saga-context-types.mjs.map +1 -0
  35. package/dist/sync/sync-saga-coordinator.d.mts +115 -125
  36. package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
  37. package/dist/sync/sync-saga-coordinator.mjs +539 -456
  38. package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
  39. package/dist/sync/sync-saga-coordinator.respec.mjs +7 -7
  40. package/dist/sync/sync-saga-coordinator.respec.mjs.map +1 -1
  41. package/dist/sync/sync-saga-message/sync-saga-message-constants.d.mts +2 -0
  42. package/dist/sync/sync-saga-message/sync-saga-message-constants.d.mts.map +1 -0
  43. package/dist/sync/sync-saga-message/sync-saga-message-constants.mjs +2 -0
  44. package/dist/sync/sync-saga-message/sync-saga-message-constants.mjs.map +1 -0
  45. package/dist/sync/sync-saga-message/sync-saga-message-helpers.d.mts +15 -0
  46. package/dist/sync/sync-saga-message/sync-saga-message-helpers.d.mts.map +1 -0
  47. package/dist/sync/sync-saga-message/sync-saga-message-helpers.mjs +43 -0
  48. package/dist/sync/sync-saga-message/sync-saga-message-helpers.mjs.map +1 -0
  49. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts +39 -0
  50. package/dist/sync/sync-saga-message/sync-saga-message-types.d.mts.map +1 -0
  51. package/dist/sync/sync-saga-message/sync-saga-message-types.mjs +2 -0
  52. package/dist/sync/sync-saga-message/sync-saga-message-types.mjs.map +1 -0
  53. package/dist/sync/sync-types.d.mts +81 -3
  54. package/dist/sync/sync-types.d.mts.map +1 -1
  55. package/dist/sync/sync-types.mjs +27 -1
  56. package/dist/sync/sync-types.mjs.map +1 -1
  57. package/dist/timeline/timeline-api.d.mts +16 -3
  58. package/dist/timeline/timeline-api.d.mts.map +1 -1
  59. package/dist/timeline/timeline-api.mjs +7 -7
  60. package/dist/timeline/timeline-api.mjs.map +1 -1
  61. package/dist/witness/space/outer-space/outer-space-types.d.mts +2 -0
  62. package/dist/witness/space/outer-space/outer-space-types.d.mts.map +1 -1
  63. package/dist/witness/space/space-base-v1.d.mts +19 -1
  64. package/dist/witness/space/space-base-v1.d.mts.map +1 -1
  65. package/dist/witness/space/space-base-v1.mjs +66 -6
  66. package/dist/witness/space/space-base-v1.mjs.map +1 -1
  67. package/dist/witness/space/space-helper.d.mts +14 -0
  68. package/dist/witness/space/space-helper.d.mts.map +1 -1
  69. package/dist/witness/space/space-helper.mjs +44 -1
  70. package/dist/witness/space/space-helper.mjs.map +1 -1
  71. package/dist/witness/space/space-respec-helper.d.mts.map +1 -1
  72. package/dist/witness/space/space-respec-helper.mjs +1 -1
  73. package/dist/witness/space/space-respec-helper.mjs.map +1 -1
  74. package/dist/witness/space/space-types.d.mts +12 -1
  75. package/dist/witness/space/space-types.d.mts.map +1 -1
  76. package/dist/witness/space/space-types.mjs +4 -0
  77. package/dist/witness/space/space-types.mjs.map +1 -1
  78. package/package.json +2 -2
  79. package/src/keystone/keystone-helpers.mts +3 -3
  80. package/src/sync/README.md +275 -0
  81. package/src/sync/sync-constants.mts +5 -0
  82. package/src/sync/sync-helpers.mts +105 -6
  83. package/src/sync/sync-innerspace.respec.mts +458 -457
  84. package/src/sync/sync-peer/sync-peer-types.mts +43 -0
  85. package/src/sync/sync-peer/sync-peer-v1.mts +28 -0
  86. package/src/sync/sync-saga-context/sync-saga-context-constants.mts +8 -0
  87. package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +147 -0
  88. package/src/sync/sync-saga-context/sync-saga-context-types.mts +80 -0
  89. package/src/sync/sync-saga-coordinator.mts +709 -526
  90. package/src/sync/sync-saga-coordinator.respec.mts +7 -7
  91. package/src/sync/sync-saga-message/sync-saga-message-constants.mts +1 -0
  92. package/src/sync/sync-saga-message/sync-saga-message-helpers.mts +59 -0
  93. package/src/sync/sync-saga-message/sync-saga-message-types.mts +53 -0
  94. package/src/sync/sync-types.mts +103 -3
  95. package/src/timeline/timeline-api.mts +20 -4
  96. package/src/witness/space/space-base-v1.mts +62 -12
  97. package/src/witness/space/space-helper.mts +50 -1
  98. package/src/witness/space/space-respec-helper.mts +2 -1
  99. package/src/witness/space/space-types.mts +13 -1
  100. package/tmp.md +3 -9
@@ -1,457 +1,458 @@
1
- /**
2
- * @module sync-innerspace.respec
3
- *
4
- * Verifies SyncSagaCoordinator using InnerSpace (in-memory/local) spaces.
5
- * This avoids disk I/O issues and focuses on the synchronization logic.
6
- */
7
-
8
- import {
9
- respecfully, lastOfAll, ifWe, iReckon,
10
- ifWeMight
11
- } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
12
- const maam = `[${import.meta.url}]`, sir = maam;
13
- import { pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
14
- import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
15
- import { IbGibData_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
16
-
17
- import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
18
- import { putInSpace, getFromSpace } from '../witness/space/space-helper.mjs';
19
- import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs';
20
- import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs';
21
- import { createStoneHelper, createTimelineRootHelper, getTestKeystoneServiceHelper } from '../agent-helpers.mjs';
22
- import { mut8Timeline, appendToTimeline, getLatestTimelineIbGibDto_nonLocking } from '../timeline/timeline-api.mjs';
23
- import { getDependencyGraph } from '../common/other/graph-helper.mjs';
24
- import { getTjpAddr } from '../common/other/ibgib-helper.mjs';
25
- import { FlatIbGibGraph } from '../common/other/graph-types.mjs';
26
-
27
- const logalot = true;
28
- const lc = `[sync-innerspace.respec]`;
29
-
30
- /**
31
- * naive helper function to determine if two dependency graphs are equal.
32
- *
33
- * naive: only checks length of keys and if keys all match.
34
- *
35
- * @param a dependency graph from one space
36
- * @param b dep. graph from another space
37
- * @returns true if graphs are equal in size and keys, else false.
38
- */
39
- function naiveGraphsAreEqual(a: FlatIbGibGraph, b: FlatIbGibGraph): boolean {
40
- const keysA = Object.keys(a);
41
- const keysB = Object.keys(b);
42
- if (keysA.length !== keysB.length) { false; }
43
- return keysA.every(key => !!b[key]);
44
- }
45
-
46
- await respecfully(sir, `Sync InnerSpaces`, async () => {
47
-
48
- let metaspace: Metaspace_Innerspace;
49
- let sourceSpace: InnerSpace_V1;
50
- let destSpace: InnerSpace_V1;
51
-
52
- // Setup before each test? Or once?
53
- // For now, let's just do it inside the test block to be safe/simple.
54
-
55
- // await ifWeMight(sir, `Basic Push Sync (Source -> Dest)`, async () => {
56
- // // 1. Setup Spaces
57
- // metaspace = new Metaspace_Innerspace(undefined);
58
- // await metaspace.initialize({
59
- // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
60
- // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
61
- // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
62
- // });
63
-
64
- // // Create two distinct inner spaces manually or via metaspace if supported
65
- // // Metaspace_Innerspace typically manages one local user space, but we can instantiate InnerSpace_V1 directly.
66
- // sourceSpace = new InnerSpace_V1({ name: 'source', uuid: 'source_uuid' } as any);
67
- // await (sourceSpace as any).initialize();
68
-
69
- // destSpace = new InnerSpace_V1({ name: 'dest', uuid: 'dest_uuid' } as any);
70
- // await (destSpace as any).initialize();
71
-
72
- // // 2. Seed Source Data
73
-
74
- // // 2.1 Create a "Stone"
75
- // console.log(`${lc} Creating Stone...`);
76
- // const stone = await createStoneHelper({
77
- // ib: 'stone_data',
78
- // data: { some: 'data' },
79
- // });
80
- // const stoneAddr = getIbGibAddr({ ibGib: stone });
81
- // await putInSpace({ space: sourceSpace, ibGibs: [stone] });
82
-
83
- // // 2.2 Create a "Living" Timeline (Root)
84
- // console.log(`${lc} Creating Timeline Root...`);
85
- // const root = await createTimelineRootHelper({
86
- // ib: 'timeline_root',
87
- // data: { type: 'root', n: 0 },
88
- // space: sourceSpace,
89
- // });
90
- // const rootAddr = getIbGibAddr({ ibGib: root });
91
-
92
- // // 2.3 Evolve Timeline (Root -> Child)
93
- // // using mut8Timeline to simulate evolution
94
- // console.log(`${lc} Evolving Timeline...`);
95
- // const child = await mut8Timeline({
96
- // timeline: root,
97
- // mut8Opts: {
98
- // dataToAddOrPatch: { type: 'child', n: 1 }
99
- // },
100
- // metaspace,
101
- // space: sourceSpace,
102
- // // We use skipLock=true for simplicity in single-threaded test setup if locking is complex,
103
- // // but timeline-api handles locking. Let's try default first.
104
- // // Actually InnerSpace locking might be no-op or simple.
105
- // });
106
- // const childAddr = getIbGibAddr({ ibGib: child });
107
-
108
- // // 2.4 Rel8 Child to Stone (Dependency)
109
- // console.log(`${lc} Linking Child to Stone...`);
110
- // const childWithRel = await appendToTimeline({
111
- // timeline: child,
112
- // rel8nInfos: [{
113
- // rel8nName: 'linked_stone',
114
- // ibGibs: [stone]
115
- // }],
116
- // metaspace,
117
- // space: sourceSpace,
118
- // });
119
- // const childWithRelAddr = getIbGibAddr({ ibGib: childWithRel });
120
-
121
- // // 3. Setup Sync Coordinator
122
- // console.log(`${lc} Setting up Coordinator...`);
123
- // const mockKeystone = await getTestKeystoneServiceHelper();
124
- // const identity = await (mockKeystone as any).getIdentity();
125
- // const coordinator = new SyncSagaCoordinator(mockKeystone);
126
-
127
- // // 4. Run Sync
128
- // console.log(`${lc} Running Sync...`);
129
- // const syncRes = await coordinator.sync({
130
- // source: sourceSpace,
131
- // dest: destSpace,
132
- // domainIbGibs: [childWithRel], // Sync starting from the tip
133
- // identity
134
- // });
135
-
136
- // // 5. Verify Dest (Full Dependency Graph)
137
- // console.log(`${lc} Verifying Destination (Full Graph)...`);
138
-
139
- // // We expect the graph to contain:
140
- // // 1. childWithRel
141
- // // 2. child (past of childWithRel)
142
- // // 3. root (ancestor of child, past of child)
143
- // // 4. stone (linked to childWithRel)
144
- // // 5. primitives (timeline_root, child, stone_data, identity, fork, mut8, rel8, root^gib, etc.)
145
-
146
- // // Let's get the graph from the source first to know what to expect (ground truth)
147
- // const sourceGraph = await getDependencyGraph({
148
- // ibGibs: [childWithRel],
149
- // space: sourceSpace,
150
- // });
151
- // const sourceAddrs = Object.keys(sourceGraph);
152
- // console.log(`${lc} Source Graph Size: ${sourceAddrs.length}`);
153
-
154
- // // Now get the graph from the destination
155
- // const destGraph = await getDependencyGraph({
156
- // ibGibs: [childWithRel],
157
- // space: destSpace,
158
- // });
159
- // const destAddrs = Object.keys(destGraph);
160
- // console.log(`${lc} Dest Graph Size: ${destAddrs.length}`);
161
-
162
- // // Assert
163
- // iReckon(sir, destAddrs.length).asTo('dest graph size').isGonnaBe(sourceAddrs.length);
164
-
165
- // for (const addr of sourceAddrs) {
166
- // const inDest = !!destGraph[addr];
167
- // if (!inDest) {
168
- // console.error(`${lc} Missing in dest: ${addr}`);
169
- // }
170
- // iReckon(sir, inDest).asTo(`addr present in dest: ${addr}`).isGonnaBeTrue();
171
- // }
172
-
173
- // console.log(`${lc} Verified Full Dependency Graph synced.`);
174
- // });
175
-
176
- // await ifWeMight(sir, `Idempotency (No-op if already synced)`, async () => {
177
- // // 1. Setup Spaces
178
- // const metaspace = new Metaspace_Innerspace(undefined);
179
- // await metaspace.initialize({
180
- // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
181
- // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
182
- // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
183
- // });
184
-
185
- // const sourceSpace = new InnerSpace_V1({ name: 'source_idem', uuid: 'source_idem_uuid' } as any);
186
- // await (sourceSpace as any).initialize();
187
-
188
- // const destSpace = new InnerSpace_V1({ name: 'dest_idem', uuid: 'dest_idem_uuid' } as any);
189
- // await (destSpace as any).initialize();
190
-
191
- // // 2. Seed Source and Sync ONCE
192
- // const root = await createTimelineRootHelper({
193
- // ib: 'timeline_root',
194
- // data: { type: 'root', n: 0 },
195
- // space: sourceSpace,
196
- // });
197
-
198
- // const mockKeystone = await getTestKeystoneServiceHelper();
199
- // const identity = await (mockKeystone as any).getIdentity();
200
- // const coordinator = new SyncSagaCoordinator(mockKeystone);
201
-
202
- // console.log(`${lc} Running First Sync...`);
203
- // await coordinator.sync({
204
- // source: sourceSpace,
205
- // dest: destSpace,
206
- // domainIbGibs: [root],
207
- // identity
208
- // });
209
-
210
- // // Verify Initial State
211
- // const rootAddr = getIbGibAddr({ ibGib: root });
212
- // const getRoot1 = await getFromSpace({ space: destSpace, addr: rootAddr });
213
- // iReckon(sir, getRoot1.success).asTo('First Sync success').isGonnaBeTrue();
214
-
215
- // // 3. Run Sync AGAIN (Should be No-op)
216
- // console.log(`${lc} Running Second Sync...`);
217
- // await coordinator.sync({
218
- // source: sourceSpace,
219
- // dest: destSpace,
220
- // domainIbGibs: [root],
221
- // identity
222
- // });
223
-
224
- // // 4. Verify State Unchanged / Still Valid
225
- // const getRoot2 = await getFromSpace({ space: destSpace, addr: rootAddr });
226
- // iReckon(sir, getRoot2.success).asTo('Second Sync success').isGonnaBeTrue();
227
-
228
- // // Ensure no duplication or errors in destination (checking count in dependency graph might be good proxy?)
229
- // const destGraph = await getDependencyGraph({
230
- // ibGibs: [root],
231
- // space: destSpace,
232
- // });
233
- // iReckon(sir, Object.keys(destGraph).length).asTo('Dest Graph Size unchanged').isGonnaBe(Object.keys(destGraph).length); // Trivial assertion, mostly checking for no throw
234
-
235
- // console.log(`${lc} Verified Idempotency.`);
236
- // });
237
-
238
- await ifWeMight(sir, `Dest Ahead (Pull/Fast-Backward)`, async () => {
239
- // 1. Setup Spaces
240
- // Metaspace_Innerspace automatically initializes spaces so we don't need manual initialize calls for these if they were standard.
241
- // But here we are instantiating them directly.
242
- // User pointed out initialize is called in constructor.
243
- const metaspace = new Metaspace_Innerspace(undefined);
244
- await metaspace.initialize({
245
- getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
246
- getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
247
- getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
248
- });
249
-
250
- // const sourceSpace = new InnerSpace_V1({ name: 'source_pull', uuid: 'source_pull_uuid' } as any);
251
- const sourceSpace = new InnerSpace_V1({ name: 'source_pull', } as any);
252
- // await (sourceSpace as any).initialize(); // Already initialized in ctor
253
-
254
- // const destSpace = new InnerSpace_V1({ name: 'dest_pull', uuid: 'dest_pull_uuid' } as any);
255
- const destSpace = new InnerSpace_V1({ name: 'dest_pull', } as any);
256
- // await (destSpace as any).initialize(); // Already initialized in ctor
257
-
258
- // 2. Setup Initial Shared State
259
- // Create root in Source, Sync to Dest
260
- console.log(`${lc} Seeding initial shared state...`);
261
- const root = await createTimelineRootHelper({
262
- ib: 'timeline_pull',
263
- data: { type: 'root', state: 'initial' },
264
- space: sourceSpace,
265
- });
266
- const rootAddr = getIbGibAddr({ ibGib: root });
267
- if (logalot) { console.log(`${lc} rootAddr: ${rootAddr} (I: 486bcba15f0a3307f8e6aa88089d1825)`); }
268
- const tjpAddr = getTjpAddr({ ibGib: root, defaultIfNone: 'incomingAddr' }) || rootAddr; // Root is typically its own TJP if not explicit
269
- if (logalot) { console.log(`${lc} tjpAddr: ${tjpAddr} (I: b9326dfb66b8efe8a829673335869c25)`); }
270
- if (logalot) { console.log(`${lc} root:\n${pretty(root)} (I: 3051584310780a36c8435d7881d2f825)`); }
271
-
272
- const mockKeystone = await getTestKeystoneServiceHelper();
273
- const identity = await (mockKeystone as any).getIdentity();
274
- const coordinator = new SyncSagaCoordinator(mockKeystone);
275
-
276
- await coordinator.sync({
277
- source: sourceSpace,
278
- dest: destSpace,
279
- domainIbGibs: [root],
280
- identity
281
- });
282
-
283
- let rootGraph_source: FlatIbGibGraph = await getDependencyGraph({ ibGib: root, space: sourceSpace });
284
- let rootGraph_dest: FlatIbGibGraph = await getDependencyGraph({ ibGib: root, space: destSpace });
285
- let rootsSynced = naiveGraphsAreEqual(rootGraph_source, rootGraph_dest);
286
- if (rootsSynced) {
287
- if (logalot) { console.log(`${lc} rootsSynced (I: 493982dbc9f40170fd26a0d8e0b69825)`); }
288
- } else {
289
- throw new Error(`(UNEXPECTED) graphs are not equal after simple root push sync? (E: f10be8a3d285f260989419c8b99bc225)`);
290
- }
291
-
292
- // 3. Evolve DESTINATION (Simulate external update)
293
- // We act "as if" we are on Dest side evolving it. This is like we are
294
- // syncing with the remote, and there were changes created by someone
295
- // else but we have no local changes.
296
- console.log(`${lc} Evolving Destination (making Source behind)...`);
297
- const childDest = await mut8Timeline({
298
- timeline: root, // works because mut8Timeline just needs the ib/data/rel8ns/n, doesn't need to be "in" the space object-wise, but we persist to destSpace
299
- mut8Opts: { dataToAddOrPatch: { type: 'modified in dest', state: 'ahead' } },
300
- metaspace,
301
- space: destSpace,
302
- });
303
- const childDestAddr = getIbGibAddr({ ibGib: childDest });
304
- if (logalot) { console.log(`${lc} childDest:\n${pretty(childDest)}(I: ff4868842d8757df98cfe59ec5bea825)`); }
305
-
306
- // Log debugging info for TJP
307
- const childDestTjpAddr = getTjpAddr({ ibGib: childDest });
308
- console.log(`${lc} childDestAddr: ${childDestAddr}`);
309
- console.log(`${lc} rootAddr (Expected TJP?): ${rootAddr}`);
310
- console.log(`${lc} childDest TJP Addr: ${childDestTjpAddr}`);
311
-
312
- // If TJP is missing or mismatched, that's why getLatestAddrs fails
313
- if (!childDestTjpAddr || childDestTjpAddr !== tjpAddr) {
314
- // NOTE: THIS DOES NOT HIT
315
- console.warn(`${lc} WARNING: childDest TJP (${childDestTjpAddr}) does not match root/expected TJP (${tjpAddr}). This likely causes sync to miss it.`);
316
- // Hack fix for test verification if mut8Timeline is slightly broken on TJP propagation for raw tests
317
- if (!childDest.rel8ns) { childDest.rel8ns = {}; }
318
- childDest.rel8ns.tjp = [tjpAddr];
319
- // Re-put to update index?? No, InnerSpace uses in-memory map references often if we modified object.
320
- // Better to re-put explicitly if we modified it.
321
- await putInSpace({ space: destSpace, ibGibs: [childDest] });
322
- }
323
-
324
- console.log(`${lc} sourceSpace UUID: ${(sourceSpace as any).data.uuid}`);
325
- console.log(`${lc} destSpace UUID: ${(destSpace as any).data.uuid}`);
326
-
327
- // Verify Source DOES NOT have childDest
328
- const getChildInSource = await getFromSpace({ space: sourceSpace, addr: childDestAddr });
329
- iReckon(sir, getChildInSource.success).asTo('Source is behind').isGonnaBeFalse(); // Or success=true/addrsNotFound depending on impl
330
-
331
- // 4. Run Sync from Source
332
- // Checks:
333
- // - Source should detect it is behind (or "conflict" in current generic terms).
334
- // - Source should Pull 'childDest' from Dest.
335
- // - Source should update itself.
336
- console.log(`${lc} Running Sync (Pull)...`);
337
- await coordinator.sync({
338
- source: sourceSpace,
339
- dest: destSpace,
340
- domainIbGibs: [root], // We start with what Source knows (root)
341
- identity
342
- });
343
-
344
- // 5. Verify Source Caught Up
345
- const getChildInSourcePost = await getFromSpace({ space: sourceSpace, addr: childDestAddr });
346
- console.log(`${lc} getChildInSourcePost success: ${getChildInSourcePost.success}`);
347
- if (getChildInSourcePost.ibGibs) {
348
- console.log(`${lc} getChildInSourcePost found: ${getChildInSourcePost.ibGibs.length} ibGibs`);
349
- } else {
350
- console.log(`${lc} getChildInSourcePost found: 0 ibGibs (undefined)`);
351
- }
352
-
353
- iReckon(sir, getChildInSourcePost.success).asTo('Source pulled new frame').isGonnaBeTrue();
354
- iReckon(sir, getChildInSourcePost.ibGibs?.[0]).asTo('Source has childDest').isGonnaBeTruthy();
355
-
356
- console.log(`${lc} Verified Dest Ahead / Pull.`);
357
- });
358
-
359
- // await ifWeMight(sir, `Divergent (Conflict Detection)`, async () => {
360
- // interface TestData extends IbGibData_V1 {
361
- // type: string;
362
- // state?: string;
363
- // branch?: string;
364
- // }
365
-
366
- // // 1. Setup
367
- // const metaspace = new Metaspace_Innerspace(undefined);
368
- // await metaspace.initialize({
369
- // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
370
- // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
371
- // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
372
- // });
373
-
374
- // const sourceSpace = new InnerSpace_V1({ name: 'source_div', uuid: 'source_div_uuid' } as any);
375
- // // await (sourceSpace as any).initialize();
376
- // const destSpace = new InnerSpace_V1({ name: 'dest_div', uuid: 'dest_div_uuid' } as any);
377
- // // await (destSpace as any).initialize();
378
-
379
- // // 2. Setup Shared Root
380
- // const root = await createTimelineRootHelper<TestData>({
381
- // ib: 'timeline_div',
382
- // data: { type: 'root' },
383
- // space: sourceSpace,
384
- // });
385
- // const rootAddr = getIbGibAddr({ ibGib: root });
386
- // const tjpAddr = getTjpAddr({ ibGib: root }) || rootAddr;
387
-
388
- // // Sync root to dest so they share base
389
- // await putInSpace({ space: destSpace, ibGibs: [root] });
390
- // // NOTE: Must manually index TJP in dest if putInSpace doesn't trigger full indexing (InnerSpace might need register)
391
- // // Ideally we run a sync to init, but manual put + hack fix is faster for setup.
392
- // // Actually, let's use coordinator to sync root first to ensure clean state.
393
- // const mockKeystone = await getTestKeystoneServiceHelper();
394
- // const identity = await (mockKeystone as any).getIdentity();
395
- // const coordinator = new SyncSagaCoordinator(mockKeystone);
396
-
397
- // await coordinator.sync({
398
- // source: sourceSpace,
399
- // dest: destSpace,
400
- // domainIbGibs: [root],
401
- // identity
402
- // });
403
-
404
- // // 3. Diverge!
405
- // // Source creates Child A
406
- // const childA = await mut8Timeline({
407
- // timeline: root,
408
- // mut8Opts: { dataToAddOrPatch: { branch: 'A' } },
409
- // metaspace,
410
- // space: sourceSpace,
411
- // });
412
- // const childAAddr = getIbGibAddr({ ibGib: childA });
413
-
414
- // // Dest (simulated) creates Child B from Root (Forking history)
415
- // const childB = await mut8Timeline({
416
- // timeline: root,
417
- // mut8Opts: { dataToAddOrPatch: { branch: 'B' } }, // Logic: same n=1, but different content/hash
418
- // metaspace,
419
- // space: destSpace,
420
- // });
421
- // const childBAddr = getIbGibAddr({ ibGib: childB });
422
-
423
- // // Ensure TJP indexing on Dest (test hack)
424
- // const childBTjp = getTjpAddr({ ibGib: childB });
425
- // if (!childBTjp || childBTjp !== tjpAddr) {
426
- // if (!childB.rel8ns) childB.rel8ns = {};
427
- // childB.rel8ns.tjp = [tjpAddr];
428
- // await putInSpace({ space: destSpace, ibGibs: [childB] });
429
- // }
430
-
431
- // console.log(`${lc} Divergence Setup:`);
432
- // console.log(`${lc} Root: ${rootAddr}`);
433
- // console.log(`${lc} Source Tip (A): ${childAAddr}`);
434
- // console.log(`${lc} Dest Tip (B): ${childBAddr}`);
435
-
436
- // // 4. Sync with Strategy: 'abort' (Expect Error)
437
- // console.log(`${lc} Running Sync (Abort Strategy)...`);
438
- // let caughtError;
439
- // try {
440
- // await coordinator.sync({
441
- // source: sourceSpace,
442
- // dest: destSpace,
443
- // domainIbGibs: [childA],
444
- // identity,
445
- // conflictStrategy: 'abort', // Should throw
446
- // });
447
- // } catch (e) {
448
- // caughtError = e;
449
- // console.log(`${lc} Caught expected error: ${e.message}`);
450
- // }
451
- // iReckon(sir, caughtError).asTo('Should throw on conflict with abort strategy').isGonnaBeTruthy();
452
-
453
- // // 5. Sync with Strategy: 'optimistic' (Expect Merge)
454
- // // TODO: Implement optimistic merging
455
- // });
456
-
457
- });
1
+ // /**
2
+ // * @module sync-innerspace.respec
3
+ // *
4
+ // * Verifies SyncSagaCoordinator using InnerSpace (in-memory/local) spaces.
5
+ // * This avoids disk I/O issues and focuses on the synchronization logic.
6
+ // */
7
+
8
+ // import {
9
+ // respecfully, lastOfAll, ifWe, iReckon,
10
+ // ifWeMight
11
+ // } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
12
+ // const maam = `[${import.meta.url}]`, sir = maam;
13
+ // import { pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
14
+ // import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
15
+ // import { IbGibData_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
16
+
17
+ // import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
18
+ // import { putInSpace, getFromSpace } from '../witness/space/space-helper.mjs';
19
+ // import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs';
20
+ // import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs';
21
+ // import { createStoneHelper, createTimelineRootHelper, getTestKeystoneServiceHelper } from '../agent-helpers.mjs';
22
+ // import { mut8Timeline, appendToTimeline, getLatestTimelineIbGibDto_nonLocking } from '../timeline/timeline-api.mjs';
23
+ // import { getDependencyGraph } from '../common/other/graph-helper.mjs';
24
+ // import { getTjpAddr } from '../common/other/ibgib-helper.mjs';
25
+ // import { FlatIbGibGraph } from '../common/other/graph-types.mjs';
26
+ // import { SyncConflictStrategy } from './sync-types.mjs';
27
+
28
+ // const logalot = true;
29
+ // const lc = `[sync-innerspace.respec]`;
30
+
31
+ // /**
32
+ // * naive helper function to determine if two dependency graphs are equal.
33
+ // *
34
+ // * naive: only checks length of keys and if keys all match.
35
+ // *
36
+ // * @param a dependency graph from one space
37
+ // * @param b dep. graph from another space
38
+ // * @returns true if graphs are equal in size and keys, else false.
39
+ // */
40
+ // function naiveGraphsAreEqual(a: FlatIbGibGraph, b: FlatIbGibGraph): boolean {
41
+ // const keysA = Object.keys(a);
42
+ // const keysB = Object.keys(b);
43
+ // if (keysA.length !== keysB.length) { false; }
44
+ // return keysA.every(key => !!b[key]);
45
+ // }
46
+
47
+ // await respecfully(sir, `Sync InnerSpaces`, async () => {
48
+
49
+ // let metaspace: Metaspace_Innerspace;
50
+ // let sourceSpace: InnerSpace_V1;
51
+ // let destSpace: InnerSpace_V1;
52
+
53
+ // // Setup before each test? Or once?
54
+ // // For now, let's just do it inside the test block to be safe/simple.
55
+
56
+ // await ifWeMight(sir, `Basic Push Sync (Source -> Dest)`, async () => {
57
+ // // 1. Setup Spaces
58
+ // metaspace = new Metaspace_Innerspace(undefined);
59
+ // await metaspace.initialize({
60
+ // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
61
+ // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
62
+ // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
63
+ // });
64
+
65
+ // // Create two distinct inner spaces manually or via metaspace if supported
66
+ // // Metaspace_Innerspace typically manages one local user space, but we can instantiate InnerSpace_V1 directly.
67
+ // sourceSpace = new InnerSpace_V1({ name: 'source', uuid: 'source_uuid' } as any);
68
+ // await (sourceSpace as any).initialize();
69
+
70
+ // destSpace = new InnerSpace_V1({ name: 'dest', uuid: 'dest_uuid' } as any);
71
+ // await (destSpace as any).initialize();
72
+
73
+ // // 2. Seed Source Data
74
+
75
+ // // 2.1 Create a "Stone"
76
+ // console.log(`${lc} Creating Stone...`);
77
+ // const stone = await createStoneHelper({
78
+ // ib: 'stone_data',
79
+ // data: { some: 'data' },
80
+ // });
81
+ // const stoneAddr = getIbGibAddr({ ibGib: stone });
82
+ // await putInSpace({ space: sourceSpace, ibGibs: [stone] });
83
+
84
+ // // 2.2 Create a "Living" Timeline (Root)
85
+ // console.log(`${lc} Creating Timeline Root...`);
86
+ // const root = await createTimelineRootHelper({
87
+ // ib: 'timeline_root',
88
+ // data: { type: 'root', n: 0 },
89
+ // space: sourceSpace,
90
+ // });
91
+ // const rootAddr = getIbGibAddr({ ibGib: root });
92
+
93
+ // // 2.3 Evolve Timeline (Root -> Child)
94
+ // // using mut8Timeline to simulate evolution
95
+ // console.log(`${lc} Evolving Timeline...`);
96
+ // const child = await mut8Timeline({
97
+ // timeline: root,
98
+ // mut8Opts: {
99
+ // dataToAddOrPatch: { type: 'child', n: 1 }
100
+ // },
101
+ // metaspace,
102
+ // space: sourceSpace,
103
+ // // We use skipLock=true for simplicity in single-threaded test setup if locking is complex,
104
+ // // but timeline-api handles locking. Let's try default first.
105
+ // // Actually InnerSpace locking might be no-op or simple.
106
+ // });
107
+ // const childAddr = getIbGibAddr({ ibGib: child });
108
+
109
+ // // 2.4 Rel8 Child to Stone (Dependency)
110
+ // console.log(`${lc} Linking Child to Stone...`);
111
+ // const childWithRel = await appendToTimeline({
112
+ // timeline: child,
113
+ // rel8nInfos: [{
114
+ // rel8nName: 'linked_stone',
115
+ // ibGibs: [stone]
116
+ // }],
117
+ // metaspace,
118
+ // space: sourceSpace,
119
+ // });
120
+ // const childWithRelAddr = getIbGibAddr({ ibGib: childWithRel });
121
+
122
+ // // 3. Setup Sync Coordinator
123
+ // console.log(`${lc} Setting up Coordinator...`);
124
+ // const mockKeystone = await getTestKeystoneServiceHelper();
125
+ // const identity = await (mockKeystone as any).getIdentity();
126
+ // const coordinator = new SyncSagaCoordinator(mockKeystone);
127
+
128
+ // // 4. Run Sync
129
+ // console.log(`${lc} Running Sync...`);
130
+ // const syncRes = await coordinator.sync({
131
+ // source: sourceSpace,
132
+ // dest: destSpace,
133
+ // domainIbGibs: [childWithRel], // Sync starting from the tip
134
+ // identity
135
+ // });
136
+
137
+ // // 5. Verify Dest (Full Dependency Graph)
138
+ // console.log(`${lc} Verifying Destination (Full Graph)...`);
139
+
140
+ // // We expect the graph to contain:
141
+ // // 1. childWithRel
142
+ // // 2. child (past of childWithRel)
143
+ // // 3. root (ancestor of child, past of child)
144
+ // // 4. stone (linked to childWithRel)
145
+ // // 5. primitives (timeline_root, child, stone_data, identity, fork, mut8, rel8, root^gib, etc.)
146
+
147
+ // // Let's get the graph from the source first to know what to expect (ground truth)
148
+ // const sourceGraph = await getDependencyGraph({
149
+ // ibGibs: [childWithRel],
150
+ // space: sourceSpace,
151
+ // });
152
+ // const sourceAddrs = Object.keys(sourceGraph);
153
+ // console.log(`${lc} Source Graph Size: ${sourceAddrs.length}`);
154
+
155
+ // // Now get the graph from the destination
156
+ // const destGraph = await getDependencyGraph({
157
+ // ibGibs: [childWithRel],
158
+ // space: destSpace,
159
+ // });
160
+ // const destAddrs = Object.keys(destGraph);
161
+ // console.log(`${lc} Dest Graph Size: ${destAddrs.length}`);
162
+
163
+ // // Assert
164
+ // iReckon(sir, destAddrs.length).asTo('dest graph size').isGonnaBe(sourceAddrs.length);
165
+
166
+ // for (const addr of sourceAddrs) {
167
+ // const inDest = !!destGraph[addr];
168
+ // if (!inDest) {
169
+ // console.error(`${lc} Missing in dest: ${addr}`);
170
+ // }
171
+ // iReckon(sir, inDest).asTo(`addr present in dest: ${addr}`).isGonnaBeTrue();
172
+ // }
173
+
174
+ // console.log(`${lc} Verified Full Dependency Graph synced.`);
175
+ // });
176
+
177
+ // await ifWeMight(sir, `Idempotency (No-op if already synced)`, async () => {
178
+ // // 1. Setup Spaces
179
+ // const metaspace = new Metaspace_Innerspace(undefined);
180
+ // await metaspace.initialize({
181
+ // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
182
+ // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
183
+ // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
184
+ // });
185
+
186
+ // const sourceSpace = new InnerSpace_V1({ name: 'source_idem', uuid: 'source_idem_uuid' } as any);
187
+ // await (sourceSpace as any).initialize();
188
+
189
+ // const destSpace = new InnerSpace_V1({ name: 'dest_idem', uuid: 'dest_idem_uuid' } as any);
190
+ // await (destSpace as any).initialize();
191
+
192
+ // // 2. Seed Source and Sync ONCE
193
+ // const root = await createTimelineRootHelper({
194
+ // ib: 'timeline_root',
195
+ // data: { type: 'root', n: 0 },
196
+ // space: sourceSpace,
197
+ // });
198
+
199
+ // const mockKeystone = await getTestKeystoneServiceHelper();
200
+ // const identity = await (mockKeystone as any).getIdentity();
201
+ // const coordinator = new SyncSagaCoordinator(mockKeystone);
202
+
203
+ // console.log(`${lc} Running First Sync...`);
204
+ // await coordinator.sync({
205
+ // source: sourceSpace,
206
+ // dest: destSpace,
207
+ // domainIbGibs: [root],
208
+ // identity
209
+ // });
210
+
211
+ // // Verify Initial State
212
+ // const rootAddr = getIbGibAddr({ ibGib: root });
213
+ // const getRoot1 = await getFromSpace({ space: destSpace, addr: rootAddr });
214
+ // iReckon(sir, getRoot1.success).asTo('First Sync success').isGonnaBeTrue();
215
+
216
+ // // 3. Run Sync AGAIN (Should be No-op)
217
+ // console.log(`${lc} Running Second Sync...`);
218
+ // await coordinator.sync({
219
+ // source: sourceSpace,
220
+ // dest: destSpace,
221
+ // domainIbGibs: [root],
222
+ // identity
223
+ // });
224
+
225
+ // // 4. Verify State Unchanged / Still Valid
226
+ // const getRoot2 = await getFromSpace({ space: destSpace, addr: rootAddr });
227
+ // iReckon(sir, getRoot2.success).asTo('Second Sync success').isGonnaBeTrue();
228
+
229
+ // // Ensure no duplication or errors in destination (checking count in dependency graph might be good proxy?)
230
+ // const destGraph = await getDependencyGraph({
231
+ // ibGibs: [root],
232
+ // space: destSpace,
233
+ // });
234
+ // iReckon(sir, Object.keys(destGraph).length).asTo('Dest Graph Size unchanged').isGonnaBe(Object.keys(destGraph).length); // Trivial assertion, mostly checking for no throw
235
+
236
+ // console.log(`${lc} Verified Idempotency.`);
237
+ // });
238
+
239
+ // await ifWeMight(sir, `Dest Ahead (Pull/Fast-Backward)`, async () => {
240
+ // // 1. Setup Spaces
241
+ // // Metaspace_Innerspace automatically initializes spaces so we don't need manual initialize calls for these if they were standard.
242
+ // // But here we are instantiating them directly.
243
+ // // User pointed out initialize is called in constructor.
244
+ // const metaspace = new Metaspace_Innerspace(undefined);
245
+ // await metaspace.initialize({
246
+ // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
247
+ // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
248
+ // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
249
+ // });
250
+
251
+ // // const sourceSpace = new InnerSpace_V1({ name: 'source_pull', uuid: 'source_pull_uuid' } as any);
252
+ // const sourceSpace = new InnerSpace_V1({ name: 'source_pull', } as any);
253
+ // // await (sourceSpace as any).initialize(); // Already initialized in ctor
254
+
255
+ // // const destSpace = new InnerSpace_V1({ name: 'dest_pull', uuid: 'dest_pull_uuid' } as any);
256
+ // const destSpace = new InnerSpace_V1({ name: 'dest_pull', } as any);
257
+ // // await (destSpace as any).initialize(); // Already initialized in ctor
258
+
259
+ // // 2. Setup Initial Shared State
260
+ // // Create root in Source, Sync to Dest
261
+ // console.log(`${lc} Seeding initial shared state...`);
262
+ // const root = await createTimelineRootHelper({
263
+ // ib: 'timeline_pull',
264
+ // data: { type: 'root', state: 'initial' },
265
+ // space: sourceSpace,
266
+ // });
267
+ // const rootAddr = getIbGibAddr({ ibGib: root });
268
+ // if (logalot) { console.log(`${lc} rootAddr: ${rootAddr} (I: 486bcba15f0a3307f8e6aa88089d1825)`); }
269
+ // const tjpAddr = getTjpAddr({ ibGib: root, defaultIfNone: 'incomingAddr' }) || rootAddr; // Root is typically its own TJP if not explicit
270
+ // if (logalot) { console.log(`${lc} tjpAddr: ${tjpAddr} (I: b9326dfb66b8efe8a829673335869c25)`); }
271
+ // if (logalot) { console.log(`${lc} root:\n${pretty(root)} (I: 3051584310780a36c8435d7881d2f825)`); }
272
+
273
+ // const mockKeystone = await getTestKeystoneServiceHelper();
274
+ // const identity = await (mockKeystone as any).getIdentity();
275
+ // const coordinator = new SyncSagaCoordinator(mockKeystone);
276
+
277
+ // await coordinator.sync({
278
+ // source: sourceSpace,
279
+ // dest: destSpace,
280
+ // domainIbGibs: [root],
281
+ // identity
282
+ // });
283
+
284
+ // let rootGraph_source: FlatIbGibGraph = await getDependencyGraph({ ibGib: root, space: sourceSpace });
285
+ // let rootGraph_dest: FlatIbGibGraph = await getDependencyGraph({ ibGib: root, space: destSpace });
286
+ // let rootsSynced = naiveGraphsAreEqual(rootGraph_source, rootGraph_dest);
287
+ // if (rootsSynced) {
288
+ // if (logalot) { console.log(`${lc} rootsSynced (I: 493982dbc9f40170fd26a0d8e0b69825)`); }
289
+ // } else {
290
+ // throw new Error(`(UNEXPECTED) graphs are not equal after simple root push sync? (E: f10be8a3d285f260989419c8b99bc225)`);
291
+ // }
292
+
293
+ // // 3. Evolve DESTINATION (Simulate external update)
294
+ // // We act "as if" we are on Dest side evolving it. This is like we are
295
+ // // syncing with the remote, and there were changes created by someone
296
+ // // else but we have no local changes.
297
+ // console.log(`${lc} Evolving Destination (making Source behind)...`);
298
+ // const childDest = await mut8Timeline({
299
+ // timeline: root, // works because mut8Timeline just needs the ib/data/rel8ns/n, doesn't need to be "in" the space object-wise, but we persist to destSpace
300
+ // mut8Opts: { dataToAddOrPatch: { type: 'modified in dest', state: 'ahead' } },
301
+ // metaspace,
302
+ // space: destSpace,
303
+ // });
304
+ // const childDestAddr = getIbGibAddr({ ibGib: childDest });
305
+ // if (logalot) { console.log(`${lc} childDest:\n${pretty(childDest)}(I: ff4868842d8757df98cfe59ec5bea825)`); }
306
+
307
+ // // Log debugging info for TJP
308
+ // const childDestTjpAddr = getTjpAddr({ ibGib: childDest });
309
+ // console.log(`${lc} childDestAddr: ${childDestAddr}`);
310
+ // console.log(`${lc} rootAddr (Expected TJP?): ${rootAddr}`);
311
+ // console.log(`${lc} childDest TJP Addr: ${childDestTjpAddr}`);
312
+
313
+ // // If TJP is missing or mismatched, that's why getLatestAddrs fails
314
+ // if (!childDestTjpAddr || childDestTjpAddr !== tjpAddr) {
315
+ // // NOTE: THIS DOES NOT HIT
316
+ // console.warn(`${lc} WARNING: childDest TJP (${childDestTjpAddr}) does not match root/expected TJP (${tjpAddr}). This likely causes sync to miss it.`);
317
+ // // Hack fix for test verification if mut8Timeline is slightly broken on TJP propagation for raw tests
318
+ // if (!childDest.rel8ns) { childDest.rel8ns = {}; }
319
+ // childDest.rel8ns.tjp = [tjpAddr];
320
+ // // Re-put to update index?? No, InnerSpace uses in-memory map references often if we modified object.
321
+ // // Better to re-put explicitly if we modified it.
322
+ // await putInSpace({ space: destSpace, ibGibs: [childDest] });
323
+ // }
324
+
325
+ // console.log(`${lc} sourceSpace UUID: ${(sourceSpace as any).data.uuid}`);
326
+ // console.log(`${lc} destSpace UUID: ${(destSpace as any).data.uuid}`);
327
+
328
+ // // Verify Source DOES NOT have childDest
329
+ // const getChildInSource = await getFromSpace({ space: sourceSpace, addr: childDestAddr });
330
+ // iReckon(sir, getChildInSource.success).asTo('Source is behind').isGonnaBeFalse(); // Or success=true/addrsNotFound depending on impl
331
+
332
+ // // 4. Run Sync from Source
333
+ // // Checks:
334
+ // // - Source should detect it is behind (or "conflict" in current generic terms).
335
+ // // - Source should Pull 'childDest' from Dest.
336
+ // // - Source should update itself.
337
+ // console.log(`${lc} Running Sync (Pull)...`);
338
+ // await coordinator.sync({
339
+ // source: sourceSpace,
340
+ // dest: destSpace,
341
+ // domainIbGibs: [root], // We start with what Source knows (root)
342
+ // identity
343
+ // });
344
+
345
+ // // 5. Verify Source Caught Up
346
+ // const getChildInSourcePost = await getFromSpace({ space: sourceSpace, addr: childDestAddr });
347
+ // console.log(`${lc} getChildInSourcePost success: ${getChildInSourcePost.success}`);
348
+ // if (getChildInSourcePost.ibGibs) {
349
+ // console.log(`${lc} getChildInSourcePost found: ${getChildInSourcePost.ibGibs.length} ibGibs`);
350
+ // } else {
351
+ // console.log(`${lc} getChildInSourcePost found: 0 ibGibs (undefined)`);
352
+ // }
353
+
354
+ // iReckon(sir, getChildInSourcePost.success).asTo('Source pulled new frame').isGonnaBeTrue();
355
+ // iReckon(sir, getChildInSourcePost.ibGibs?.[0]).asTo('Source has childDest').isGonnaBeTruthy();
356
+
357
+ // console.log(`${lc} Verified Dest Ahead / Pull.`);
358
+ // });
359
+
360
+ // await ifWeMight(sir, `Divergent (Conflict Detection)`, async () => {
361
+ // interface TestData extends IbGibData_V1 {
362
+ // type: string;
363
+ // state?: string;
364
+ // branch?: string;
365
+ // }
366
+
367
+ // // 1. Setup
368
+ // const metaspace = new Metaspace_Innerspace(undefined);
369
+ // await metaspace.initialize({
370
+ // getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); },
371
+ // getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; },
372
+ // getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; },
373
+ // });
374
+
375
+ // const sourceSpace = new InnerSpace_V1({ name: 'source_div', uuid: 'source_div_uuid' } as any);
376
+ // // await (sourceSpace as any).initialize();
377
+ // const destSpace = new InnerSpace_V1({ name: 'dest_div', uuid: 'dest_div_uuid' } as any);
378
+ // // await (destSpace as any).initialize();
379
+
380
+ // // 2. Setup Shared Root
381
+ // const root = await createTimelineRootHelper<TestData>({
382
+ // ib: 'timeline_div',
383
+ // data: { type: 'root' },
384
+ // space: sourceSpace,
385
+ // });
386
+ // const rootAddr = getIbGibAddr({ ibGib: root });
387
+ // const tjpAddr = getTjpAddr({ ibGib: root }) || rootAddr;
388
+
389
+ // // Sync root to dest so they share base
390
+ // await putInSpace({ space: destSpace, ibGibs: [root] });
391
+ // // NOTE: Must manually index TJP in dest if putInSpace doesn't trigger full indexing (InnerSpace might need register)
392
+ // // Ideally we run a sync to init, but manual put + hack fix is faster for setup.
393
+ // // Actually, let's use coordinator to sync root first to ensure clean state.
394
+ // const mockKeystone = await getTestKeystoneServiceHelper();
395
+ // const identity = await (mockKeystone as any).getIdentity();
396
+ // const coordinator = new SyncSagaCoordinator(mockKeystone);
397
+
398
+ // await coordinator.sync({
399
+ // source: sourceSpace,
400
+ // dest: destSpace,
401
+ // domainIbGibs: [root],
402
+ // identity
403
+ // });
404
+
405
+ // // 3. Diverge!
406
+ // // Source creates Child A
407
+ // const childA = await mut8Timeline({
408
+ // timeline: root,
409
+ // mut8Opts: { dataToAddOrPatch: { branch: 'A' } },
410
+ // metaspace,
411
+ // space: sourceSpace,
412
+ // });
413
+ // const childAAddr = getIbGibAddr({ ibGib: childA });
414
+
415
+ // // Dest (simulated) creates Child B from Root (Forking history)
416
+ // const childB = await mut8Timeline({
417
+ // timeline: root,
418
+ // mut8Opts: { dataToAddOrPatch: { branch: 'B' } }, // Logic: same n=1, but different content/hash
419
+ // metaspace,
420
+ // space: destSpace,
421
+ // });
422
+ // const childBAddr = getIbGibAddr({ ibGib: childB });
423
+
424
+ // // Ensure TJP indexing on Dest (test hack)
425
+ // const childBTjp = getTjpAddr({ ibGib: childB });
426
+ // if (!childBTjp || childBTjp !== tjpAddr) {
427
+ // if (!childB.rel8ns) childB.rel8ns = {};
428
+ // childB.rel8ns.tjp = [tjpAddr];
429
+ // await putInSpace({ space: destSpace, ibGibs: [childB] });
430
+ // }
431
+
432
+ // console.log(`${lc} Divergence Setup:`);
433
+ // console.log(`${lc} Root: ${rootAddr}`);
434
+ // console.log(`${lc} Source Tip (A): ${childAAddr}`);
435
+ // console.log(`${lc} Dest Tip (B): ${childBAddr}`);
436
+
437
+ // // 4. Sync with Strategy: 'abort' (Expect Error)
438
+ // console.log(`${lc} Running Sync (Abort Strategy)...`);
439
+ // let caughtError;
440
+ // try {
441
+ // await coordinator.sync({
442
+ // source: sourceSpace,
443
+ // dest: destSpace,
444
+ // domainIbGibs: [childA],
445
+ // identity,
446
+ // conflictStrategy: SyncConflictStrategy.abort, // Should throw
447
+ // });
448
+ // } catch (e) {
449
+ // caughtError = e;
450
+ // console.log(`${lc} Caught expected error: ${e.message}`);
451
+ // }
452
+ // iReckon(sir, caughtError).asTo('Should throw on conflict with abort strategy').isGonnaBeTruthy();
453
+
454
+ // // 5. Sync with Strategy: 'optimistic' (Expect Merge)
455
+ // // TODO: Implement optimistic merging
456
+ // });
457
+
458
+ // });