Spaces:
Runtime error
Runtime error
File size: 10,328 Bytes
ea8da24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
import documentProcessor from '../document-processor';
import geminiClient from '../gemini';
import redisClient from '../redis';
import { ViolationPattern } from '../housing-law-database';
// Mock dependencies
jest.mock('../gemini');
jest.mock('../redis');
jest.mock('../housing-law-database');
const mockGeminiClient = geminiClient as jest.Mocked<typeof geminiClient>;
const mockRedisClient = redisClient as jest.Mocked<typeof redisClient>;
describe('DocumentProcessor', () => {
beforeEach(() => {
jest.clearAllMocks();
// Mock Redis client methods
mockRedisClient.getClient.mockReturnValue({
json: {
set: jest.fn().mockResolvedValue(undefined),
get: jest.fn().mockResolvedValue(null),
},
expire: jest.fn().mockResolvedValue(undefined),
ft: {
search: jest.fn().mockResolvedValue([]),
create: jest.fn().mockResolvedValue(undefined),
info: jest.fn().mockResolvedValue(false),
},
ping: jest.fn().mockResolvedValue('PONG'),
} as any);
});
describe('validateFile', () => {
it('should validate PDF files correctly', () => {
const pdfFile = new File(['test'], 'lease.pdf', { type: 'application/pdf' });
const result = documentProcessor.validateFile(pdfFile);
expect(result.valid).toBe(true);
expect(result.error).toBeUndefined();
});
it('should validate image files correctly', () => {
const imageFile = new File(['test'], 'lease.jpg', { type: 'image/jpeg' });
const result = documentProcessor.validateFile(imageFile);
expect(result.valid).toBe(true);
expect(result.error).toBeUndefined();
});
it('should reject files larger than 10MB', () => {
const largeFile = new File(['x'.repeat(11 * 1024 * 1024)], 'large.pdf', { type: 'application/pdf' });
const result = documentProcessor.validateFile(largeFile);
expect(result.valid).toBe(false);
expect(result.error).toBe('File size must be less than 10MB');
});
it('should reject unsupported file types', () => {
const unsupportedFile = new File(['test'], 'lease.txt', { type: 'text/plain' });
const result = documentProcessor.validateFile(unsupportedFile);
expect(result.valid).toBe(false);
expect(result.error).toBe('File type not supported. Please upload a PDF or image file.');
});
});
describe('healthCheck', () => {
it('should return true when Tesseract is available', async () => {
const result = await documentProcessor.healthCheck();
expect(result).toBe(true);
});
});
describe('processDocument', () => {
const mockLeaseId = 'test-lease-123';
const mockExtractedText = 'This is a sample lease agreement. Rent is $2000 per month.';
const mockExtractedClauses = [
{ text: 'Rent is $2000 per month', section: 'rent_payment' },
{ text: 'Security deposit is $3000', section: 'security_deposit' }
];
const mockProcessedClauses = [
{
id: 'test-lease-123_0',
text: 'Rent is $2000 per month',
section: 'rent_payment',
vector: [0.1, 0.2, 0.3],
metadata: {
leaseId: mockLeaseId,
flagged: false,
confidence: 0.0
}
},
{
id: 'test-lease-123_1',
text: 'Security deposit is $3000',
section: 'security_deposit',
vector: [0.4, 0.5, 0.6],
metadata: {
leaseId: mockLeaseId,
flagged: true,
severity: 'Critical',
violationType: 'security_deposit_violation',
legalReference: 'NYC Housing Maintenance Code §27-2056',
confidence: 0.85
}
}
];
beforeEach(() => {
// Mock Gemini client methods
mockGeminiClient.extractClauses.mockResolvedValue(mockExtractedClauses);
mockGeminiClient.generateEmbedding.mockResolvedValue([0.1, 0.2, 0.3]);
});
it('should process a valid PDF document successfully', async () => {
const pdfFile = new File(['test pdf content'], 'lease.pdf', { type: 'application/pdf' });
// Mock the private methods by spying on them
const extractTextSpy = jest.spyOn(documentProcessor as any, 'extractTextFromPDF')
.mockResolvedValue(mockExtractedText);
const processClausesSpy = jest.spyOn(documentProcessor as any, 'processClauses')
.mockResolvedValue(mockProcessedClauses);
const detectViolationsSpy = jest.spyOn(documentProcessor as any, 'detectViolations')
.mockResolvedValue([
{
clauseId: 'test-lease-123_1',
type: 'security_deposit_violation',
description: 'Security deposit exceeds legal limit',
legalReference: 'NYC Housing Maintenance Code §27-2056',
severity: 'Critical'
}
]);
const storeInRedisSpy = jest.spyOn(documentProcessor as any, 'storeInRedis')
.mockResolvedValue(undefined);
const result = await documentProcessor.processDocument(pdfFile, mockLeaseId);
// Verify the result structure
expect(result.leaseId).toBe(mockLeaseId);
expect(result.clauses).toHaveLength(2);
expect(result.violations).toHaveLength(1);
expect(result.summary.totalClauses).toBe(2);
expect(result.summary.flaggedClauses).toBe(1);
expect(result.summary.criticalViolations).toBe(1);
// Verify method calls
expect(extractTextSpy).toHaveBeenCalledWith(pdfFile);
expect(mockGeminiClient.extractClauses).toHaveBeenCalledWith(mockExtractedText);
expect(processClausesSpy).toHaveBeenCalledWith(mockExtractedClauses, mockLeaseId);
expect(detectViolationsSpy).toHaveBeenCalledWith(mockProcessedClauses);
expect(storeInRedisSpy).toHaveBeenCalledWith(mockProcessedClauses, mockLeaseId);
// Clean up spies
extractTextSpy.mockRestore();
processClausesSpy.mockRestore();
detectViolationsSpy.mockRestore();
storeInRedisSpy.mockRestore();
});
it('should process a valid image document successfully', async () => {
const imageFile = new File(['test image content'], 'lease.jpg', { type: 'image/jpeg' });
const extractTextSpy = jest.spyOn(documentProcessor as any, 'extractTextFromImage')
.mockResolvedValue(mockExtractedText);
const processClausesSpy = jest.spyOn(documentProcessor as any, 'processClauses')
.mockResolvedValue(mockProcessedClauses);
const detectViolationsSpy = jest.spyOn(documentProcessor as any, 'detectViolations')
.mockResolvedValue([]);
const storeInRedisSpy = jest.spyOn(documentProcessor as any, 'storeInRedis')
.mockResolvedValue(undefined);
const result = await documentProcessor.processDocument(imageFile, mockLeaseId);
expect(result.leaseId).toBe(mockLeaseId);
expect(extractTextSpy).toHaveBeenCalledWith(imageFile);
// Clean up spies
extractTextSpy.mockRestore();
processClausesSpy.mockRestore();
detectViolationsSpy.mockRestore();
storeInRedisSpy.mockRestore();
});
it('should throw error for unsupported file type', async () => {
const unsupportedFile = new File(['test'], 'lease.txt', { type: 'text/plain' });
await expect(documentProcessor.processDocument(unsupportedFile, mockLeaseId))
.rejects.toThrow('Unsupported file type. Please upload a PDF or image file.');
});
it('should handle Gemini client errors gracefully', async () => {
const pdfFile = new File(['test'], 'lease.pdf', { type: 'application/pdf' });
mockGeminiClient.extractClauses.mockRejectedValue(new Error('Gemini API error'));
const extractTextSpy = jest.spyOn(documentProcessor as any, 'extractTextFromPDF')
.mockResolvedValue(mockExtractedText);
await expect(documentProcessor.processDocument(pdfFile, mockLeaseId))
.rejects.toThrow('Failed to process document: Gemini API error');
extractTextSpy.mockRestore();
});
it('should handle Redis storage errors gracefully', async () => {
const pdfFile = new File(['test'], 'lease.pdf', { type: 'application/pdf' });
const extractTextSpy = jest.spyOn(documentProcessor as any, 'extractTextFromPDF')
.mockResolvedValue(mockExtractedText);
const processClausesSpy = jest.spyOn(documentProcessor as any, 'processClauses')
.mockResolvedValue(mockProcessedClauses);
const detectViolationsSpy = jest.spyOn(documentProcessor as any, 'detectViolations')
.mockResolvedValue([]);
const storeInRedisSpy = jest.spyOn(documentProcessor as any, 'storeInRedis')
.mockRejectedValue(new Error('Redis connection failed'));
await expect(documentProcessor.processDocument(pdfFile, mockLeaseId))
.rejects.toThrow('Failed to process document: Redis connection failed');
// Clean up spies
extractTextSpy.mockRestore();
processClausesSpy.mockRestore();
detectViolationsSpy.mockRestore();
storeInRedisSpy.mockRestore();
});
});
describe('generateSummary', () => {
it('should generate correct summary statistics', () => {
const clauses = [
{
id: '1',
text: 'Rent clause',
section: 'rent',
vector: [0.1, 0.2, 0.3],
metadata: { leaseId: 'test', flagged: false, confidence: 0.0 }
},
{
id: '2',
text: 'Security deposit clause',
section: 'deposit',
vector: [0.4, 0.5, 0.6],
metadata: { leaseId: 'test', flagged: true, confidence: 0.85 }
}
];
const violations = [
{
clauseId: '2',
type: 'security_deposit_violation',
description: 'Deposit too high',
legalReference: 'NYC Code',
severity: 'Critical'
}
];
const summary = (documentProcessor as any).generateSummary(clauses, violations);
expect(summary.totalClauses).toBe(2);
expect(summary.flaggedClauses).toBe(1);
expect(summary.criticalViolations).toBe(1);
expect(summary.highViolations).toBe(0);
expect(summary.mediumViolations).toBe(0);
expect(summary.lowViolations).toBe(0);
});
});
}); |