Skip to content

Commit 16a17ae

Browse files
committed
test: add buffer independence and SEA VFS coverage tests
Add test verifying MemoryProvider readFileSync returns independent buffer copies that don't share underlying memory. Add SEA fixture tests for node:sea API coexistence with VFS and node_modules package lookups. Use Uint8Array from primordials in SEA provider.
1 parent 606185e commit 16a17ae

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

lib/internal/vfs/providers/sea.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
SafeSet,
99
StringPrototypeStartsWith,
1010
Symbol,
11+
Uint8Array,
1112
} = primordials;
1213

1314
const { Buffer } = require('buffer');

test/fixtures/sea/vfs/sea-config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"config.json": "config.json",
77
"data/greeting.txt": "greeting.txt",
88
"modules/math.js": "math.js",
9-
"modules/calculator.js": "calculator.js"
9+
"modules/calculator.js": "calculator.js",
10+
"node_modules/test-pkg/package.json": "test-pkg-package.json",
11+
"node_modules/test-pkg/index.js": "test-pkg-index.js"
1012
}
1113
}

test/fixtures/sea/vfs/sea.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,17 @@ assert.strictEqual(calculator.sum(10, 20), 30, 'calculator.sum should work');
4848
assert.strictEqual(calculator.product(3, 7), 21, 'calculator.product should work');
4949
console.log('transitive require from VFS tests passed');
5050

51+
// Test that node:sea API and VFS can load the same asset
52+
const sea = require('node:sea');
53+
const seaAsset = sea.getAsset('data/greeting.txt', 'utf8');
54+
const vfsAsset = fs.readFileSync('/sea/data/greeting.txt', 'utf8');
55+
assert.strictEqual(seaAsset, vfsAsset, 'node:sea and VFS should return the same content');
56+
console.log('node:sea API and VFS coexistence test passed');
57+
58+
// Test node_modules package lookup via VFS
59+
const testPkg = require('test-pkg');
60+
assert.strictEqual(testPkg.name, 'test-pkg', 'package name should match');
61+
assert.strictEqual(testPkg.greet('World'), 'Hello, World!', 'package function should work');
62+
console.log('node_modules package lookup test passed');
63+
5164
console.log('All SEA VFS tests passed!');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
module.exports = {
3+
name: 'test-pkg',
4+
greet: (name) => `Hello, ${name}!`,
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "test-pkg",
3+
"version": "1.0.0",
4+
"main": "index.js"
5+
}

test/parallel/test-vfs-provider-memory.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,28 @@ const vfs = require('node:vfs');
363363
}, { code: 'EISDIR' });
364364
}
365365

366+
// Test that readFileSync returns independent buffer copies
367+
{
368+
const myVfs = vfs.create();
369+
370+
myVfs.writeFileSync('/independent.txt', 'original content');
371+
372+
const buf1 = myVfs.readFileSync('/independent.txt');
373+
const buf2 = myVfs.readFileSync('/independent.txt');
374+
375+
// Both should have the same content
376+
assert.deepStrictEqual(buf1, buf2);
377+
378+
// Mutating one should not affect the other
379+
buf1[0] = 0xFF;
380+
assert.notDeepStrictEqual(buf1, buf2);
381+
assert.strictEqual(buf2.toString(), 'original content');
382+
383+
// A third read should still return the original content
384+
const buf3 = myVfs.readFileSync('/independent.txt');
385+
assert.strictEqual(buf3.toString(), 'original content');
386+
}
387+
366388
// ==================== Async Operations ====================
367389

368390
// Test async read and write operations

0 commit comments

Comments
 (0)