OverviewFilesCommitsIssuesDocsReleasesPeers

keet-daemon / test / test-extract-clusters.mjs

73 lines · javascript ref:
/**
 * Test _extractClusters: orphan rooms (no MI pairs) get their own solo group.
 */
import { LocalGraph } from '../lib/local-graph.js'

// Minimal mock backend
const mockBackend = {
  api: { core: { getRecentRooms: async () => [], getMembers: async () => [], getDmRequestsByStatus: async () => [] } }
}

const graph = new LocalGraph(mockBackend, null)

// --- Test 1: Orphans become solo clusters ---
{
  const edges = [
    { from: 'A', to: 'B', mi: 1.5, shared: 3 }
  ]
  const allRooms = ['A', 'B', 'C', 'D'] // C and D are orphans
  const clusters = graph._extractClusters(edges, allRooms, 0.5, 2)

  const roomSets = clusters.map(c => c.rooms.sort().join(','))
  console.log('Test 1 clusters:', roomSets)

  // A+B form a merged cluster, C and D each get solo clusters
  const hasAB = roomSets.includes('A,B')
  const hasC = roomSets.includes('C')
  const hasD = roomSets.includes('D')

  if (!hasAB) throw new Error('Expected A,B cluster')
  if (!hasC) throw new Error('Expected solo cluster for orphan C')
  if (!hasD) throw new Error('Expected solo cluster for orphan D')
  if (clusters.length !== 3) throw new Error(`Expected 3 clusters, got ${clusters.length}`)
  console.log('PASS: orphans get solo clusters')
}

// --- Test 2: All rooms connected → one cluster, no orphans ---
{
  const edges = [
    { from: 'X', to: 'Y', mi: 2.0, shared: 4 },
    { from: 'Y', to: 'Z', mi: 1.8, shared: 3 }
  ]
  const allRooms = ['X', 'Y', 'Z']
  const clusters = graph._extractClusters(edges, allRooms, 0.5, 2)
  if (clusters.length !== 1) throw new Error(`Expected 1 cluster, got ${clusters.length}`)
  if (clusters[0].rooms.length !== 3) throw new Error(`Expected 3 rooms, got ${clusters[0].rooms.length}`)
  console.log('PASS: fully connected → single cluster')
}

// --- Test 3: No edges at all → every room is an orphan ---
{
  const edges = []
  const allRooms = ['P', 'Q', 'R']
  const clusters = graph._extractClusters(edges, allRooms, 0.5, 2)
  if (clusters.length !== 3) throw new Error(`Expected 3 solo clusters, got ${clusters.length}`)
  for (const c of clusters) {
    if (c.rooms.length !== 1) throw new Error(`Expected solo cluster, got ${c.rooms.length} rooms`)
  }
  console.log('PASS: no edges → all orphans get solo clusters')
}

// --- Test 4: Edge below threshold → treated as orphans ---
{
  const edges = [
    { from: 'M', to: 'N', mi: 0.3, shared: 1 }  // below threshold of 0.5
  ]
  const allRooms = ['M', 'N']
  const clusters = graph._extractClusters(edges, allRooms, 0.5, 2)
  if (clusters.length !== 2) throw new Error(`Expected 2 solo clusters, got ${clusters.length}`)
  console.log('PASS: below-threshold edge → orphans')
}

console.log('\nAll _extractClusters tests passed!')