From 50b412ab3023d15e0b85033e8c61b647de241698 Mon Sep 17 00:00:00 2001
From: MarcosPierasNL <pieras.marcos@gmail.com>
Date: Thu, 6 Mar 2025 17:02:58 +0100
Subject: [PATCH] test: first version

---
 src/readers/diffCheck.ts                   |  8 ++-
 src/readers/testInsights/diffCheck.test.ts | 64 ++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 src/readers/testInsights/diffCheck.test.ts

diff --git a/src/readers/diffCheck.ts b/src/readers/diffCheck.ts
index ba3dd09..638bd5e 100644
--- a/src/readers/diffCheck.ts
+++ b/src/readers/diffCheck.ts
@@ -5,6 +5,10 @@ import type { GraphQueryResultMetaFromBackend } from 'ts-common/src/model/webSoc
 import { ums } from '../variables';
 import type { InsightModel } from 'ts-common';
 
+export const compareHashedQueryResults = (previousHash: string | null, currentHash: string): boolean => {
+  return !previousHash || !hashIsEqual(currentHash, previousHash);
+};
+
 export const diffCheck = async (
   insight: InsightModel,
   ss: SaveState,
@@ -20,7 +24,9 @@ export const diffCheck = async (
   });
 
   log.debug('Comparing hash values from current and previous query');
-  const changed = !previousQueryResult || !hashIsEqual(queryResultHash, previousQueryResult);
+
+  const changed = compareHashedQueryResults(previousQueryResult, queryResultHash);
+
   insight.status ||= changed;
   log.debug('Updated node and edge ids in SaveState');
 
diff --git a/src/readers/testInsights/diffCheck.test.ts b/src/readers/testInsights/diffCheck.test.ts
new file mode 100644
index 0000000..4c56a3e
--- /dev/null
+++ b/src/readers/testInsights/diffCheck.test.ts
@@ -0,0 +1,64 @@
+import { expect, test, describe, it } from 'bun:test';
+import type { GraphQueryResultMetaFromBackend } from 'ts-common';
+import { hashDictionary, hashIsEqual } from '../../utils/hashing';
+import { compareHashedQueryResults } from '../diffCheck';
+
+describe('Hash Comparison Tests', () => {
+  it('should detect different hashes for different graph structures', () => {
+    // First query result
+    const query1: GraphQueryResultMetaFromBackend = {
+      nodes: [{ _id: 'node1' }, { _id: 'node2' }],
+      edges: [{ _id: 'edge1' }],
+    } as GraphQueryResultMetaFromBackend;
+
+    // Second query result with different structure
+    const query2: GraphQueryResultMetaFromBackend = {
+      nodes: [{ _id: 'node1' }, { _id: 'node2' }, { _id: 'node3' }],
+      edges: [{ _id: 'edge1' }, { _id: 'edge2' }],
+    } as GraphQueryResultMetaFromBackend;
+
+    const hash1 = hashDictionary({
+      nodes: query1.nodes.map(node => node._id),
+      edges: query1.edges.map(edge => edge._id),
+    });
+
+    const hash2 = hashDictionary({
+      nodes: query2.nodes.map(node => node._id),
+      edges: query2.edges.map(edge => edge._id),
+    });
+
+    // Test direct hash comparison
+    expect(hashIsEqual(hash1, hash2)).toBe(false);
+
+    // Test using compareHashedQueryResults
+    expect(compareHashedQueryResults(hash1, hash2)).toBe(true);
+  });
+
+  it('should detect identical hashes for same graph structures', () => {
+    const query1 = {
+      nodes: [{ _id: 'node1' }, { _id: 'node2' }],
+      edges: [{ _id: 'edge1' }],
+    } as GraphQueryResultMetaFromBackend;
+
+    const query2 = {
+      nodes: [{ _id: 'node1' }, { _id: 'node2' }],
+      edges: [{ _id: 'edge1' }],
+    } as GraphQueryResultMetaFromBackend;
+
+    const hash1 = hashDictionary({
+      nodes: query1.nodes.map(node => node._id),
+      edges: query1.edges.map(edge => edge._id),
+    });
+
+    const hash2 = hashDictionary({
+      nodes: query2.nodes.map(node => node._id),
+      edges: query2.edges.map(edge => edge._id),
+    });
+
+    // Test direct hash comparison
+    expect(hashIsEqual(hash1, hash2)).toBe(true);
+
+    // Test using compareHashedQueryResults
+    expect(compareHashedQueryResults(hash1, hash2)).toBe(false);
+  });
+});
-- 
GitLab