Repository navigation
feat: add tests #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||||||||
| import { describe, it, expect, vi, beforeAll } from 'vitest'; | ||||||||||||||||||||||||||||||||
| import { Embedder } from '../src/embedder/embedder'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Embedder tests — uses a mock pipeline to avoid downloading HuggingFace models. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| describe('Embedder', () => { | ||||||||||||||||||||||||||||||||
| let embedder: Embedder; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| beforeAll(() => { | ||||||||||||||||||||||||||||||||
| embedder = new Embedder(); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| describe('properties', () => { | ||||||||||||||||||||||||||||||||
| it('should have default dimensions of 512', () => { | ||||||||||||||||||||||||||||||||
| expect(embedder.dimensions).toBe(512); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| describe('embed with mock pipeline', () => { | ||||||||||||||||||||||||||||||||
| // Mock the static pipeline so we don't need to download models | ||||||||||||||||||||||||||||||||
| const mockPipeline = vi.fn(async (texts: string[], options: Record<string, unknown>) => { | ||||||||||||||||||||||||||||||||
| const dim = 512; | ||||||||||||||||||||||||||||||||
| const vectors = texts.map(() => Array(dim).fill(0.1)); | ||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||
| tolist: () => vectors, | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| beforeAll(() => { | ||||||||||||||||||||||||||||||||
| // Inject mock pipeline into the static field | ||||||||||||||||||||||||||||||||
| Embedder.pipeline = mockPipeline as any; | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing the private static property
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should embed a single text string', async () => { | ||||||||||||||||||||||||||||||||
| const vector = await embedder.embed('hello world'); | ||||||||||||||||||||||||||||||||
| expect(vector).toBeDefined(); | ||||||||||||||||||||||||||||||||
| expect(vector.length).toBe(512); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should embed a batch of texts', async () => { | ||||||||||||||||||||||||||||||||
| const vectors = await embedder.embedBatch(['hello', 'world']); | ||||||||||||||||||||||||||||||||
| expect(vectors.length).toBe(2); | ||||||||||||||||||||||||||||||||
| expect(vectors[0].length).toBe(512); | ||||||||||||||||||||||||||||||||
| expect(vectors[1].length).toBe(512); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should call pipeline with provided texts', async () => { | ||||||||||||||||||||||||||||||||
| mockPipeline.mockClear(); | ||||||||||||||||||||||||||||||||
| await embedder.embedBatch(['test1', 'test2']); | ||||||||||||||||||||||||||||||||
| expect(mockPipeline).toHaveBeenCalledWith(['test1', 'test2'], { | ||||||||||||||||||||||||||||||||
| pooling: 'mean', | ||||||||||||||||||||||||||||||||
| normalize: true, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should return first element for single embed', async () => { | ||||||||||||||||||||||||||||||||
| mockPipeline.mockClear(); | ||||||||||||||||||||||||||||||||
| const vector = await embedder.embed('single'); | ||||||||||||||||||||||||||||||||
| // embed internally calls embedBatch and takes [0] | ||||||||||||||||||||||||||||||||
| expect(mockPipeline).toHaveBeenCalledWith(['single'], { | ||||||||||||||||||||||||||||||||
| pooling: 'mean', | ||||||||||||||||||||||||||||||||
| normalize: true, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| expect(vector.length).toBe(512); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should handle empty batch', async () => { | ||||||||||||||||||||||||||||||||
| mockPipeline.mockClear(); | ||||||||||||||||||||||||||||||||
| const vectors = await embedder.embedBatch([]); | ||||||||||||||||||||||||||||||||
| expect(vectors).toEqual([]); | ||||||||||||||||||||||||||||||||
| expect(mockPipeline).toHaveBeenCalledWith([], { | ||||||||||||||||||||||||||||||||
| pooling: 'mean', | ||||||||||||||||||||||||||||||||
| normalize: true, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "Nested", | ||
| "items": [ | ||
| { "key": "a", "value": 1 }, | ||
| { "key": "b", "value": 2 } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Plain Markdown | ||
|
|
||
| This is a markdown file without any front-matter metadata. | ||
|
|
||
| Just regular paragraphs and headings. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| "hello" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import * as path from 'path'; | ||
| import { MarkdownLoader } from '../src/loaders/markdown'; | ||
| import { JsonLoader } from '../src/loaders/json'; | ||
| import { TextLoader } from '../src/loaders/text'; | ||
| import { getLoader } from '../src/loaders'; | ||
|
|
||
| const FIXTURES_DIR = path.join(__dirname, 'fixtures/docs'); | ||
|
|
||
| describe('Loaders', () => { | ||
| describe('MarkdownLoader', () => { | ||
| const loader = new MarkdownLoader(); | ||
|
|
||
| it('should load markdown file with front-matter', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'getting-started.md'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toContain('Getting Started'); | ||
| expect(doc.content).toContain('npm install'); | ||
| expect(doc.meta).toEqual({ | ||
| title: 'Getting Started', | ||
| category: 'guide', | ||
| }); | ||
| }); | ||
|
|
||
| it('should load markdown file without front-matter', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'plain.md'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toContain('Plain Markdown'); | ||
| expect(doc.content).toContain('without any front-matter'); | ||
| // gray-matter returns {} for files without front-matter | ||
| expect(doc.meta).toEqual({}); | ||
| }); | ||
|
|
||
| it('should trim content whitespace', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'plain.md'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).not.toMatch(/^\s/); | ||
| expect(doc.content).not.toMatch(/\s$/); | ||
| }); | ||
|
|
||
| it('should handle .md and .markdown extensions', () => { | ||
| expect(loader.canHandle('file.md')).toBe(true); | ||
| expect(loader.canHandle('file.markdown')).toBe(true); | ||
| }); | ||
|
|
||
| it('should reject non-markdown extensions', () => { | ||
| expect(loader.canHandle('file.json')).toBe(false); | ||
| expect(loader.canHandle('file.txt')).toBe(false); | ||
| expect(loader.canHandle('file.css')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('JsonLoader', () => { | ||
| const loader = new JsonLoader(); | ||
|
|
||
| it('should load json file', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'api.json'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toContain('API Reference'); | ||
| expect(doc.content).toContain('/users'); | ||
| }); | ||
|
|
||
| it('should load pure string JSON', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'string.json'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toBe('hello'); | ||
| }); | ||
|
|
||
| it('should load empty object JSON', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'empty-object.json'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toContain('{}'); | ||
| }); | ||
|
|
||
| it('should load nested JSON with pretty formatting', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'nested.json'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toContain('Nested'); | ||
| expect(doc.content).toContain('items'); | ||
| }); | ||
|
|
||
| it('should handle .json extension', () => { | ||
| expect(loader.canHandle('file.json')).toBe(true); | ||
| }); | ||
|
|
||
| it('should reject non-JSON extensions', () => { | ||
| expect(loader.canHandle('file.md')).toBe(false); | ||
| expect(loader.canHandle('file.txt')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('TextLoader', () => { | ||
| const loader = new TextLoader(); | ||
|
|
||
| it('should load text file', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'notes.txt'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).toContain('notes'); | ||
| }); | ||
|
|
||
| it('should trim content from text file', async () => { | ||
| const filePath = path.join(FIXTURES_DIR, 'notes.txt'); | ||
| const doc = await loader.load(filePath); | ||
|
|
||
| expect(doc.content).not.toMatch(/^\s/); | ||
| }); | ||
|
|
||
| it('should handle .txt extension', () => { | ||
| expect(loader.canHandle('file.txt')).toBe(true); | ||
| }); | ||
|
|
||
| it('should reject non-text extensions', () => { | ||
| expect(loader.canHandle('file.md')).toBe(false); | ||
| expect(loader.canHandle('file.json')).toBe(false); | ||
| expect(loader.canHandle('file.csv')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getLoader', () => { | ||
| it('should return MarkdownLoader for .md files', () => { | ||
| const loader = getLoader('docs/guide.md'); | ||
| expect(loader).toBeDefined(); | ||
| expect(loader!.canHandle('guide.md')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return JsonLoader for .json files', () => { | ||
| const loader = getLoader('data/config.json'); | ||
| expect(loader).toBeDefined(); | ||
| expect(loader!.canHandle('config.json')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return TextLoader for .txt files', () => { | ||
| const loader = getLoader('readme.txt'); | ||
| expect(loader).toBeDefined(); | ||
| expect(loader!.canHandle('readme.txt')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return MarkdownLoader for .markdown extension', () => { | ||
| const loader = getLoader('docs/readme.markdown'); | ||
| expect(loader).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should return undefined for unsupported extensions', () => { | ||
| expect(getLoader('style.css')).toBeUndefined(); | ||
| expect(getLoader('script.js')).toBeUndefined(); | ||
| expect(getLoader('data.csv')).toBeUndefined(); | ||
| expect(getLoader('image.png')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined for files with no extension', () => { | ||
| expect(getLoader('Makefile')).toBeUndefined(); | ||
| expect(getLoader('Dockerfile')).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
afterAllfromvitestto clean up the mocked static pipeline after all tests in this suite run, preventing test pollution.