'use strict'; // The extension has never been run in a real VS Code by these tests. This is the next best check: // every member of the `vscode` API that extension.js uses must be one this file knows, with the // version of VS Code it came in, and none may be newer than what package.json says the extension // needs (`engines.vscode`). The same for the editor's own commands and theme colours it names, and // for what the manifest contributes. Using something new means adding it here, with its version. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const ROOT = path.join(__dirname, '..'); const source = fs.readFileSync(path.join(ROOT, 'extension.js'), 'utf8'); const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')); // Member -> the VS Code version it is in at the latest (the API's own "since" where it is later // than 1.30; everything older is written as 1.30). const API = { 'window.showWarningMessage': '1.30', 'window.showInformationMessage': '1.30', 'window.showErrorMessage': '1.30', 'window.activeTextEditor': '1.30', 'window.visibleTextEditors': '1.30', 'window.showTextDocument': '1.30', 'window.createTextEditorDecorationType': '1.30', 'window.createStatusBarItem': '1.30', 'window.createOutputChannel': '1.30', 'window.onDidChangeVisibleTextEditors': '1.30', 'window.onDidChangeActiveTextEditor': '1.30', 'window.registerWebviewViewProvider': '1.50', 'window.tabGroups': '1.67', 'workspace.textDocuments': '1.30', 'workspace.applyEdit': '1.30', 'workspace.getWorkspaceFolder': '1.30', 'workspace.asRelativePath': '1.30', 'workspace.workspaceFolders': '1.30', 'workspace.registerTextDocumentContentProvider': '1.30', 'workspace.openTextDocument': '1.30', 'workspace.onDidCloseTextDocument': '1.30', 'workspace.onDidChangeTextDocument': '1.30', 'workspace.onDidChangeConfiguration': '1.30', 'workspace.getConfiguration': '1.30', 'workspace.onDidSaveTextDocument': '1.30', 'workspace.onDidChangeWorkspaceFolders': '1.30', 'commands.executeCommand': '1.30', 'commands.registerCommand': '1.30', 'languages.registerCodeLensProvider': '1.30', 'languages.getDiagnostics': '1.30', 'languages.registerInlineCompletionItemProvider': '1.68', 'env.openExternal': '1.31', 'env.clipboard': '1.30', 'Uri.file': '1.30', 'Uri.parse': '1.30', 'Uri.joinPath': '1.45', 'Uri.from': '1.45', Range: '1.30', Position: '1.30', ThemeColor: '1.30', CodeLens: '1.30', WorkspaceEdit: '1.30', EventEmitter: '1.30', MarkdownString: '1.30', 'EndOfLine.CRLF': '1.30', 'OverviewRulerLane.Left': '1.30', 'ConfigurationTarget.Global': '1.30', 'ConfigurationTarget.Workspace': '1.30', 'ViewColumn.Beside': '1.30', ConfigurationTarget: '1.30', 'StatusBarAlignment.Right': '1.30', 'DiagnosticSeverity.Warning': '1.30', TabInputText: '1.67', TabInputTextDiff: '1.67', InlineCompletionItem: '1.68', 'InlineCompletionTriggerKind.Invoke': '1.68', }; // Words after `vscode.` that are not the API: the names of the editor's own commands, in strings. const NOT_API = new Set(['diff', 'open']); const EDITOR_COMMANDS = { 'vscode.diff': '1.30', 'vscode.open': '1.30', setContext: '1.30', revealFileInOS: '1.30', 'editor.action.inlineSuggest.trigger': '1.58', // The Settings editor with a query (`@ext:` since 1.45), and the keyboard back to the editor. 'workbench.action.openSettings': '1.45', 'workbench.action.focusActiveEditorGroup': '1.30', }; const THEME_COLORS = { 'diffEditor.insertedTextBackground': '1.30', 'diffEditor.removedLineBackground': '1.65', 'editorGhostText.foreground': '1.59', 'editorOverviewRuler.addedForeground': '1.30', 'editorOverviewRuler.deletedForeground': '1.30', 'editorOverviewRuler.modifiedForeground': '1.30', }; const needs = /^\^?(\d+)\.(\d+)/.exec(pkg.engines.vscode); const atMost = (since) => { const [major, minor] = since.split('.').map(Number); return major < Number(needs[1]) || (major === Number(needs[1]) && minor <= Number(needs[2])); }; test('every member of the vscode API that is used is known, and none is newer than engines.vscode', () => { assert.ok(needs, 'engines.vscode is a version'); const used = new Set(); for (const m of source.matchAll(/\bvscode\.([A-Za-z]+)(?:\.([A-Za-z]+))?/g)) { if (NOT_API.has(m[1])) continue; const namespace = /^[a-z]/.test(m[1]); // A namespace is used through its members; a class or an enum by itself or by one member. if (namespace && !m[2]) continue; used.add(namespace || (m[2] && API[`${m[1]}.${m[2]}`]) ? `${m[1]}.${m[2]}` : m[1]); } assert.ok(used.size > 40, 'the source was read'); for (const name of [...used].sort()) { assert.ok(API[name], `vscode.${name} is used and not in this test's list: add it, with the VS Code version it came in`); assert.ok(atMost(API[name]), `vscode.${name} came in ${API[name]}, after the ${pkg.engines.vscode} the extension says it needs`); } }); test('the panel is the only place that asks: no Quick Pick, no input box, no progress notification, no message with an answer', () => { const code = source.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, ''); for (const banned of ['showQuickPick', 'showInputBox', 'withProgress', 'createQuickPick', 'createInputBox', 'showOpenDialog', 'showSaveDialog', 'modal:']) { assert.ok(!code.includes(banned), `extension.js must not use ${banned}`); } // A message of the editor's is said and nothing more: one argument, the text. const calls = [...code.matchAll(/show(?:Information|Warning|Error)Message\(/g)]; assert.ok(calls.length > 10, 'the source was read'); for (const m of calls) { let depth = 1; let i = m.index + m[0].length; let commas = 0; let quote = ''; for (; i < code.length && depth > 0; i++) { const ch = code[i]; if (quote) { if (ch === '\\') i++; else if (ch === quote) quote = ''; // A template's own expressions are not arguments: commas inside `${…}` are counted by depth below. continue; } if (ch === "'" || ch === '"' || ch === '`') quote = ch; else if (ch === '(' || ch === '[' || ch === '{') depth++; else if (ch === ')' || ch === ']' || ch === '}') depth--; else if (ch === ',' && depth === 1) commas++; } const text = code.slice(m.index, i); if (/,\s*\)$/.test(text)) commas--; // a comma after the last argument is not another one assert.equal(commas, 0, 'a message with more than its text: ' + text.slice(0, 120)); } }); test("the editor's own commands and theme colours that are named exist in that version too", () => { const commands = new Set([...source.matchAll(/executeCommand\('([^']+)'/g)].map((m) => m[1])); for (const c of commands) { assert.ok(EDITOR_COMMANDS[c], `the command ${c} is run and not in this test's list`); assert.ok(atMost(EDITOR_COMMANDS[c]), c); } const colours = new Set([...source.matchAll(/ThemeColor\('([^']+)'\)/g)].map((m) => m[1])); assert.ok(colours.size >= 5); for (const c of colours) { assert.ok(THEME_COLORS[c], `the theme colour ${c} is used and not in this test's list`); assert.ok(atMost(THEME_COLORS[c]), c); } assert.ok(!/#[0-9a-fA-F]{3,8}\b|rgba?\(/.test(source.replace(/\/\/.*$/gm, '')), 'no colour of its own in the glue code'); }); test('the manifest contributes only what exists: commands, menus, keys, the view, the settings', () => { const c = pkg.contributes; const commands = new Set(c.commands.map((x) => x.command)); assert.equal(commands.size, c.commands.length, 'no command twice'); for (const x of c.commands) assert.ok(x.title && x.category === 'LLeMbas', x.command); for (const [menu, entries] of Object.entries(c.menus)) for (const e of entries) assert.ok(commands.has(e.command), `${menu}: ${e.command}`); for (const k of c.keybindings) assert.ok(commands.has(k.command) && k.key, k.command); for (const name of commands) assert.ok(source.includes(`command('${name}'`), `${name} is registered in extension.js`); // The view: its container, its id as the code uses it, and what wakes the extension. assert.equal(c.viewsContainers.activitybar[0].id, 'llembas'); assert.deepEqual(c.views.llembas, [{ type: 'webview', id: 'llembas.chat', name: 'Chat' }]); assert.equal(Object.keys(c.views).length, 1, 'one panel: everything is a view inside it'); assert.ok(source.includes("const VIEW_ID = 'llembas.chat'")); assert.ok(fs.existsSync(path.join(ROOT, c.viewsContainers.activitybar[0].icon)) && fs.existsSync(path.join(ROOT, pkg.icon)) && fs.existsSync(path.join(ROOT, pkg.main))); assert.ok(pkg.activationEvents.includes('onStartupFinished'), 'inline suggestions and the status item are there before the view is opened'); for (const e of pkg.activationEvents) assert.match(e, /^(onStartupFinished|onView:llembas\.chat)$/); assert.deepEqual(pkg.extensionKind, ['workspace'], 'it runs where the files and the CLI are'); assert.equal(pkg.capabilities.untrustedWorkspaces.supported, false, 'it starts a program: off in a folder that is not trusted'); // Settings: each is read by the code, and what starts or reaches a program cannot be set by a folder. const settings = c.configuration.properties; for (const key of Object.keys(settings)) { assert.ok(source.includes(`'${key.replace(/^llembas\./, '')}'`), `${key} is read in extension.js`); assert.ok(settings[key].description || settings[key].markdownDescription, key); } for (const key of ['llembas.command', 'llembas.login.caFile', 'llembas.chat.startMode']) assert.equal(settings[key].scope, 'machine', key); assert.equal(settings['llembas.command'].default, 'lembas', 'nothing is predefined but the program\'s name'); });