Files
ims/lib/incidents/bulk-import.test.ts
adminandClaude Haiku 4.5 d2707ac3df fix: resolve TypeScript test type errors in report-pdf and bulk-import tests
- Updated mockSession to include required SessionPayload fields (siteId, name)
- Added optional chaining for metadata.location to handle null/undefined cases
- Tests now pass without validation errors

Deployed to production 2026-09-30. App running with local postgres migration complete.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015e81pY6swiKHQZDcN86CTD
2026-09-30 14:53:10 +08:00

79 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { analyzePdfsWithDeepSeek, type ExtractedIncidentMetadata, type FileInfo } from './bulk-import'
vi.mock('@/lib/claude/client', () => ({
createDeepSeekClient: vi.fn(() => ({
messages: {
create: vi.fn(async () => ({
content: [
{
type: 'text',
text: JSON.stringify({
description: 'Forklift collision with rack',
date: '2026-08-10',
location: { site: 'Main Warehouse', zone: 'Zone 7' },
incident_type: 'asset',
priority: 'high'
})
}
]
}))
}
}))
}))
describe('analyzePdfsWithDeepSeek', () => {
it('extracts incident metadata from PDF text', async () => {
const pdfTexts = [
'Incident Date: 10 August 2026\nLocation: Main Warehouse Zone 7\nDescription: Forklift collision with rack'
]
const metadata = await analyzePdfsWithDeepSeek(pdfTexts)
expect(metadata).toMatchObject({
description: expect.any(String),
date: '2026-08-10',
location: {
site: 'Main Warehouse',
zone: 'Zone 7'
}
})
})
it('returns empty fields for missing data', async () => {
const pdfTexts = ['Random text with no incident info']
const metadata = await analyzePdfsWithDeepSeek(pdfTexts)
expect(metadata.description).toBeDefined()
expect(metadata.date).toBeDefined()
expect(metadata.location?.site).toBeDefined()
})
})
describe('createBulkIncidents', () => {
it('creates closed incidents with evidence files', async () => {
const metadata: ExtractedIncidentMetadata[] = [
{
description: 'Test incident',
date: '2026-08-10',
location: { site: 'Main WH', zone: 'Zone 7' },
incident_type: 'asset',
priority: 'high'
}
]
const files: FileInfo[] = [
{
path: 'folder/test.pdf',
name: 'test.pdf',
mimeType: 'application/pdf',
buffer: Buffer.from('PDF content')
}
]
// Note: In actual testing, mock db calls or use test database
// This tests the function signature is correct
expect(metadata).toBeDefined()
expect(files).toBeDefined()
})
})