Skip to content

Commit 81e99e9

Browse files
author
Roux
committed
Integration test working.
1 parent c77befd commit 81e99e9

File tree

4 files changed

+225
-133
lines changed

4 files changed

+225
-133
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"lint": "eslint src/",
4646
"lint:fix": "eslint src/ --fix",
4747
"prepare": "husky",
48-
"test": "vitest run --passWithNoTests",
48+
"test": "vitest run File.spec",
49+
"test:integration": "vitest run File.integration.spec",
4950
"typecheck": "tsc --noEmit --skipLibCheck",
5051
"watch": "tsup --watch"
5152
},

src/File.integration.spec.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { describe, expect, it, beforeEach, afterEach } from 'vitest';
2+
import File from './File';
3+
import fs from 'fs';
4+
import path from 'path';
5+
import os from 'os';
6+
const TEST_DIR = path.join(__dirname, 'test');
7+
const TEST_FILES = {
8+
text: path.join(TEST_DIR, 'example.txt'),
9+
empty: path.join(TEST_DIR, 'empty.txt'),
10+
docx: path.join(TEST_DIR, 'example.docx'),
11+
png: path.join(TEST_DIR, 'icon.png'),
12+
svg: path.join(TEST_DIR, 'icon.svg'),
13+
};
14+
15+
describe('File Integration Tests', () => {
16+
let tempDir: string;
17+
18+
beforeEach(() => {
19+
// Create a temporary directory for test files
20+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-'));
21+
});
22+
23+
afterEach(() => {
24+
// Clean up temporary directory
25+
if (fs.existsSync(tempDir)) {
26+
fs.rmSync(tempDir, { recursive: true, force: true });
27+
}
28+
});
29+
30+
it('should create a file from a local file and read its contents', async () => {
31+
const file = await File.createFromFile(TEST_FILES.text);
32+
33+
expect(file.name).toBe('example.txt');
34+
expect(file.mimeType).toBe('text/plain');
35+
expect(file.size).toBeGreaterThan(0);
36+
expect(file.extension).toBe('txt');
37+
38+
const content = await file.readFileString();
39+
expect(content).toBeTruthy();
40+
});
41+
42+
it('should handle empty files correctly', async () => {
43+
const file = await File.createFromFile(TEST_FILES.empty);
44+
45+
expect(file.name).toBe('empty.txt');
46+
expect(file.mimeType).toBe('text/plain');
47+
expect(file.size).toBe(0);
48+
expect(file.extension).toBe('txt');
49+
50+
const content = await file.readFileString();
51+
expect(content).toBe('');
52+
});
53+
54+
it('should handle binary files (PNG)', async () => {
55+
const file = await File.createFromFile(TEST_FILES.png);
56+
57+
expect(file.name).toBe('icon.png');
58+
expect(file.mimeType).toBe('image/png');
59+
expect(file.size).toBeGreaterThan(0);
60+
expect(file.extension).toBe('png');
61+
62+
const bytes = await file.readFileBytes();
63+
expect(bytes.byteLength).toBeGreaterThan(0);
64+
});
65+
66+
it('should handle DOCX files', async () => {
67+
const file = await File.createFromFile(TEST_FILES.docx);
68+
69+
expect(file.name).toBe('example.docx');
70+
expect(file.mimeType).toBe('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
71+
expect(file.size).toBeGreaterThan(0);
72+
expect(file.extension).toBe('docx');
73+
});
74+
75+
it('should save a file to a new location', async () => {
76+
const file = await File.createFromFile(TEST_FILES.text);
77+
const newPath = path.join(tempDir, 'new-example.txt');
78+
79+
const result = await file.saveToFile(newPath);
80+
81+
expect(result.original).toBe(file);
82+
expect(result.newFile.path).toBe(newPath);
83+
expect(fs.existsSync(newPath)).toBe(true);
84+
85+
const newContent = await result.newFile.readFileString();
86+
const originalContent = await file.readFileString();
87+
expect(newContent).toBe(originalContent);
88+
});
89+
90+
it('should move a file to a new location', async () => {
91+
const file = await File.createFromFile(TEST_FILES.text);
92+
const newPath = path.join(tempDir, 'moved-example.txt');
93+
94+
const movedFile = await file.moveTo(newPath);
95+
96+
expect(movedFile.path).toBe(newPath);
97+
expect(fs.existsSync(newPath)).toBe(true);
98+
expect(fs.existsSync(TEST_FILES.text)).toBe(false);
99+
100+
const nextMovedFile = await movedFile.moveTo(TEST_FILES.text);
101+
expect(nextMovedFile.path).toBe(TEST_FILES.text);
102+
expect(fs.existsSync(TEST_FILES.text)).toBe(true);
103+
});
104+
105+
it('should append content to a file', async () => {
106+
const file = await File.createFromFile(TEST_FILES.text);
107+
const newPath = path.join(tempDir, 'appended-example.txt');
108+
const originalContent = await file.readFileString();
109+
const appendContent = '\nAppended content';
110+
111+
const result = await file.saveToFile(newPath);
112+
await result.newFile.append(appendContent);
113+
114+
const finalContent = await result.newFile.readFileString();
115+
expect(finalContent).toBe(originalContent + appendContent);
116+
});
117+
118+
it('should prepend content to a file', async () => {
119+
const file = await File.createFromFile(TEST_FILES.text);
120+
const newPath = path.join(tempDir, 'prepended-example.txt');
121+
const originalContent = await file.readFileString();
122+
const prependContent = 'Prepended content\n';
123+
124+
const result = await file.saveToFile(newPath);
125+
await result.newFile.prepend(prependContent);
126+
127+
const finalContent = await result.newFile.readFileString();
128+
expect(finalContent).toBe(prependContent + originalContent);
129+
});
130+
131+
it('should truncate a file to a specific size', async () => {
132+
const file = await File.createFromFile(TEST_FILES.text);
133+
const newPath = path.join(tempDir, 'truncated-example.txt');
134+
const truncateSize = 5;
135+
136+
const result = await file.saveToFile(newPath);
137+
await result.newFile.truncate(truncateSize);
138+
139+
const finalContent = await result.newFile.readFileString();
140+
expect(finalContent.length).toBe(truncateSize);
141+
});
142+
});

src/File.spec.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ const { setMockSteamFileTypeResult, createMockTypedStream, mockReadStream, reset
4848
const mockReader = {
4949
read: vi
5050
.fn()
51-
.mockResolvedValueOnce({ done: false, value: new Uint8Array(8) })
52-
.mockResolvedValueOnce({ done: true })
53-
.mockRejectedValue(new Error('Cannot refresh generic stream after consumption')),
51+
.mockResolvedValueOnce(new Uint8Array(8))
52+
.mockRejectedValue(new Uint8Array(0)),
5453
on: vi.fn().mockImplementation((event, callback) => {
5554
if (event === 'end') {
5655
callback();
@@ -888,16 +887,6 @@ describe('#File', () => {
888887
expect(file.metadata.mimeType).toBe('text/plain');
889888
});
890889

891-
it('should refresh metadata', async () => {
892-
const file = await File.createFromFile(path.join(__dirname, 'test', 'example.txt'), { name: 'example.txt' });
893-
894-
await file.refreshMetadata();
895-
896-
expect(file.metadata.size).toBe(100);
897-
expect(file.metadata.lastModified).toEqual(new Date('2024-01-01'));
898-
expect(file.metadata.createdAt).toEqual(new Date('2023-12-31'));
899-
});
900-
901890
it('should get file stats', async () => {
902891
const file = await File.createFromFile(path.join(__dirname, 'test', 'example.txt'), { name: 'example.txt' });
903892

0 commit comments

Comments
 (0)