@dxos/app-graph 0.8.4-main.406dc2a → 0.8.4-main.40e3dcdf1b

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 (46) hide show
  1. package/dist/lib/browser/index.mjs +1416 -695
  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 +1415 -695
  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 +112 -66
  10. package/dist/types/src/graph-builder.d.ts.map +1 -1
  11. package/dist/types/src/graph.d.ts +187 -221
  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.map +1 -1
  22. package/dist/types/src/util.d.ts +34 -0
  23. package/dist/types/src/util.d.ts.map +1 -0
  24. package/dist/types/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +36 -34
  26. package/src/atoms.ts +25 -0
  27. package/src/graph-builder.test.ts +895 -137
  28. package/src/graph-builder.ts +703 -289
  29. package/src/graph.test.ts +450 -122
  30. package/src/graph.ts +1052 -401
  31. package/src/index.ts +9 -3
  32. package/src/node-matcher.test.ts +301 -0
  33. package/src/node-matcher.ts +282 -0
  34. package/src/node.ts +56 -8
  35. package/src/stories/EchoGraph.stories.tsx +159 -120
  36. package/src/stories/Tree.tsx +1 -1
  37. package/src/util.ts +71 -0
  38. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  39. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  40. package/dist/types/src/signals-integration.test.d.ts +0 -2
  41. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  42. package/dist/types/src/testing.d.ts +0 -5
  43. package/dist/types/src/testing.d.ts.map +0 -1
  44. package/src/experimental/graph-projections.test.ts +0 -56
  45. package/src/signals-integration.test.ts +0 -218
  46. package/src/testing.ts +0 -20
@@ -2,46 +2,52 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Registry, Rx } from '@effect-rx/rx-react';
5
+ import { Atom, Registry } from '@effect-atom/atom-react';
6
+ import * as Context from 'effect/Context';
7
+ import * as Effect from 'effect/Effect';
6
8
  import * as Function from 'effect/Function';
7
9
  import * as Option from 'effect/Option';
8
10
  import { describe, expect, onTestFinished, test } from 'vitest';
9
11
 
10
- import { Trigger, sleep } from '@dxos/async';
12
+ import { Trigger } from '@dxos/async';
13
+ import { Obj } from '@dxos/echo';
14
+ import { TestSchema } from '@dxos/echo/testing';
11
15
 
12
- import { ROOT_ID } from './graph';
13
- import { GraphBuilder, createExtension } from './graph-builder';
14
- 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';
15
20
 
16
21
  const exampleId = (id: number) => `dx:test:${id}`;
17
22
  const EXAMPLE_ID = exampleId(1);
18
- const EXAMPLE_TYPE = 'dxos.org/type/example';
23
+ const EXAMPLE_TYPE = 'org.dxos.type.example';
24
+
25
+ const qualifiedId = (...segments: string[]) => segments.join('/');
19
26
 
