@dxos/app-graph 0.8.4-main.67995b8 → 0.8.4-main.6fa680abb7

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 (47) hide show
  1. package/dist/lib/browser/index.mjs +1421 -620
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +1420 -620
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/atoms.d.ts +8 -0
  8. package/dist/types/src/atoms.d.ts.map +1 -0
  9. package/dist/types/src/graph-builder.d.ts +117 -60
  10. package/dist/types/src/graph-builder.d.ts.map +1 -1
  11. package/dist/types/src/graph.d.ts +188 -218
  12. package/dist/types/src/graph.d.ts.map +1 -1
  13. package/dist/types/src/index.d.ts +6 -3
  14. package/dist/types/src/index.d.ts.map +1 -1
  15. package/dist/types/src/node-matcher.d.ts +218 -0
  16. package/dist/types/src/node-matcher.d.ts.map +1 -0
  17. package/dist/types/src/node-matcher.test.d.ts +2 -0
  18. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  19. package/dist/types/src/node.d.ts +44 -5
  20. package/dist/types/src/node.d.ts.map +1 -1
  21. package/dist/types/src/stories/EchoGraph.stories.d.ts +8 -10
  22. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  23. package/dist/types/src/util.d.ts +34 -0
  24. package/dist/types/src/util.d.ts.map +1 -0
  25. package/dist/types/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +38 -37
  27. package/src/atoms.ts +25 -0
  28. package/src/graph-builder.test.ts +942 -126
  29. package/src/graph-builder.ts +725 -264
  30. package/src/graph.test.ts +451 -123
  31. package/src/graph.ts +1059 -407
  32. package/src/index.ts +9 -3
  33. package/src/node-matcher.test.ts +301 -0
  34. package/src/node-matcher.ts +282 -0
  35. package/src/node.ts +56 -8
  36. package/src/stories/EchoGraph.stories.tsx +173 -132
  37. package/src/stories/Tree.tsx +1 -1
  38. package/src/util.ts +71 -0
  39. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  40. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  41. package/dist/types/src/signals-integration.test.d.ts +0 -2
  42. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  43. package/dist/types/src/testing.d.ts +0 -5
  44. package/dist/types/src/testing.d.ts.map +0 -1
  45. package/src/experimental/graph-projections.test.ts +0 -56
  46. package/src/signals-integration.test.ts +0 -218
  47. package/src/testing.ts +0 -20
@@ -2,122 +2,254 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Registry, Rx } from '@effect-rx/rx-react';
6
- import { Option, pipe } from 'effect';
5
+ import { Atom, Registry } from '@effect-atom/atom-react';
6
+ import * as Context from 'effect/Context';
7
+ import * as Effect from 'effect/Effect';
8
+ import * as Function from 'effect/Function';
9
+ import * as Option from 'effect/Option';
7
10
  import { describe, expect, onTestFinished, test } from 'vitest';
8
11
 
9
- import { sleep, Trigger } from '@dxos/async';
12
+ import { Trigger } from '@dxos/async';
13
+ import { Obj } from '@dxos/echo';
14
+ import { TestSchema } from '@dxos/echo/testing';
10
15
 
11
- import { ROOT_ID } from './graph';
12
- import { createExtension, GraphBuilder } from './graph-builder';
13
- import { type Node } from './node';
16
+ import * as Graph from './graph';
17
+ import * as GraphBuilder from './graph-builder';
18
+ import * as Node from './node';
19
+ import * as NodeMatcher from './node-matcher';
14
20
 
15
21
  const exampleId = (id: number) => `dx:test:${id}`;
16
22
  const EXAMPLE_ID = exampleId(1);
17
- const EXAMPLE_TYPE = 'dxos.org/type/example';
23
+ const EXAMPLE_TYPE = 'org.dxos.type.example';
24
+
25
+ const qualifiedId = (...segments: string[]) => segments.join('/');
18
26
 
