@coasys/ad4m 0.13.0-test-4 → 0.13.0-test-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.
@@ -1059,7 +1059,6 @@ describe("formatSPARQLValue", () => {
1059
1059
  // Comprehensive SPARQL migration unit tests
1060
1060
  // ──────────────────────────────────────────────────────────
1061
1061
 
1062
- import { matchesCondition } from "./hydration";
1063
1062
  import { Literal } from "../Literal";
1064
1063
 
1065
1064
  // Helper: create literal URLs using JSON encoding (which handles all types including booleans)
@@ -1069,102 +1068,6 @@ function literalUrl(value: any): string {
1069
1068
  return `literal:json:${encodeURIComponent(JSON.stringify(value))}`;
1070
1069
  }
1071
1070
 
1072
- describe("matchesCondition()", () => {
1073
- // Simple equality
1074
- it("simple string equality — match", () => {
1075
- expect(matchesCondition("hello", "hello")).toBe(true);
1076
- });
1077
- it("simple string equality — mismatch", () => {
1078
- expect(matchesCondition("hello", "world")).toBe(false);
1079
- });
1080
- it("simple boolean equality — true", () => {
1081
- expect(matchesCondition(true, true)).toBe(true);
1082
- });
1083
- it("simple boolean equality — false", () => {
1084
- expect(matchesCondition(false, false)).toBe(true);
1085
- });
1086
- it("boolean mismatch", () => {
1087
- expect(matchesCondition(true, false)).toBe(false);
1088
- });
1089
- it("simple number equality", () => {
1090
- expect(matchesCondition(42, 42)).toBe(true);
1091
- });
1092
- it("number mismatch", () => {
1093
- expect(matchesCondition(42, 99)).toBe(false);
1094
- });
1095
-
1096
- // Array IN clause
1097
- it("array IN clause — value present", () => {
1098
- expect(matchesCondition("a", ["a", "b", "c"])).toBe(true);
1099
- });
1100
- it("array IN clause — value absent", () => {
1101
- expect(matchesCondition("d", ["a", "b", "c"])).toBe(false);
1102
- });
1103
-
1104
- // NOT operator
1105
- it("not operator with string", () => {
1106
- expect(matchesCondition("bob", { not: "alice" })).toBe(true);
1107
- expect(matchesCondition("alice", { not: "alice" })).toBe(false);
1108
- });
1109
- it("not operator with array (NOT IN)", () => {
1110
- expect(matchesCondition("a", { not: ["b", "c"] })).toBe(true);
1111
- expect(matchesCondition("b", { not: ["b", "c"] })).toBe(false);
1112
- });
1113
-
1114
- // Comparison operators
1115
- it("gt", () => {
1116
- expect(matchesCondition(10, { gt: 5 })).toBe(true);
1117
- expect(matchesCondition(5, { gt: 5 })).toBe(false);
1118
- });
1119
- it("gte", () => {
1120
- expect(matchesCondition(5, { gte: 5 })).toBe(true);
1121
- expect(matchesCondition(4, { gte: 5 })).toBe(false);
1122
- });
1123
- it("lt", () => {
1124
- expect(matchesCondition(3, { lt: 5 })).toBe(true);
1125
- expect(matchesCondition(5, { lt: 5 })).toBe(false);
1126
- });
1127
- it("lte", () => {
1128
- expect(matchesCondition(5, { lte: 5 })).toBe(true);
1129
- expect(matchesCondition(6, { lte: 5 })).toBe(false);
1130
- });
1131
- it("combined gt + lt (range)", () => {
1132
- expect(matchesCondition(5, { gt: 3, lt: 7 })).toBe(true);
1133
- expect(matchesCondition(3, { gt: 3, lt: 7 })).toBe(false);
1134
- expect(matchesCondition(7, { gt: 3, lt: 7 })).toBe(false);
1135
- });
1136
-
1137
- // Between
1138
- it("between — inclusive range", () => {
1139
- expect(matchesCondition(5, { between: [3, 7] })).toBe(true);
1140
- expect(matchesCondition(3, { between: [3, 7] })).toBe(true);
1141
- expect(matchesCondition(7, { between: [3, 7] })).toBe(true);
1142
- expect(matchesCondition(2, { between: [3, 7] })).toBe(false);
1143
- expect(matchesCondition(8, { between: [3, 7] })).toBe(false);
1144
- });
1145
-
1146
- // Contains
1147
- it("contains with string", () => {
1148
- expect(matchesCondition("hello world", { contains: "world" })).toBe(true);
1149
- expect(matchesCondition("hello world", { contains: "xyz" })).toBe(false);
1150
- });
1151
- it("contains with array", () => {
1152
- expect(matchesCondition(["a", "b", "c"], { contains: "b" })).toBe(true);
1153
- expect(matchesCondition(["a", "b", "c"], { contains: "d" })).toBe(false);
1154
- });
1155
-
1156
- // Edge cases
1157
- it("returns false for undefined value with equality check", () => {
1158
- expect(matchesCondition(undefined, "hello")).toBe(false);
1159
- });
1160
- it("returns false for empty string when condition is non-empty", () => {
1161
- expect(matchesCondition("", "Recipe 2")).toBe(false);
1162
- });
1163
- it("returns true for undefined with not operator", () => {
1164
- expect(matchesCondition(undefined, { not: "hello" })).toBe(true);
1165
- });
1166
- });
1167
-
1168
1071
  describe("buildSPARQLQuery edge cases", () => {
1169
1072
  @Model({ name: "FlagModel" })
1170
1073
  class FlagModel extends Ad4mModel {
@@ -1400,192 +1303,7 @@ describe("Lightweight fingerprint optimization", () => {
1400
1303
  });
1401
1304
  });
1402
1305
 
1403
- // ── Native SPARQL getter evaluation ──────────────────────────────────────────
1404
- describe("Native SPARQL getter evaluation", () => {
1405
- const { evaluateCustomGettersForInstance } = require("./hydration");
1406
-
1407
- const mockPerspective = {
1408
- getLinks: jest.fn().mockResolvedValue([]),
1409
- querySparql: jest.fn(),
1410
- get: jest.fn().mockResolvedValue([]),
1411
- };
1412
-
1413
- beforeEach(() => {
1414
- jest.clearAllMocks();
1415
- });
1416
1306
 
1417
- it("should execute SELECT getter directly via querySparql", async () => {
1418
- const metadata = {
1419
- properties: {
1420
- replyingTo: {
1421
- predicate: "flux://has_reply",
1422
- getter: 'SELECT ?target WHERE { ?target <flux://has_reply> ?source . } LIMIT 1',
1423
- type: "string",
1424
- }
1425
- },
1426
- relations: {},
1427
- };
1428
-
1429
- mockPerspective.querySparql.mockResolvedValue([
1430
- { target: { value: "literal:string:msg123" } }
1431
- ]);
1432
-
1433
- const instance = { id: "test://msg1", replyingTo: undefined };
1434
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1435
-
1436
- expect(mockPerspective.querySparql).toHaveBeenCalledWith(
1437
- expect.stringContaining("SELECT ?target WHERE")
1438
- );
1439
- expect(instance.replyingTo).toBe("literal:string:msg123");
1440
- });
1441
-
1442
- it("should execute ASK getter and return boolean", async () => {
1443
- const metadata = {
1444
- properties: {
1445
- isPopular: {
1446
- predicate: "flux://is_popular",
1447
- getter: 'ASK WHERE { SELECT (COUNT(DISTINCT ?reactor) AS ?count) WHERE { ?reactor <flux://reaction> ?source . } HAVING(?count > 5) }',
1448
- type: "boolean",
1449
- readOnly: true,
1450
- }
1451
- },
1452
- relations: {},
1453
- };
1454
-
1455
- mockPerspective.querySparql.mockResolvedValue(true);
1456
-
1457
- const instance = { id: "test://msg1", isPopular: false };
1458
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1459
-
1460
- expect(mockPerspective.querySparql).toHaveBeenCalledWith(
1461
- expect.stringContaining("ASK WHERE")
1462
- );
1463
- expect(instance.isPopular).toBe(true);
1464
- });
1465
-
1466
- it("should replace ?source with instance ID in SPARQL getter", async () => {
1467
- const metadata = {
1468
- properties: {
1469
- author: {
1470
- predicate: "test://author",
1471
- getter: 'SELECT ?target WHERE { ?source <test://author> ?target . }',
1472
- type: "string",
1473
- }
1474
- },
1475
- relations: {},
1476
- };
1477
-
1478
- mockPerspective.querySparql.mockResolvedValue([
1479
- { target: { value: "did:key:abc123" } }
1480
- ]);
1481
-
1482
- const instance = { id: "test://post1", author: undefined };
1483
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1484
-
1485
- expect(mockPerspective.querySparql).toHaveBeenCalledWith(
1486
- expect.stringContaining("<test://post1>")
1487
- );
1488
- expect(mockPerspective.querySparql).not.toHaveBeenCalledWith(
1489
- expect.stringContaining("?source")
1490
- );
1491
- });
1492
-
1493
- it("should handle empty SPARQL results gracefully", async () => {
1494
- const metadata = {
1495
- properties: {
1496
- replyingTo: {
1497
- predicate: "flux://has_reply",
1498
- getter: 'SELECT ?target WHERE { ?target <flux://has_reply> ?source . } LIMIT 1',
1499
- type: "string",
1500
- }
1501
- },
1502
- relations: {},
1503
- };
1504
-
1505
- mockPerspective.querySparql.mockResolvedValue([]);
1506
-
1507
- const instance = { id: "test://msg1", replyingTo: "original" };
1508
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1509
-
1510
- // Should not overwrite with undefined
1511
- expect(instance.replyingTo).toBe("original");
1512
- });
1513
-
1514
- it("should handle SPARQL getter errors without crashing", async () => {
1515
- const metadata = {
1516
- properties: {
1517
- broken: {
1518
- predicate: "test://broken",
1519
- getter: 'SELECT ?target WHERE { INVALID SPARQL }',
1520
- type: "string",
1521
- }
1522
- },
1523
- relations: {},
1524
- };
1525
-
1526
- mockPerspective.querySparql.mockRejectedValue(new Error("SPARQL parse error"));
1527
-
1528
- const instance = { id: "test://msg1", broken: undefined };
1529
- const consoleSpy = jest.spyOn(console, "warn").mockImplementation();
1530
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1531
- consoleSpy.mockRestore();
1532
-
1533
- // Should not throw, should warn
1534
- expect(instance.broken).toBeUndefined();
1535
- });
1536
-
1537
- it("should execute SPARQL getter for relations", async () => {
1538
- const metadata = {
1539
- properties: {},
1540
- relations: {
1541
- tags: {
1542
- predicate: "test://has_tag",
1543
- getter: 'SELECT ?target WHERE { ?source <test://has_tag> ?target . }',
1544
- direction: "forward",
1545
- }
1546
- },
1547
- };
1548
-
1549
- mockPerspective.querySparql.mockResolvedValue([
1550
- { target: { value: "tag:a" } },
1551
- { target: { value: "tag:b" } },
1552
- ]);
1553
-
1554
- const instance = { id: "test://post1", tags: [] };
1555
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1556
-
1557
- expect(instance.tags).toEqual(["tag:a", "tag:b"]);
1558
- });
1559
-
1560
- it("should warn for legacy SurrealDB-style getter syntax instead of converting", async () => {
1561
- const metadata = {
1562
- properties: {
1563
- legacy: {
1564
- predicate: "test://legacy",
1565
- getter: "(<-link[WHERE predicate = 'test://legacy'].in.uri)[0]",
1566
- type: "string",
1567
- }
1568
- },
1569
- relations: {},
1570
- };
1571
-
1572
- const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
1573
-
1574
- const instance = { id: "test://msg1", legacy: undefined };
1575
- await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
1576
-
1577
- // Should NOT call querySparql — legacy getters are no longer converted
1578
- expect(mockPerspective.querySparql).not.toHaveBeenCalled();
1579
- // Should warn about unsupported syntax
1580
- expect(warnSpy).toHaveBeenCalledWith(
1581
- expect.stringContaining("Unsupported legacy getter syntax")
1582
- );
1583
- // Value should remain undefined
1584
- expect(instance.legacy).toBeUndefined();
1585
-
1586
- warnSpy.mockRestore();
1587
- });
1588
- });
1589
1307
 
1590
1308
  // ── Subscribe callback timing ──────────────────────────────────────────
1591
1309
  describe("ModelQueryBuilder subscribe callback timing", () => {
@@ -1961,10 +1679,10 @@ describe("Lazy Conformance Filters", () => {
1961
1679
  });
1962
1680
 
1963
1681
  // ──────────────────────────────────────────────────────────
1964
- // deepQuery opt-in — getter evaluation on collection queries
1682
+ // deepQuery — getter evaluation on collection queries
1965
1683
  // ──────────────────────────────────────────────────────────
1966
1684
 
1967
- describe("deepQuery opt-in — getter evaluation", () => {
1685
+ describe("deepQuery — getter evaluation", () => {
1968
1686
  @Model({ name: "DeepQueryTestMessage" })
1969
1687
  class DeepQueryTestMessage extends Ad4mModel {
1970
1688
  @Flag({ through: "flux://entry_type", value: "flux://message" })
@@ -2004,6 +1722,11 @@ describe("deepQuery opt-in — getter evaluation", () => {
2004
1722
  }
2005
1723
  return { instances: [], totalCount: 0 };
2006
1724
  }),
1725
+ evaluateGetters: jest.fn(async (className: string, instanceIds: string[], shapeJson: string, propertyNames?: string[]) => {
1726
+ // Return empty results by default — tests can override this mock
1727
+ const result: Record<string, Record<string, any>> = {};
1728
+ return result;
1729
+ }),
2007
1730
  querySparql: jest.fn(async (q: string) => {
2008
1731
  sparqlCalls.push(q);
2009
1732
  return [];
@@ -2016,6 +1739,8 @@ describe("deepQuery opt-in — getter evaluation", () => {
2016
1739
  beforeEach(() => {
2017
1740
  sparqlCalls = [];
2018
1741
  mockPerspective.querySparql.mockClear();
1742
+ mockPerspective.evaluateGetters.mockClear();
1743
+ mockPerspective.modelQuery.mockClear();
2019
1744
  mockPerspective.get.mockClear();
2020
1745
  });
2021
1746
 
@@ -2047,34 +1772,50 @@ describe("deepQuery opt-in — getter evaluation", () => {
2047
1772
  });
2048
1773
 
2049
1774
  describe("Ad4mModel.evaluateGetters()", () => {
2050
- it("resolves getters for a batch of instances", async () => {
1775
+ it("resolves getters for a batch of instances via single RPC", async () => {
2051
1776
  const instances = makeInstances(4);
2052
- sparqlCalls = [];
2053
1777
 
2054
1778
  await DeepQueryTestMessage.evaluateGetters(instances.slice(0, 2), mockPerspective, ["replyingTo"]);
2055
1779
 
2056
- const replyCalls = sparqlCalls.filter((q) => q.includes("flux://has_reply"));
2057
- expect(replyCalls.length).toBe(2);
2058
- const popularCalls = sparqlCalls.filter((q) => q.includes("flux://is_popular"));
2059
- expect(popularCalls.length).toBe(0);
1780
+ // Should make exactly 1 RPC call via perspective.evaluateGetters
1781
+ expect(mockPerspective.evaluateGetters).toHaveBeenCalledTimes(1);
1782
+ const [className, instanceIds, shapeJson, propertyNames] = mockPerspective.evaluateGetters.mock.calls[0];
1783
+ expect(className).toBe("DeepQueryTestMessage");
1784
+ expect(instanceIds).toEqual(["flux://msg-0", "flux://msg-1"]);
1785
+ expect(propertyNames).toEqual(["replyingTo"]);
1786
+ // No querySparql calls — all done in-process
1787
+ expect(sparqlCalls.length).toBe(0);
2060
1788
  });
2061
1789
 
2062
1790
  it("evaluates all getters when propertyNames is omitted", async () => {
2063
1791
  const instances = makeInstances(2);
2064
- sparqlCalls = [];
2065
1792
 
2066
1793
  await DeepQueryTestMessage.evaluateGetters(instances, mockPerspective);
2067
1794
 
2068
- const replyCalls = sparqlCalls.filter((q) => q.includes("flux://has_reply"));
2069
- const popularCalls = sparqlCalls.filter((q) => q.includes("flux://is_popular"));
2070
- expect(replyCalls.length).toBe(2);
2071
- expect(popularCalls.length).toBe(2);
1795
+ expect(mockPerspective.evaluateGetters).toHaveBeenCalledTimes(1);
1796
+ const [className, instanceIds, shapeJson, propertyNames] = mockPerspective.evaluateGetters.mock.calls[0];
1797
+ expect(className).toBe("DeepQueryTestMessage");
1798
+ expect(instanceIds).toEqual(["flux://msg-0", "flux://msg-1"]);
1799
+ expect(propertyNames).toBeUndefined();
1800
+ });
1801
+
1802
+ it("applies results to instances and syncs snapshots", async () => {
1803
+ const instances = makeInstances(2);
1804
+ // Simulate Rust returning getter results
1805
+ mockPerspective.evaluateGetters.mockResolvedValueOnce({
1806
+ "flux://msg-0": { replyingTo: "flux://msg-99" },
1807
+ "flux://msg-1": { replyingTo: "flux://msg-88" },
1808
+ });
1809
+
1810
+ await DeepQueryTestMessage.evaluateGetters(instances, mockPerspective, ["replyingTo"]);
1811
+
1812
+ expect((instances[0] as any).replyingTo).toBe("flux://msg-99");
1813
+ expect((instances[1] as any).replyingTo).toBe("flux://msg-88");
2072
1814
  });
2073
1815
 
2074
1816
  it("handles empty array gracefully", async () => {
2075
- sparqlCalls = [];
2076
1817
  await DeepQueryTestMessage.evaluateGetters([], mockPerspective);
2077
- expect(sparqlCalls.length).toBe(0);
1818
+ expect(mockPerspective.evaluateGetters).not.toHaveBeenCalled();
2078
1819
  });
2079
1820
  });
2080
1821
 
@@ -2084,6 +1825,26 @@ describe("deepQuery opt-in — getter evaluation", () => {
2084
1825
  (builder as any).deepQuery();
2085
1826
  expect((builder as any).queryParams.deepQuery).toBe(true);
2086
1827
  });
1828
+
1829
+ it("deepQuery defaults to true when not explicitly set", async () => {
1830
+ mockPerspective.modelQuery.mockResolvedValueOnce({ instances: [], totalCount: 0 });
1831
+ await DeepQueryTestMessage.query(mockPerspective).get();
1832
+
1833
+ expect(mockPerspective.modelQuery).toHaveBeenCalledTimes(1);
1834
+ const [, queryJson] = mockPerspective.modelQuery.mock.calls[0];
1835
+ const qi = JSON.parse(queryJson);
1836
+ expect(qi.deepQuery).toBe(true);
1837
+ });
1838
+
1839
+ it("deepQuery can be explicitly set to false", async () => {
1840
+ mockPerspective.modelQuery.mockResolvedValueOnce({ instances: [], totalCount: 0 });
1841
+ await DeepQueryTestMessage.query(mockPerspective).deepQuery(false).get();
1842
+
1843
+ expect(mockPerspective.modelQuery).toHaveBeenCalledTimes(1);
1844
+ const [, queryJson] = mockPerspective.modelQuery.mock.calls[0];
1845
+ const qi = JSON.parse(queryJson);
1846
+ expect(qi.deepQuery).toBe(false);
1847
+ });
2087
1848
  });
2088
1849
  });
2089
1850