-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorm.test.ts
More file actions
1155 lines (1067 loc) · 40.4 KB
/
Copy pathorm.test.ts
File metadata and controls
1155 lines (1067 loc) · 40.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ORM Integration Test for agentic-db
*
* Tests the full codegen -> ORM runtime chain against a real PostgreSQL database:
* 1. Seeds DB with agentic_db_app_public tables
* 2. Builds a Graphile schema via graphile-test with ConstructivePreset
* 3. Runs the codegen pipeline (introspection -> inferTables -> generateOrm)
* 4. Loads the generated createClient and instantiates models
* 5. Exercises ORM model methods (findMany, create, delete) via QueryBuilder
*
* This validates that the codegen pipeline produces valid ORM code that
* works against a real PostGraphile schema with ConstructivePreset enabled.
*
* Following the pattern from constructive/graphql/orm-test.
*/
import path from 'path';
import { getConnectionsObject, seed } from 'graphile-test';
import type { GraphQLQueryFnObj } from 'graphile-test';
import type { PgTestClient } from 'pgsql-test';
import { ConstructivePreset } from 'graphile-settings';
import { runCodegenAndLoad } from './helpers/codegen-helper';
import { GraphileTestAdapter } from './helpers/graphile-adapter';
jest.setTimeout(120000);
const seedRoot = path.join(__dirname, '..', '__fixtures__', 'seed');
const sql = (file: string) => path.join(seedRoot, file);
// Real pgpm package — pgsql-test's seed.pgpm() walks the `requires` chain in
// agentic-db.control and deploys the real schema (no hand-rolled SQL).
const AGENTIC_DB_PKG = path.resolve(__dirname, '..', '..', 'agentic-db');
const SCHEMAS = ['agentic_db_app_public'];
// Fixed IDs from seed data
const CONTACT_ALICE = '11111111-1111-1111-1111-111111111111';
const CONTACT_BOB = '22222222-2222-2222-2222-222222222222';
const NOTE_KICKOFF = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa';
const AGENT_RESEARCH = 'cccccccc-cccc-cccc-cccc-cccccccccccc';
// Memories with seeded PostGIS Point locations (see test-data.sql)
const MEMORY_SF = 'eeeeeeee-eeee-eeee-eeee-eeeeeeee0001';
const MEMORY_OAKLAND = 'eeeeeeee-eeee-eeee-eeee-eeeeeeee0002';
const MEMORY_NYC = 'eeeeeeee-eeee-eeee-eeee-eeeeeeee0003';
// ~200 m from MEMORY_SF so the self-relation nearbyMemories has a
// matching "other" row (the plugin excludes owner-row from self-joins).
const MEMORY_FERRY = 'eeeeeeee-eeee-eeee-eeee-eeeeeeee0004';
// Bounding-box polygon around the Bay Area: covers SF + Oakland, excludes NYC.
const BAY_AREA_POLYGON = {
type: 'Polygon',
coordinates: [
[
[-122.55, 37.70],
[-122.20, 37.70],
[-122.20, 37.85],
[-122.55, 37.85],
[-122.55, 37.70],
],
],
};
describe('ORM integration', () => {
let db: PgTestClient;
let teardown: () => Promise<void>;
let query: GraphQLQueryFnObj;
let orm: Record<string, any>;
beforeAll(async () => {
const connections = await getConnectionsObject(
{
schemas: SCHEMAS,
useRoot: true,
authRole: 'postgres',
preset: {
extends: [ConstructivePreset],
},
},
[
seed.pgpm(AGENTIC_DB_PKG),
seed.sqlfile([
sql('test-bootstrap.sql'),
sql('test-data.sql'),
sql('spatial-relations.sql'),
]),
],
);
db = connections.db;
teardown = connections.teardown;
query = connections.query;
// Run the full codegen pipeline against the live schema
const { createClient } = await runCodegenAndLoad(query, 'orm');
// Create the ORM client with the GraphileTestAdapter
const adapter = new GraphileTestAdapter(query);
orm = createClient({ adapter });
});
afterAll(async () => {
if (teardown) {
await teardown();
}
});
/** Extract the first connection/mutation result regardless of field name */
function unwrapData(data: any): any {
return Object.values(data)[0];
}
/** Assert result.ok and log errors if it fails */
function expectOk(result: any, label?: string) {
if (!result.ok) {
console.error(
`[${label ?? 'ORM'}] query failed:`,
JSON.stringify(result.errors, null, 2),
);
}
expect(result.ok).toBe(true);
}
// =========================================================================
// Smoke test: verify codegen produced the expected models
// =========================================================================
describe('codegen smoke test', () => {
it('createClient returns model instances for all tables', () => {
expect(orm).toBeDefined();
expect(orm.contact).toBeDefined();
expect(orm.note).toBeDefined();
expect(orm.agent).toBeDefined();
expect(orm.task).toBeDefined();
expect(orm.contactNote).toBeDefined();
});
it('models have the expected CRUD methods', () => {
expect(typeof orm.contact.findMany).toBe('function');
expect(typeof orm.contact.findFirst).toBe('function');
expect(typeof orm.contact.create).toBe('function');
expect(typeof orm.note.findMany).toBe('function');
expect(typeof orm.note.create).toBe('function');
expect(typeof orm.agent.findMany).toBe('function');
expect(typeof orm.agent.create).toBe('function');
expect(typeof orm.contactNote.findMany).toBe('function');
expect(typeof orm.contactNote.create).toBe('function');
});
});
// =========================================================================
// Test: Contact CRUD via ORM
// =========================================================================
describe('Contact CRUD via ORM', () => {
it('contact.findMany returns seeded contacts', async () => {
const result = await orm.contact
.findMany({
select: {
id: true,
firstName: true,
lastName: true,
email: true,
},
})
.execute();
expectOk(result, 'contact.findMany');
const nodes = unwrapData(result.data).nodes;
expect(nodes).toBeDefined();
expect(nodes.length).toBeGreaterThanOrEqual(2);
const alice = nodes.find((c: any) => c.id === CONTACT_ALICE);
expect(alice).toBeDefined();
expect(alice.firstName).toBe('Alice');
expect(alice.email).toBe('alice@example.com');
});
it('contact.create creates a new contact', async () => {
const result = await orm.contact
.create({
data: {
firstName: 'Test',
lastName: 'User',
email: 'test@example.com',
headline: 'Integration Test Contact',
},
select: {
id: true,
firstName: true,
lastName: true,
email: true,
headline: true,
},
})
.execute();
expectOk(result, 'contact.create');
const contact = unwrapData(result.data).contact;
expect(contact).toBeDefined();
expect(contact.firstName).toBe('Test');
expect(contact.lastName).toBe('User');
expect(contact.email).toBe('test@example.com');
});
});
// =========================================================================
// Test: Note CRUD via ORM
// =========================================================================
describe('Note CRUD via ORM', () => {
it('note.findMany returns seeded notes', async () => {
const result = await orm.note
.findMany({
select: {
id: true,
content: true,
},
})
.execute();
expectOk(result, 'note.findMany');
const nodes = unwrapData(result.data).nodes;
expect(nodes).toBeDefined();
expect(nodes.length).toBeGreaterThanOrEqual(2);
});
it('note.create creates a new note', async () => {
const result = await orm.note
.create({
data: {
content: 'This is a test note for integration testing.',
},
select: {
id: true,
content: true,
},
})
.execute();
expectOk(result, 'note.create');
const note = unwrapData(result.data).note;
expect(note).toBeDefined();
expect(note.content).toBe('This is a test note for integration testing.');
});
});
// =========================================================================
// Test: Agent CRUD via ORM
// =========================================================================
describe('Agent CRUD via ORM', () => {
it('agent.findMany returns seeded agents', async () => {
const result = await orm.agent
.findMany({
select: {
id: true,
name: true,
description: true,
systemPrompt: true,
},
})
.execute();
expectOk(result, 'agent.findMany');
const nodes = unwrapData(result.data).nodes;
expect(nodes).toBeDefined();
const agent = nodes.find((a: any) => a.id === AGENT_RESEARCH);
expect(agent).toBeDefined();
expect(agent.name).toBe('Research Agent');
});
it('agent.create creates a new agent', async () => {
const result = await orm.agent
.create({
data: {
name: 'Test Agent',
description: 'An agent for integration testing',
systemPrompt: 'You are a helpful test agent.',
},
select: {
id: true,
name: true,
description: true,
systemPrompt: true,
},
})
.execute();
expectOk(result, 'agent.create');
const agent = unwrapData(result.data).agent;
expect(agent.name).toBe('Test Agent');
expect(agent.description).toBe('An agent for integration testing');
});
});
// =========================================================================
// Test: M:N relations (contact <-> notes via contact_notes junction)
// =========================================================================
describe('M:N relations (contact <-> notes)', () => {
it('contact.findMany returns contacts with M:N notes connection', async () => {
const result = await orm.contact
.findMany({
select: {
id: true,
firstName: true,
notes: {
select: { id: true, content: true },
},
},
where: { id: { equalTo: CONTACT_ALICE } },
})
.execute();
expectOk(result, 'contact.findMany+notes');
const nodes = unwrapData(result.data).nodes;
expect(nodes).toBeDefined();
expect(nodes.length).toBe(1);
const alice = nodes[0];
expect(alice.firstName).toBe('Alice');
expect(alice.notes.nodes).toHaveLength(1);
expect(alice.notes.nodes[0].id).toBe(NOTE_KICKOFF);
});
it('creates a junction row via contactNote.create', async () => {
// Create a new contact
const contactResult = await orm.contact
.create({
data: { firstName: 'Rel', lastName: 'Test' },
select: { id: true },
})
.execute();
expectOk(contactResult, 'contactNote.contact.create');
const contactId = unwrapData(contactResult.data).contact.id;
// Create a new note
const noteResult = await orm.note
.create({
data: { content: 'Note linked to new contact' },
select: { id: true },
})
.execute();
expectOk(noteResult, 'contactNote.note.create');
const noteId = unwrapData(noteResult.data).note.id;
// Link them via junction
const linkResult = await orm.contactNote
.create({
data: { contactId, noteId },
select: { contactId: true, noteId: true },
})
.execute();
expectOk(linkResult, 'contactNote.create');
const link = unwrapData(linkResult.data).contactNote;
expect(link.contactId).toBe(contactId);
expect(link.noteId).toBe(noteId);
});
});
// =========================================================================
// Test: 1:N relations (agent -> tasks)
// =========================================================================
describe('1:N relations (agent -> tasks)', () => {
it('creates an agent with a task via ORM', async () => {
// Create agent
const agentResult = await orm.agent
.create({
data: { name: 'Task Agent' },
select: { id: true },
})
.execute();
expectOk(agentResult, 'agent.create(task)');
const agentId = unwrapData(agentResult.data).agent.id;
// Create task linked to agent
const taskResult = await orm.task
.create({
data: {
agentId,
title: 'Test Task',
description: 'Task for integration testing',
status: 'pending',
},
select: {
id: true,
title: true,
agentId: true,
},
})
.execute();
expectOk(taskResult, 'task.create');
const task = unwrapData(taskResult.data).task;
expect(task.title).toBe('Test Task');
expect(task.agentId).toBe(agentId);
});
it('agent.findMany returns agents with tasks connection', async () => {
const result = await orm.agent
.findMany({
select: {
id: true,
name: true,
tasks: {
select: { title: true, status: true },
},
},
where: { id: { equalTo: AGENT_RESEARCH } },
})
.execute();
expectOk(result, 'agent.findMany+tasks');
const nodes = unwrapData(result.data).nodes;
expect(nodes).toBeDefined();
expect(nodes.length).toBe(1);
const agent = nodes[0];
expect(agent.name).toBe('Research Agent');
expect(agent.tasks.nodes.length).toBeGreaterThanOrEqual(1);
});
});
// =========================================================================
// Test: Task CRUD + filters + update
// =========================================================================
describe('Task CRUD + filters + update', () => {
let createdTaskId: string;
it('task.create with a status and priority', async () => {
const result = await orm.task
.create({
data: {
agentId: AGENT_RESEARCH,
title: 'Filter test task',
description: 'Used to exercise task filters',
status: 'in_progress',
priority: 2,
},
select: { id: true, title: true, status: true, priority: true },
})
.execute();
expectOk(result, 'task.create(filter seed)');
createdTaskId = unwrapData(result.data).task.id;
});
it('task.findMany with where filter by status', async () => {
const result = await orm.task
.findMany({
where: { status: { equalTo: 'in_progress' } },
select: { id: true, title: true, status: true },
})
.execute();
expectOk(result, 'task.findMany(status)');
const nodes = unwrapData(result.data).nodes;
expect(nodes.length).toBeGreaterThanOrEqual(1);
for (const n of nodes) {
expect(n.status).toBe('in_progress');
}
});
it('task.update patches status and result', async () => {
const result = await orm.task
.update({
where: { id: createdTaskId },
data: { status: 'completed', result: 'done' },
select: { id: true, status: true, result: true },
})
.execute();
expectOk(result, 'task.update');
const task = unwrapData(result.data).task;
expect(task.status).toBe('completed');
expect(task.result).toBe('done');
});
it('task.delete removes the row', async () => {
const result = await orm.task
.delete({ where: { id: createdTaskId }, select: { id: true } })
.execute();
expectOk(result, 'task.delete');
});
});
// =========================================================================
// Test: Memory CRUD (long-term episodic memory)
// =========================================================================
describe('Memory CRUD via ORM', () => {
it('memory.create + memory.findMany + memory.update', async () => {
const create = await orm.memory
.create({
data: {
agentId: AGENT_RESEARCH,
title: 'First-run checklist',
content: 'Verified docker + ollama connectivity before running suite.',
location: 'ci',
mood: 'neutral',
tags: ['setup', 'smoke'],
embeddingText: 'Verified docker + ollama connectivity',
},
select: { id: true, title: true, tags: true },
})
.execute();
expectOk(create, 'memory.create');
const memory = unwrapData(create.data).memory;
expect(memory.title).toBe('First-run checklist');
expect(memory.tags).toEqual(['setup', 'smoke']);
const list = await orm.memory
.findMany({
where: { id: { equalTo: memory.id } },
select: { id: true, title: true, mood: true, agentId: true },
})
.execute();
expectOk(list, 'memory.findMany');
const nodes = unwrapData(list.data).nodes;
expect(nodes).toHaveLength(1);
expect(nodes[0].agentId).toBe(AGENT_RESEARCH);
const patched = await orm.memory
.update({
where: { id: memory.id },
data: { mood: 'confident' },
select: { id: true, mood: true },
})
.execute();
expectOk(patched, 'memory.update');
expect(unwrapData(patched.data).memory.mood).toBe('confident');
});
});
// =========================================================================
// Test: PostGIS spatial support on memories.location_geo
//
// What IS testable through the typed ORM today:
// - geography columns round-trip correctly (insert WKT/GeoJSON, read
// back GeoJSON + srid through the generated output type)
// - `locationGeo: { isNull: false }` filters out rows without geometry
//
// What is NOT testable through the typed ORM today (verified against the
// real schema + graphile-postgis@2.9.7 in CI): the generated
// `GeographyInterfaceFilter` exposes `bboxIntersects2D`, `coveredBy`,
// `covers`, `exactlyEquals`, `intersects` — but all of them fail at
// runtime with `parse error - invalid geometry` because the spatial
// function operators in graphile-postgis 2.9.7 pass the GeoJSON value
// straight through to PostgreSQL instead of wrapping it with
// ST_GeomFromGeoJSON(...)::geography. Only the `withinDistance` operator
// has the correct wrapping, and it is not exposed on the generated
// filter type at all. As a result, "find memories within 5km of here"
// and "find memories inside this polygon" are NOT possible through the
// typed ORM today — they require raw SQL (ST_DWithin / ST_Covers).
// =========================================================================
describe('PostGIS spatial support on memory.location_geo', () => {
it('returns the stored Point as GeoJSON + srid via the generated output type', async () => {
const result = await orm.memory
.findMany({
where: { id: { equalTo: MEMORY_SF } },
select: {
id: true,
title: true,
locationGeo: { select: { geojson: true, srid: true } },
},
})
.execute();
expectOk(result, 'memory.findMany(MEMORY_SF)');
const nodes = unwrapData(result.data).nodes;
expect(nodes).toHaveLength(1);
const sf = nodes[0];
expect(sf.locationGeo).toBeTruthy();
expect(sf.locationGeo.srid).toBe(4326);
// GeoJSON is serialized as a string by the postgis plugin.
const geojson =
typeof sf.locationGeo.geojson === 'string'
? JSON.parse(sf.locationGeo.geojson)
: sf.locationGeo.geojson;
expect(geojson.type).toBe('Point');
expect(geojson.coordinates[0]).toBeCloseTo(-122.4194, 3);
expect(geojson.coordinates[1]).toBeCloseTo(37.7749, 3);
});
it('locationGeo: { isNull: false } returns the geo-tagged memories', async () => {
const result = await orm.memory
.findMany({
where: { locationGeo: { isNull: false } },
select: {
id: true,
title: true,
locationGeo: { select: { srid: true } },
},
})
.execute();
expectOk(result, 'memory.findMany(isNull:false)');
const nodes = unwrapData(result.data).nodes;
// Every returned row must actually have geometry attached.
for (const n of nodes) {
expect(n.locationGeo).toBeTruthy();
expect(n.locationGeo.srid).toBe(4326);
}
const ids = nodes.map((n: any) => n.id);
expect(ids).toEqual(
expect.arrayContaining([MEMORY_SF, MEMORY_OAKLAND, MEMORY_NYC]),
);
});
// Scalar bbox overlap filter: `&&` (graphile-postgis's
// PostgisOperatorFactory). Bay-Area polygon covers SF + Oakland
// but excludes NYC, so we expect the two Bay-Area memories
// (plus the close-by MEMORY_FERRY fixture) and NOT the NYC memory.
it('bboxIntersects2D(Polygon) returns memories whose points fall inside the box', async () => {
const result = await orm.memory
.findMany({
where: { locationGeo: { bboxIntersects2D: BAY_AREA_POLYGON } },
select: { id: true, title: true },
})
.execute();
expectOk(result, 'memory.findMany(bboxIntersects2D)');
const ids = unwrapData(result.data).nodes.map((n: any) => n.id);
expect(ids).toEqual(
expect.arrayContaining([MEMORY_SF, MEMORY_OAKLAND, MEMORY_FERRY]),
);
expect(ids).not.toContain(MEMORY_NYC);
});
});
// =========================================================================
// Test: RelationSpatial — @spatialRelation smart tags on the owner columns
// expose cross-table spatial filters via graphile-postgis's
// PostgisSpatialRelationsPlugin. The blueprint defines 5 relations
// (see packages/provision/src/schemas/spatial-relations.ts); the fixture
// `spatial-relations.sql` bakes matching smart tags into the column
// comments so the plugin picks them up during introspection.
//
// Each test exercises the generated filter shape:
// where: { <relationName>: { distance: <meters>, some: { ... } } }
//
// Seed coordinates (see test-data.sql) are chosen so each test has a
// positive match (SF or NYC) plus a negative control (Tokyo / Paris /
// London / Berlin).
// =========================================================================
describe('RelationSpatial via ORM', () => {
it('memory.nearbyPlaces: memories within 5 km of any place near SF', async () => {
const result = await orm.memory
.findMany({
where: {
nearbyPlaces: {
distance: 5000,
some: { category: { equalTo: 'market' } },
},
},
select: { id: true, title: true },
})
.execute();
expectOk(result, 'memory.findMany(nearbyPlaces)');
const ids = unwrapData(result.data).nodes.map((n: any) => n.id);
// Ferry Building is ~200 m from the SF memory and ~13 km from
// Oakland, so only SF matches.
expect(ids).toContain(MEMORY_SF);
expect(ids).not.toContain(MEMORY_OAKLAND);
expect(ids).not.toContain(MEMORY_NYC);
});
it('memory.nearbyContacts: memories within 2 km of any contact in SF', async () => {
const result = await orm.memory
.findMany({
where: {
nearbyContacts: {
distance: 2000,
some: { firstName: { equalTo: 'Alice' } },
},
},
select: { id: true, title: true },
})
.execute();
expectOk(result, 'memory.findMany(nearbyContacts)');
const ids = unwrapData(result.data).nodes.map((n: any) => n.id);
// Alice is ~250 m from the SF memory, ~13 km from Oakland,
// and ~4100 km from NYC.
expect(ids).toContain(MEMORY_SF);
expect(ids).not.toContain(MEMORY_OAKLAND);
expect(ids).not.toContain(MEMORY_NYC);
});
it('trip.nearbyVenues: trips within 1 km of any SoMa venue', async () => {
const result = await orm.trip
.findMany({
where: {
nearbyVenues: {
distance: 1000,
some: { neighborhood: { equalTo: 'SoMa' } },
},
},
select: { id: true, name: true },
})
.execute();
expectOk(result, 'trip.findMany(nearbyVenues)');
const names = unwrapData(result.data).nodes.map((n: any) => n.name);
expect(names).toContain('SF Retrieval Summit');
expect(names).not.toContain('NYC AI Conf');
expect(names).not.toContain('Paris Offsite');
});
it('event.nearbyVenues: events within 500 m of any Midtown venue', async () => {
const result = await orm.event
.findMany({
where: {
nearbyVenues: {
distance: 500,
some: { neighborhood: { equalTo: 'Midtown' } },
},
},
select: { id: true, name: true },
})
.execute();
expectOk(result, 'event.findMany(nearbyVenues)');
const names = unwrapData(result.data).nodes.map((n: any) => n.name);
// AI Conf Welcome Reception is ~50 m from Times Square Diner
// (Midtown). The SF event is far from any Midtown venue.
expect(names).toContain('AI Conf Welcome Reception');
expect(names).not.toContain('SF Ferry Building Mixer');
expect(names).not.toContain('Berlin Hackathon');
});
it('memory.nearbyMemories: self-relation returns memories within 1 km of another Bay-Area memory', async () => {
const result = await orm.memory
.findMany({
where: {
nearbyMemories: {
distance: 1000,
// Target is MEMORY_FERRY (~260 m from SF). The plugin
// excludes the owner row from self-relations, so we
// filter the target on a DIFFERENT row than we expect
// back in the result set.
some: { id: { equalTo: MEMORY_FERRY } },
},
},
select: { id: true, title: true },
})
.execute();
expectOk(result, 'memory.findMany(nearbyMemories)');
const ids = unwrapData(result.data).nodes.map((n: any) => n.id);
// MEMORY_SF is ~200 m from MEMORY_FERRY, so SF matches.
// Oakland is ~13 km from Ferry Building; NYC is ~4100 km.
// The FERRY row itself is excluded by the self-relation.
expect(ids).toContain(MEMORY_SF);
expect(ids).not.toContain(MEMORY_FERRY);
expect(ids).not.toContain(MEMORY_OAKLAND);
expect(ids).not.toContain(MEMORY_NYC);
});
});
// =========================================================================
// Test: Conversation + Message (1:N) with message ordering via orderBy
// =========================================================================
describe('Conversation + Message CRUD via ORM', () => {
let conversationId: string;
it('conversation.create returns a conversation with id', async () => {
const res = await orm.conversation
.create({
data: {
agentId: AGENT_RESEARCH,
title: 'Integration test conversation',
status: 'active',
meta: { source: 'jest', runId: 1 },
},
select: { id: true, title: true, status: true },
})
.execute();
expectOk(res, 'conversation.create');
const conv = unwrapData(res.data).conversation;
conversationId = conv.id;
expect(conv.title).toBe('Integration test conversation');
});
it('message.create (user + assistant) appends to the conversation', async () => {
const user = await orm.message
.create({
data: {
conversationId,
role: 'user',
content: 'What is vector search?',
tokenCount: 6,
},
select: { id: true, role: true, content: true },
})
.execute();
expectOk(user, 'message.create(user)');
expect(unwrapData(user.data).message.role).toBe('user');
const assistant = await orm.message
.create({
data: {
conversationId,
role: 'assistant',
content: 'It is similarity search in embedding space.',
tokenCount: 11,
toolCalls: { calls: [] },
},
select: { id: true, role: true, tokenCount: true },
})
.execute();
expectOk(assistant, 'message.create(assistant)');
expect(unwrapData(assistant.data).message.tokenCount).toBe(11);
});
it('conversation.findMany includes messages ordered by createdAt', async () => {
const res = await orm.conversation
.findMany({
where: { id: { equalTo: conversationId } },
select: {
id: true,
title: true,
messages: {
orderBy: ['CREATED_AT_ASC'],
select: { role: true, content: true },
},
},
})
.execute();
expectOk(res, 'conversation.findMany+messages');
const conv = unwrapData(res.data).nodes[0];
expect(conv.messages.nodes.length).toBeGreaterThanOrEqual(2);
expect(conv.messages.nodes[0].role).toBe('user');
expect(conv.messages.nodes[1].role).toBe('assistant');
});
});
// =========================================================================
// Test: Skill + ToolDefinition + SkillTool (M:N junction)
// =========================================================================
describe('Skill, ToolDefinition, SkillTool (M:N)', () => {
it('connects a skill to a toolDefinition via the skillTool junction', async () => {
const skillRes = await orm.skill
.create({
data: {
agentId: AGENT_RESEARCH,
name: 'Summarize document',
description: 'Produces a concise summary of a document',
category: 'writing',
intentTrigger: 'summarize this document',
isActive: true,
},
select: { id: true, name: true, isActive: true },
})
.execute();
expectOk(skillRes, 'skill.create');
const skillId = unwrapData(skillRes.data).skill.id;
const toolRes = await orm.toolDefinition
.create({
data: {
name: 'fetchUrl',
description: 'HTTP GET a URL and return text',
toolType: 'http',
schema: { type: 'object', properties: { url: { type: 'string' } } },
isActive: true,
},
select: { id: true, name: true, toolType: true },
})
.execute();
expectOk(toolRes, 'toolDefinition.create');
const toolDefinitionId = unwrapData(toolRes.data).toolDefinition.id;
const linkRes = await orm.skillTool
.create({
data: { skillId, toolDefinitionId },
select: { skillId: true, toolDefinitionId: true },
})
.execute();
expectOk(linkRes, 'skillTool.create');
const link = unwrapData(linkRes.data).skillTool;
expect(link.skillId).toBe(skillId);
expect(link.toolDefinitionId).toBe(toolDefinitionId);
});
});
// =========================================================================
// Test: Rule CRUD (requires agentId)
// =========================================================================
describe('Rule CRUD via ORM', () => {
it('rule.create + findMany by agentId', async () => {
const create = await orm.rule
.create({
data: {
agentId: AGENT_RESEARCH,
name: 'Auto-tag meeting notes',
description: 'When a note is created with "meeting" keyword, tag it.',
triggerType: 'note_created',
triggerConfig: { keyword: 'meeting' },
actionType: 'add_tag',
actionConfig: { tag: 'meeting' },
isActive: true,
priority: 10,
},
select: { id: true, name: true, agentId: true, priority: true },
})
.execute();
expectOk(create, 'rule.create');
expect(unwrapData(create.data).rule.priority).toBe(10);
const list = await orm.rule
.findMany({
where: { agentId: { equalTo: AGENT_RESEARCH } },
select: { id: true, name: true, triggerType: true },
})
.execute();
expectOk(list, 'rule.findMany(agent)');
expect(unwrapData(list.data).nodes.length).toBeGreaterThanOrEqual(1);
});
});
// =========================================================================
// Test: Project CRUD + Goal + Habit (Life-OS headline features)
// =========================================================================
describe('Project / Goal / Habit CRUD via ORM', () => {
it('project.create + update status', async () => {
const create = await orm.project
.create({
data: {
name: 'Release 1.2',
description: 'Shipping the vector-search docs rewrite',
status: 'active',
projectType: 'engineering',
priority: 1,
tags: ['docs', 'search'],
},
select: { id: true, name: true, status: true },
})
.execute();
expectOk(create, 'project.create');
const projectId = unwrapData(create.data).project.id;
const update = await orm.project
.update({
where: { id: projectId },
data: { status: 'completed' },
select: { id: true, status: true },
})
.execute();
expectOk(update, 'project.update');
expect(unwrapData(update.data).project.status).toBe('completed');
});
it('goal.create + habit.create', async () => {
const goal = await orm.goal
.create({
data: {
title: 'Read 12 books this year',
description: 'One per month, non-fiction preferred',
status: 'active',
progress: '0.25',
tags: ['reading', 'learning'],
},
select: { id: true, title: true, progress: true },
})
.execute();
expectOk(goal, 'goal.create');
// progress is numeric; postgraphile returns it as a string
expect(parseFloat(unwrapData(goal.data).goal.progress)).toBeCloseTo(0.25);
const habit = await orm.habit
.create({
data: {
name: 'Morning walk',
frequency: 'daily',
streak: 5,
tags: ['health'],
},
select: { id: true, name: true, streak: true },
})
.execute();
expectOk(habit, 'habit.create');
expect(unwrapData(habit.data).habit.streak).toBe(5);
});
});
// =========================================================================
// Test: Expense CRUD (accounting)
// =========================================================================
describe('Expense CRUD via ORM', () => {
it('expense.create + findMany by category', async () => {
const create = await orm.expense
.create({
data: {
description: 'Cloud hosting — April',
amount: '42.50',
currency: 'USD',
category: 'infrastructure',
vendor: 'CloudProvider',
tags: ['recurring'],
},
select: { id: true, description: true, category: true, amount: true },
})
.execute();
expectOk(create, 'expense.create');
expect(unwrapData(create.data).expense.category).toBe('infrastructure');
const list = await orm.expense
.findMany({
where: { category: { equalTo: 'infrastructure' } },
select: { id: true, description: true, vendor: true },