|
| 1 | +import {setup} from '../../../../../../setup.mjs'; |
| 2 | + |
| 3 | +const appName = 'GraphServiceTest'; |
| 4 | + |
| 5 | +setup({ |
| 6 | + neoConfig: { |
| 7 | + unitTestMode: true |
| 8 | + }, |
| 9 | + appConfig: { |
| 10 | + name : appName, |
| 11 | + isMounted : () => true, |
| 12 | + vnodeInitialising: false |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +import {test, expect} from '@playwright/test'; |
| 17 | +import Neo from '../../../../../../../../src/Neo.mjs'; |
| 18 | +import * as core from '../../../../../../../../src/core/_export.mjs'; |
| 19 | +import GraphService from '../../../../../../../../ai/mcp/server/memory-core/services/GraphService.mjs'; |
| 20 | +import aiConfig from '../../../../../../../../ai/mcp/server/memory-core/config.mjs'; |
| 21 | +import fs from 'fs-extra'; |
| 22 | +import path from 'path'; |
| 23 | +import os from 'os'; |
| 24 | + |
| 25 | +test.describe('Neo.ai.mcp.server.memory-core.services.GraphService', () => { |
| 26 | + let service; |
| 27 | + const testDbPath = path.join(os.tmpdir(), `memory-core-graph-test-${process.pid}-${Date.now()}.db`); |
| 28 | + |
| 29 | + test.beforeAll(async () => { |
| 30 | + // Mock the SQLite target path to a safe pure temporary location |
| 31 | + aiConfig.sqlitePath = testDbPath; |
| 32 | + if (fs.existsSync(testDbPath)) { |
| 33 | + fs.unlinkSync(testDbPath); |
| 34 | + } |
| 35 | + await GraphService.initAsync(); |
| 36 | + }); |
| 37 | + |
| 38 | + test.beforeEach(async () => { |
| 39 | + // Clear graph nodes and edges before each test for isolation |
| 40 | + if (GraphService.db) { |
| 41 | + GraphService.db.nodes.clear(); |
| 42 | + GraphService.db.edges.clear(); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + test.afterEach(() => { |
| 47 | + if (GraphService.db) { |
| 48 | + GraphService.db.nodes.clear(); |
| 49 | + GraphService.db.edges.clear(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + test.afterAll(() => { |
| 54 | + if (fs.existsSync(testDbPath)) { |
| 55 | + try { |
| 56 | + fs.unlinkSync(testDbPath); |
| 57 | + } catch (e) {} |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + test('should extract node neighbors properly', async () => { |
| 62 | + GraphService.upsertNode({ id: 'EpicA', name: 'Roadmap Planner' }); |
| 63 | + GraphService.upsertNode({ id: 'Task1', name: 'Implementation' }); |
| 64 | + GraphService.upsertNode({ id: 'Task2', name: 'Documentation' }); |
| 65 | + |
| 66 | + GraphService.linkNodes('EpicA', 'Task1', 'CONTAINS', 1.0); |
| 67 | + GraphService.linkNodes('EpicA', 'Task2', 'CONTAINS', 0.8); |
| 68 | + GraphService.linkNodes('Task1', 'Task2', 'DEPENDENCY', 0.5); |
| 69 | + |
| 70 | + const neighbors = GraphService.getNeighbors({ id: 'EpicA' }); |
| 71 | + |
| 72 | + // Validation of extraction |
| 73 | + expect(neighbors.length).toBe(2); |
| 74 | + |
| 75 | + const task1 = neighbors.find(n => n.id === 'Task1'); |
| 76 | + const task2 = neighbors.find(n => n.id === 'Task2'); |
| 77 | + |
| 78 | + expect(task1).toBeDefined(); |
| 79 | + expect(task1.weight).toBe(1.0); |
| 80 | + expect(task1.relationship).toBe('CONTAINS'); |
| 81 | + |
| 82 | + expect(task2).toBeDefined(); |
| 83 | + expect(task2.weight).toBe(0.8); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should correctly expose getContextFrontier topology', async () => { |
| 87 | + GraphService.upsertNode({ id: 'frontier', type: 'SYSTEM_ANCHOR' }); |
| 88 | + GraphService.upsertNode({ id: 'EpicB' }); |
| 89 | + |
| 90 | + // Weight < 0.8 should be filtered out by getContextFrontier originally |
| 91 | + GraphService.linkNodes('frontier', 'EpicB', 'STRATEGIC_PIVOT', 0.9); |
| 92 | + |
| 93 | + const topology = GraphService.getContextFrontier(); |
| 94 | + expect(topology).toBeDefined(); |
| 95 | + expect(topology.frontier.id).toBe('frontier'); |
| 96 | + expect(topology.strategicNeighbors.length).toBe(1); |
| 97 | + expect(topology.strategicNeighbors[0].id).toBe('EpicB'); |
| 98 | + }); |
| 99 | +}); |
0 commit comments