// Checks for `extract.js`: the input box that names the module the server is // about to extract, and what it does when the user does not name one. // // node extract_test.js // // Exits 0 on success, 1 on any failure. Plain node, no dependencies and no // test runner — the same style as `restart_test.js` next to it. // // The fakes below script only what `extract.js` is allowed to depend on: an // input box that resolves to a string or to `undefined`, and a `next` that // records what it was handed. Nothing here re-implements VS Code. // See docs/features/extract-inline-module/README.md. const { EXTRACT_MODULE, executeCommand, validateModuleName } = require('./extract'); let checks = 0; function ok(condition, what) { checks += 1; if (!condition) fail(what); } function equal(got, want, what) { checks += 1; const a = JSON.stringify(got); const b = JSON.stringify(want); if (a !== b) fail(`${what}: got ${a}, want ${b}`); } function fail(message) { console.error(`extract: FAIL — ${message}`); process.exit(1); } /** A `next` that records every call and answers with a marker. */ function recorder() { const calls = []; const next = (command, args) => { calls.push({ command, args }); return Promise.resolve('applied'); }; return { calls, next }; } /** An input box that answers `answer` and records the options it was given. */ function box(answer) { const seen = []; const showInputBox = (options) => { seen.push(options); return Promise.resolve(answer); }; return { seen, showInputBox }; } const ARGS = ['file:///rtl/m.sv', { start: { line: 6 }, end: { line: 6 } }, 'm_extracted']; async function aNameTypedIntoTheBoxReachesTheServer() { const { calls, next } = recorder(); const { seen, showInputBox } = box('adder'); const result = await executeCommand({ showInputBox }, EXTRACT_MODULE, ARGS, next); equal(result, 'applied', 'the server’s answer comes back to the client'); equal(calls.length, 1, 'the command is sent once'); equal(calls[0].command, EXTRACT_MODULE, 'under its own name'); equal(calls[0].args[0], ARGS[0], 'the uri is passed through'); equal(calls[0].args[1], ARGS[1], 'and so is the range'); equal(calls[0].args[2], 'adder', 'the typed name replaces the default'); equal(seen.length, 1, 'one box is shown'); equal(seen[0].value, 'm_extracted', 'prefilled with the default the server proposed'); } async function surroundingWhitespaceIsTrimmedOffTheName() { const { calls, next } = recorder(); const { showInputBox } = box(' adder\t'); await executeCommand({ showInputBox }, EXTRACT_MODULE, ARGS, next); equal(calls[0].args[2], 'adder', 'a name is what is left after the spaces'); } async function anEscapedBoxSendsNothingAtAll() { const { calls, next } = recorder(); const { showInputBox } = box(undefined); const result = await executeCommand({ showInputBox }, EXTRACT_MODULE, ARGS, next); equal(result, undefined, 'a cancelled extraction answers nothing'); equal(calls.length, 0, 'and reaches the server not at all'); } async function aNameTheEngineWouldRefuseIsNotSentEither() { // The box's own validator should stop this, but a client that ignores // `validateInput` must not get a file called `2fast.sv` out of it. for (const typed of ['', ' ', '2fast', 'with space', '\\odd-name']) { const { calls, next } = recorder(); const { showInputBox } = box(typed); const result = await executeCommand({ showInputBox }, EXTRACT_MODULE, ARGS, next); equal(result, undefined, `\`${typed}\` is not a module name`); equal(calls.length, 0, `\`${typed}\` never reaches the server`); } } async function everyOtherCommandGoesStraightThrough() { const { calls, next } = recorder(); let shown = 0; const showInputBox = () => { shown += 1; return Promise.resolve('never'); }; const args = ['file:///rtl/m.sv', { line: 3, character: 0 }]; const result = await executeCommand({ showInputBox }, 'lucisv.organizeImports', args, next); equal(result, 'applied', 'the other commands answer as they always did'); equal(shown, 0, 'and no box is shown for them'); equal(calls, [{ command: 'lucisv.organizeImports', args }], 'passed through unchanged'); } function theValidatorSaysWhatTheEngineWouldSay() { equal(validateModuleName('adder'), undefined, 'a plain identifier is fine'); equal(validateModuleName('_adder$2'), undefined, 'so are `_`, digits and `$` after the first'); ok(validateModuleName('').includes('needs a name'), 'an empty box says so'); ok(validateModuleName(' ').includes('needs a name'), 'and so does a blank one'); ok(validateModuleName('2fast').includes('plain SystemVerilog identifier'), 'a leading digit'); ok(validateModuleName('a b').includes('plain SystemVerilog identifier'), 'a space'); ok(validateModuleName('\\odd-name').includes('plain SystemVerilog identifier'), 'an escaped name'); } async function main() { const watchdog = setTimeout( () => fail('timed out after 5s — a promise this test waits on never settled'), 5000, ); theValidatorSaysWhatTheEngineWouldSay(); await aNameTypedIntoTheBoxReachesTheServer(); await surroundingWhitespaceIsTrimmedOffTheName(); await anEscapedBoxSendsNothingAtAll(); await aNameTheEngineWouldRefuseIsNotSentEither(); await everyOtherCommandGoesStraightThrough(); clearTimeout(watchdog); console.log(`extract: OK — ${checks} checks over the extract input box`); } main().catch((error) => fail(error.stack || String(error)));