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' , ( ) => {
1+ import { describe , it , expect } from "vitest" ;
2+ import {
3+ calculateMD5Checksum ,
4+ calculateFileChecksum ,
5+ generateFileChecksums ,
6+ } from "../fileChecksum" ;
7+ import { FileInput } from "@gcforms/types" ;
8+ import { readFileSync } from "fs" ;
9+ import { join } from "path" ;
10+
11+ describe ( "fileChecksum" , ( ) => {
812 // Helper function to load CSV fixtures
913 const loadCsvFixture = ( filename : string ) : ArrayBuffer => {
10- const csvPath = join ( __dirname , ' fixtures' , filename ) ;
14+ const csvPath = join ( __dirname , " fixtures" , filename ) ;
1115 return readFileSync ( csvPath ) . buffer ;
1216 } ;
1317
14- describe ( ' calculateMD5Checksum' , ( ) => {
15- it ( ' should calculate MD5 checksum for file content' , ( ) => {
16- const content = loadCsvFixture ( ' sample.csv' ) ;
17- const checksum = calculateMD5Checksum ( content ) ;
18+ describe ( " calculateMD5Checksum" , ( ) => {
19+ it ( " should calculate MD5 checksum for file content" , async ( ) => {
20+ const content = loadCsvFixture ( " sample.csv" ) ;
21+ const checksum = await calculateMD5Checksum ( content ) ;
1822
1923 // This checksum should be consistent for the sample.csv file
2024 expect ( checksum ) . toBeTruthy ( ) ;
21- expect ( typeof checksum ) . toBe ( ' string' ) ;
25+ expect ( typeof checksum ) . toBe ( " string" ) ;
2226 } ) ;
2327
24- it ( ' should return different checksums for different content' , ( ) => {
25- const content1 = loadCsvFixture ( ' sample.csv' ) ;
26- const content2 = new TextEncoder ( ) . encode ( ' Different content' ) . buffer ;
28+ it ( " should return different checksums for different content" , async ( ) => {
29+ const content1 = loadCsvFixture ( " sample.csv" ) ;
30+ const content2 = new TextEncoder ( ) . encode ( " Different content" ) . buffer ;
2731
28- const checksum1 = calculateMD5Checksum ( content1 ) ;
29- const checksum2 = calculateMD5Checksum ( content2 ) ;
32+ const checksum1 = await calculateMD5Checksum ( content1 ) ;
33+ const checksum2 = await calculateMD5Checksum ( content2 ) ;
3034
3135 expect ( checksum1 ) . not . toBe ( checksum2 ) ;
3236 } ) ;
3337
34- it ( ' should return same checksum for same content' , ( ) => {
35- const content1 = loadCsvFixture ( ' sample.csv' ) ;
36- const content2 = loadCsvFixture ( ' sample-copy.csv' ) ;
38+ it ( " should return same checksum for same content" , async ( ) => {
39+ const content1 = loadCsvFixture ( " sample.csv" ) ;
40+ const content2 = loadCsvFixture ( " sample-copy.csv" ) ;
3741
38- const checksum1 = calculateMD5Checksum ( content1 ) ;
39- const checksum2 = calculateMD5Checksum ( content2 ) ;
42+ const checksum1 = await calculateMD5Checksum ( content1 ) ;
43+ const checksum2 = await calculateMD5Checksum ( content2 ) ;
4044
4145 expect ( checksum1 ) . toBe ( checksum2 ) ;
4246 } ) ;
4347 } ) ;
4448
45- describe ( ' calculateFileChecksum' , ( ) => {
46- it ( ' should calculate checksum for FileInput object' , ( ) => {
47- const csvContent = loadCsvFixture ( ' sample.csv' ) ;
49+ describe ( " calculateFileChecksum" , ( ) => {
50+ it ( " should calculate checksum for FileInput object" , async ( ) => {
51+ const csvContent = loadCsvFixture ( " sample.csv" ) ;
4852 const file : FileInput = {
49- name : ' sample.csv' ,
53+ name : " sample.csv" ,
5054 size : csvContent . byteLength ,
5155 content : csvContent ,
5256 } ;
5357
54- const checksum = calculateFileChecksum ( file ) ;
58+ const checksum = await calculateFileChecksum ( file ) ;
5559 expect ( checksum ) . toBeTruthy ( ) ;
56- expect ( typeof checksum ) . toBe ( ' string' ) ;
60+ expect ( typeof checksum ) . toBe ( " string" ) ;
5761 } ) ;
5862
59- it ( ' should throw error for file without content' , ( ) => {
63+ it ( " should throw error for file without content" , async ( ) => {
6064 const file = {
61- name : ' test.txt' ,
65+ name : " test.txt" ,
6266 size : 0 ,
6367 content : null ,
6468 } as unknown as FileInput ;
6569
66- expect ( ( ) => calculateFileChecksum ( file ) ) . toThrow (
67- ' Unable to calculate checksum for file test.txt: no content available'
70+ expect ( calculateFileChecksum ( file ) ) . rejects . toThrow (
71+ " Unable to calculate checksum for file test.txt: no content available"
6872 ) ;
6973 } ) ;
7074 } ) ;
7175
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+ describe ( " generateFileChecksums" , ( ) => {
77+ it ( " should generate checksums for multiple files" , async ( ) => {
78+ const csvContent1 = loadCsvFixture ( " sample.csv" ) ;
79+ const csvContent2 = loadCsvFixture ( " sample-copy.csv" ) ;
7680
7781 const fileObjsRef = {
78- ' file1' : {
79- name : ' sample.csv' ,
82+ file1 : {
83+ name : " sample.csv" ,
8084 size : csvContent1 . byteLength ,
8185 content : csvContent1 ,
8286 } as FileInput ,
83- ' file2' : {
84- name : ' sample-copy.csv' ,
87+ file2 : {
88+ name : " sample-copy.csv" ,
8589 size : csvContent2 . byteLength ,
8690 content : csvContent2 ,
8791 } as FileInput ,
8892 } ;
8993
90- const checksums = generateFileChecksums ( fileObjsRef ) ;
94+ const checksums = await generateFileChecksums ( fileObjsRef ) ;
9195
92- expect ( checksums ) . toHaveProperty ( ' file1' ) ;
93- expect ( checksums ) . toHaveProperty ( ' file2' ) ;
96+ expect ( checksums ) . toHaveProperty ( " file1" ) ;
97+ expect ( checksums ) . toHaveProperty ( " file2" ) ;
9498 // Both files have identical content, so checksums should be the same
9599 expect ( checksums . file1 ) . toBe ( checksums . file2 ) ;
96100 } ) ;
97101
98- it ( ' should handle empty file objects' , ( ) => {
99- const checksums = generateFileChecksums ( { } ) ;
102+ it ( " should handle empty file objects" , async ( ) => {
103+ const checksums = await generateFileChecksums ( { } ) ;
100104 expect ( checksums ) . toEqual ( { } ) ;
101105 } ) ;
102106
103- it ( ' should continue processing when one file fails' , ( ) => {
104- const csvContent = loadCsvFixture ( ' sample.csv' ) ;
107+ it ( " should continue processing when one file fails" , async ( ) => {
108+ const csvContent = loadCsvFixture ( " sample.csv" ) ;
105109
106110 const fileObjsRef = {
107- ' file1' : {
108- name : ' sample.csv' ,
111+ file1 : {
112+ name : " sample.csv" ,
109113 size : csvContent . byteLength ,
110114 content : csvContent ,
111115 } as FileInput ,
112- ' file2' : {
113- name : ' broken.csv' ,
116+ file2 : {
117+ name : " broken.csv" ,
114118 size : 0 ,
115119 content : null ,
116120 } as unknown as FileInput ,
117121 } ;
118122
119- const checksums = generateFileChecksums ( fileObjsRef ) ;
123+ const checksums = await generateFileChecksums ( fileObjsRef ) ;
120124
121- expect ( checksums ) . toHaveProperty ( ' file1' ) ;
122- expect ( checksums ) . not . toHaveProperty ( ' file2' ) ;
123- expect ( typeof checksums . file1 ) . toBe ( ' string' ) ;
125+ expect ( checksums ) . toHaveProperty ( " file1" ) ;
126+ expect ( checksums ) . not . toHaveProperty ( " file2" ) ;
127+ expect ( typeof checksums . file1 ) . toBe ( " string" ) ;
124128 } ) ;
125129 } ) ;
126- } ) ;
130+ } ) ;
0 commit comments