@coasys/ad4m 0.12.0-rc1-dev.3 → 0.12.0-rc1-dev.5

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.
@@ -0,0 +1,804 @@
1
+ /**
2
+ * Tests for relation filtering via conformance getters stored in SHACL shapes.
3
+ *
4
+ * Validates that:
5
+ * - buildConformanceFilter() correctly derives conditions from target model metadata
6
+ * - SHACL shapes serialize & deserialize getter + conformanceConditions through
7
+ * all serialization paths (toLinks/fromLinks, toJSON/fromJSON)
8
+ * - generateSHACL() populates getter on relation shapes when target has flags/required
9
+ * - filter:false and explicit getter options are respected
10
+ */
11
+ import { Ad4mModel } from "./Ad4mModel";
12
+ import type { Where } from "./Ad4mModel";
13
+ import {
14
+ Model,
15
+ Property,
16
+ Flag,
17
+ HasMany,
18
+ Optional,
19
+ buildConformanceFilter,
20
+ } from "./decorators";
21
+ import {
22
+ SHACLShape,
23
+ SHACLPropertyShape,
24
+ ConformanceCondition,
25
+ } from "../shacl/SHACLShape";
26
+ import { compileWhereClause } from "./surreal-utils";
27
+
28
+ // ============================================================================
29
+ // Test models
30
+ // ============================================================================
31
+
32
+ @Model({ name: "FlaggedTarget" })
33
+ class FlaggedTarget extends Ad4mModel {
34
+ @Flag({ through: "test://type", value: "test://flagged_type" })
35
+ type: string = "";
36
+
37
+ @Property({ through: "test://name", required: true })
38
+ name: string = "";
39
+ }
40
+
41
+ @Model({ name: "MinimalTarget" })
42
+ class MinimalTarget extends Ad4mModel {
43
+ @Optional({ through: "test://optionalProp" })
44
+ optionalProp: string = "";
45
+ }
46
+
47
+ @Model({ name: "ParentWithRelations" })
48
+ class ParentWithRelations extends Ad4mModel {
49
+ @HasMany({
50
+ through: "test://has_flagged",
51
+ target: () => FlaggedTarget,
52
+ })
53
+ flaggedItems: string[] = [];
54
+
55
+ @HasMany({
56
+ through: "test://has_minimal",
57
+ target: () => MinimalTarget,
58
+ })
59
+ minimalItems: string[] = [];
60
+
61
+ @HasMany({
62
+ through: "test://has_unfiltered",
63
+ target: () => FlaggedTarget,
64
+ filter: false,
65
+ })
66
+ unfilteredItems: string[] = [];
67
+
68
+ @HasMany({
69
+ getter: "(<-link[WHERE predicate = 'test://custom'].in.uri)",
70
+ })
71
+ customItems: string[] = [];
72
+ }
73
+
74
+ // ============================================================================
75
+ // buildConformanceFilter tests
76
+ // ============================================================================
77
+
78
+ describe("buildConformanceFilter()", () => {
79
+ it("should derive flag + required conditions from target model", () => {
80
+ const result = buildConformanceFilter("test://has_flagged", FlaggedTarget);
81
+
82
+ expect(result).toBeDefined();
83
+ expect(result!.conformanceConditions).toHaveLength(2);
84
+
85
+ // Flag condition
86
+ const flagCond = result!.conformanceConditions.find(c => c.type === "flag");
87
+ expect(flagCond).toBeDefined();
88
+ expect(flagCond!.predicate).toBe("test://type");
89
+ expect(flagCond!.value).toBe("test://flagged_type");
90
+
91
+ // Required condition
92
+ const reqCond = result!.conformanceConditions.find(c => c.type === "required");
93
+ expect(reqCond).toBeDefined();
94
+ expect(reqCond!.predicate).toBe("test://name");
95
+ expect(reqCond!.value).toBeUndefined();
96
+
97
+ // Getter string should be a SurrealQL expression
98
+ expect(result!.getter).toContain("->link[WHERE predicate =");
99
+ expect(result!.getter).toContain("test://has_flagged");
100
+ expect(result!.getter).toContain("test://type");
101
+ expect(result!.getter).toContain("test://flagged_type");
102
+ expect(result!.getter).toContain("test://name");
103
+ });
104
+
105
+ it("should return undefined for a target with no conformance conditions", () => {
106
+ const result = buildConformanceFilter("test://has_minimal", MinimalTarget);
107
+ expect(result).toBeUndefined();
108
+ });
109
+
110
+ it("should handle class with only flags (no required properties)", () => {
111
+ @Model({ name: "FlagOnly" })
112
+ class FlagOnly extends Ad4mModel {
113
+ @Flag({ through: "test://kind", value: "test://special" })
114
+ kind: string = "";
115
+ }
116
+
117
+ const result = buildConformanceFilter("test://pred", FlagOnly);
118
+ expect(result).toBeDefined();
119
+ expect(result!.conformanceConditions).toHaveLength(1);
120
+ expect(result!.conformanceConditions[0].type).toBe("flag");
121
+ expect(result!.conformanceConditions[0].predicate).toBe("test://kind");
122
+ expect(result!.conformanceConditions[0].value).toBe("test://special");
123
+ });
124
+
125
+ it("should skip properties with custom getters in required check", () => {
126
+ @Model({ name: "CustomGetterTarget" })
127
+ class CustomGetterTarget extends Ad4mModel {
128
+ @Flag({ through: "test://type", value: "test://cgt" })
129
+ type: string = "";
130
+
131
+ @Property({
132
+ through: "test://computed",
133
+ required: true,
134
+ getter: "(<-link[WHERE predicate = 'test://custom'].in.uri)[0]",
135
+ })
136
+ computed: string = "";
137
+ }
138
+
139
+ const result = buildConformanceFilter("test://pred", CustomGetterTarget);
140
+ expect(result).toBeDefined();
141
+ // Should have flag condition but NOT required condition for 'computed' (has custom getter)
142
+ expect(result!.conformanceConditions).toHaveLength(1);
143
+ expect(result!.conformanceConditions[0].type).toBe("flag");
144
+ });
145
+ });
146
+
147
+ // ============================================================================
148
+ // SHACL serialization round-trip tests
149
+ // ============================================================================
150
+
151
+ describe("SHACL shape getter serialization", () => {
152
+ const testGetter = "(->link[WHERE predicate = 'test://pred'].out[WHERE count(->link[WHERE predicate = 'test://type' AND out.uri = 'test://flag']) > 0].uri)";
153
+ const testConditions: ConformanceCondition[] = [
154
+ { type: "flag", predicate: "test://type", value: "test://flag" },
155
+ { type: "required", predicate: "test://name" },
156
+ ];
157
+
158
+ it("should round-trip getter + conditions through toLinks/fromLinks", () => {
159
+ const shape = new SHACLShape("test://Parent");
160
+ const prop: SHACLPropertyShape = {
161
+ name: "items",
162
+ path: "test://has_items",
163
+ nodeKind: "IRI",
164
+ getter: testGetter,
165
+ conformanceConditions: testConditions,
166
+ };
167
+ shape.addProperty(prop);
168
+
169
+ const links = shape.toLinks();
170
+
171
+ // Verify getter link exists
172
+ const getterLink = links.find(l => l.predicate === "ad4m://getter");
173
+ expect(getterLink).toBeDefined();
174
+ expect(getterLink!.target).toBe(`literal://string:${testGetter}`);
175
+
176
+ // Verify conditions link exists
177
+ const conditionsLink = links.find(l => l.predicate === "ad4m://conformanceConditions");
178
+ expect(conditionsLink).toBeDefined();
179
+
180
+ // Reconstruct from links
181
+ const shapeUri = shape.nodeShapeUri;
182
+ const linkObjs = links.map(l => ({
183
+ source: l.source,
184
+ predicate: l.predicate,
185
+ target: l.target,
186
+ }));
187
+ const reconstructed = SHACLShape.fromLinks(linkObjs as any, shapeUri);
188
+
189
+ expect(reconstructed.properties).toHaveLength(1);
190
+ expect(reconstructed.properties[0].getter).toBe(testGetter);
191
+ expect(reconstructed.properties[0].conformanceConditions).toEqual(testConditions);
192
+ });
193
+
194
+ it("should round-trip getter + conditions through toJSON/fromJSON", () => {
195
+ const shape = new SHACLShape("test://Parent");
196
+ const prop: SHACLPropertyShape = {
197
+ name: "items",
198
+ path: "test://has_items",
199
+ getter: testGetter,
200
+ conformanceConditions: testConditions,
201
+ };
202
+ shape.addProperty(prop);
203
+
204
+ const json = shape.toJSON();
205
+ const reconstructed = SHACLShape.fromJSON(json);
206
+
207
+ expect(reconstructed.properties).toHaveLength(1);
208
+ expect(reconstructed.properties[0].getter).toBe(testGetter);
209
+ expect(reconstructed.properties[0].conformanceConditions).toEqual(testConditions);
210
+ });
211
+
212
+ it("should omit getter/conditions links when not set", () => {
213
+ const shape = new SHACLShape("test://Basic");
214
+ const prop: SHACLPropertyShape = {
215
+ name: "simple",
216
+ path: "test://simple",
217
+ };
218
+ shape.addProperty(prop);
219
+
220
+ const links = shape.toLinks();
221
+ expect(links.find(l => l.predicate === "ad4m://getter")).toBeUndefined();
222
+ expect(links.find(l => l.predicate === "ad4m://conformanceConditions")).toBeUndefined();
223
+ });
224
+ });
225
+
226
+ // ============================================================================
227
+ // generateSHACL integration tests
228
+ // ============================================================================
229
+
230
+ describe("generateSHACL() relation getter population", () => {
231
+ it("should populate getter on relation with flagged target", () => {
232
+ const { shape } = (ParentWithRelations as any).generateSHACL();
233
+
234
+ // Find the flaggedItems property shape
235
+ const flaggedProp = shape.properties.find(
236
+ (p: SHACLPropertyShape) => p.name === "flaggedItems"
237
+ );
238
+ expect(flaggedProp).toBeDefined();
239
+ expect(flaggedProp!.getter).toBeDefined();
240
+ expect(flaggedProp!.getter).toContain("test://has_flagged");
241
+ expect(flaggedProp!.getter).toContain("test://type");
242
+ expect(flaggedProp!.getter).toContain("test://flagged_type");
243
+ expect(flaggedProp!.conformanceConditions).toBeDefined();
244
+ expect(flaggedProp!.conformanceConditions!.length).toBeGreaterThan(0);
245
+ });
246
+
247
+ it("should NOT populate getter when target has no conformance conditions", () => {
248
+ const { shape } = (ParentWithRelations as any).generateSHACL();
249
+
250
+ const minimalProp = shape.properties.find(
251
+ (p: SHACLPropertyShape) => p.name === "minimalItems"
252
+ );
253
+ expect(minimalProp).toBeDefined();
254
+ // MinimalTarget has only optional property — no flags/required — no getter
255
+ expect(minimalProp!.getter).toBeUndefined();
256
+ expect(minimalProp!.conformanceConditions).toBeUndefined();
257
+ });
258
+
259
+ it("should NOT populate getter when filter is false", () => {
260
+ const { shape } = (ParentWithRelations as any).generateSHACL();
261
+
262
+ const unfilteredProp = shape.properties.find(
263
+ (p: SHACLPropertyShape) => p.name === "unfilteredItems"
264
+ );
265
+ expect(unfilteredProp).toBeDefined();
266
+ // filter: false → no auto-generated getter even though target has flags
267
+ expect(unfilteredProp!.getter).toBeUndefined();
268
+ expect(unfilteredProp!.conformanceConditions).toBeUndefined();
269
+ });
270
+
271
+ it("should use explicit getter when provided (ignoring auto-generation)", () => {
272
+ const { shape } = (ParentWithRelations as any).generateSHACL();
273
+
274
+ // Getter-only relations have no predicate and are excluded from SHACL shapes
275
+ const customProp = shape.properties.find(
276
+ (p: SHACLPropertyShape) => p.name === "customItems"
277
+ );
278
+ expect(customProp).toBeUndefined();
279
+ });
280
+
281
+ it("should survive full round-trip: generateSHACL → toLinks → fromLinks", () => {
282
+ const { shape } = (ParentWithRelations as any).generateSHACL();
283
+ const links = shape.toLinks();
284
+ const reconstructed = SHACLShape.fromLinks(
285
+ links.map((l: any) => ({
286
+ source: l.source,
287
+ predicate: l.predicate,
288
+ target: l.target,
289
+ })),
290
+ shape.nodeShapeUri
291
+ );
292
+
293
+ // Find the flaggedItems property in reconstructed shape
294
+ const flaggedProp = reconstructed.properties.find(
295
+ (p: SHACLPropertyShape) => p.name === "flaggedItems"
296
+ );
297
+ expect(flaggedProp).toBeDefined();
298
+ expect(flaggedProp!.getter).toBeDefined();
299
+ expect(flaggedProp!.getter).toContain("test://has_flagged");
300
+ expect(flaggedProp!.conformanceConditions).toBeDefined();
301
+ expect(flaggedProp!.conformanceConditions!.length).toBeGreaterThan(0);
302
+
303
+ // The custom getter-only relation should NOT be in the SHACL shape
304
+ const customProp = reconstructed.properties.find(
305
+ (p: SHACLPropertyShape) => p.name === "customItems"
306
+ );
307
+ expect(customProp).toBeUndefined();
308
+ });
309
+ });
310
+
311
+ // ============================================================================
312
+ // Ad4mModel API tests — getModelMetadata() relation fields
313
+ // ============================================================================
314
+
315
+ describe("Ad4mModel.getModelMetadata() relation filtering fields", () => {
316
+ it("should expose target thunk on relations with target", () => {
317
+ const metadata = ParentWithRelations.getModelMetadata();
318
+
319
+ expect(metadata.relations.flaggedItems).toBeDefined();
320
+ expect(metadata.relations.flaggedItems.target).toBeDefined();
321
+ expect(metadata.relations.flaggedItems.target!()).toBe(FlaggedTarget);
322
+
323
+ expect(metadata.relations.minimalItems.target).toBeDefined();
324
+ expect(metadata.relations.minimalItems.target!()).toBe(MinimalTarget);
325
+ });
326
+
327
+ it("should expose filter:false on opt-out relations", () => {
328
+ const metadata = ParentWithRelations.getModelMetadata();
329
+
330
+ // Default (not set) — filter should be undefined (treated as true)
331
+ expect(metadata.relations.flaggedItems.filter).toBeUndefined();
332
+
333
+ // Explicit opt-out
334
+ expect(metadata.relations.unfilteredItems.filter).toBe(false);
335
+ });
336
+
337
+ it("should expose explicit getter on relations with custom getter", () => {
338
+ const metadata = ParentWithRelations.getModelMetadata();
339
+
340
+ // Explicit getter provided via decorator (getter-only, no predicate)
341
+ expect(metadata.relations.customItems.getter).toBe(
342
+ "(<-link[WHERE predicate = 'test://custom'].in.uri)"
343
+ );
344
+ expect(metadata.relations.customItems.predicate).toBe("");
345
+
346
+ // Auto-generated getter is NOT in decorator metadata (it's in the SHACL shape)
347
+ expect(metadata.relations.flaggedItems.getter).toBeUndefined();
348
+ });
349
+
350
+ it("should produce matching getter between getModelMetadata target and SHACL shape", () => {
351
+ // This verifies that the auto-generated getter in the SHACL shape
352
+ // is consistent with what buildConformanceFilter produces from the
353
+ // target model that getModelMetadata() exposes.
354
+ const metadata = ParentWithRelations.getModelMetadata();
355
+ const { shape } = (ParentWithRelations as any).generateSHACL();
356
+
357
+ const flaggedRel = metadata.relations.flaggedItems;
358
+ const TargetClass = flaggedRel.target!();
359
+
360
+ // Build filter from the target class exposed by metadata
361
+ const filter = buildConformanceFilter(flaggedRel.predicate, TargetClass);
362
+ expect(filter).toBeDefined();
363
+
364
+ // Should match what generateSHACL() stored on the property shape
365
+ const shapeProp = shape.properties.find(
366
+ (p: SHACLPropertyShape) => p.name === "flaggedItems"
367
+ );
368
+ expect(shapeProp!.getter).toBe(filter!.getter);
369
+ expect(shapeProp!.conformanceConditions).toEqual(filter!.conformanceConditions);
370
+ });
371
+
372
+ it("should have correct direction for forward relations", () => {
373
+ const metadata = ParentWithRelations.getModelMetadata();
374
+
375
+ expect(metadata.relations.flaggedItems.direction).toBe("forward");
376
+ expect(metadata.relations.minimalItems.direction).toBe("forward");
377
+ expect(metadata.relations.unfilteredItems.direction).toBe("forward");
378
+ expect(metadata.relations.customItems.direction).toBe("forward");
379
+ });
380
+ });
381
+
382
+ // ============================================================================
383
+ // Validation tests
384
+ // ============================================================================
385
+
386
+ describe("Relation decorator validation", () => {
387
+ it("should throw if both getter and target are provided", () => {
388
+ expect(() => {
389
+ @Model({ name: "InvalidGetterTarget" })
390
+ class _Invalid extends Ad4mModel {
391
+ @HasMany({
392
+ getter: "(<-link.in.uri)",
393
+ target: () => FlaggedTarget,
394
+ })
395
+ items: string[] = [];
396
+ }
397
+ }).toThrow(/getter.*target.*mutually exclusive/i);
398
+ });
399
+
400
+ it("should throw if both getter and through are provided", () => {
401
+ expect(() => {
402
+ @Model({ name: "InvalidGetterThrough" })
403
+ class _Invalid extends Ad4mModel {
404
+ @HasMany({
405
+ getter: "(<-link.in.uri)",
406
+ through: "test://pred",
407
+ })
408
+ items: string[] = [];
409
+ }
410
+ }).toThrow(/getter.*through.*mutually exclusive/i);
411
+ });
412
+
413
+ it("should allow getter-only relations (read-only)", () => {
414
+ expect(() => {
415
+ @Model({ name: "ValidGetterOnly" })
416
+ class _Valid extends Ad4mModel {
417
+ @HasMany({
418
+ getter: "(<-link.in.uri)",
419
+ })
420
+ items: string[] = [];
421
+ }
422
+ }).not.toThrow();
423
+ });
424
+ });
425
+
426
+ // ============================================================================
427
+ // Default through tests
428
+ // ============================================================================
429
+
430
+ describe("Default through predicate", () => {
431
+ it("should default through to ad4m://has_child when omitted", () => {
432
+ @Model({ name: "DefaultThrough" })
433
+ class DefaultThrough extends Ad4mModel {
434
+ @HasMany(() => FlaggedTarget)
435
+ items: string[] = [];
436
+ }
437
+
438
+ const { shape } = (DefaultThrough as any).generateSHACL();
439
+ const itemsProp = shape.properties.find(
440
+ (p: SHACLPropertyShape) => p.name === "items"
441
+ );
442
+ expect(itemsProp).toBeDefined();
443
+ expect(itemsProp!.path).toBe("ad4m://has_child");
444
+ });
445
+
446
+ it("should use explicit through when provided", () => {
447
+ @Model({ name: "ExplicitThrough" })
448
+ class ExplicitThrough extends Ad4mModel {
449
+ @HasMany(() => FlaggedTarget, { through: "test://custom_pred" })
450
+ items: string[] = [];
451
+ }
452
+
453
+ const { shape } = (ExplicitThrough as any).generateSHACL();
454
+ const itemsProp = shape.properties.find(
455
+ (p: SHACLPropertyShape) => p.name === "items"
456
+ );
457
+ expect(itemsProp).toBeDefined();
458
+ expect(itemsProp!.path).toBe("test://custom_pred");
459
+ });
460
+ });
461
+
462
+ // ============================================================================
463
+ // sh:class tests
464
+ // ============================================================================
465
+
466
+ describe("sh:class target shape reference", () => {
467
+ it("should set sh:class on relation shapes when target is provided", () => {
468
+ const { shape } = (ParentWithRelations as any).generateSHACL();
469
+
470
+ const flaggedProp = shape.properties.find(
471
+ (p: SHACLPropertyShape) => p.name === "flaggedItems"
472
+ );
473
+ expect(flaggedProp).toBeDefined();
474
+ expect(flaggedProp!.class).toBeDefined();
475
+ expect(flaggedProp!.class).toContain("FlaggedTarget");
476
+ expect(flaggedProp!.class).toContain("Shape");
477
+ });
478
+
479
+ it("should set sh:class even when filter is false", () => {
480
+ const { shape } = (ParentWithRelations as any).generateSHACL();
481
+
482
+ const unfilteredProp = shape.properties.find(
483
+ (p: SHACLPropertyShape) => p.name === "unfilteredItems"
484
+ );
485
+ expect(unfilteredProp).toBeDefined();
486
+ // filter:false suppresses getter but NOT sh:class
487
+ expect(unfilteredProp!.getter).toBeUndefined();
488
+ expect(unfilteredProp!.class).toBeDefined();
489
+ expect(unfilteredProp!.class).toContain("FlaggedTarget");
490
+ });
491
+
492
+ it("should NOT set sh:class when no target is provided", () => {
493
+ @Model({ name: "NoTargetParent" })
494
+ class NoTargetParent extends Ad4mModel {
495
+ @HasMany({ through: "test://pred" })
496
+ items: string[] = [];
497
+ }
498
+
499
+ const { shape } = (NoTargetParent as any).generateSHACL();
500
+ const itemsProp = shape.properties.find(
501
+ (p: SHACLPropertyShape) => p.name === "items"
502
+ );
503
+ expect(itemsProp).toBeDefined();
504
+ expect(itemsProp!.class).toBeUndefined();
505
+ });
506
+
507
+ it("should round-trip sh:class through toLinks/fromLinks", () => {
508
+ const shape = new SHACLShape("test://Parent");
509
+ const prop: SHACLPropertyShape = {
510
+ name: "items",
511
+ path: "test://has_items",
512
+ nodeKind: "IRI",
513
+ class: "test://TargetShape",
514
+ };
515
+ shape.addProperty(prop);
516
+
517
+ const links = shape.toLinks();
518
+ const classLink = links.find(l => l.predicate === "sh://class");
519
+ expect(classLink).toBeDefined();
520
+ expect(classLink!.target).toBe("test://TargetShape");
521
+
522
+ const reconstructed = SHACLShape.fromLinks(
523
+ links.map((l: any) => ({ source: l.source, predicate: l.predicate, target: l.target })),
524
+ shape.nodeShapeUri
525
+ );
526
+ expect(reconstructed.properties[0].class).toBe("test://TargetShape");
527
+ });
528
+
529
+ it("should round-trip sh:class through toJSON/fromJSON", () => {
530
+ const shape = new SHACLShape("test://Parent");
531
+ const prop: SHACLPropertyShape = {
532
+ name: "items",
533
+ path: "test://has_items",
534
+ class: "test://TargetShape",
535
+ };
536
+ shape.addProperty(prop);
537
+
538
+ const json = shape.toJSON();
539
+ const reconstructed = SHACLShape.fromJSON(json);
540
+ expect(reconstructed.properties[0].class).toBe("test://TargetShape");
541
+ });
542
+ });
543
+
544
+ // ============================================================================
545
+ // where clause tests
546
+ // ============================================================================
547
+
548
+ describe("where clause validation", () => {
549
+ it("should throw if both where and getter are provided", () => {
550
+ expect(() => {
551
+ @Model({ name: "InvalidWhereGetter" })
552
+ class _Invalid extends Ad4mModel {
553
+ @HasMany({
554
+ where: { status: "active" },
555
+ getter: "(<-link.in.uri)",
556
+ })
557
+ items: string[] = [];
558
+ }
559
+ }).toThrow(/where.*getter.*mutually exclusive/i);
560
+ });
561
+
562
+ it("should throw if both where and filter:false are provided", () => {
563
+ expect(() => {
564
+ @Model({ name: "InvalidWhereFilterFalse" })
565
+ class _Invalid extends Ad4mModel {
566
+ @HasMany(() => FlaggedTarget, {
567
+ through: "test://pred",
568
+ where: { status: "active" },
569
+ filter: false,
570
+ })
571
+ items: string[] = [];
572
+ }
573
+ }).toThrow(/where.*filter.*contradictory/i);
574
+ });
575
+ });
576
+
577
+ describe("where clause compilation", () => {
578
+ it("should compile where clause to SurrealQL getter", () => {
579
+ @Model({ name: "WhereTarget" })
580
+ class WhereTarget extends Ad4mModel {
581
+ @Property({ through: "test://status", required: true })
582
+ status: string = "";
583
+ }
584
+
585
+ @Model({ name: "WhereParent" })
586
+ class WhereParent extends Ad4mModel {
587
+ @HasMany(() => WhereTarget, {
588
+ through: "test://has_item",
589
+ where: { status: "active" },
590
+ })
591
+ items: string[] = [];
592
+ }
593
+
594
+ const { shape } = (WhereParent as any).generateSHACL();
595
+ const itemsProp = shape.properties.find(
596
+ (p: SHACLPropertyShape) => p.name === "items"
597
+ );
598
+ expect(itemsProp).toBeDefined();
599
+ expect(itemsProp!.getter).toBeDefined();
600
+ expect(itemsProp!.getter).toContain("test://has_item");
601
+ expect(itemsProp!.getter).toContain("test://status");
602
+ expect(itemsProp!.getter).toContain("active");
603
+ });
604
+
605
+ it("should use where clause instead of auto-derived conformance when both target and where are set", () => {
606
+ @Model({ name: "WhereOverrideParent" })
607
+ class WhereOverrideParent extends Ad4mModel {
608
+ @HasMany(() => FlaggedTarget, {
609
+ through: "test://has_flagged",
610
+ where: { name: "specific" },
611
+ })
612
+ flaggedItems: string[] = [];
613
+ }
614
+
615
+ const { shape } = (WhereOverrideParent as any).generateSHACL();
616
+ const prop = shape.properties.find(
617
+ (p: SHACLPropertyShape) => p.name === "flaggedItems"
618
+ );
619
+ expect(prop).toBeDefined();
620
+ expect(prop!.getter).toBeDefined();
621
+ // Should contain the where condition (name = specific)
622
+ expect(prop!.getter).toContain("test://name");
623
+ expect(prop!.getter).toContain("specific");
624
+ // Should NOT contain the auto-derived flag condition
625
+ // (where overrides auto-conformance)
626
+ expect(prop!.getter).not.toContain("test://flagged_type");
627
+ });
628
+
629
+ it("should compile where clause with not operator", () => {
630
+ @Model({ name: "WhereNotTarget" })
631
+ class WhereNotTarget extends Ad4mModel {
632
+ @Property({ through: "test://status" })
633
+ status: string = "";
634
+ }
635
+
636
+ @Model({ name: "WhereNotParent" })
637
+ class WhereNotParent extends Ad4mModel {
638
+ @HasMany(() => WhereNotTarget, {
639
+ through: "test://has_item",
640
+ where: { status: { not: "archived" } },
641
+ })
642
+ items: string[] = [];
643
+ }
644
+
645
+ const { shape } = (WhereNotParent as any).generateSHACL();
646
+ const prop = shape.properties.find(
647
+ (p: SHACLPropertyShape) => p.name === "items"
648
+ );
649
+ expect(prop).toBeDefined();
650
+ expect(prop!.getter).toBeDefined();
651
+ // Should contain a negation condition
652
+ expect(prop!.getter).toContain("test://status");
653
+ expect(prop!.getter).toContain("archived");
654
+ });
655
+
656
+ it("should compile where clause with array values (IN)", () => {
657
+ @Model({ name: "WhereArrayTarget" })
658
+ class WhereArrayTarget extends Ad4mModel {
659
+ @Property({ through: "test://category" })
660
+ category: string = "";
661
+ }
662
+
663
+ @Model({ name: "WhereArrayParent" })
664
+ class WhereArrayParent extends Ad4mModel {
665
+ @HasMany(() => WhereArrayTarget, {
666
+ through: "test://has_item",
667
+ where: { category: ["food", "drink"] },
668
+ })
669
+ items: string[] = [];
670
+ }
671
+
672
+ const { shape } = (WhereArrayParent as any).generateSHACL();
673
+ const prop = shape.properties.find(
674
+ (p: SHACLPropertyShape) => p.name === "items"
675
+ );
676
+ expect(prop).toBeDefined();
677
+ expect(prop!.getter).toBeDefined();
678
+ expect(prop!.getter).toContain("test://category");
679
+ expect(prop!.getter).toContain("food");
680
+ expect(prop!.getter).toContain("drink");
681
+ });
682
+
683
+ it("should compile where clause without target using raw predicate URIs", () => {
684
+ @Model({ name: "WhereNoTargetParent" })
685
+ class WhereNoTargetParent extends Ad4mModel {
686
+ @HasMany({
687
+ through: "test://has_item",
688
+ where: { "test://status": "active" },
689
+ })
690
+ items: string[] = [];
691
+ }
692
+
693
+ const { shape } = (WhereNoTargetParent as any).generateSHACL();
694
+ const prop = shape.properties.find(
695
+ (p: SHACLPropertyShape) => p.name === "items"
696
+ );
697
+ expect(prop).toBeDefined();
698
+ expect(prop!.getter).toBeDefined();
699
+ expect(prop!.getter).toContain("test://status");
700
+ expect(prop!.getter).toContain("active");
701
+ });
702
+
703
+ it("should set both where-compiled getter and sh:class when target is present", () => {
704
+ @Model({ name: "WherePlusClassParent" })
705
+ class WherePlusClassParent extends Ad4mModel {
706
+ @HasMany(() => FlaggedTarget, {
707
+ through: "test://has_flagged",
708
+ where: { name: "specific" },
709
+ })
710
+ flaggedItems: string[] = [];
711
+ }
712
+
713
+ const { shape } = (WherePlusClassParent as any).generateSHACL();
714
+ const prop = shape.properties.find(
715
+ (p: SHACLPropertyShape) => p.name === "flaggedItems"
716
+ );
717
+ expect(prop).toBeDefined();
718
+ // where-compiled getter
719
+ expect(prop!.getter).toBeDefined();
720
+ expect(prop!.getter).toContain("specific");
721
+ // sh:class still set (target is present)
722
+ expect(prop!.class).toBeDefined();
723
+ expect(prop!.class).toContain("FlaggedTarget");
724
+ });
725
+
726
+ it("should round-trip where-compiled getter through toLinks/fromLinks", () => {
727
+ @Model({ name: "WhereRoundTripTarget" })
728
+ class WhereRoundTripTarget extends Ad4mModel {
729
+ @Property({ through: "test://status", required: true })
730
+ status: string = "";
731
+ }
732
+
733
+ @Model({ name: "WhereRoundTripParent" })
734
+ class WhereRoundTripParent extends Ad4mModel {
735
+ @HasMany(() => WhereRoundTripTarget, {
736
+ through: "test://has_item",
737
+ where: { status: "active" },
738
+ })
739
+ items: string[] = [];
740
+ }
741
+
742
+ const { shape } = (WhereRoundTripParent as any).generateSHACL();
743
+ const links = shape.toLinks();
744
+ const reconstructed = SHACLShape.fromLinks(
745
+ links.map((l: any) => ({ source: l.source, predicate: l.predicate, target: l.target })),
746
+ shape.nodeShapeUri
747
+ );
748
+
749
+ const prop = reconstructed.properties.find(
750
+ (p: SHACLPropertyShape) => p.name === "items"
751
+ );
752
+ expect(prop).toBeDefined();
753
+ expect(prop!.getter).toBeDefined();
754
+ expect(prop!.getter).toContain("test://status");
755
+ expect(prop!.getter).toContain("active");
756
+ });
757
+ });
758
+
759
+ // ============================================================================
760
+ // compileWhereClause() unit tests
761
+ // ============================================================================
762
+
763
+ describe("compileWhereClause()", () => {
764
+ it("should compile simple equality to SurrealQL condition", () => {
765
+ const conditions = compileWhereClause(
766
+ { status: "active" },
767
+ { properties: { status: { name: "status", predicate: "test://status", required: false, readOnly: false } }, relations: {}, className: "Test" }
768
+ );
769
+ expect(conditions).toHaveLength(1);
770
+ expect(conditions[0]).toContain("test://status");
771
+ expect(conditions[0]).toContain("active");
772
+ });
773
+
774
+ it("should compile not operator", () => {
775
+ const conditions = compileWhereClause(
776
+ { status: { not: "archived" } },
777
+ { properties: { status: { name: "status", predicate: "test://status", required: false, readOnly: false } }, relations: {}, className: "Test" }
778
+ );
779
+ expect(conditions).toHaveLength(1);
780
+ expect(conditions[0]).toContain("= 0"); // negation
781
+ expect(conditions[0]).toContain("archived");
782
+ });
783
+
784
+ it("should compile array values to IN clause", () => {
785
+ const conditions = compileWhereClause(
786
+ { category: ["food", "drink"] },
787
+ { properties: { category: { name: "category", predicate: "test://category", required: false, readOnly: false } }, relations: {}, className: "Test" }
788
+ );
789
+ expect(conditions).toHaveLength(1);
790
+ expect(conditions[0]).toContain("food");
791
+ expect(conditions[0]).toContain("drink");
792
+ expect(conditions[0]).toContain("IN");
793
+ });
794
+
795
+ it("should fall back to raw predicate URIs when metadata is null", () => {
796
+ const conditions = compileWhereClause(
797
+ { "test://raw_pred": "value" },
798
+ null as any
799
+ );
800
+ expect(conditions).toHaveLength(1);
801
+ expect(conditions[0]).toContain("test://raw_pred");
802
+ expect(conditions[0]).toContain("value");
803
+ });
804
+ });