From 9f3133a4037d243e15cdfacc169e209eb6497d0f Mon Sep 17 00:00:00 2001 From: Shinwoo PARK Date: Mon, 11 May 2026 10:50:43 +0900 Subject: [PATCH] test: specify pluggable fact ingestion behavior --- tests/ingestion.test.ts | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/ingestion.test.ts diff --git a/tests/ingestion.test.ts b/tests/ingestion.test.ts new file mode 100644 index 0000000..0a5ceee --- /dev/null +++ b/tests/ingestion.test.ts @@ -0,0 +1,54 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; + +import { IdentityDB } from '../src/core/identity-db'; +import { NaiveExtractor } from '../src/ingestion/naive-extractor'; +import type { FactExtractor } from '../src/ingestion/types'; + +describe('IdentityDB ingestion', () => { + let db: IdentityDB; + + beforeEach(async () => { + db = await IdentityDB.connect({ client: 'sqlite', filename: ':memory:' }); + await db.initialize(); + }); + + afterEach(async () => { + await db.close(); + }); + + it('ingests a statement using a provided extractor', async () => { + const extractor: FactExtractor = { + async extract(input) { + return { + statement: input, + topics: [ + { name: 'I', category: 'entity', granularity: 'concrete', role: 'subject' }, + { name: 'TypeScript', category: 'entity', granularity: 'concrete', role: 'object' }, + { name: '2025', category: 'temporal', granularity: 'concrete', role: 'time' }, + ], + }; + }, + }; + + const fact = await db.ingestStatement('I have worked with TypeScript since 2025.', { + extractor, + }); + + expect(fact.topics.map((topic) => topic.name)).toEqual(['I', 'TypeScript', '2025']); + + const linkedFacts = await db.getTopicFactsLinkedTo('TypeScript', '2025'); + expect(linkedFacts).toHaveLength(1); + expect(linkedFacts[0]?.statement).toBe('I have worked with TypeScript since 2025.'); + }); + + it('ships a deterministic naive extractor for local usage', async () => { + const fact = await db.ingestStatement('I have worked with TypeScript since 2025.', { + extractor: new NaiveExtractor(), + }); + + expect(fact.topics.map((topic) => topic.name)).toEqual(['I', 'TypeScript', '2025']); + + const topic = await db.getTopicByName('TypeScript', { includeFacts: true }); + expect(topic?.facts).toHaveLength(1); + }); +});