Added a couple of tests for key areas such as parser and utils
This commit is contained in:
parent
f70b4b34e4
commit
f710dc3214
60
src/cases/parser/parser.service.spec.ts
Normal file
60
src/cases/parser/parser.service.spec.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { ParserService } from './parser.service';
|
||||||
|
import { BadRequestException } from '@nestjs/common';
|
||||||
|
|
||||||
|
jest.mock('@langchain/google-genai', () => ({
|
||||||
|
ChatGoogleGenerativeAI: jest.fn().mockImplementation(() => ({
|
||||||
|
withStructuredOutput: jest.fn().mockReturnValue({
|
||||||
|
invoke: jest.fn().mockResolvedValue({
|
||||||
|
title: 'Mock Case',
|
||||||
|
decisionType: 'Judgment',
|
||||||
|
decisionDate: '2024-01-01',
|
||||||
|
office: 'Office X',
|
||||||
|
court: 'Mock Court',
|
||||||
|
caseNumber: '123/2024',
|
||||||
|
summary: 'This is a mock summary for testing purposes.',
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('ParserService', () => {
|
||||||
|
let service: ParserService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [ParserService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<ParserService>(ParserService);
|
||||||
|
process.env.GOOGLE_API_KEY = 'test_key';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw BadRequestException for unsupported file types', async () => {
|
||||||
|
const buffer = Buffer.from('test');
|
||||||
|
await expect(service.process(buffer, 'image/png')).rejects.toThrow(
|
||||||
|
BadRequestException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should extract text from HTML using cheerio', async () => {
|
||||||
|
const html = '<html><body><h1>Case Title</h1><p>Case content</p></body></html>';
|
||||||
|
const buffer = Buffer.from(html);
|
||||||
|
|
||||||
|
const result = await service.process(buffer, 'text/html');
|
||||||
|
|
||||||
|
expect(result.title).toBe('Mock Case');
|
||||||
|
expect(result.metadata.rawLength).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fallback to buffer string if body is empty in HTML', async () => {
|
||||||
|
const text = 'Raw text content';
|
||||||
|
const buffer = Buffer.from(text);
|
||||||
|
const result = await service.process(buffer, 'text/html');
|
||||||
|
expect(result.metadata.rawLength).toBe(text.length);
|
||||||
|
});
|
||||||
|
});
|
||||||
14
src/common/utils/string.utils.spec.ts
Normal file
14
src/common/utils/string.utils.spec.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { isUuid } from './string.utils';
|
||||||
|
|
||||||
|
describe('String Utils', () => {
|
||||||
|
describe('isUuid', () => {
|
||||||
|
it('should return true for valid UUID v4', () => {
|
||||||
|
expect(isUuid('550e8400-e29b-41d4-a716-446655440000')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false for invalid strings', () => {
|
||||||
|
expect(isUuid('not-a-uuid')).toBe(false);
|
||||||
|
expect(isUuid('12345')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue