1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
import { test, expect, beforeEach, afterEach } from 'bun:test';
import { calculateFileChecksum, ensureDir, cleanupDir } from '../scripts/cache/files';
import { mkdir, rm, writeFile } from 'fs/promises';
import { join } from 'path';
import { tmpdir } from 'os';
const testDir = join(tmpdir(), 'bun-test-cache');
beforeEach(async () => {
await mkdir(testDir, { recursive: true });
});
afterEach(async () => {
await rm(testDir, { recursive: true, force: true });
});
test('calculateFileChecksum returns consistent hash for same content', async () => {
const filePath = join(testDir, 'test.txt');
await writeFile(filePath, 'Hello, World!');
const hash1 = await calculateFileChecksum(filePath);
const hash2 = await calculateFileChecksum(filePath);
expect(hash1).toBe(hash2);
expect(hash1).toHaveLength(64); // SHA256 produces 64 hex characters
});
test('calculateFileChecksum returns different hashes for different content', async () => {
const filePath1 = join(testDir, 'test1.txt');
const filePath2 = join(testDir, 'test2.txt');
await writeFile(filePath1, 'Hello, World!');
await writeFile(filePath2, 'Goodbye, World!');
const hash1 = await calculateFileChecksum(filePath1);
const hash2 = await calculateFileChecksum(filePath2);
expect(hash1).not.toBe(hash2);
});
test('calculateFileChecksum handles empty file', async () => {
const filePath = join(testDir, 'empty.txt');
await writeFile(filePath, '');
const hash = await calculateFileChecksum(filePath);
expect(hash).toHaveLength(64);
});
test('calculateFileChecksum handles larger file', async () => {
const filePath = join(testDir, 'large.txt');
const content = 'x'.repeat(11 * 1024 * 1024); // ~11MB (over 10MB threshold)
await writeFile(filePath, content);
const hash = await calculateFileChecksum(filePath);
expect(hash).toHaveLength(64);
});
test('ensureDir creates directory if it does not exist', async () => {
const newDir = join(testDir, 'new-directory');
await ensureDir(newDir);
// Check if directory exists by trying to create a file in it
await writeFile(join(newDir, 'test.txt'), 'test');
expect(true).toBe(true); // If we get here, directory exists
});
test('ensureDir does not error if directory already exists', async () => {
await ensureDir(testDir);
await ensureDir(testDir); // Should not throw
expect(true).toBe(true);
});
test('cleanupDir removes directory and all contents', async () => {
const subDir = join(testDir, 'subdir');
await mkdir(subDir);
await writeFile(join(subDir, 'file.txt'), 'content');
await writeFile(join(testDir, 'file2.txt'), 'content2');
await cleanupDir(testDir);
// Directory should be gone
let exists = true;
try {
await writeFile(join(testDir, 'test.txt'), 'test');
} catch {
exists = false;
}
expect(exists).toBe(false);
});
test('cleanupDir handles non-existent directory', async () => {
const nonExistent = join(testDir, 'does-not-exist');
// Should not throw
await cleanupDir(nonExistent);
expect(true).toBe(true);
});
|