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