New Base func
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { eventBus } from '@/lib/events/bus'
|
||||
import { makeDocumentAttachment } from '@/tests/helpers'
|
||||
|
||||
// ============================================================
|
||||
// Mock — separate client (no .then) from query builder (thenable)
|
||||
// ============================================================
|
||||
|
||||
let resultIdx: number
|
||||
let results: Array<{ data?: unknown; error?: unknown }>
|
||||
|
||||
function makeBuilder() {
|
||||
const b: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'insert', 'update', 'delete', 'lte', 'gte', 'in', 'not', 'or', 'order', 'limit', 'is']) {
|
||||
b[m] = vi.fn().mockReturnValue(b)
|
||||
}
|
||||
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
b.maybeSingle = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
b.then = (resolve: (v: unknown) => void) => resolve(results[resultIdx++] ?? { data: null, error: null })
|
||||
return b
|
||||
}
|
||||
|
||||
function makeClient(storageOverrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
from: vi.fn().mockImplementation(() => makeBuilder()),
|
||||
rpc: vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null }),
|
||||
storage: {
|
||||
from: vi.fn().mockReturnValue({
|
||||
upload: vi.fn().mockResolvedValue({ data: {}, error: null }),
|
||||
download: vi.fn().mockResolvedValue({
|
||||
data: new Blob(['test content']),
|
||||
error: null,
|
||||
}),
|
||||
remove: vi.fn().mockResolvedValue({ data: [], error: null }),
|
||||
getPublicUrl: vi.fn().mockReturnValue({
|
||||
data: { publicUrl: 'https://example.com/file.pdf' },
|
||||
}),
|
||||
...storageOverrides,
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: vi.fn(async () => makeClient()),
|
||||
}))
|
||||
|
||||
import { uploadDocument, createNewVersion, verifyIntegrity } from '../document-service'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
eventBus.clear()
|
||||
resultIdx = 0
|
||||
results = []
|
||||
// Reset the mock to use default makeClient
|
||||
vi.mocked(createClient).mockImplementation(async () => makeClient() as never)
|
||||
})
|
||||
|
||||
describe('uploadDocument', () => {
|
||||
it('computes SHA-256 hash, stores metadata, emits document.uploaded', async () => {
|
||||
const doc = makeDocumentAttachment({
|
||||
id: 'doc-1',
|
||||
file_name: 'test.pdf',
|
||||
sha256_hash: 'computed-hash',
|
||||
})
|
||||
|
||||
results = [
|
||||
{ data: doc, error: null }, // insert record
|
||||
]
|
||||
|
||||
const handler = vi.fn()
|
||||
eventBus.on('document.uploaded', handler)
|
||||
|
||||
const buffer = new TextEncoder().encode('test content').buffer
|
||||
const result = await uploadDocument('user-1', {
|
||||
name: 'test.pdf',
|
||||
buffer: buffer as ArrayBuffer,
|
||||
type: 'application/pdf',
|
||||
})
|
||||
|
||||
expect(result.id).toBe('doc-1')
|
||||
expect(result.file_name).toBe('test.pdf')
|
||||
expect(handler).toHaveBeenCalledOnce()
|
||||
expect(handler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
document: expect.objectContaining({ id: 'doc-1' }),
|
||||
userId: 'user-1',
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createNewVersion', () => {
|
||||
it('increments version and supersedes previous', async () => {
|
||||
const current = makeDocumentAttachment({
|
||||
id: 'doc-1',
|
||||
version: 1,
|
||||
is_current_version: true,
|
||||
original_id: null,
|
||||
})
|
||||
const newVersion = makeDocumentAttachment({
|
||||
id: 'doc-2',
|
||||
version: 2,
|
||||
is_current_version: true,
|
||||
original_id: 'doc-1',
|
||||
})
|
||||
|
||||
results = [
|
||||
{ data: current, error: null }, // fetch current
|
||||
{ data: newVersion, error: null }, // insert new version
|
||||
]
|
||||
|
||||
const buffer = new TextEncoder().encode('new content').buffer
|
||||
const result = await createNewVersion('user-1', 'doc-1', {
|
||||
name: 'test-v2.pdf',
|
||||
buffer: buffer as ArrayBuffer,
|
||||
type: 'application/pdf',
|
||||
})
|
||||
|
||||
expect(result.version).toBe(2)
|
||||
expect(result.original_id).toBe('doc-1')
|
||||
expect(result.is_current_version).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('verifyIntegrity', () => {
|
||||
it('returns valid when hashes match', async () => {
|
||||
const content = 'test content for integrity check'
|
||||
const buffer = new TextEncoder().encode(content)
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer)
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
||||
const expectedHash = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')
|
||||
|
||||
results = [
|
||||
{ data: { storage_path: 'docs/test.pdf', sha256_hash: expectedHash }, error: null },
|
||||
]
|
||||
|
||||
// Override createClient to provide matching download content
|
||||
vi.mocked(createClient).mockImplementation(async () =>
|
||||
makeClient({
|
||||
download: vi.fn().mockResolvedValue({
|
||||
data: new Blob([content]),
|
||||
error: null,
|
||||
}),
|
||||
}) as never
|
||||
)
|
||||
|
||||
const result = await verifyIntegrity('user-1', 'doc-1')
|
||||
expect(result.valid).toBe(true)
|
||||
expect(result.storedHash).toBe(expectedHash)
|
||||
expect(result.computedHash).toBe(expectedHash)
|
||||
})
|
||||
|
||||
it('returns invalid when hashes do not match', async () => {
|
||||
results = [
|
||||
{ data: { storage_path: 'docs/test.pdf', sha256_hash: 'stored-hash-abc' }, error: null },
|
||||
]
|
||||
|
||||
vi.mocked(createClient).mockImplementation(async () =>
|
||||
makeClient({
|
||||
download: vi.fn().mockResolvedValue({
|
||||
data: new Blob(['different content']),
|
||||
error: null,
|
||||
}),
|
||||
}) as never
|
||||
)
|
||||
|
||||
const result = await verifyIntegrity('user-1', 'doc-1')
|
||||
expect(result.valid).toBe(false)
|
||||
expect(result.storedHash).toBe('stored-hash-abc')
|
||||
expect(result.computedHash).not.toBe('stored-hash-abc')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,243 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { eventBus } from '@/lib/events'
|
||||
import type { DocumentAttachment, CreateDocumentAttachmentInput, DocumentUploadSource } from '@/types'
|
||||
|
||||
/**
|
||||
* Document Service - WORM-style document archive
|
||||
*
|
||||
* Handles document upload with SHA-256 integrity, version chains,
|
||||
* and linking to journal entries. Deletion is blocked by DB triggers
|
||||
* for documents linked to committed entries.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compute SHA-256 hash of a file buffer
|
||||
*/
|
||||
async function computeSHA256(buffer: ArrayBuffer): Promise<string> {
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer)
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
||||
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a document and create a record with SHA-256 integrity hash
|
||||
*/
|
||||
export async function uploadDocument(
|
||||
userId: string,
|
||||
file: { name: string; buffer: ArrayBuffer; type?: string },
|
||||
metadata: {
|
||||
upload_source?: DocumentUploadSource
|
||||
journal_entry_id?: string
|
||||
journal_entry_line_id?: string
|
||||
} = {}
|
||||
): Promise<DocumentAttachment> {
|
||||
const supabase = await createClient()
|
||||
|
||||
// Compute SHA-256 hash
|
||||
const sha256Hash = await computeSHA256(file.buffer)
|
||||
|
||||
// Generate storage path
|
||||
const timestamp = Date.now()
|
||||
const storagePath = `documents/${userId}/${timestamp}_${file.name}`
|
||||
|
||||
// Upload to Supabase Storage
|
||||
const { error: uploadError } = await supabase.storage
|
||||
.from('documents')
|
||||
.upload(storagePath, file.buffer, {
|
||||
contentType: file.type || 'application/octet-stream',
|
||||
upsert: false,
|
||||
})
|
||||
|
||||
if (uploadError) {
|
||||
throw new Error(`Failed to upload document: ${uploadError.message}`)
|
||||
}
|
||||
|
||||
// Create document record
|
||||
const { data, error } = await supabase
|
||||
.from('document_attachments')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
storage_path: storagePath,
|
||||
file_name: file.name,
|
||||
file_size_bytes: file.buffer.byteLength,
|
||||
mime_type: file.type || null,
|
||||
sha256_hash: sha256Hash,
|
||||
version: 1,
|
||||
is_current_version: true,
|
||||
uploaded_by: userId,
|
||||
upload_source: metadata.upload_source || 'file_upload',
|
||||
digitization_date: new Date().toISOString(),
|
||||
journal_entry_id: metadata.journal_entry_id || null,
|
||||
journal_entry_line_id: metadata.journal_entry_line_id || null,
|
||||
})
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
// Clean up uploaded file on record creation failure
|
||||
await supabase.storage.from('documents').remove([storagePath])
|
||||
throw new Error(`Failed to create document record: ${error.message}`)
|
||||
}
|
||||
|
||||
const result = data as DocumentAttachment
|
||||
|
||||
await eventBus.emit({
|
||||
type: 'document.uploaded',
|
||||
payload: { document: result, userId },
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new version of an existing document (WORM: old version is superseded)
|
||||
*/
|
||||
export async function createNewVersion(
|
||||
userId: string,
|
||||
originalId: string,
|
||||
file: { name: string; buffer: ArrayBuffer; type?: string }
|
||||
): Promise<DocumentAttachment> {
|
||||
const supabase = await createClient()
|
||||
|
||||
// Fetch the original/current version
|
||||
const { data: current, error: fetchError } = await supabase
|
||||
.from('document_attachments')
|
||||
.select('*')
|
||||
.eq('id', originalId)
|
||||
.eq('user_id', userId)
|
||||
.eq('is_current_version', true)
|
||||
.single()
|
||||
|
||||
if (fetchError || !current) {
|
||||
throw new Error('Original document not found or not the current version')
|
||||
}
|
||||
|
||||
const rootOriginalId = current.original_id || current.id
|
||||
const newVersion = current.version + 1
|
||||
|
||||
// Compute SHA-256 hash
|
||||
const sha256Hash = await computeSHA256(file.buffer)
|
||||
|
||||
// Upload new file
|
||||
const timestamp = Date.now()
|
||||
const storagePath = `documents/${userId}/${timestamp}_v${newVersion}_${file.name}`
|
||||
|
||||
const { error: uploadError } = await supabase.storage
|
||||
.from('documents')
|
||||
.upload(storagePath, file.buffer, {
|
||||
contentType: file.type || 'application/octet-stream',
|
||||
upsert: false,
|
||||
})
|
||||
|
||||
if (uploadError) {
|
||||
throw new Error(`Failed to upload new version: ${uploadError.message}`)
|
||||
}
|
||||
|
||||
// Create new version record
|
||||
const { data: newDoc, error: insertError } = await supabase
|
||||
.from('document_attachments')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
storage_path: storagePath,
|
||||
file_name: file.name,
|
||||
file_size_bytes: file.buffer.byteLength,
|
||||
mime_type: file.type || null,
|
||||
sha256_hash: sha256Hash,
|
||||
version: newVersion,
|
||||
original_id: rootOriginalId,
|
||||
is_current_version: true,
|
||||
uploaded_by: userId,
|
||||
upload_source: current.upload_source,
|
||||
digitization_date: new Date().toISOString(),
|
||||
journal_entry_id: current.journal_entry_id,
|
||||
journal_entry_line_id: current.journal_entry_line_id,
|
||||
})
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (insertError) {
|
||||
await supabase.storage.from('documents').remove([storagePath])
|
||||
throw new Error(`Failed to create new version record: ${insertError.message}`)
|
||||
}
|
||||
|
||||
// Mark old version as superseded
|
||||
await supabase
|
||||
.from('document_attachments')
|
||||
.update({
|
||||
is_current_version: false,
|
||||
superseded_by_id: newDoc.id,
|
||||
})
|
||||
.eq('id', current.id)
|
||||
|
||||
return newDoc as DocumentAttachment
|
||||
}
|
||||
|
||||
/**
|
||||
* Link an existing document to a journal entry
|
||||
*/
|
||||
export async function linkToJournalEntry(
|
||||
userId: string,
|
||||
documentId: string,
|
||||
journalEntryId: string,
|
||||
journalEntryLineId?: string
|
||||
): Promise<DocumentAttachment> {
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('document_attachments')
|
||||
.update({
|
||||
journal_entry_id: journalEntryId,
|
||||
journal_entry_line_id: journalEntryLineId || null,
|
||||
})
|
||||
.eq('id', documentId)
|
||||
.eq('user_id', userId)
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
throw new Error(`Failed to link document: ${error.message}`)
|
||||
}
|
||||
|
||||
return data as DocumentAttachment
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify document integrity by re-hashing and comparing
|
||||
*/
|
||||
export async function verifyIntegrity(
|
||||
userId: string,
|
||||
documentId: string
|
||||
): Promise<{ valid: boolean; storedHash: string; computedHash: string }> {
|
||||
const supabase = await createClient()
|
||||
|
||||
// Fetch document record
|
||||
const { data: doc, error: docError } = await supabase
|
||||
.from('document_attachments')
|
||||
.select('storage_path, sha256_hash')
|
||||
.eq('id', documentId)
|
||||
.eq('user_id', userId)
|
||||
.single()
|
||||
|
||||
if (docError || !doc) {
|
||||
throw new Error('Document not found')
|
||||
}
|
||||
|
||||
// Download file from storage
|
||||
const { data: fileData, error: downloadError } = await supabase.storage
|
||||
.from('documents')
|
||||
.download(doc.storage_path)
|
||||
|
||||
if (downloadError || !fileData) {
|
||||
throw new Error(`Failed to download document: ${downloadError?.message}`)
|
||||
}
|
||||
|
||||
// Re-compute hash
|
||||
const buffer = await fileData.arrayBuffer()
|
||||
const computedHash = await computeSHA256(buffer)
|
||||
|
||||
return {
|
||||
valid: computedHash === doc.sha256_hash,
|
||||
storedHash: doc.sha256_hash,
|
||||
computedHash,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user