Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions test/embedder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { describe, it, expect, vi, beforeAll } from 'vitest';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import afterAll from vitest to clean up the mocked static pipeline after all tests in this suite run, preventing test pollution.

Suggested change
import { describe, it, expect, vi, beforeAll } from 'vitest';
import { describe, it, expect, vi, beforeAll, afterAll } 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Accessing the private static property Embedder.pipeline directly causes a TypeScript compilation error (Property 'pipeline' is private and only accessible within class 'Embedder'). Bypassing this with a cast to any fixes the compilation error. Additionally, we should store the original pipeline and restore it in afterAll to prevent test pollution.

Suggested change
beforeAll(() => {
// Inject mock pipeline into the static field
Embedder.pipeline = mockPipeline as any;
});
let originalPipeline: any;
beforeAll(() => {
// Inject mock pipeline into the static field
originalPipeline = (Embedder as any).pipeline;
(Embedder as any).pipeline = mockPipeline;
});
afterAll(() => {
(Embedder as any).pipeline = originalPipeline;
});


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,
});
});
});
});
1 change: 1 addition & 0 deletions test/fixtures/docs/empty-object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions test/fixtures/docs/nested.json
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 }
]
}
5 changes: 5 additions & 0 deletions test/fixtures/docs/plain.md
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.
1 change: 1 addition & 0 deletions test/fixtures/docs/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello"
163 changes: 163 additions & 0 deletions test/loaders-edge.test.ts
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();
});
});
});
64 changes: 0 additions & 64 deletions test/loaders.test.ts

This file was deleted.

Loading
Loading