Files
ims/lib/incidents/bulk-import.test.ts
T
admin 1422d3df47 fix(bulk-import): skip __MACOSX folder when processing zip imports
Build errors fixed:
- Fixed session field access (session.sub instead of session.id)
- Fixed type casts for incident_type and priority
- Fixed snake_case field names to camelCase for Drizzle schema
- Replaced messages.create with chat.completions.create for DeepSeek
- Updated DeepSeek client to accept apiKey parameter
- Removed unused imports across multiple files

Route now accessible at /bulk-import. Redirects unauthenticated users to
/login?redirect=%2Fbulk-import.

Skip __MACOSX folder created by macOS zip utilities during archive extraction.
2026-08-19 18:45:34 +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()
})
})