6 "github.com/stretchr/testify/assert"
12 func TestEncodeFilePath(t *testing.T) {
13 var tests = []struct {
18 filePath: "kitten1.jpg",
20 0x00, 0x01, // number of items in path
21 0x00, 0x00, // leading path separator
22 0x0b, // length of next path section (11)
23 0x6b, 0x69, 0x74, 0x74, 0x65, 0x6e, 0x31, 0x2e, 0x6a, 0x70, 0x67, // kitten1.jpg
27 filePath: "foo/kitten1.jpg",
29 0x00, 0x02, // number of items in path
33 0x00, 0x00, // leading path separator
34 0x0b, // length of next path section (11)
35 0x6b, 0x69, 0x74, 0x74, 0x65, 0x6e, 0x31, 0x2e, 0x6a, 0x70, 0x67, // kitten1.jpg
40 for _, test := range tests {
41 got := EncodeFilePath(test.filePath)
42 if !bytes.Equal(got, test.want) {
43 t.Errorf("field mismatch: want: %#v got: %#v", test.want, got)
48 func TestCalcTotalSize(t *testing.T) {
50 defer func() { _ = os.Chdir(cwd) }()
52 _ = os.Chdir("test/config/Files")
68 want: []byte{0x00, 0x00, 0x18, 0x00},
71 // TODO: Add more test cases.
73 for _, tt := range tests {
74 t.Run(tt.name, func(t *testing.T) {
75 got, err := CalcTotalSize(tt.args.filePath)
76 if (err != nil) != tt.wantErr {
77 t.Errorf("CalcTotalSize() error = %v, wantErr %v", err, tt.wantErr)
80 if !assert.Equal(t, tt.want, got) {
81 t.Errorf("CalcTotalSize() got = %v, want %v", got, tt.want)
87 func createTestDirStructure(baseDir string, structure map[string]string) error {
88 // First pass: create directories
89 for path, content := range structure {
91 if err := os.MkdirAll(filepath.Join(baseDir, path), 0755); err != nil {
97 // Second pass: create files
98 for path, content := range structure {
100 fullPath := filepath.Join(baseDir, path)
101 dir := filepath.Dir(fullPath)
102 if err := os.MkdirAll(dir, 0755); err != nil {
105 if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
113 func TestCalcItemCount(t *testing.T) {
116 structure map[string]string
120 name: "directory with files",
121 structure: map[string]string{
122 "file1.txt": "content1",
123 "file2.txt": "content2",
125 "subdir/file3.txt": "content3",
127 expected: 4, // 3 files and 1 directory, should count 4 items
130 name: "directory with hidden files",
131 structure: map[string]string{
132 ".hiddenfile": "hiddencontent",
133 "file1.txt": "content1",
135 expected: 1, // 1 non-hidden file
138 name: "empty directory",
139 structure: map[string]string{},
140 expected: 0, // 0 files
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 // Create a temporary directory for the test
147 tempDir, err := os.MkdirTemp("", "test")
149 t.Fatalf("Failed to create temp dir: %v", err)
151 defer os.RemoveAll(tempDir)
153 // Create the test directory structure
154 if err := createTestDirStructure(tempDir, tt.structure); err != nil {
155 t.Fatalf("Failed to create test dir structure: %v", err)
158 // Calculate item count
159 result, err := CalcItemCount(tempDir)
161 t.Fatalf("CalcItemCount returned an error: %v", err)
164 // Convert result to uint16
165 count := binary.BigEndian.Uint16(result)
166 if count != tt.expected {
167 t.Errorf("expected %d, got %d", tt.expected, count)