From f710dc3214b355af0956f213246da4d17b1dc813 Mon Sep 17 00:00:00 2001 From: GeorgeWebberley Date: Sun, 1 Mar 2026 14:03:34 +0100 Subject: [PATCH] Added a couple of tests for key areas such as parser and utils --- src/cases/parser/parser.service.spec.ts | 60 +++++++++++++++++++++++++ src/common/utils/string.utils.spec.ts | 14 ++++++ 2 files changed, 74 insertions(+) create mode 100644 src/cases/parser/parser.service.spec.ts create mode 100644 src/common/utils/string.utils.spec.ts diff --git a/src/cases/parser/parser.service.spec.ts b/src/cases/parser/parser.service.spec.ts new file mode 100644 index 0000000..6089819 --- /dev/null +++ b/src/cases/parser/parser.service.spec.ts @@ -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); + 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 = '

Case Title

Case content

'; + 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); + }); +}); diff --git a/src/common/utils/string.utils.spec.ts b/src/common/utils/string.utils.spec.ts new file mode 100644 index 0000000..54009c4 --- /dev/null +++ b/src/common/utils/string.utils.spec.ts @@ -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); + }); + }); +});