1+ import { describe , it , expect } from 'vitest' ;
2+ import { calculateMD5Checksum , calculateFileChecksum , generateFileChecksums } from '../fileChecksum' ;
3+ import { FileInput } from '@gcforms/types' ;
4+ import { readFileSync } from 'fs' ;
5+ import { join } from 'path' ;
6+
7+ describe ( 'fileChecksum' , ( ) => {
8+ // Helper function to load CSV fixtures
9+ const loadCsvFixture = ( filename : string ) : ArrayBuffer => {
10+ const csvPath = join ( __dirname , 'fixtures' , filename ) ;
11+ return readFileSync ( csvPath ) . buffer ;
12+ } ;
13+
14+ describe ( 'calculateMD5Checksum' , ( ) => {
15+ it ( 'should calculate MD5 checksum for file content' , ( ) => {
16+ const content = loadCsvFixture ( 'sample.csv' ) ;
17+ const checksum = calculateMD5Checksum ( content ) ;
18+
19+ // This checksum should be consistent for the sample.csv file
20+ expect ( checksum ) . toBeTruthy ( ) ;
21+ expect ( typeof checksum ) . toBe ( 'string' ) ;
22+ } ) ;
23+
24+ it ( 'should return different checksums for different content' , ( ) => {
25+ const content1 = loadCsvFixture ( 'sample.csv' ) ;
26+ const content2 = new TextEncoder ( ) . encode ( 'Different content' ) . buffer ;
27+
28+ const checksum1 = calculateMD5Checksum ( content1 ) ;
29+ const checksum2 = calculateMD5Checksum ( content2 ) ;
30+
31+ expect ( checksum1 ) . not . toBe ( checksum2 ) ;
32+ } ) ;
33+
34+ it ( 'should return same checksum for same content' , ( ) => {
35+ const content1 = loadCsvFixture ( 'sample.csv' ) ;
36+ const content2 = loadCsvFixture ( 'sample-copy.csv' ) ;
37+
38+ const checksum1 = calculateMD5Checksum ( content1 ) ;
39+ const checksum2 = calculateMD5Checksum ( content2 ) ;
40+
41+ expect ( checksum1 ) . toBe ( checksum2 ) ;
42+ } ) ;
43+ } ) ;
44+
45+ describe ( 'calculateFileChecksum' , ( ) => {
46+ it ( 'should calculate checksum for FileInput object' , ( ) => {
47+ const csvContent = loadCsvFixture ( 'sample.csv' ) ;
48+ const file : FileInput = {
49+ name : 'sample.csv' ,
50+ size : csvContent . byteLength ,
51+ content : csvContent ,
52+ } ;
53+
54+ const checksum = calculateFileChecksum ( file ) ;
55+ expect ( checksum ) . toBeTruthy ( ) ;
56+ expect ( typeof checksum ) . toBe ( 'string' ) ;
57+ } ) ;
58+
59+ it ( 'should throw error for file without content' , ( ) => {
60+ const file = {
61+ name : 'test.txt' ,
62+ size : 0 ,
63+ content : null ,
64+ } as unknown as FileInput ;
65+
66+ expect ( ( ) => calculateFileChecksum ( file ) ) . toThrow (
67+ 'Unable to calculate checksum for file test.txt: no content available'
68+ ) ;
69+ } ) ;
70+ } ) ;
71+
72+ describe ( 'generateFileChecksums' , ( ) => {
73+ it ( 'should generate checksums for multiple files' , ( ) => {
74+ const csvContent1 = loadCsvFixture ( 'sample.csv' ) ;
75+ const csvContent2 = loadCsvFixture ( 'sample-copy.csv' ) ;
76+
77+ const fileObjsRef = {
78+ 'file1' : {
79+ name : 'sample.csv' ,
80+ size : csvContent1 . byteLength ,
81+ content : csvContent1 ,
82+ } as FileInput ,
83+ 'file2' : {
84+ name : 'sample-copy.csv' ,
85+ size : csvContent2 . byteLength ,
86+ content : csvContent2 ,
87+ } as FileInput ,
88+ } ;
89+
90+ const checksums = generateFileChecksums ( fileObjsRef ) ;
91+
92+ expect ( checksums ) . toHaveProperty ( 'file1' ) ;
93+ expect ( checksums ) . toHaveProperty ( 'file2' ) ;
94+ // Both files have identical content, so checksums should be the same
95+ expect ( checksums . file1 ) . toBe ( checksums . file2 ) ;
96+ } ) ;
97+
98+ it ( 'should handle empty file objects' , ( ) => {
99+ const checksums = generateFileChecksums ( { } ) ;
100+ expect ( checksums ) . toEqual ( { } ) ;
101+ } ) ;
102+
103+ it ( 'should continue processing when one file fails' , ( ) => {
104+ const csvContent = loadCsvFixture ( 'sample.csv' ) ;
105+
106+ const fileObjsRef = {
107+ 'file1' : {
108+ name : 'sample.csv' ,
109+ size : csvContent . byteLength ,
110+ content : csvContent ,
111+ } as FileInput ,
112+ 'file2' : {
113+ name : 'broken.csv' ,
114+ size : 0 ,
115+ content : null ,
116+ } as unknown as FileInput ,
117+ } ;
118+
119+ const checksums = generateFileChecksums ( fileObjsRef ) ;
120+
121+ expect ( checksums ) . toHaveProperty ( 'file1' ) ;
122+ expect ( checksums ) . not . toHaveProperty ( 'file2' ) ;
123+ expect ( typeof checksums . file1 ) . toBe ( 'string' ) ;
124+ } ) ;
125+ } ) ;
126+ } ) ;
0 commit comments