test: execute the extension against a mocked VS Code API

This commit is contained in:
faumaray committed 2026-12-05 09:21:17 +00:00
1 parent 5d87ec5c93
commit e1c02d5bcc
2 files changed
+38

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
// Minimal VS Code API mock: records terminal commands without launching VS Code.
const Module = require('module');
const registered = new Map();
const terminals = [];
const vscodeMock = {
window: {
createTerminal: ({ name }) => ({
name,
show() {},
sendText(text) { terminals.push({ name, text }); },
}),
},
commands: {
registerCommand: (id, fn) => { registered.set(id, fn); return { dispose() {} }; },
},
workspace: { workspaceFolders: [{ uri: { fsPath: '/tmp/vbs-workspace' } }] },
};
const originalLoad = Module._load;
Module._load = function (request) {
if (request === 'vscode') return vscodeMock;
return originalLoad.apply(this, arguments);
};
const extension = require('../extension.js');
const context = { subscriptions: [] };
extension.activate(context);
for (const [id, fn] of registered) fn();
if (registered.size !== 6) throw new Error('expected 6 commands, got ' + registered.size);
if (terminals.length !== registered.size) throw new Error('every command must open a terminal');
const build = terminals.find((t) => t.name === 'VBS: Build');
if (!build) throw new Error('build command missing');
if (!build.text.includes('build')) throw new Error('build args missing: ' + build.text);
if (!build.text.includes('--project /tmp/vbs-workspace'))
throw new Error('workspace project not injected: ' + build.text);
const install = terminals.find((t) => t.name === 'VBS: Install');
if (!install || !install.text.includes('--prefix')) throw new Error('install prefix missing');
extension.deactivate();
console.log('extension tests passed: ' + terminals.length + ' commands');