Skip to content

Commit 6e093a7

Browse files
committed
Mount tests
1 parent 9a9262c commit 6e093a7

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { createNodeFsMountHandler, loadNodeRuntime } from '..';
2+
import { __private__dont__use, PHP } from '@php-wasm/universal';
3+
import { RecommendedPHPVersion } from '@wp-playground/common';
4+
import path from 'path';
5+
import fs from 'fs';
6+
7+
describe('Mounting', () => {
8+
let php: PHP;
9+
10+
beforeEach(async () => {
11+
php = new PHP(await loadNodeRuntime(RecommendedPHPVersion));
12+
});
13+
afterEach(async () => {
14+
php.exit();
15+
});
16+
17+
describe('Basic mounting functionality', () => {
18+
it('Should mount a file with exact content match', async () => {
19+
const testFilePath = path.join(
20+
__dirname,
21+
'test-data',
22+
'long-post-body.txt'
23+
);
24+
25+
await php.mount(
26+
'/single-file.txt',
27+
createNodeFsMountHandler(testFilePath)
28+
);
29+
30+
const vfsContent = await php.readFileAsText('/single-file.txt');
31+
const localContent = fs.readFileSync(testFilePath, 'utf8');
32+
expect(vfsContent).toEqual(localContent);
33+
});
34+
35+
it('Should mount nested directories with recursive structure matching', async () => {
36+
const testDataPath = path.join(__dirname, 'test-data');
37+
await php.mount(
38+
'/nested-test',
39+
createNodeFsMountHandler(testDataPath)
40+
);
41+
42+
// Recursively compare directory structure
43+
const compareDirectories = (vfsPath: string, localPath: string) => {
44+
if (!fs.existsSync(localPath)) return;
45+
46+
const localFiles = fs.readdirSync(localPath);
47+
const vfsFiles = php.listFiles(vfsPath);
48+
expect(vfsFiles.sort()).toEqual(localFiles.sort());
49+
50+
localFiles.forEach((file) => {
51+
const localFilePath = path.join(localPath, file);
52+
const vfsFilePath = `${vfsPath}/${file}`;
53+
const localStats = fs.statSync(localFilePath);
54+
55+
expect(php.isFile(vfsFilePath)).toBe(localStats.isFile());
56+
expect(php.isDir(vfsFilePath)).toBe(
57+
localStats.isDirectory()
58+
);
59+
60+
if (localStats.isDirectory()) {
61+
compareDirectories(vfsFilePath, localFilePath);
62+
}
63+
});
64+
};
65+
66+
compareDirectories('/nested-test', testDataPath);
67+
68+
// Test specific nested file content
69+
const nestedFilePath =
70+
'/nested-test/nested-symlinked-folder/nested-document.txt';
71+
const localNestedPath = path.join(
72+
testDataPath,
73+
'nested-symlinked-folder',
74+
'nested-document.txt'
75+
);
76+
77+
if (fs.existsSync(localNestedPath)) {
78+
const vfsContent = await php.readFileAsText(nestedFilePath);
79+
const localContent = fs.readFileSync(localNestedPath, 'utf8');
80+
expect(vfsContent).toEqual(localContent);
81+
}
82+
});
83+
});
84+
85+
describe('File types and system operations', () => {
86+
it('Should handle all file types with comprehensive FS comparison', async () => {
87+
const testDataPath = path.join(__dirname, 'test-data');
88+
await php.mount(
89+
'/comprehensive-test',
90+
createNodeFsMountHandler(testDataPath)
91+
);
92+
93+
const localFiles = fs.readdirSync(testDataPath);
94+
95+
for (const file of localFiles) {
96+
const localPath = path.join(testDataPath, file);
97+
const vfsPath = `/comprehensive-test/${file}`;
98+
const localStat = fs.statSync(localPath);
99+
100+
if (localStat.isFile()) {
101+
// Test binary files (images)
102+
if (file.endsWith('.jpg') || file.endsWith('.png')) {
103+
const vfsBinary = await php.readFileAsBuffer(vfsPath);
104+
const localBinary = fs.readFileSync(localPath);
105+
expect(Buffer.from(vfsBinary)).toEqual(localBinary);
106+
expect(vfsBinary.length).toBe(localStat.size);
107+
}
108+
// Test text files (certificates, documents)
109+
else if (file.endsWith('.txt') || file.endsWith('.pem')) {
110+
const vfsText = await php.readFileAsText(vfsPath);
111+
const localText = fs.readFileSync(localPath, 'utf8');
112+
expect(vfsText).toEqual(localText);
113+
expect(vfsText.length).toBe(localStat.size);
114+
115+
// Special certificate validation
116+
if (
117+
file.endsWith('.pem') &&
118+
localText.includes('-----BEGIN CERTIFICATE-----')
119+
) {
120+
expect(vfsText).toContain(
121+
'-----BEGIN CERTIFICATE-----'
122+
);
123+
}
124+
}
125+
126+
// Test stat operations
127+
const phpStat = await php.run({
128+
code: `<?php
129+
$stat = stat('/comprehensive-test/${file}');
130+
echo json_encode([
131+
'size' => $stat['size'],
132+
'mode' => $stat['mode'],
133+
'mtime' => $stat['mtime'],
134+
'is_file' => is_file('/comprehensive-test/${file}'),
135+
'is_readable' => is_readable('/comprehensive-test/${file}'),
136+
'filesize' => filesize('/comprehensive-test/${file}')
137+
]);
138+
`,
139+
});
140+
141+
const vfsStatResult = JSON.parse(phpStat.text);
142+
expect(vfsStatResult.size).toBe(localStat.size);
143+
expect(vfsStatResult.mtime).toBe(
144+
Math.floor(localStat.mtime.getTime() / 1000)
145+
);
146+
expect(vfsStatResult.is_file).toBe(true);
147+
expect(vfsStatResult.is_readable).toBe(true);
148+
expect(vfsStatResult.filesize).toBe(localStat.size);
149+
}
150+
}
151+
152+
// Test directory listing through PHP
153+
const phpListing = await php.run({
154+
code: `<?php
155+
$files = scandir('/comprehensive-test');
156+
echo json_encode(array_filter($files, function($file) {
157+
return !in_array($file, ['.', '..']);
158+
}));
159+
`,
160+
});
161+
162+
const vfsPhpFiles = JSON.parse(phpListing.text);
163+
expect(vfsPhpFiles.sort()).toEqual(localFiles.sort());
164+
});
165+
});
166+
});

0 commit comments

Comments
 (0)