20
27
  describe('GraphBuilder', () => {
21
28
  describe('resolver', () => {
22
29
  test('works', async () => {
23
30
  const registry = Registry.make();
24
- const builder = new GraphBuilder({ registry });
31
+ const builder = GraphBuilder.make({ registry });
25
32
  const graph = builder.graph;
26
33
 
27
34
  {
28
- const node = graph.getNode(EXAMPLE_ID).pipe(Option.getOrNull);
35
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
29
36
  expect(node).to.be.null;
30
37
  }
31
38
 
32
- builder.addExtension(
33
- createExtension({
39
+ // Test direct API
40
+ GraphBuilder.addExtension(
41
+ builder,
42
+ GraphBuilder.createExtensionRaw({
34
43
  id: 'resolver',
35
- resolver: () => {
36
- console.log('resolver');
37
- return Rx.make({ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: 1 });
38
- },
44
+ resolver: () => Atom.make({ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: 1 }),
39
45
  }),
40
46
  );
41
- await graph.initialize(EXAMPLE_ID);
47
+ await Graph.initialize(graph, EXAMPLE_ID);
42
48
 
43
49
  {
44
- const node = graph.getNode(EXAMPLE_ID).pipe(Option.getOrNull);
50
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
45
51
  expect(node?.id).to.equal(EXAMPLE_ID);
46
52
  expect(node?.type).to.equal(EXAMPLE_TYPE);
47
53
  expect(node?.data).to.equal(1);
@@ -50,132 +56,200 @@ describe('GraphBuilder', () => {
50
56
 
51
57
  test('updates', async () => {
52
58
  const registry = Registry.make();
53
- const builder = new GraphBuilder({ registry });
54
- const name = Rx.make('default');
55
- builder.addExtension(
56
- createExtension({
59
+ const builder = GraphBuilder.make({ registry });
60
+ const name = Atom.make('default');
61
+ GraphBuilder.addExtension(
62
+ builder,
63
+ GraphBuilder.createExtensionRaw({
57
64
  id: 'resolver',
58
- resolver: () => Rx.make((get) => ({ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(name) })),
65
+ resolver: () => Atom.make((get) => ({ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: get(name) })),
59
66
  }),
60
67
  );
61
68
  const graph = builder.graph;
62
- await graph.initialize(EXAMPLE_ID);
69
+ await Graph.initialize(graph, EXAMPLE_ID);
63
70
 
64
71
  {
65
- const node = graph.getNode(EXAMPLE_ID).pipe(Option.getOrNull);
72
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
66
73
  expect(node?.data).to.equal('default');
67
74
  }
68
75
 
69
76
  registry.set(name, 'updated');
70
77
 
71
78
  {
72
- const node = graph.getNode(EXAMPLE_ID).pipe(Option.getOrNull);
79
+ const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrNull);
73
80
  expect(node?.data).to.equal('updated');
74
81
  }
75
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
+ });
76
138
  });
77
139
 
78
140
  describe('connector', () => {
79
- test('works', () => {
141
+ test('works', async () => {
80
142
  const registry = Registry.make();
81
- const builder = new GraphBuilder({ registry });
82
- builder.addExtension(
83
- createExtension({
143
+ const builder = GraphBuilder.make({ registry });
144
+ GraphBuilder.addExtension(
145
+ builder,
146
+ GraphBuilder.createExtensionRaw({
84
147
  id: 'outbound-connector',
85
- connector: () => Rx.make([{ id: 'child', type: EXAMPLE_TYPE, data: 2 }]),
148
+ connector: () => Atom.make([{ id: 'child', type: EXAMPLE_TYPE, data: 2 }]),
86
149
  }),
87
150
  );
88
- builder.addExtension(
89
- createExtension({
151
+ GraphBuilder.addExtension(
152
+ builder,
153
+ GraphBuilder.createExtensionRaw({
90
154
  id: 'inbound-connector',
91
- relation: 'inbound',
92
- 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 }]),
93
157
  }),
94
158
  );
95
159
 
96
160
  const graph = builder.graph;
97
- graph.expand(ROOT_ID);
98
- 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);
99
164
 
100
- const outbound = registry.get(graph.connections(ROOT_ID));
101
- 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')));
102
167
 
103
168
  expect(outbound).has.length(1);
104
- expect(outbound[0].id).to.equal('child');
169
+ expect(outbound[0].id).to.equal('root/child');
105
170
  expect(outbound[0].data).to.equal(2);
106
171
  expect(inbound).has.length(1);
107
- expect(inbound[0].id).to.equal('parent');
172
+ expect(inbound[0].id).to.equal('root/parent');
108
173
  expect(inbound[0].data).to.equal(0);
109
174
  });
110
175
 
111
- test('updates', () => {
176
+ test('updates', async () => {
112
177
  const registry = Registry.make();
113
- const builder = new GraphBuilder({ registry });
114
- const state = Rx.make(0);
115
- builder.addExtension(
116
- createExtension({
178
+ const builder = GraphBuilder.make({ registry });
179
+ const state = Atom.make(0);
180
+ GraphBuilder.addExtension(
181
+ builder,
182
+ GraphBuilder.createExtensionRaw({
117
183
  id: 'connector',
118
- 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) }]),
119
185
  }),
120
186
  );
121
187
  const graph = builder.graph;
122
- graph.expand(ROOT_ID);
188
+ Graph.expand(graph, Node.RootId, 'child');
189
+ await GraphBuilder.flush(builder);
123
190
 
124
191
  {
125
- const [node] = registry.get(graph.connections(ROOT_ID));
192
+ const [node] = registry.get(graph.connections(Node.RootId, 'child'));
126
193
  expect(node.data).to.equal(0);
127
194
  }
128
195
 
129
196
  {
130
197
  registry.set(state, 1);
131
- const [node] = registry.get(graph.connections(ROOT_ID));
198
+ await GraphBuilder.flush(builder);
199
+ const [node] = registry.get(graph.connections(Node.RootId, 'child'));
132
200
  expect(node.data).to.equal(1);
133
201
  }
134
202
  });
135
203
 
136
- test('subscribes to updates', () => {
204
+ test('subscribes to updates', async () => {
137
205
  const registry = Registry.make();
138
- const builder = new GraphBuilder({ registry });
139
- const state = Rx.make(0);
140
- builder.addExtension(
141
- createExtension({
206
+ const builder = GraphBuilder.make({ registry });
207
+ const state = Atom.make(0);
208
+ GraphBuilder.addExtension(
209
+ builder,
210
+ GraphBuilder.createExtensionRaw({
142
211
  id: 'connector',
143
- 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) }]),
144
213
  }),
145
214
  );
146
215
  const graph = builder.graph;
147
216
 
148
217
  let count = 0;
149
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (_) => {
218
+ const cancel = registry.subscribe(graph.connections(Node.RootId, 'child'), (_) => {
150
219
  count++;
151
220
  });
152
221
  onTestFinished(() => cancel());
153
222
 
154
223
  expect(count).to.equal(0);
155
- expect(registry.get(graph.connections(ROOT_ID))).to.have.length(0);
224
+ expect(registry.get(graph.connections(Node.RootId, 'child'))).to.have.length(0);
156
225
  expect(count).to.equal(1);
157
226
 
158
- graph.expand(ROOT_ID);
227
+ Graph.expand(graph, Node.RootId, 'child');
228
+ await GraphBuilder.flush(builder);
159
229
  expect(count).to.equal(2);
230
+
160
231
  registry.set(state, 1);
232
+ await GraphBuilder.flush(builder);
161
233
  expect(count).to.equal(3);
162
234
  });
163
235
 
164
- test('updates with new extensions', () => {
236
+ test('updates with new extensions', async () => {
165
237
  const registry = Registry.make();
166
- const builder = new GraphBuilder({ registry });
167
- builder.addExtension(
168
- createExtension({
238
+ const builder = GraphBuilder.make({ registry });
239
+ GraphBuilder.addExtension(
240
+ builder,
241
+ GraphBuilder.createExtensionRaw({
169
242
  id: 'connector',
170
- connector: () => Rx.make([{ id: EXAMPLE_ID, type: EXAMPLE_TYPE }]),
243
+ connector: () => Atom.make([{ id: EXAMPLE_ID, type: EXAMPLE_TYPE }]),
171
244
  }),
172
245
  );
173
246
  const graph = builder.graph;
174
- graph.expand(ROOT_ID);
247
+ Graph.expand(graph, Node.RootId, 'child');
248
+ await GraphBuilder.flush(builder);
175
249
 
176
- let nodes: Node[] = [];
250
+ let nodes: Node.Node[] = [];
177
251
  let count = 0;
178
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (_nodes) => {
252
+ const cancel = registry.subscribe(graph.connections(Node.RootId, 'child'), (_nodes) => {
179
253
  count++;
180
254
  nodes = _nodes;
181
255
  });
@@ -183,62 +257,67 @@ describe('GraphBuilder', () => {
183
257
 
184
258
  expect(nodes).has.length(0);
185
259
  expect(count).to.equal(0);
186
- registry.get(graph.connections(ROOT_ID));
260
+ registry.get(graph.connections(Node.RootId, 'child'));
187
261
  expect(nodes).has.length(1);
188
262
  expect(count).to.equal(1);
189
263
 
190
- builder.addExtension(
191
- createExtension({
264
+ GraphBuilder.addExtension(
265
+ builder,
266
+ GraphBuilder.createExtensionRaw({
192
267
  id: 'connector-2',
193
- connector: () => Rx.make([{ id: exampleId(2), type: EXAMPLE_TYPE }]),
268
+ connector: () => Atom.make([{ id: exampleId(2), type: EXAMPLE_TYPE }]),
194
269
  }),
195
270
  );
271
+ await GraphBuilder.flush(builder);
196
272
  expect(nodes).has.length(2);
197
273
  expect(count).to.equal(2);
198
274
  });
199
275
 
200
- test('removes', () => {
276
+ test('removes', async () => {
201
277
  const registry = Registry.make();
202
- const builder = new GraphBuilder({ registry });
203
- const nodes = Rx.make([
278
+ const builder = GraphBuilder.make({ registry });
279
+ const nodes = Atom.make([
204
280
  { id: exampleId(1), type: EXAMPLE_TYPE },
205
281
  { id: exampleId(2), type: EXAMPLE_TYPE },
206
282
  ]);
207
- builder.addExtension(
208
- createExtension({
283
+ GraphBuilder.addExtension(
284
+ builder,
285
+ GraphBuilder.createExtensionRaw({
209
286
  id: 'connector',
210
- connector: () => Rx.make((get) => get(nodes)),
287
+ connector: () => Atom.make((get) => get(nodes)),
211
288
  }),
212
289
  );
213
290
  const graph = builder.graph;
214
- graph.expand(ROOT_ID);
291
+ Graph.expand(graph, Node.RootId, 'child');
292
+ await GraphBuilder.flush(builder);
215
293
 
216
294
  {
217
- const nodes = registry.get(graph.connections(ROOT_ID));
295
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
218
296
  expect(nodes).has.length(2);
219
- expect(nodes[0].id).to.equal(exampleId(1));
220
- 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)));
221
299
  }
222
300
 
223
301
  registry.set(nodes, [{ id: exampleId(3), type: EXAMPLE_TYPE }]);
302
+ await GraphBuilder.flush(builder);
224
303
 
225
304
  {
226
- const nodes = registry.get(graph.connections(ROOT_ID));
305
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
227
306
  expect(nodes).has.length(1);
228
- expect(nodes[0].id).to.equal(exampleId(3));
307
+ expect(nodes[0].id).to.equal(qualifiedId('root', exampleId(3)));
229
308
  }
230
309
  });
231
310
 
232
- test('nodes are updated when removed', () => {
311
+ test('nodes are updated when removed', async () => {
233
312
  const registry = Registry.make();
234
- const builder = new GraphBuilder({ registry });
235
- const name = Rx.make('removed');
313
+ const builder = GraphBuilder.make({ registry });
314
+ const name = Atom.make('removed');
236
315
 
237
- builder.addExtension([
238
- createExtension({
316
+ GraphBuilder.addExtension(builder, [
317
+ GraphBuilder.createExtensionRaw({
239
318
  id: 'root',
240
319
  connector: (node) =>
241
- Rx.make((get) =>
320
+ Atom.make((get) =>
242
321
  Function.pipe(
243
322
  get(node),
244
323
  Option.flatMap((node) => (node.id === 'root' ? Option.some(get(name)) : Option.none())),
@@ -254,52 +333,58 @@ describe('GraphBuilder', () => {
254
333
 
255
334
  let count = 0;
256
335
  let exists = false;
257
- const cancel = registry.subscribe(graph.node(EXAMPLE_ID), (node) => {
336
+ const cancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID)), (node) => {
258
337
  count++;
259
338
  exists = Option.isSome(node);
260
339
  });
261
340
  onTestFinished(() => cancel());
262
341
 
263
- graph.expand(ROOT_ID);
342
+ Graph.expand(graph, Node.RootId, 'child');
343
+ await GraphBuilder.flush(builder);
264
344
  expect(count).to.equal(0);
265
345
  expect(exists).to.be.false;
266
346
 
267
347
  registry.set(name, 'default');
348
+ await GraphBuilder.flush(builder);
268
349
  expect(count).to.equal(1);
269
350
  expect(exists).to.be.true;
270
351
 
271
352
  registry.set(name, 'removed');
353
+ await GraphBuilder.flush(builder);
272
354
  expect(count).to.equal(2);
273
355
  expect(exists).to.be.false;
274
356
 
275
357
  registry.set(name, 'added');
358
+ await GraphBuilder.flush(builder);
276
359
  expect(count).to.equal(3);
277
360
  expect(exists).to.be.true;
278
361
  });
279
362
 
280
363
  test('sort edges', async () => {
281
364
  const registry = Registry.make();
282
- const builder = new GraphBuilder({ registry });
283
- const nodes = Rx.make([
365
+ const builder = GraphBuilder.make({ registry });
366
+ const nodes = Atom.make([
284
367
  { id: exampleId(1), type: EXAMPLE_TYPE, data: 1 },
285
368
  { id: exampleId(2), type: EXAMPLE_TYPE, data: 2 },
286
369
  { id: exampleId(3), type: EXAMPLE_TYPE, data: 3 },
287
370
  ]);
288
- builder.addExtension(
289
- createExtension({
371
+ GraphBuilder.addExtension(
372
+ builder,
373
+ GraphBuilder.createExtensionRaw({
290
374
  id: 'connector',
291
- connector: () => Rx.make((get) => get(nodes)),
375
+ connector: () => Atom.make((get) => get(nodes)),
292
376
  }),
293
377
  );
294
378
  const graph = builder.graph;
295
- graph.expand(ROOT_ID);
379
+ Graph.expand(graph, Node.RootId, 'child');
380
+ await GraphBuilder.flush(builder);
296
381
 
297
382
  {
298
- const nodes = registry.get(graph.connections(ROOT_ID));
383
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
299
384
  expect(nodes).has.length(3);
300
- expect(nodes[0].id).to.equal(exampleId(1));
301
- expect(nodes[1].id).to.equal(exampleId(2));
302
- 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)));
303
388
  }
304
389
 
305
390
  registry.set(nodes, [
@@ -307,30 +392,28 @@ describe('GraphBuilder', () => {
307
392
  { id: exampleId(1), type: EXAMPLE_TYPE, data: 1 },
308
393
  { id: exampleId(2), type: EXAMPLE_TYPE, data: 2 },
309
394
  ]);
310
-
311
- // TODO(wittjosiah): Why is this needed for the following conditions to pass?
312
- await sleep(0);
395
+ await GraphBuilder.flush(builder);
313
396
 
314
397
  {
315
- const nodes = registry.get(graph.connections(ROOT_ID));
398
+ const nodes = registry.get(graph.connections(Node.RootId, 'child'));
316
399
  expect(nodes).has.length(3);
317
- expect(nodes[0].id).to.equal(exampleId(3));
318
- expect(nodes[1].id).to.equal(exampleId(1));
319
- 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)));
320
403
  }
321
404
  });
322
405
 
323
- test('updates are constrained', () => {
406
+ test('updates are constrained', async () => {
324
407
  const registry = Registry.make();
325
- const builder = new GraphBuilder({ registry });
326
- const name = Rx.make('default');
327
- const sub = Rx.make('default');
408
+ const builder = GraphBuilder.make({ registry });
409
+ const name = Atom.make('default');
410
+ const sub = Atom.make('default');
328
411
 
329
- builder.addExtension([
330
- createExtension({
412
+ GraphBuilder.addExtension(builder, [
413
+ GraphBuilder.createExtensionRaw({
331
414
  id: 'root',
332
415
  connector: (node) =>
333
- Rx.make((get) =>
416
+ Atom.make((get) =>
334
417
  Function.pipe(
335
418
  get(node),
336
419
  Option.flatMap((node) => (node.id === 'root' ? Option.some(get(name)) : Option.none())),
@@ -340,25 +423,29 @@ describe('GraphBuilder', () => {
340
423
  ),
341
424
  ),
342
425
  }),
343
- createExtension({
426
+ GraphBuilder.createExtensionRaw({
344
427
  id: 'connector1',
345
428
  connector: (node) =>
346
- Rx.make((get) =>
429
+ Atom.make((get) =>
347
430
  Function.pipe(
348
431
  get(node),
349
- 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
+ ),
350
435
  Option.map((sub) => [{ id: exampleId(2), type: EXAMPLE_TYPE, data: sub }]),
351
436
  Option.getOrElse(() => []),
352
437
  ),
353
438
  ),
354
439
  }),
355
- createExtension({
440
+ GraphBuilder.createExtensionRaw({
356
441
  id: 'connector2',
357
442
  connector: (node) =>
358
- Rx.make((get) =>
443
+ Atom.make((get) =>
359
444
  Function.pipe(
360
445
  get(node),
361
- 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
+ ),
362
449
  Option.map((data) => [{ id: exampleId(3), type: EXAMPLE_TYPE, data }]),
363
450
  Option.getOrElse(() => []),
364
451
  ),
@@ -369,64 +456,71 @@ describe('GraphBuilder', () => {
369
456
  const graph = builder.graph;
370
457
 
371
458
  let parentCount = 0;
372
- const parentCancel = registry.subscribe(graph.node(EXAMPLE_ID), (_) => {
459
+ const parentCancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID)), (_) => {
373
460
  parentCount++;
374
461
  });
375
462
  onTestFinished(() => parentCancel());
376
463
 
377
464
  let independentCount = 0;
378
- const independentCancel = registry.subscribe(graph.node(exampleId(2)), (_) => {
465
+ const independentCancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID, exampleId(2))), (_) => {
379
466
  independentCount++;
380
467
  });
381
468
  onTestFinished(() => independentCancel());
382
469
 
383
470
  let dependentCount = 0;
384
- const dependentCancel = registry.subscribe(graph.node(exampleId(3)), (_) => {
471
+ const dependentCancel = registry.subscribe(graph.node(qualifiedId('root', EXAMPLE_ID, exampleId(3))), (_) => {
385
472
  dependentCount++;
386
473
  });
387
474
  onTestFinished(() => dependentCancel());
388
475
 
389
476
  // Counts should not increment until the node is expanded.
390
- graph.expand(ROOT_ID);
477
+ Graph.expand(graph, Node.RootId, 'child');
478
+ await GraphBuilder.flush(builder);
391
479
  expect(parentCount).to.equal(1);
392
480
  expect(independentCount).to.equal(0);
393
481
  expect(dependentCount).to.equal(0);
394
482
 
395
483
  // Counts should increment when the node is expanded.
396
- graph.expand(EXAMPLE_ID);
484
+ Graph.expand(graph, qualifiedId('root', EXAMPLE_ID), 'child');
485
+ await GraphBuilder.flush(builder);
397
486
  expect(parentCount).to.equal(1);
398
487
  expect(independentCount).to.equal(1);
399
488
  expect(dependentCount).to.equal(1);
400
489
 
401
490
  // Only dependent count should increment when the parent changes.
402
491
  registry.set(name, 'updated');
492
+ await GraphBuilder.flush(builder);
403
493
  expect(parentCount).to.equal(2);
404
494
  expect(independentCount).to.equal(1);
405
495
  expect(dependentCount).to.equal(2);
406
496
 
407
497
  // Only independent count should increment when its state changes.
408
498
  registry.set(sub, 'updated');
499
+ await GraphBuilder.flush(builder);
409
500
  expect(parentCount).to.equal(2);
410
501
  expect(independentCount).to.equal(2);
411
502
  expect(dependentCount).to.equal(2);
412
503
 
413
504
  // Independent count should update if its state changes even if the parent is removed.
414
- Rx.batch(() => {
505
+ Atom.batch(() => {
415
506
  registry.set(name, 'removed');
416
507
  registry.set(sub, 'batch');
417
508
  });
509
+ await GraphBuilder.flush(builder);
418
510
  expect(parentCount).to.equal(2);
419
511
  expect(independentCount).to.equal(3);
420
512
  expect(dependentCount).to.equal(2);
421
513
 
422
514
  // Dependent count should increment when the node is added back.
423
515
  registry.set(name, 'added');
516
+ await GraphBuilder.flush(builder);
424
517
  expect(parentCount).to.equal(3);
425
518
  expect(independentCount).to.equal(3);
426
519
  expect(dependentCount).to.equal(3);
427
520
 
428
521
  // Counts should not increment when the node is expanded again.
429
- graph.expand(EXAMPLE_ID);
522
+ Graph.expand(graph, qualifiedId('root', EXAMPLE_ID), 'child');
523
+ await GraphBuilder.flush(builder);
430
524
  expect(parentCount).to.equal(3);
431
525
  expect(independentCount).to.equal(3);
432
526
  expect(dependentCount).to.equal(3);
@@ -434,12 +528,13 @@ describe('GraphBuilder', () => {
434
528
 
435
529
  test('eager graph expansion', async () => {
436
530
  const registry = Registry.make();
437
- const builder = new GraphBuilder({ registry });
438
- builder.addExtension(
439
- createExtension({
531
+ const builder = GraphBuilder.make({ registry });
532
+ GraphBuilder.addExtension(
533
+ builder,
534
+ GraphBuilder.createExtensionRaw({
440
535
  id: 'connector',
441
536
  connector: (node) => {
442
- return Rx.make((get) =>
537
+ return Atom.make((get) =>
443
538
  Function.pipe(
444
539
  get(node),
445
540
  Option.map((node) => (node.data ? node.data + 1 : 1)),
@@ -455,14 +550,14 @@ describe('GraphBuilder', () => {
455
550
  let count = 0;
456
551
  const trigger = new Trigger();
457
552
  builder.graph.onNodeChanged.on(({ id }) => {
458
- builder.graph.expand(id);
553
+ Graph.expand(builder.graph, id, 'child');
459
554
  count++;
460
555
  if (count === 5) {
461
556
  trigger.wake();
462
557
  }
463
558
  });
464
559
 
465
- builder.graph.expand(ROOT_ID);
560
+ Graph.expand(builder.graph, Node.RootId, 'child');
466
561
  await trigger.wait();
467
562
  expect(count).to.equal(5);
468
563
  });
@@ -470,12 +565,13 @@ describe('GraphBuilder', () => {
470
565
 
471
566
  describe('explore', () => {
472
567
  test('works', async () => {
473
- const builder = new GraphBuilder();
474
- builder.addExtension(
475
- createExtension({
568
+ const builder = GraphBuilder.make();
569
+ GraphBuilder.addExtension(
570
+ builder,
571
+ GraphBuilder.createExtensionRaw({
476
572
  id: 'connector',
477
573
  connector: (node) =>
478
- Rx.make((get) =>
574
+ Atom.make((get) =>
479
575
  Function.pipe(
480
576
  get(node),
481
577
  Option.map((node) => (node.data ? node.data + 1 : 1)),
@@ -488,7 +584,8 @@ describe('GraphBuilder', () => {
488
584
  );
489
585
 
490
586
  let count = 0;
491
- await builder.explore({
587
+ await GraphBuilder.explore(builder, {
588
+ relation: 'child',
492
589
  visitor: () => {
493
590
  count++;
494
591
  },
@@ -497,4 +594,665 @@ describe('GraphBuilder', () => {
497
594
  expect(count).to.equal(6);
498
595
  });
499
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
+ });
500
1258
  });