19
27
  describe('GraphBuilder', () => {
28
+ describe('resolver', () => {
29
+ test('works', async () => {
30
+ const registry = Registry.make();
31
+ const builder = GraphBuilder.make({ registry });
32
+ const graph = builder.graph;
33
+
34
+ {
35
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
36
+ expect(node).to.be.null;
37
+ }
38
+
39
+ // Test direct API
40
+ GraphBuilder.addExtension(
41
+ builder,
42
+ GraphBuilder.createExtensionRaw({
43
+ id: 'resolver',
44
+ resolver: () => Atom.make({ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: 1 }),
45
+ }),
46
+ );
47
+ await Graph.initialize(graph, EXAMPLE_ID);
48
+
49
+ {
50
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
51
+ expect(node?.id).to.equal(EXAMPLE_ID);
52
+ expect(node?.type).to.equal(EXAMPLE_TYPE);
53
+ expect(node?.data).to.equal(1);
54
+ }
55
+ });
56
+
57
+ test('updates', async () => {
58
+ const registry = Registry.make();
59
+ const builder = GraphBuilder.make({ registry });
60
+ const name = Atom.make('default');
61
+ GraphBuilder.addExtension(
62
+ builder,
63
+ GraphBuilder.createExtensionRaw({
64
+ id: 'resolver',
65
+ resolver: () => Atom.make((get) => ({ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(name) })),
66
+ }),
67
+ );
68
+ const graph = builder.graph;
69
+ await Graph.initialize(graph, EXAMPLE_ID);
70
+
71
+ {
72
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
73
+ expect(node?.data).to.equal('default');
74
+ }
75
+
76
+ registry.set(name, 'updated');
77
+
78
+ {
79
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
80
+ expect(node?.data).to.equal('updated');
81
+ }
82
+ });
83
+
84
+ test('does not overwrite connector-produced node', async () => {
85
+ const registry = Registry.make();
86
+ const builder = GraphBuilder.make({ registry });
87
+ const resolverData = Atom.make('from-resolver');
88
+
89
+ GraphBuilder.addExtension(builder, [
90
+ GraphBuilder.createExtensionRaw({
91
+ id: 'resolver',
92
+ resolver: (id) =>
93
+ id === qualifiedId('root', 'shared')
94
+ ? Atom.make((get) => ({ id: qualifiedId('root', 'shared'), type: EXAMPLE_TYPE, data: get(resolverData) }))
95
+ : Atom.make(null),
96
+ }),
97
+ GraphBuilder.createExtensionRaw({
98
+ id: 'connector',
99
+ connector: (node) =>
100
+ Atom.make((get) =>
101
+ Function.pipe(
102
+ get(node),
103
+ Option.filter((n) => n.id === 'root'),
104
+ Option.map(() => [{ id: 'shared', type: EXAMPLE_TYPE, data: 'from-connector' }]),
105
+ Option.getOrElse(() => []),
106
+ ),
107
+ ),
108
+ }),
109
+ ]);
110
+
111
+ const graph = builder.graph;
112
+
113
+ // Connector produces root/shared.
114
+ Graph.expand(graph, Node.RootId, 'child');
115
+ await GraphBuilder.flush(builder);
116
+
117
+ {
118
+ const node = Graph.getNode(graph, qualifiedId('root', 'shared')).pipe(Option.getOrNull);
119
+ expect(node?.data).to.equal('from-connector');
120
+ }
121
+
122
+ // Resolver fires for the same ID but should not overwrite.
123
+ await Graph.initialize(graph, qualifiedId('root', 'shared'));
124
+
125
+ {
126
+ const node = Graph.getNode(graph, qualifiedId('root', 'shared')).pipe(Option.getOrNull);
127
+ expect(node?.data).to.equal('from-connector');
128
+ }
129
+
130
+ // Updating the resolver's atom should still not overwrite.
131
+ registry.set(resolverData, 'updated-resolver');
132
+
133
+ {
134
+ const node = Graph.getNode(graph, qualifiedId('root', 'shared')).pipe(Option.getOrNull);
135
+ expect(node?.data).to.equal('from-connector');
136
+ }
137
+ });
138
+ });
139
+
20
140
  describe('connector', () => {
21
- test('works', () => {
141
+ test('works', async () => {
22
142
  const registry = Registry.make();
23
- const builder = new GraphBuilder({ registry });
24
- builder.addExtension(
25
- createExtension({
143
+ const builder = GraphBuilder.make({ registry });
144
+ GraphBuilder.addExtension(
145
+ builder,
146
+ GraphBuilder.createExtensionRaw({
26
147
  id: 'outbound-connector',
27
- connector: () => Rx.make([{ id: 'child', type: EXAMPLE_TYPE, data: 2 }]),
148
+ connector: () => Atom.make([{ id: 'child', type: EXAMPLE_TYPE, data: 2 }]),
28
149
  }),
29
150
  );
30
- builder.addExtension(
31
- createExtension({
151
+ GraphBuilder.addExtension(
152
+ builder,
153
+ GraphBuilder.createExtensionRaw({
32
154
  id: 'inbound-connector',
33
- relation: 'inbound',
34
- connector: () => Rx.make([{ id: 'parent', type: EXAMPLE_TYPE, data: 0 }]),
155
+ relation: Node.childRelation('inbound'),
156
+ connector: () => Atom.make([{ id: 'parent', type: EXAMPLE_TYPE, data: 0 }]),
35
157
  }),
36
158
  );
37
159
 
38
160
  const graph = builder.graph;
39
- graph.expand(ROOT_ID);
40
- graph.expand(ROOT_ID, 'inbound');
161
+ Graph.expand(graph, Node.RootId, 'child');
162
+ Graph.expand(graph, Node.RootId, Node.childRelation('inbound'));
163
+ await GraphBuilder.flush(builder);
41
164
 
42
- const outbound = registry.get(graph.connections(ROOT_ID));
43
- const inbound = registry.get(graph.connections(ROOT_ID, 'inbound'));
165
+ const outbound = registry.get(graph.connections(Node.RootId, 'child'));
166
+ const inbound = registry.get(graph.connections(Node.RootId, Node.childRelation('inbound')));
44
167
 
45
168
  expect(outbound).has.length(1);
46
- expect(outbound[0].id).to.equal('child');
169
+ expect(outbound[0].id).to.equal('root/child');
47
170
  expect(outbound[0].data).to.equal(2);
48
171
  expect(inbound).has.length(1);
49
- expect(inbound[0].id).to.equal('parent');
172
+ expect(inbound[0].id).to.equal('root/parent');
50
173
  expect(inbound[0].data).to.equal(0);
51
174
  });
52
175
 
53
- test('updates', () => {
176
+ test('updates', async () => {
54
177
  const registry = Registry.make();
55
- const builder = new GraphBuilder({ registry });
56
- const state = Rx.make(0);
57
- builder.addExtension(
58
- createExtension({
178
+ const builder = GraphBuilder.make({ registry });
179
+ const state = Atom.make(0);
180
+ GraphBuilder.addExtension(
181
+ builder,
182
+ GraphBuilder.createExtensionRaw({
59
183
  id: 'connector',
60
- connector: () => Rx.make((get) => [{ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(state) }]),
184
+ connector: () => Atom.make((get) => [{ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(state) }]),
61
185
  }),
62
186
  );
63
187
  const graph = builder.graph;
64
- graph.expand(ROOT_ID);
188
+ Graph.expand(graph, Node.RootId, 'child');
189
+ await GraphBuilder.flush(builder);
65
190
 
66
191
  {
67
- const [node] = registry.get(graph.connections(ROOT_ID));
192
+ const [node] = registry.get(graph.connections(Node.RootId, 'child'));
68
193
  expect(node.data).to.equal(0);
69
194
  }
70
195
 
71
196
  {
72
197
  registry.set(state, 1);
73
- const [node] = registry.get(graph.connections(ROOT_ID));
198
+ await GraphBuilder.flush(builder);
199
+ const [node] = registry.get(graph.connections(Node.RootId, 'child'));
74
200
  expect(node.data).to.equal(1);
75
201
  }
76
202
  });
77
203
 
78
- test('subscribes to updates', () => {
204
+ test('subscribes to updates', async () => {
79
205
  const registry = Registry.make();
80
- const builder = new GraphBuilder({ registry });
81
- const state = Rx.make(0);
82
- builder.addExtension(
83
- createExtension({
206
+ const builder = GraphBuilder.make({ registry });
207
+ const state = Atom.make(0);
208
+ GraphBuilder.addExtension(
209
+ builder,
210
+ GraphBuilder.createExtensionRaw({
84
211
  id: 'connector',
85
- connector: () => Rx.make((get) => [{ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(state) }]),
212
+ connector: () => Atom.make((get) => [{ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(state) }]),
86
213
  }),
87
214
  );
88
215
  const graph = builder.graph;
89
216
 
90
217
  let count = 0;
91
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (_) => {
218
+ const cancel = registry.subscribe(graph.connections(Node.RootId, 'child'), (_) => {
92
219
  count++;
93
220
  });
94
221
  onTestFinished(() => cancel());
95
222
 
96
223
  expect(count).to.equal(0);
97
- expect(registry.get(graph.connections(ROOT_ID))).to.have.length(0);
224
+ expect(registry.get(graph.connections(Node.RootId, 'child'))).to.have.length(0);
98
225
  expect(count).to.equal(1);
99
226
 
100
- graph.expand(ROOT_ID);
227
+ Graph.expand(graph, Node.RootId, 'child');
228
+ await GraphBuilder.flush(builder);
101
229
  expect(count).to.equal(2);
230
+
102
231
  registry.set(state, 1);
232
+ await GraphBuilder.flush(builder);
103
233
  expect(count).to.equal(3);
104
234
  });
105
235
 
106
- test('updates with new extensions', () => {
236
+ test('updates with new extensions', async () => {
107
237
  const registry = Registry.make();
108
- const builder = new GraphBuilder({ registry });
109
- builder.addExtension(
110
- createExtension({
238
+ const builder = GraphBuilder.make({ registry });
239
+ GraphBuilder.addExtension(
240
+ builder,
241
+ GraphBuilder.createExtensionRaw({
111
242
  id: 'connector',
112
- connector: () => Rx.make([{ id: EXAMPLE_ID, type: EXAMPLE_TYPE }]),
243
+ connector: () => Atom.make([{ id: EXAMPLE_ID, type: EXAMPLE_TYPE }]),
113
244
  }),
114
245
  );
115
246
  const graph = builder.graph;
116
- graph.expand(ROOT_ID);
247
+ Graph.expand(graph, Node.RootId, 'child');
248
+ await GraphBuilder.flush(builder);
117
249
 
118
- let nodes: Node[] = [];
250
+ let nodes: Node.Node[] = [];
119
251
  let count = 0;
120
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (_nodes) => {
252
+ const cancel = registry.subscribe(graph.connections(Node.RootId, 'child'), (_nodes) => {
121
253
  count++;
122
254
  nodes = _nodes;
123
255
  });
@@ -125,63 +257,68 @@ describe('GraphBuilder', () => {
125
257
 
126
258
  expect(nodes).has.length(0);
127
259
  expect(count).to.equal(0);
128
- registry.get(graph.connections(ROOT_ID));
260
+ registry.get(graph.connections(Node.RootId, 'child'));
129
261
  expect(nodes).has.length(1);
130
262
  expect(count).to.equal(1);
131
263
 
132
- builder.addExtension(
133
- createExtension({
264
+ GraphBuilder.addExtension(
265
+ builder,
266
+ GraphBuilder.createExtensionRaw({
134
267
  id: 'connector-2',
135
- connector: () => Rx.make([{ id: exampleId(2), type: EXAMPLE_TYPE }]),
268
+ connector: () => Atom.make([{ id: exampleId(2), type: EXAMPLE_TYPE }]),
136
269
  }),
137
270
  );
271
+ await GraphBuilder.flush(builder);
138
272
  expect(nodes).has.length(2);
139
273
  expect(count).to.equal(2);
140
274
  });
141
275
 
142
- test('removes', () => {
276
+ test('removes', async () => {
143
277
  const registry = Registry.make();
144
- const builder = new GraphBuilder({ registry });
145
- const nodes = Rx.make([
278
+ const builder = GraphBuilder.make({ registry });
279
+ const nodes = Atom.make([
146
280
  { id: exampleId(1), type: EXAMPLE_TYPE },
147
281
  { id: exampleId(2), type: EXAMPLE_TYPE },
148
282
  ]);
149
- builder.addExtension(
150
- createExtension({
283
+ GraphBuilder.addExtension(
284
+ builder,
285
+ GraphBuilder.createExtensionRaw({
151
286
  id: 'connector',
152
- connector: () => Rx.make((get) => get(nodes)),
287
+ connector: () => Atom.make((get) => get(nodes)),
153
288
  }),
154
289
  );
155
290
  const graph = builder.graph;
156
- graph.expand(ROOT_ID);
291
+ Graph.expand(graph, Node.RootId, 'child');
292
+ await GraphBuilder.flush(builder);
157
293
 
158
294
  {
159
- const nodes = registry.get(graph.connections(ROOT_ID));
295
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
160
296
  expect(nodes).has.length(2);
161
- expect(nodes[0].id).to.equal(exampleId(1));
162
- expect(nodes[1].id).to.equal(exampleId(2));
297
+ expect(nodes[0].id).to.equal(qualifiedId('root', exampleId(1)));
298
+ expect(nodes[1].id).to.equal(qualifiedId('root', exampleId(2)));
163
299
  }
164
300
 
165
301
  registry.set(nodes, [{ id: exampleId(3), type: EXAMPLE_TYPE }]);
302
+ await GraphBuilder.flush(builder);
166
303
 
167
304
  {
168
- const nodes = registry.get(graph.connections(ROOT_ID));
305
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
169
306
  expect(nodes).has.length(1);
170
- expect(nodes[0].id).to.equal(exampleId(3));
307
+ expect(nodes[0].id).to.equal(qualifiedId('root', exampleId(3)));
171
308
  }
172
309
  });
173
310
 
174
- test('nodes are updated when removed', () => {
311
+ test('nodes are updated when removed', async () => {
175
312
  const registry = Registry.make();
176
- const builder = new GraphBuilder({ registry });
177
- const name = Rx.make('removed');
313
+ const builder = GraphBuilder.make({ registry });
314
+ const name = Atom.make('removed');
178
315
 
179
- builder.addExtension([
180
- createExtension({
316
+ GraphBuilder.addExtension(builder, [
317
+ GraphBuilder.createExtensionRaw({
181
318
  id: 'root',
182
319
  connector: (node) =>
183
- Rx.make((get) =>
184
- pipe(
320
+ Atom.make((get) =>
321
+ Function.pipe(
185
322
  get(node),
186
323
  Option.flatMap((node) => (node.id === 'root' ? Option.some(get(name)) : Option.none())),
187
324
  Option.filter((name) => name !== 'removed'),
@@ -196,52 +333,58 @@ describe('GraphBuilder', () => {
196
333
 
197
334
  let count = 0;
198
335
  let exists = false;
199
- const cancel = registry.subscribe(graph.node(EXAMPLE_ID), (node) => {
336
+ const cancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID)), (node) => {
200
337
  count++;
201
338
  exists = Option.isSome(node);
202
339
  });
203
340
  onTestFinished(() => cancel());
204
341
 
205
- graph.expand(ROOT_ID);
342
+ Graph.expand(graph, Node.RootId, 'child');
343
+ await GraphBuilder.flush(builder);
206
344
  expect(count).to.equal(0);
207
345
  expect(exists).to.be.false;
208
346
 
209
347
  registry.set(name, 'default');
348
+ await GraphBuilder.flush(builder);
210
349
  expect(count).to.equal(1);
211
350
  expect(exists).to.be.true;
212
351
 
213
352
  registry.set(name, 'removed');
353
+ await GraphBuilder.flush(builder);
214
354
  expect(count).to.equal(2);
215
355
  expect(exists).to.be.false;
216
356
 
217
357
  registry.set(name, 'added');
358
+ await GraphBuilder.flush(builder);
218
359
  expect(count).to.equal(3);
219
360
  expect(exists).to.be.true;
220
361
  });
221
362
 
222
363
  test('sort edges', async () => {
223
364
  const registry = Registry.make();
224
- const builder = new GraphBuilder({ registry });
225
- const nodes = Rx.make([
365
+ const builder = GraphBuilder.make({ registry });
366
+ const nodes = Atom.make([
226
367
  { id: exampleId(1), type: EXAMPLE_TYPE, data: 1 },
227
368
  { id: exampleId(2), type: EXAMPLE_TYPE, data: 2 },
228
369
  { id: exampleId(3), type: EXAMPLE_TYPE, data: 3 },
229
370
  ]);
230
- builder.addExtension(
231
- createExtension({
371
+ GraphBuilder.addExtension(
372
+ builder,
373
+ GraphBuilder.createExtensionRaw({
232
374
  id: 'connector',
233
- connector: () => Rx.make((get) => get(nodes)),
375
+ connector: () => Atom.make((get) => get(nodes)),
234
376
  }),
235
377
  );
236
378
  const graph = builder.graph;
237
- graph.expand(ROOT_ID);
379
+ Graph.expand(graph, Node.RootId, 'child');
380
+ await GraphBuilder.flush(builder);
238
381
 
239
382
  {
240
- const nodes = registry.get(graph.connections(ROOT_ID));
383
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
241
384
  expect(nodes).has.length(3);
242
- expect(nodes[0].id).to.equal(exampleId(1));
243
- expect(nodes[1].id).to.equal(exampleId(2));
244
- expect(nodes[2].id).to.equal(exampleId(3));
385
+ expect(nodes[0].id).to.equal(qualifiedId('root', exampleId(1)));
386
+ expect(nodes[1].id).to.equal(qualifiedId('root', exampleId(2)));
387
+ expect(nodes[2].id).to.equal(qualifiedId('root', exampleId(3)));
245
388
  }
246
389
 
247
390
  registry.set(nodes, [
@@ -249,31 +392,29 @@ describe('GraphBuilder', () => {
249
392
  { id: exampleId(1), type: EXAMPLE_TYPE, data: 1 },
250
393
  { id: exampleId(2), type: EXAMPLE_TYPE, data: 2 },
251
394
  ]);
252
-
253
- // TODO(wittjosiah): Why is this needed for the following conditions to pass?
254
- await sleep(0);
395
+ await GraphBuilder.flush(builder);
255
396
 
256
397
  {
257
- const nodes = registry.get(graph.connections(ROOT_ID));
398
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
258
399
  expect(nodes).has.length(3);
259
- expect(nodes[0].id).to.equal(exampleId(3));
260
- expect(nodes[1].id).to.equal(exampleId(1));
261
- expect(nodes[2].id).to.equal(exampleId(2));
400
+ expect(nodes[0].id).to.equal(qualifiedId('root', exampleId(3)));
401
+ expect(nodes[1].id).to.equal(qualifiedId('root', exampleId(1)));
402
+ expect(nodes[2].id).to.equal(qualifiedId('root', exampleId(2)));
262
403
  }
263
404
  });
264
405
 
265
- test('updates are constrained', () => {
406
+ test('updates are constrained', async () => {
266
407
  const registry = Registry.make();
267
- const builder = new GraphBuilder({ registry });
268
- const name = Rx.make('default');
269
- const sub = Rx.make('default');
408
+ const builder = GraphBuilder.make({ registry });
409
+ const name = Atom.make('default');
410
+ const sub = Atom.make('default');
270
411
 
271
- builder.addExtension([
272
- createExtension({
412
+ GraphBuilder.addExtension(builder, [
413
+ GraphBuilder.createExtensionRaw({
273
414
  id: 'root',
274
415
  connector: (node) =>
275
- Rx.make((get) =>
276
- pipe(
416
+ Atom.make((get) =>
417
+ Function.pipe(
277
418
  get(node),
278
419
  Option.flatMap((node) => (node.id === 'root' ? Option.some(get(name)) : Option.none())),
279
420
  Option.filter((name) => name !== 'removed'),
@@ -282,25 +423,29 @@ describe('GraphBuilder', () => {
282
423
  ),
283
424
  ),
284
425
  }),
285
- createExtension({
426
+ GraphBuilder.createExtensionRaw({
286
427
  id: 'connector1',
287
428
  connector: (node) =>
288
- Rx.make((get) =>
289
- pipe(
429
+ Atom.make((get) =>
430
+ Function.pipe(
290
431
  get(node),
291
- Option.flatMap((node) => (node.id === EXAMPLE_ID ? Option.some(get(sub)) : Option.none())),
432
+ Option.flatMap((node) =>
433
+ node.id === qualifiedId('root', EXAMPLE_ID) ? Option.some(get(sub)) : Option.none(),
434
+ ),
292
435
  Option.map((sub) => [{ id: exampleId(2), type: EXAMPLE_TYPE, data: sub }]),
293
436
  Option.getOrElse(() => []),
294
437
  ),
295
438
  ),
296
439
  }),
297
- createExtension({
440
+ GraphBuilder.createExtensionRaw({
298
441
  id: 'connector2',
299
442
  connector: (node) =>
300
- Rx.make((get) =>
301
- pipe(
443
+ Atom.make((get) =>
444
+ Function.pipe(
302
445
  get(node),
303
- Option.flatMap((node) => (node.id === EXAMPLE_ID ? Option.some(node.data) : Option.none())),
446
+ Option.flatMap((node) =>
447
+ node.id === qualifiedId('root', EXAMPLE_ID) ? Option.some(node.data) : Option.none(),
448
+ ),
304
449
  Option.map((data) => [{ id: exampleId(3), type: EXAMPLE_TYPE, data }]),
305
450
  Option.getOrElse(() => []),
306
451
  ),
@@ -311,64 +456,71 @@ describe('GraphBuilder', () => {
311
456
  const graph = builder.graph;
312
457
 
313
458
  let parentCount = 0;
314
- const parentCancel = registry.subscribe(graph.node(EXAMPLE_ID), (_) => {
459
+ const parentCancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID)), (_) => {
315
460
  parentCount++;
316
461
  });
317
462
  onTestFinished(() => parentCancel());
318
463
 
319
464
  let independentCount = 0;
320
- const independentCancel = registry.subscribe(graph.node(exampleId(2)), (_) => {
465
+ const independentCancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID, exampleId(2))), (_) => {
321
466
  independentCount++;
322
467
  });
323
468
  onTestFinished(() => independentCancel());
324
469
 
325
470
  let dependentCount = 0;
326
- const dependentCancel = registry.subscribe(graph.node(exampleId(3)), (_) => {
471
+ const dependentCancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID, exampleId(3))), (_) => {
327
472
  dependentCount++;
328
473
  });
329
474
  onTestFinished(() => dependentCancel());
330
475
 
331
476
  // Counts should not increment until the node is expanded.
332
- graph.expand(ROOT_ID);
477
+ Graph.expand(graph, Node.RootId, 'child');
478
+ await GraphBuilder.flush(builder);
333
479
  expect(parentCount).to.equal(1);
334
480
  expect(independentCount).to.equal(0);
335
481
  expect(dependentCount).to.equal(0);
336
482
 
337
483
  // Counts should increment when the node is expanded.
338
- graph.expand(EXAMPLE_ID);
484
+ Graph.expand(graph, qualifiedId('root', EXAMPLE_ID), 'child');
485
+ await GraphBuilder.flush(builder);
339
486
  expect(parentCount).to.equal(1);
340
487
  expect(independentCount).to.equal(1);
341
488
  expect(dependentCount).to.equal(1);
342
489
 
343
490
  // Only dependent count should increment when the parent changes.
344
491
  registry.set(name, 'updated');
492
+ await GraphBuilder.flush(builder);
345
493
  expect(parentCount).to.equal(2);
346
494
  expect(independentCount).to.equal(1);
347
495
  expect(dependentCount).to.equal(2);
348
496
 
349
497
  // Only independent count should increment when its state changes.
350
498
  registry.set(sub, 'updated');
499
+ await GraphBuilder.flush(builder);
351
500
  expect(parentCount).to.equal(2);
352
501
  expect(independentCount).to.equal(2);
353
502
  expect(dependentCount).to.equal(2);
354
503
 
355
504
  // Independent count should update if its state changes even if the parent is removed.
356
- Rx.batch(() => {
505
+ Atom.batch(() => {
357
506
  registry.set(name, 'removed');
358
507
  registry.set(sub, 'batch');
359
508
  });
509
+ await GraphBuilder.flush(builder);
360
510
  expect(parentCount).to.equal(2);
361
511
  expect(independentCount).to.equal(3);
362
512
  expect(dependentCount).to.equal(2);
363
513
 
364
514
  // Dependent count should increment when the node is added back.
365
515
  registry.set(name, 'added');
516
+ await GraphBuilder.flush(builder);
366
517
  expect(parentCount).to.equal(3);
367
518
  expect(independentCount).to.equal(3);
368
519
  expect(dependentCount).to.equal(3);
369
520
 
370
521
  // Counts should not increment when the node is expanded again.
371
- graph.expand(EXAMPLE_ID);
522
+ Graph.expand(graph, qualifiedId('root', EXAMPLE_ID), 'child');
523
+ await GraphBuilder.flush(builder);
372
524
  expect(parentCount).to.equal(3);
373
525
  expect(independentCount).to.equal(3);
374
526
  expect(dependentCount).to.equal(3);
@@ -376,13 +528,14 @@ describe('GraphBuilder', () => {
376
528
 
377
529
  test('eager graph expansion', async () => {
378
530
  const registry = Registry.make();
379
- const builder = new GraphBuilder({ registry });
380
- builder.addExtension(
381
- createExtension({
531
+ const builder = GraphBuilder.make({ registry });
532
+ GraphBuilder.addExtension(
533
+ builder,
534
+ GraphBuilder.createExtensionRaw({
382
535
  id: 'connector',
383
536
  connector: (node) => {
384
- return Rx.make((get) =>
385
- pipe(
537
+ return Atom.make((get) =>
538
+ Function.pipe(
386
539
  get(node),
387
540
  Option.map((node) => (node.data ? node.data + 1 : 1)),
388
541
  Option.filter((data) => data <= 5),
@@ -397,14 +550,14 @@ describe('GraphBuilder', () => {
397
550
  let count = 0;
398
551
  const trigger = new Trigger();
399
552
  builder.graph.onNodeChanged.on(({ id }) => {
400
- builder.graph.expand(id);
553
+ Graph.expand(builder.graph, id, 'child');
401
554
  count++;
402
555
  if (count === 5) {
403
556
  trigger.wake();
404
557
  }
405
558
  });
406
559
 
407
- builder.graph.expand(ROOT_ID);
560
+ Graph.expand(builder.graph, Node.RootId, 'child');
408
561
  await trigger.wait();
409
562
  expect(count).to.equal(5);
410
563
  });
@@ -412,13 +565,14 @@ describe('GraphBuilder', () => {
412
565
 
413
566
  describe('explore', () => {
414
567
  test('works', async () => {
415
- const builder = new GraphBuilder();
416
- builder.addExtension(
417
- createExtension({
568
+ const builder = GraphBuilder.make();
569
+ GraphBuilder.addExtension(
570
+ builder,
571
+ GraphBuilder.createExtensionRaw({
418
572
  id: 'connector',
419
573
  connector: (node) =>
420
- Rx.make((get) =>
421
- pipe(
574
+ Atom.make((get) =>
575
+ Function.pipe(
422
576
  get(node),
423
577
  Option.map((node) => (node.data ? node.data + 1 : 1)),
424
578
  Option.filter((data) => data <= 5),
@@ -430,7 +584,8 @@ describe('GraphBuilder', () => {
430
584
  );
431
585
 
432
586
  let count = 0;
433
- await builder.explore({
587
+ await GraphBuilder.explore(builder, {
588
+ relation: 'child',
434
589
  visitor: () => {
435
590
  count++;
436
591
  },
@@ -439,4 +594,665 @@ describe('GraphBuilder', () => {
439
594
  expect(count).to.equal(6);
440
595
  });
441
596
  });
597
+
598
+ describe('helpers', () => {
599
+ describe('createConnector', () => {
600
+ test('creates connector with type inference', async () => {
601
+ const registry = Registry.make();
602
+ const builder = GraphBuilder.make({ registry });
603
+ const graph = builder.graph;
604
+
605
+ const matcher = (node: Node.Node) => NodeMatcher.whenId('root')(node);
606
+ const factory = (node: Node.Node) => [{ id: 'child', type: EXAMPLE_TYPE, data: node.id }];
607
+
608
+ const connector = GraphBuilder.createConnector(matcher, factory);
609
+
610
+ GraphBuilder.addExtension(
611
+ builder,
612
+ GraphBuilder.createExtensionRaw({
613
+ id: 'test-connector',
614
+ connector,
615
+ }),
616
+ );
617
+
618
+ Graph.expand(graph, Node.RootId, 'child');
619
+ await GraphBuilder.flush(builder);
620
+
621
+ const connections = registry.get(graph.connections(Node.RootId, 'child'));
622
+ expect(connections).has.length(1);
623
+ expect(connections[0].id).to.equal('root/child');
624
+ });
625
+ });
626
+
627
+ describe('createExtension', () => {
628
+ test('works with Effect connector', async () => {
629
+ const registry = Registry.make();
630
+ const builder = GraphBuilder.make({ registry });
631
+ const graph = builder.graph;
632
+
633
+ const extensions = Effect.runSync(
634
+ GraphBuilder.createExtension({
635
+ id: 'test-extension',
636
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
637
+ connector: (node, get) => Effect.succeed([{ id: 'child', type: EXAMPLE_TYPE, data: node.data }]),
638
+ }),
639
+ );
640
+
641
+ GraphBuilder.addExtension(builder, extensions);
642
+
643
+ const writableGraph = graph as Graph.WritableGraph;
644
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
645
+ Graph.expand(graph, 'parent', 'child');
646
+ await GraphBuilder.flush(builder);
647
+
648
+ const connections = registry.get(graph.connections('parent', 'child'));
649
+ expect(connections).has.length(1);
650
+ expect(connections[0].id).to.equal('parent/child');
651
+ expect(connections[0].data).to.equal('test');
652
+ });
653
+
654
+ test('works with Effect actions', async () => {
655
+ const registry = Registry.make();
656
+ const builder = GraphBuilder.make({ registry });
657
+ const graph = builder.graph;
658
+
659
+ const extensions = Effect.runSync(
660
+ GraphBuilder.createExtension({
661
+ id: 'test-extension',
662
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
663
+ actions: (node, get) =>
664
+ Effect.succeed([
665
+ {
666
+ id: 'test-action',
667
+ data: () => Effect.void,
668
+ properties: { label: 'Test' },
669
+ },
670
+ ]),
671
+ }),
672
+ );
673
+
674
+ GraphBuilder.addExtension(builder, extensions);
675
+
676
+ const writableGraph = graph as Graph.WritableGraph;
677
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
678
+ Graph.expand(graph, 'parent', 'child');
679
+ await GraphBuilder.flush(builder);
680
+
681
+ const edges = registry.get(graph.edges('parent'));
682
+ expect(edges[Graph.relationKey('action')] ?? []).to.have.length(1);
683
+ expect(edges[Graph.relationKey('action')] ?? []).to.include('parent/test-action');
684
+ expect(edges[Graph.relationKey('child')] ?? []).to.have.length(0);
685
+ const actions = registry.get(graph.actions('parent'));
686
+ expect(actions).has.length(1);
687
+ expect(actions[0].id).to.equal('parent/test-action');
688
+ });
689
+
690
+ test('actions expand automatically with child relation', async ({ expect }) => {
691
+ const registry = Registry.make();
692
+ const builder = GraphBuilder.make({ registry });
693
+ const graph = builder.graph;
694
+
695
+ const extensions = Effect.runSync(
696
+ GraphBuilder.createExtension({
697
+ id: 'test-extension',
698
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
699
+ connector: (node, get) => Effect.succeed([{ id: 'child', type: EXAMPLE_TYPE, data: 'c' }]),
700
+ actions: (node, get) =>
701
+ Effect.succeed([{ id: 'act1', data: () => Effect.void, properties: { label: 'A' } }]),
702
+ }),
703
+ );
704
+
705
+ GraphBuilder.addExtension(builder, extensions);
706
+
707
+ const writableGraph = graph as Graph.WritableGraph;
708
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
709
+ Graph.expand(graph, 'parent', 'child');
710
+ await GraphBuilder.flush(builder);
711
+
712
+ const edges = registry.get(graph.edges('parent'));
713
+ expect(edges[Graph.relationKey('child')] ?? []).to.include('parent/child');
714
+ expect(edges[Graph.relationKey('action')] ?? []).to.include('parent/act1');
715
+ const actions = registry.get(graph.actions('parent'));
716
+ expect(actions).has.length(1);
717
+ expect(actions[0].id).to.equal('parent/act1');
718
+ const connections = registry.get(graph.connections('parent', 'child'));
719
+ expect(connections).has.length(1);
720
+ expect(connections[0].id).to.equal('parent/child');
721
+ });
722
+
723
+ test('actions appear when extension registered after expand', async ({ expect }) => {
724
+ const registry = Registry.make();
725
+ const builder = GraphBuilder.make({ registry });
726
+ const graph = builder.graph;
727
+ const writableGraph = graph as Graph.WritableGraph;
728
+
729
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
730
+ Graph.expand(graph, 'parent', 'child');
731
+ await GraphBuilder.flush(builder);
732
+
733
+ expect(registry.get(graph.actions('parent'))).to.have.length(0);
734
+
735
+ const extensions = Effect.runSync(
736
+ GraphBuilder.createExtension({
737
+ id: 'late-extension',
738
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
739
+ actions: (node, get) =>
740
+ Effect.succeed([{ id: 'late-act', data: () => Effect.void, properties: { label: 'Late' } }]),
741
+ }),
742
+ );
743
+
744
+ GraphBuilder.addExtension(builder, extensions);
745
+ await GraphBuilder.flush(builder);
746
+
747
+ const edges = registry.get(graph.edges('parent'));
748
+ expect(edges[Graph.relationKey('action')] ?? []).to.include('parent/late-act');
749
+ const actions = registry.get(graph.actions('parent'));
750
+ expect(actions).has.length(1);
751
+ expect(actions[0].id).to.equal('parent/late-act');
752
+ });
753
+
754
+ test('_actionContext captures and provides services to action execution', async () => {
755
+ const registry = Registry.make();
756
+ const builder = GraphBuilder.make({ registry });
757
+ const graph = builder.graph;
758
+
759
+ // Define a test service using Context.GenericTag pattern.
760
+ interface TestServiceInterface {
761
+ getValue(): number;
762
+ }
763
+ const TestService = Context.GenericTag<TestServiceInterface>('TestService');
764
+
765
+ // Track whether the action was executed with the correct context.
766
+ let executionResult: number | null = null;
767
+
768
+ // Create extension with service requirement.
769
+ // Note: The actions callback must USE the service for R to be inferred correctly.
770
+ const extensions = Effect.runSync(
771
+ GraphBuilder.createExtension({
772
+ id: 'test-extension',
773
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
774
+ actions: (node, get) =>
775
+ // Use TestService in the callback to include it in R.
776
+ Effect.gen(function* () {
777
+ const service = yield* TestService;
778
+ return [
779
+ {
780
+ id: 'test-action',
781
+ data: () =>
782
+ Effect.gen(function* () {
783
+ // Action can use the same service from captured context.
784
+ const svc = yield* TestService;
785
+ executionResult = svc.getValue();
786
+ }).pipe(Effect.asVoid),
787
+ properties: { label: `Test ${service.getValue()}` },
788
+ },
789
+ ];
790
+ }),
791
+ }).pipe(Effect.provideService(TestService, { getValue: () => 42 })),
792
+ );
793
+
794
+ GraphBuilder.addExtension(builder, extensions);
795
+
796
+ const writableGraph = graph as Graph.WritableGraph;
797
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
798
+ Graph.expand(graph, 'parent', 'child');
799
+ await GraphBuilder.flush(builder);
800
+
801
+ const actions = registry.get(graph.actions('parent'));
802
+ expect(actions).has.length(1);
803
+
804
+ // Verify _actionContext is captured.
805
+ const action = actions[0] as Node.Action;
806
+ expect(action._actionContext).to.not.be.undefined;
807
+
808
+ // Execute the action with the captured context.
809
+ const actionEffect = action.data();
810
+ const effectWithContext = action._actionContext
811
+ ? actionEffect.pipe(Effect.provide(action._actionContext))
812
+ : actionEffect;
813
+
814
+ Effect.runSync(effectWithContext);
815
+
816
+ // Verify the service was accessible during execution.
817
+ expect(executionResult).to.equal(42);
818
+ });
819
+
820
+ test('works with resolver', async () => {
821
+ const registry = Registry.make();
822
+ const builder = GraphBuilder.make({ registry });
823
+ const graph = builder.graph;
824
+
825
+ const extensions = Effect.runSync(
826
+ GraphBuilder.createExtension({
827
+ id: 'test-extension',
828
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
829
+ resolver: (id, get) => Effect.succeed({ id, type: EXAMPLE_TYPE, properties: {}, data: 'resolved' }),
830
+ }),
831
+ );
832
+
833
+ GraphBuilder.addExtension(builder, extensions);
834
+ await Graph.initialize(graph, EXAMPLE_ID);
835
+
836
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
837
+ expect(node).to.not.be.null;
838
+ expect(node?.id).to.equal(EXAMPLE_ID);
839
+ expect(node?.data).to.equal('resolved');
840
+ });
841
+
842
+ test('works with connector and actions together', async () => {
843
+ const registry = Registry.make();
844
+ const builder = GraphBuilder.make({ registry });
845
+ const graph = builder.graph;
846
+
847
+ const extensions = Effect.runSync(
848
+ GraphBuilder.createExtension({
849
+ id: 'test-extension',
850
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
851
+ connector: (node, get) => Effect.succeed([{ id: 'child', type: EXAMPLE_TYPE, data: node.data }]),
852
+ actions: (node, get) =>
853
+ Effect.succeed([
854
+ {
855
+ id: 'test-action',
856
+ data: () => Effect.void,
857
+ properties: { label: 'Test' },
858
+ },
859
+ ]),
860
+ }),
861
+ );
862
+
863
+ GraphBuilder.addExtension(builder, extensions);
864
+
865
+ const writableGraph = graph as Graph.WritableGraph;
866
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
867
+ Graph.expand(graph, 'parent', 'child');
868
+ await GraphBuilder.flush(builder);
869
+
870
+ const connections = registry.get(graph.connections('parent', 'child'));
871
+ // Should have both the child node and the action node.
872
+ expect(connections.length).to.be.greaterThanOrEqual(1);
873
+ const childNode = connections.find((n) => n.id === 'parent/child');
874
+ expect(childNode).to.not.be.undefined;
875
+ expect(childNode?.data).to.equal('test');
876
+
877
+ const actions = registry.get(graph.actions('parent'));
878
+ expect(actions).has.length(1);
879
+ expect(actions[0].id).to.equal('parent/test-action');
880
+ });
881
+
882
+ test('works with reactive connector using get context', async () => {
883
+ const registry = Registry.make();
884
+ const builder = GraphBuilder.make({ registry });
885
+ const graph = builder.graph;
886
+
887
+ const state = Atom.make('initial');
888
+
889
+ const extensions = Effect.runSync(
890
+ GraphBuilder.createExtension({
891
+ id: 'test-extension',
892
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
893
+ connector: (node, get) => Effect.succeed([{ id: 'child', type: EXAMPLE_TYPE, data: get(state) }]),
894
+ }),
895
+ );
896
+
897
+ GraphBuilder.addExtension(builder, extensions);
898
+
899
+ const writableGraph = graph as Graph.WritableGraph;
900
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
901
+ Graph.expand(graph, 'parent', 'child');
902
+ await GraphBuilder.flush(builder);
903
+
904
+ {
905
+ const connections = registry.get(graph.connections('parent', 'child'));
906
+ expect(connections).has.length(1);
907
+ expect(connections[0].data).to.equal('initial');
908
+ }
909
+
910
+ registry.set(state, 'updated');
911
+ await GraphBuilder.flush(builder);
912
+
913
+ {
914
+ const connections = registry.get(graph.connections('parent', 'child'));
915
+ expect(connections).has.length(1);
916
+ expect(connections[0].data).to.equal('updated');
917
+ }
918
+ });
919
+ });
920
+
921
+ describe('extension error handling', () => {
922
+ test('connector failure is caught and logged, returns empty array', async () => {
923
+ const registry = Registry.make();
924
+ const builder = GraphBuilder.make({ registry });
925
+ const graph = builder.graph;
926
+
927
+ const extensions = Effect.runSync(
928
+ GraphBuilder.createExtension({
929
+ id: 'failing-extension',
930
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
931
+ connector: (node, get) => Effect.fail(new Error('Connector failed intentionally')),
932
+ }),
933
+ );
934
+
935
+ GraphBuilder.addExtension(builder, extensions);
936
+
937
+ const writableGraph = graph as Graph.WritableGraph;
938
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
939
+
940
+ // Should not throw, error is caught internally.
941
+ Graph.expand(graph, 'parent', 'child');
942
+ await GraphBuilder.flush(builder);
943
+
944
+ // Should return empty connections since the connector failed.
945
+ const connections = registry.get(graph.connections('parent', 'child'));
946
+ expect(connections).has.length(0);
947
+ });
948
+
949
+ test('actions failure is caught and logged, returns empty array', async () => {
950
+ const registry = Registry.make();
951
+ const builder = GraphBuilder.make({ registry });
952
+ const graph = builder.graph;
953
+
954
+ const extensions = Effect.runSync(
955
+ GraphBuilder.createExtension({
956
+ id: 'failing-actions-extension',
957
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
958
+ actions: (node, get) => Effect.fail(new Error('Actions failed intentionally')),
959
+ }),
960
+ );
961
+
962
+ GraphBuilder.addExtension(builder, extensions);
963
+
964
+ const writableGraph = graph as Graph.WritableGraph;
965
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
966
+
967
+ // Should not throw, error is caught internally.
968
+ Graph.expand(graph, 'parent', 'child');
969
+ await GraphBuilder.flush(builder);
970
+
971
+ // Should return empty actions since the actions callback failed.
972
+ const actions = registry.get(graph.actions('parent'));
973
+ expect(actions).has.length(0);
974
+ });
975
+
976
+ test('resolver failure is caught and logged, returns null', async () => {
977
+ const registry = Registry.make();
978
+ const builder = GraphBuilder.make({ registry });
979
+ const graph = builder.graph;
980
+
981
+ const extensions = Effect.runSync(
982
+ GraphBuilder.createExtension({
983
+ id: 'failing-resolver-extension',
984
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
985
+ resolver: (id, get) => Effect.fail(new Error('Resolver failed intentionally')),
986
+ }),
987
+ );
988
+
989
+ GraphBuilder.addExtension(builder, extensions);
990
+
991
+ // Should not throw, error is caught internally.
992
+ await Graph.initialize(graph, EXAMPLE_ID);
993
+
994
+ // Should return null/none since the resolver failed.
995
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
996
+ expect(node).to.be.null;
997
+ });
998
+
999
+ test('failing extension does not affect other extensions', async () => {
1000
+ const registry = Registry.make();
1001
+ const builder = GraphBuilder.make({ registry });
1002
+ const graph = builder.graph;
1003
+
1004
+ // Add a failing extension.
1005
+ const failingExtensions = Effect.runSync(
1006
+ GraphBuilder.createExtension({
1007
+ id: 'failing-extension',
1008
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
1009
+ connector: (node, get) => Effect.fail(new Error('This one fails')),
1010
+ }),
1011
+ );
1012
+
1013
+ // Add a working extension.
1014
+ const workingExtensions = Effect.runSync(
1015
+ GraphBuilder.createExtension({
1016
+ id: 'working-extension',
1017
+ match: NodeMatcher.whenNodeType(EXAMPLE_TYPE),
1018
+ connector: (node, get) =>
1019
+ Effect.succeed([{ id: 'child-from-working', type: EXAMPLE_TYPE, data: 'success' }]),
1020
+ }),
1021
+ );
1022
+
1023
+ GraphBuilder.addExtension(builder, failingExtensions);
1024
+ GraphBuilder.addExtension(builder, workingExtensions);
1025
+
1026
+ const writableGraph = graph as Graph.WritableGraph;
1027
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: 'test' });
1028
+ Graph.expand(graph, 'parent', 'child');
1029
+ await GraphBuilder.flush(builder);
1030
+
1031
+ // The working extension should still produce its node.
1032
+ const connections = registry.get(graph.connections('parent', 'child'));
1033
+ expect(connections).has.length(1);
1034
+ expect(connections[0].id).to.equal('parent/child-from-working');
1035
+ expect(connections[0].data).to.equal('success');
1036
+ });
1037
+ });
1038
+
1039
+ describe('createTypeExtension', () => {
1040
+ test('creates extension matching by schema type with inferred object type', async () => {
1041
+ const registry = Registry.make();
1042
+ const builder = GraphBuilder.make({ registry });
1043
+ const graph = builder.graph;
1044
+
1045
+ const extensions = Effect.runSync(
1046
+ GraphBuilder.createTypeExtension({
1047
+ id: 'type-extension',
1048
+ type: TestSchema.Person,
1049
+ connector: (object) => Effect.succeed([{ id: 'child', type: EXAMPLE_TYPE, data: object }]),
1050
+ }),
1051
+ );
1052
+
1053
+ GraphBuilder.addExtension(builder, extensions);
1054
+
1055
+ const writableGraph = graph as Graph.WritableGraph;
1056
+ const testObject = Obj.make(TestSchema.Person, { name: 'Test' });
1057
+ Graph.addNode(writableGraph, { id: 'parent', type: EXAMPLE_TYPE, properties: {}, data: testObject });
1058
+ Graph.expand(graph, 'parent', 'child');
1059
+ await GraphBuilder.flush(builder);
1060
+
1061
+ const connections = registry.get(graph.connections('parent', 'child'));
1062
+ expect(connections).has.length(1);
1063
+ expect(connections[0].id).to.equal('parent/child');
1064
+ expect(connections[0].data).to.equal(testObject);
1065
+ });
1066
+ });
1067
+ });
1068
+ describe('path-based ID qualification', () => {
1069
+ test('rejects segment IDs containing slash', async () => {
1070
+ const registry = Registry.make();
1071
+ const builder = GraphBuilder.make({ registry });
1072
+ GraphBuilder.addExtension(
1073
+ builder,
1074
+ GraphBuilder.createExtensionRaw({
1075
+ id: 'bad-connector',
1076
+ connector: () => Atom.make([{ id: 'foo/bar', type: EXAMPLE_TYPE, data: null }]),
1077
+ }),
1078
+ );
1079
+
1080
+ expect(() => Graph.expand(builder.graph, Node.RootId, 'child')).toThrow(/must not contain/);
1081
+ });
1082
+
1083
+ test('multi-level path qualification', async () => {
1084
+ const registry = Registry.make();
1085
+ const builder = GraphBuilder.make({ registry });
1086
+ GraphBuilder.addExtension(builder, [
1087
+ GraphBuilder.createExtensionRaw({
1088
+ id: 'level1',
1089
+ connector: (node) =>
1090
+ Atom.make((get) =>
1091
+ Function.pipe(
1092
+ get(node),
1093
+ Option.filter((n) => n.id === 'root'),
1094
+ Option.map(() => [{ id: 'A', type: EXAMPLE_TYPE, data: 'a' }]),
1095
+ Option.getOrElse(() => []),
1096
+ ),
1097
+ ),
1098
+ }),
1099
+ GraphBuilder.createExtensionRaw({
1100
+ id: 'level2',
1101
+ connector: (node) =>
1102
+ Atom.make((get) =>
1103
+ Function.pipe(
1104
+ get(node),
1105
+ Option.filter((n) => n.id === 'root/A'),
1106
+ Option.map(() => [{ id: 'B', type: EXAMPLE_TYPE, data: 'b' }]),
1107
+ Option.getOrElse(() => []),
1108
+ ),
1109
+ ),
1110
+ }),
1111
+ ]);
1112
+
1113
+ const graph = builder.graph;
1114
+
1115
+ Graph.expand(graph, Node.RootId, 'child');
1116
+ await GraphBuilder.flush(builder);
1117
+
1118
+ const level1 = registry.get(graph.connections(Node.RootId, 'child'));
1119
+ expect(level1).has.length(1);
1120
+ expect(level1[0].id).to.equal('root/A');
1121
+
1122
+ Graph.expand(graph, 'root/A', 'child');
1123
+ await GraphBuilder.flush(builder);
1124
+
1125
+ const level2 = registry.get(graph.connections('root/A', 'child'));
1126
+ expect(level2).has.length(1);
1127
+ expect(level2[0].id).to.equal('root/A/B');
1128
+ });
1129
+
1130
+ test('inline nodes are recursively qualified', async () => {
1131
+ const registry = Registry.make();
1132
+ const builder = GraphBuilder.make({ registry });
1133
+ GraphBuilder.addExtension(
1134
+ builder,
1135
+ GraphBuilder.createExtensionRaw({
1136
+ id: 'inline-connector',
1137
+ connector: () =>
1138
+ Atom.make([
1139
+ {
1140
+ id: 'parent-node',
1141
+ type: EXAMPLE_TYPE,
1142
+ data: null,
1143
+ nodes: [
1144
+ {
1145
+ id: 'inline-child',
1146
+ type: EXAMPLE_TYPE,
1147
+ data: null,
1148
+ nodes: [{ id: 'deep-child', type: EXAMPLE_TYPE, data: null }],
1149
+ },
1150
+ ],
1151
+ },
1152
+ ]),
1153
+ }),
1154
+ );
1155
+
1156
+ const graph = builder.graph;
1157
+ Graph.expand(graph, Node.RootId, 'child');
1158
+ await GraphBuilder.flush(builder);
1159
+
1160
+ const connections = registry.get(graph.connections(Node.RootId, 'child'));
1161
+ expect(connections).has.length(1);
1162
+ expect(connections[0].id).to.equal('root/parent-node');
1163
+
1164
+ const inlineNode = Graph.getNode(graph, 'root/parent-node/inline-child').pipe(Option.getOrNull);
1165
+ expect(inlineNode).to.not.be.null;
1166
+ expect(inlineNode?.id).to.equal('root/parent-node/inline-child');
1167
+
1168
+ const deepNode = Graph.getNode(graph, 'root/parent-node/inline-child/deep-child').pipe(Option.getOrNull);
1169
+ expect(deepNode).to.not.be.null;
1170
+ expect(deepNode?.id).to.equal('root/parent-node/inline-child/deep-child');
1171
+ });
1172
+
1173
+ test('constant connector produces distinct nodes under different parents', async () => {
1174
+ const registry = Registry.make();
1175
+ const builder = GraphBuilder.make({ registry });
1176
+
1177
+ GraphBuilder.addExtension(builder, [
1178
+ GraphBuilder.createExtensionRaw({
1179
+ id: 'parents',
1180
+ connector: (node) =>
1181
+ Atom.make((get) =>
1182
+ Function.pipe(
1183
+ get(node),
1184
+ Option.filter((n) => n.id === 'root'),
1185
+ Option.map(() => [
1186
+ { id: 'A', type: EXAMPLE_TYPE, data: 'a' },
1187
+ { id: 'B', type: EXAMPLE_TYPE, data: 'b' },
1188
+ ]),
1189
+ Option.getOrElse(() => []),
1190
+ ),
1191
+ ),
1192
+ }),
1193
+ GraphBuilder.createExtensionRaw({
1194
+ id: 'constant-child',
1195
+ connector: () => Atom.make([{ id: 'shared', type: EXAMPLE_TYPE, data: 'constant' }]),
1196
+ }),
1197
+ ]);
1198
+
1199
+ const graph = builder.graph;
1200
+
1201
+ Graph.expand(graph, Node.RootId, 'child');
1202
+ await GraphBuilder.flush(builder);
1203
+
1204
+ Graph.expand(graph, 'root/A', 'child');
1205
+ Graph.expand(graph, 'root/B', 'child');
1206
+ await GraphBuilder.flush(builder);
1207
+
1208
+ const childrenOfA = registry.get(graph.connections('root/A', 'child'));
1209
+ const childrenOfB = registry.get(graph.connections('root/B', 'child'));
1210
+
1211
+ expect(childrenOfA).has.length(1);
1212
+ expect(childrenOfB).has.length(1);
1213
+ expect(childrenOfA[0].id).to.equal('root/A/shared');
1214
+ expect(childrenOfB[0].id).to.equal('root/B/shared');
1215
+
1216
+ const nodeA = Graph.getNode(graph, 'root/A/shared').pipe(Option.getOrNull);
1217
+ const nodeB = Graph.getNode(graph, 'root/B/shared').pipe(Option.getOrNull);
1218
+ expect(nodeA).to.not.be.null;
1219
+ expect(nodeB).to.not.be.null;
1220
+ expect(nodeA?.id).to.not.equal(nodeB?.id);
1221
+ });
1222
+
1223
+ test('explore qualifies node IDs', async () => {
1224
+ const builder = GraphBuilder.make();
1225
+ GraphBuilder.addExtension(
1226
+ builder,
1227
+ GraphBuilder.createExtensionRaw({
1228
+ id: 'connector',
1229
+ connector: (node) =>
1230
+ Atom.make((get) =>
1231
+ Function.pipe(
1232
+ get(node),
1233
+ Option.filter((n) => n.id === 'root'),
1234
+ Option.map(() => [
1235
+ { id: 'first', type: EXAMPLE_TYPE, data: 1 },
1236
+ { id: 'second', type: EXAMPLE_TYPE, data: 2 },
1237
+ ]),
1238
+ Option.getOrElse(() => []),
1239
+ ),
1240
+ ),
1241
+ }),
1242
+ );
1243
+
1244
+ const visited: Array<{ id: string; path: string[] }> = [];
1245
+ await GraphBuilder.explore(builder, {
1246
+ relation: 'child',
1247
+ visitor: (node, path) => {
1248
+ visited.push({ id: node.id, path });
1249
+ },
1250
+ });
1251
+
1252
+ expect(visited).has.length(3);
1253
+ expect(visited[0].id).to.equal('root');
1254
+ expect(visited[1].id).to.equal('root/first');
1255
+ expect(visited[2].id).to.equal('root/second');
1256
+ });
1257
+ });
442
1258
  });