const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const vm = require('node:vm'); const { spawnSync } = require('node:child_process'); const ts = require('typescript'); const source = fs.readFileSync(path.join(__dirname, '../src/main/remoteModuleFiles.ts'), 'utf8'); const context = { exports: {}, Buffer }; vm.runInNewContext(ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText, context); const script = context.exports.remoteModuleFilesScript; const root = fs.mkdtempSync(path.join(os.tmpdir(), 'module-files-')); function run(request) { return spawnSync('bash', ['-s'], { input: script(root, request), encoding: 'utf8' }); } function read(request) { const result = run(request); assert.equal(result.status, 0, result.stderr); return JSON.parse(result.stdout); } try { fs.mkdirSync(path.join(root, 'src')); fs.mkdirSync(path.join(root, 'build')); fs.writeFileSync(path.join(root, 'src', 'hello.py'), 'first\nHello world\n'); fs.writeFileSync(path.join(root, 'build', 'hidden.py'), 'Hello'); fs.writeFileSync(path.join(root, 'large.txt'), 'x'.repeat(1048577)); fs.writeFileSync(path.join(root, 'binary.txt'), Buffer.from([0, 1])); fs.symlinkSync('/etc/passwd', path.join(root, 'outside.txt')); const listing = read({ action: 'list' }); assert.deepEqual(listing, { filePaths: ['binary.txt', 'large.txt', 'src/hello.py'], folders: ['src'] }); assert.equal(read({ action: 'content', path: 'src/hello.py' }).content, 'first\nHello world\n'); for (const file of ['large.txt', 'binary.txt']) { assert.equal(read({ action: 'content', path: file }).content, 'Binary or large file preview is not available.'); } for (const file of ['../outside.txt', '/etc/passwd', 'outside.txt', 'missing.py']) { assert.notEqual(run({ action: 'content', path: file }).status, 0); } assert.deepEqual(read({ action: 'search', query: 'HELLO' }), [{ file: 'src/hello.py', lineNumber: 2, lineText: 'Hello world' }]); fs.writeFileSync(path.join(root, 'src', 'many.txt'), 'hello\n'.repeat(300)); assert.equal(read({ action: 'search', query: 'hello' }).length, 200); console.log('Remote module listing, on-demand content, path validation and search tests passed.'); } finally { fs.rmSync(root, { recursive: true, force: true }); }