import test from 'node:test'; import assert from 'node:assert/strict'; import { createExplorerGraph } from '../js/pages/network-explorer/graph-model.js'; import { getExplorerGraph, clearExplorerGraphCache, readExplorerCachedGraphs, EXPLORER_CACHE_TTL_MS, explorerGraphSavedAt, } from '../js/pages/network-explorer/graph-cache.js'; function friends(login, links = [], close = [], inFriends = [], marked = []) { return { login, outFriends: links, outCloseFriends: close, inFriends, allUsers: marked.map(([user, shine]) => ({ login: user, shine })), }; } const find = (model, login) => model.nodes.find((node) => node.id === login); test('V3: BFS, радиусы X2 + 0..4, переход в любую сторону без истории центров', () => { const g = createExplorerGraph(); g.add(friends('a', ['b'])); g.add(friends('b', ['c'])); g.add(friends('c', ['d'])); g.add(friends('d', ['e'])); g.add(friends('e', ['f'])); g.add(friends('f', ['g'])); const zero = g.snapshot('a', { x2: true, depth: 0 }); assert.deepEqual(zero.nodes.map((n) => n.id), ['a', 'b', 'c']); const one = g.snapshot('d', { x2: true, depth: 1 }); assert.deepEqual(one.nodes.map((n) => n.id), ['d', 'c', 'e', 'b', 'f', 'a', 'g']); const two = g.snapshot('d', { x2: true, depth: 2 }); assert.equal(two.nodes.length, 7); const back = g.snapshot('b', { x2: false, depth: 0 }); assert.deepEqual(back.nodes.map((n) => n.id), ['b', 'a', 'c']); }); test('V3: маленький при скрытом друге; раскрывается, когда все друзья видны', () => { const g = createExplorerGraph(); g.add(friends('a', ['b'])); g.add(friends('b', ['c'])); g.add(friends('c', ['d'])); const shallow = g.snapshot('a', { x2: false, depth: 0 }); assert.equal(find(shallow, 'a').visualSmall, false); assert.equal(find(shallow, 'b').visualSmall, true); const deeper = g.snapshot('a', { x2: true, depth: 1 }); assert.equal(find(deeper, 'b').visualSmall, false); assert.equal(find(deeper, 'c').visualSmall, false); assert.equal(find(deeper, 'd').visualSmall, true); // список d пока не был загружен assert.deepEqual(deeper.nodes.filter((n) => n.id === 'b').flatMap((n) => n.edgeParents).map((n) => n.id), ['a']); }); test('V3: кратчайший путь через пересечение, без дубликатов; направление ребра', () => { const g = createExplorerGraph(); g.add(friends('z', ['a', 'b'])); g.add(friends('a', ['z', 'b'], [], ['z'])); g.add(friends('b', ['c'])); g.add(friends('c', ['d'])); const m = g.snapshot('z', { x2: true, depth: 0 }); assert.equal(m.nodes.length, 4); assert.equal(find(m, 'b').distance, 1); assert.equal(find(m, 'c').distance, 2); assert.ok(find(m, 'a').edgeParents.some((e) => e.id === 'z')); assert.equal(m.nodes.length, new Set(m.nodes.map((n) => n.id)).size); }); test('V3: фильтры близкие/сияющие и полнота раскрытия', () => { const g = createExplorerGraph(); g.add(friends('a', ['b'], ['c'], [], [['b', false], ['c', true]])); g.add(friends('c', [])); const all = g.snapshot('a', { x2: false, depth: 0 }); assert.equal(all.nodes.length, 3); assert.deepEqual(g.snapshot('a', { close: true, depth: 0 }).nodes.map((n) => n.id), ['a', 'c']); const shiny = g.snapshot('a', { shining: true, depth: 0 }); assert.deepEqual(shiny.nodes.map((n) => n.id), ['a', 'c']); assert.equal(find(shiny, 'a').visualSmall, false); }); test('V3: TTL 24h, ручная очистка, разделение серверов/аккаунтов и восстановление', async () => { const clock = Date.now; let now = 1_900_000_000_000; Date.now = () => now; try { await clearExplorerGraphCache(); let count = 0; const service = { serverUrl: 'wss://test', getUserConnectionsGraph: async (login) => ({ login, count: ++count }) }; const scope = 'wss://test|alice'; const scopeOther = 'wss://test|bob'; await getExplorerGraph(service, 'alice', { scope }); now += EXPLORER_CACHE_TTL_MS - 1; await getExplorerGraph(service, 'alice', { scope }); assert.equal(count, 1); assert.equal(explorerGraphSavedAt(scope, 'alice'), 1_900_000_000_000); assert.equal((await readExplorerCachedGraphs(scope)).length, 1); now += 1; await getExplorerGraph(service, 'alice', { scope }); assert.equal(count, 2); await getExplorerGraph(service, 'alice', { scope: scopeOther }); assert.equal(count, 3); await clearExplorerGraphCache(); assert.equal((await readExplorerCachedGraphs(scope)).length, 0); await getExplorerGraph(service, 'alice', { scope }); assert.equal(count, 4); } finally { Date.now = clock; await clearExplorerGraphCache(); } }); test('V3: регистр логина для запросов, обновление списка и статуса сияния', () => { const g = createExplorerGraph(); g.add(friends('Alice', ['BobUser', 'Carol'], [], [], [['BobUser', true]])); assert.deepEqual(g.getFriends('ALICE'), ['BobUser', 'Carol']); assert.equal(g.snapshot('Alice').nodes.find((n) => n.id === 'bobuser').shining, true); g.add(friends('Alice', ['BobUser'], [], [], [['BobUser', false]])); assert.deepEqual(g.getFriends('ALICE'), ['BobUser']); assert.equal(g.snapshot('Alice').nodes.find((n) => n.id === 'bobuser').shining, false); assert.equal(g.snapshot('Alice').nodes.some((n) => n.id === 'carol'), false); }); test('V3: взаимная связь сохраняет оба типа для красно-синего перехода', () => { const g = createExplorerGraph(); g.add(friends('a', ['b'])); g.add(friends('b', [], ['a'])); const edge = find(g.snapshot('a', { depth: 0 }), 'b').edgeParents[0]; assert.equal(edge.relationType, 'friend'); assert.equal(edge.reverseRelationType, 'close_friend'); assert.equal(edge.mutual, true); });