|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import { KnowledgeGraphManager, Entity, Relation } from '../index.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The delete tools stay silent when a target is absent, which the README |
| 9 | + * documents. What they must not do is report a deletion that did not happen: |
| 10 | + * an agent that mistypes a name is told its memory is clean while the data is |
| 11 | + * still on disk, and nothing in the response contradicts that. |
| 12 | + */ |
| 13 | +describe('delete reporting', () => { |
| 14 | + let manager: KnowledgeGraphManager; |
| 15 | + let testFilePath: string; |
| 16 | + |
| 17 | + const entities: Entity[] = [ |
| 18 | + { name: 'Alice', entityType: 'person', observations: ['works at Acme Corp', 'likes tea'] }, |
| 19 | + { name: 'Bob', entityType: 'person', observations: ['likes programming'] }, |
| 20 | + ]; |
| 21 | + const relations: Relation[] = [{ from: 'Alice', to: 'Bob', relationType: 'works_with' }]; |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + testFilePath = path.join( |
| 25 | + path.dirname(fileURLToPath(import.meta.url)), |
| 26 | + `test-delete-reporting-${Date.now()}-${Math.random().toString(16).slice(2)}.jsonl` |
| 27 | + ); |
| 28 | + manager = new KnowledgeGraphManager(testFilePath); |
| 29 | + await manager.createEntities(entities); |
| 30 | + await manager.createRelations(relations); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + try { |
| 35 | + await fs.unlink(testFilePath); |
| 36 | + } catch { |
| 37 | + // the file is gone already |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + describe('deleteEntities', () => { |
| 42 | + it('reports which names matched and which did not', async () => { |
| 43 | + const result = await manager.deleteEntities(['Alice', 'Alise']); |
| 44 | + expect(result).toEqual({ deleted: ['Alice'], notFound: ['Alise'] }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('reports nothing deleted when no name matches', async () => { |
| 48 | + const result = await manager.deleteEntities(['Nobody']); |
| 49 | + expect(result).toEqual({ deleted: [], notFound: ['Nobody'] }); |
| 50 | + |
| 51 | + const graph = await manager.readGraph(); |
| 52 | + expect(graph.entities).toHaveLength(2); |
| 53 | + }); |
| 54 | + |
| 55 | + it('still deletes the entity and its relations', async () => { |
| 56 | + await manager.deleteEntities(['Alice']); |
| 57 | + |
| 58 | + const graph = await manager.readGraph(); |
| 59 | + expect(graph.entities.map(e => e.name)).toEqual(['Bob']); |
| 60 | + expect(graph.relations).toHaveLength(0); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('deleteObservations', () => { |
| 65 | + it('counts only the observations that were present', async () => { |
| 66 | + const result = await manager.deleteObservations([ |
| 67 | + { entityName: 'Alice', observations: ['likes tea', 'never said this'] }, |
| 68 | + ]); |
| 69 | + expect(result).toEqual({ deletedCount: 1, missingEntities: [] }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('names an entity that does not exist', async () => { |
| 73 | + const result = await manager.deleteObservations([ |
| 74 | + { entityName: 'Carol', observations: ['anything'] }, |
| 75 | + ]); |
| 76 | + expect(result).toEqual({ deletedCount: 0, missingEntities: ['Carol'] }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('deleteRelations', () => { |
| 81 | + it('counts only the relations that matched', async () => { |
| 82 | + const result = await manager.deleteRelations([ |
| 83 | + { from: 'Alice', to: 'Bob', relationType: 'works_with' }, |
| 84 | + { from: 'Alice', to: 'Bob', relationType: 'never_existed' }, |
| 85 | + ]); |
| 86 | + expect(result).toEqual({ deletedCount: 1 }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('reports nothing deleted when the relation type is wrong', async () => { |
| 90 | + const result = await manager.deleteRelations([ |
| 91 | + { from: 'Alice', to: 'Bob', relationType: 'manages' }, |
| 92 | + ]); |
| 93 | + expect(result).toEqual({ deletedCount: 0 }); |
| 94 | + |
| 95 | + const graph = await manager.readGraph(); |
| 96 | + expect(graph.relations).toHaveLength(1); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments