11import type { DocumentData } from '@doc-agent/core' ;
22import { beforeEach , describe , expect , it } from 'vitest' ;
33import { createDb } from '../db' ;
4- import { DocumentRepository } from '../index' ;
4+ import { computePathHash , DocumentRepository } from '../index' ;
55
66describe ( 'DocumentRepository' , ( ) => {
77 let repo : DocumentRepository ;
@@ -32,10 +32,11 @@ describe('DocumentRepository', () => {
3232 ...mockDoc ,
3333 extractedAt : mockDoc . extractedAt . toISOString ( ) ,
3434 } ) ;
35- expect ( result ?. path ) . toBe ( '/tmp/invoice.pdf' ) ;
35+ expect ( result ?. filename ) . toBe ( 'invoice.pdf' ) ;
36+ expect ( result ?. pathHash ) . toBe ( computePathHash ( '/tmp/invoice.pdf' ) ) ;
3637 } ) ;
3738
38- it ( 'should update an existing document on save ' , async ( ) => {
39+ it ( 'should upsert by path (same file = update, not duplicate) ' , async ( ) => {
3940 const mockDoc : DocumentData = {
4041 id : '123' ,
4142 filename : 'invoice.pdf' ,
@@ -47,22 +48,46 @@ describe('DocumentRepository', () => {
4748
4849 await repo . saveDocument ( mockDoc , '/tmp/invoice.pdf' ) ;
4950
50- // Update amount
51- const updatedDoc = { ...mockDoc , amount : 200 } ;
51+ // Re-extract same file with new ID and updated data
52+ const updatedDoc = { ...mockDoc , id : '456' , amount : 200 } ;
5253 await repo . saveDocument ( updatedDoc , '/tmp/invoice.pdf' ) ;
5354
54- const result = await repo . getDocument ( '123' ) ;
55- expect ( result ?. data . amount ) . toBe ( 200 ) ;
55+ // Should have updated the existing record, not created a new one
56+ const list = await repo . listDocuments ( ) ;
57+ expect ( list ) . toHaveLength ( 1 ) ;
58+ expect ( list [ 0 ] . id ) . toBe ( '456' ) ; // ID updated
59+ expect ( list [ 0 ] . data . amount ) . toBe ( 200 ) ; // Data updated
5660 } ) ;
5761
5862 it ( 'should list all documents' , async ( ) => {
5963 const doc1 = { id : '1' , filename : 'a.pdf' , type : 'invoice' as const , extractedAt : new Date ( ) } ;
6064 const doc2 = { id : '2' , filename : 'b.pdf' , type : 'receipt' as const , extractedAt : new Date ( ) } ;
6165
62- await repo . saveDocument ( doc1 , '/a' ) ;
63- await repo . saveDocument ( doc2 , '/b' ) ;
66+ await repo . saveDocument ( doc1 , '/a.pdf ' ) ;
67+ await repo . saveDocument ( doc2 , '/b.pdf ' ) ;
6468
6569 const list = await repo . listDocuments ( ) ;
6670 expect ( list ) . toHaveLength ( 2 ) ;
6771 } ) ;
6872} ) ;
73+
74+ describe ( 'computePathHash' , ( ) => {
75+ it ( 'should return consistent hash for same absolute path' , ( ) => {
76+ const hash1 = computePathHash ( '/tmp/invoice.pdf' ) ;
77+ const hash2 = computePathHash ( '/tmp/invoice.pdf' ) ;
78+ expect ( hash1 ) . toBe ( hash2 ) ;
79+ } ) ;
80+
81+ it ( 'should return different hash for different paths' , ( ) => {
82+ const hash1 = computePathHash ( '/tmp/a.pdf' ) ;
83+ const hash2 = computePathHash ( '/tmp/b.pdf' ) ;
84+ expect ( hash1 ) . not . toBe ( hash2 ) ;
85+ } ) ;
86+
87+ it ( 'should resolve relative paths to absolute' , ( ) => {
88+ // Same file, different ways of referring to it
89+ const hash1 = computePathHash ( './test.pdf' ) ;
90+ const hash2 = computePathHash ( 'test.pdf' ) ;
91+ expect ( hash1 ) . toBe ( hash2 ) ;
92+ } ) ;
93+ } ) ;
0 commit comments