|
| 1 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { FileSystemError } from "../core/index"; |
| 5 | +import { compareFileHash } from "./file-compare"; |
| 6 | + |
| 7 | +describe("compareFileHash", () => { |
| 8 | + let tempDir: string; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + tempDir = await mkdtemp(join(tmpdir(), "baedal-test-")); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async () => { |
| 15 | + await rm(tempDir, { force: true, recursive: true }); |
| 16 | + }); |
| 17 | + |
| 18 | + describe("basic functionality", () => { |
| 19 | + it("should return true for identical files", async () => { |
| 20 | + const file1 = join(tempDir, "file1.txt"); |
| 21 | + const file2 = join(tempDir, "file2.txt"); |
| 22 | + |
| 23 | + await writeFile(file1, "Hello World"); |
| 24 | + await writeFile(file2, "Hello World"); |
| 25 | + |
| 26 | + const result = await compareFileHash(file1, file2); |
| 27 | + expect(result).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should return false for different files", async () => { |
| 31 | + const file1 = join(tempDir, "file1.txt"); |
| 32 | + const file2 = join(tempDir, "file2.txt"); |
| 33 | + |
| 34 | + await writeFile(file1, "Hello World"); |
| 35 | + await writeFile(file2, "Goodbye World"); |
| 36 | + |
| 37 | + const result = await compareFileHash(file1, file2); |
| 38 | + expect(result).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return true for empty files", async () => { |
| 42 | + const file1 = join(tempDir, "empty1.txt"); |
| 43 | + const file2 = join(tempDir, "empty2.txt"); |
| 44 | + |
| 45 | + await writeFile(file1, ""); |
| 46 | + await writeFile(file2, ""); |
| 47 | + |
| 48 | + const result = await compareFileHash(file1, file2); |
| 49 | + expect(result).toBe(true); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("edge cases", () => { |
| 54 | + it("should return false when files differ by a single character", async () => { |
| 55 | + const file1 = join(tempDir, "file1.txt"); |
| 56 | + const file2 = join(tempDir, "file2.txt"); |
| 57 | + |
| 58 | + await writeFile(file1, "Hello World"); |
| 59 | + await writeFile(file2, "Hello world"); |
| 60 | + |
| 61 | + const result = await compareFileHash(file1, file2); |
| 62 | + expect(result).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should return false when files differ by whitespace", async () => { |
| 66 | + const file1 = join(tempDir, "file1.txt"); |
| 67 | + const file2 = join(tempDir, "file2.txt"); |
| 68 | + |
| 69 | + await writeFile(file1, "Hello World"); |
| 70 | + await writeFile(file2, "Hello World "); |
| 71 | + |
| 72 | + const result = await compareFileHash(file1, file2); |
| 73 | + expect(result).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return false when files differ by newlines", async () => { |
| 77 | + const file1 = join(tempDir, "file1.txt"); |
| 78 | + const file2 = join(tempDir, "file2.txt"); |
| 79 | + |
| 80 | + await writeFile(file1, "Hello\nWorld"); |
| 81 | + await writeFile(file2, "Hello\r\nWorld"); |
| 82 | + |
| 83 | + const result = await compareFileHash(file1, file2); |
| 84 | + expect(result).toBe(false); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("binary files", () => { |
| 89 | + it("should correctly compare identical binary files", async () => { |
| 90 | + const file1 = join(tempDir, "binary1.bin"); |
| 91 | + const file2 = join(tempDir, "binary2.bin"); |
| 92 | + |
| 93 | + const buffer = Buffer.from([0x00, 0x01, 0x02, 0xff, 0xfe, 0xfd]); |
| 94 | + await writeFile(file1, buffer); |
| 95 | + await writeFile(file2, buffer); |
| 96 | + |
| 97 | + const result = await compareFileHash(file1, file2); |
| 98 | + expect(result).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should correctly detect different binary files", async () => { |
| 102 | + const file1 = join(tempDir, "binary1.bin"); |
| 103 | + const file2 = join(tempDir, "binary2.bin"); |
| 104 | + |
| 105 | + await writeFile(file1, Buffer.from([0x00, 0x01, 0x02])); |
| 106 | + await writeFile(file2, Buffer.from([0x00, 0x01, 0x03])); |
| 107 | + |
| 108 | + const result = await compareFileHash(file1, file2); |
| 109 | + expect(result).toBe(false); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("large files", () => { |
| 114 | + it("should handle large files efficiently", async () => { |
| 115 | + const file1 = join(tempDir, "large1.txt"); |
| 116 | + const file2 = join(tempDir, "large2.txt"); |
| 117 | + |
| 118 | + const largeContent = "x".repeat(1024 * 1024); |
| 119 | + await writeFile(file1, largeContent); |
| 120 | + await writeFile(file2, largeContent); |
| 121 | + |
| 122 | + const result = await compareFileHash(file1, file2); |
| 123 | + expect(result).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should detect differences in large files", async () => { |
| 127 | + const file1 = join(tempDir, "large1.txt"); |
| 128 | + const file2 = join(tempDir, "large2.txt"); |
| 129 | + |
| 130 | + const largeContent1 = "x".repeat(1024 * 1024); |
| 131 | + const largeContent2 = "x".repeat(1024 * 1024 - 1) + "y"; |
| 132 | + |
| 133 | + await writeFile(file1, largeContent1); |
| 134 | + await writeFile(file2, largeContent2); |
| 135 | + |
| 136 | + const result = await compareFileHash(file1, file2); |
| 137 | + expect(result).toBe(false); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("error handling", () => { |
| 142 | + it("should throw FileSystemError when first file does not exist", async () => { |
| 143 | + const nonExistentFile = join(tempDir, "nonexistent1.txt"); |
| 144 | + const existingFile = join(tempDir, "existing.txt"); |
| 145 | + |
| 146 | + await writeFile(existingFile, "content"); |
| 147 | + |
| 148 | + await expect(compareFileHash(nonExistentFile, existingFile)).rejects.toThrow(FileSystemError); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should throw FileSystemError when second file does not exist", async () => { |
| 152 | + const existingFile = join(tempDir, "existing.txt"); |
| 153 | + const nonExistentFile = join(tempDir, "nonexistent2.txt"); |
| 154 | + |
| 155 | + await writeFile(existingFile, "content"); |
| 156 | + |
| 157 | + await expect(compareFileHash(existingFile, nonExistentFile)).rejects.toThrow(FileSystemError); |
| 158 | + }); |
| 159 | + |
| 160 | + it("should throw FileSystemError with file path context", async () => { |
| 161 | + const nonExistentFile = join(tempDir, "nonexistent.txt"); |
| 162 | + const existingFile = join(tempDir, "existing.txt"); |
| 163 | + |
| 164 | + await writeFile(existingFile, "content"); |
| 165 | + |
| 166 | + await expect(compareFileHash(nonExistentFile, existingFile)).rejects.toThrow( |
| 167 | + expect.objectContaining({ |
| 168 | + message: expect.stringContaining("Failed to calculate hash"), |
| 169 | + }) |
| 170 | + ); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments