|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { window, commands, env, Uri, Terminal, Pseudoterminal, Event, TerminalDimensions, EventEmitter } from 'vscode'; |
| 20 | +import { START_DIDACT_COMMAND, sendTerminalText, gatherAllCommandsLinks, getContext, findTerminal } from '../../extensionFunctions'; |
| 21 | +import { didactManager } from '../../didactManager'; |
| 22 | +import { DidactUri } from '../../didactUri'; |
| 23 | +import { handleText } from '../../commandHandler'; |
| 24 | + |
| 25 | +const testMD = Uri.parse('vscode://redhat.vscode-didact?extension=demos/markdown/didact-demo.didact.md'); |
| 26 | + |
| 27 | +const delayTime = 1000; |
| 28 | + |
| 29 | +suite('stub out a tutorial', () => { |
| 30 | + |
| 31 | + test('that we can send an echo command to the terminal and get the response', async () => { |
| 32 | + const name = 'echoTerminal'; |
| 33 | + const text = `echo Hello World ${name}`; |
| 34 | + const result = `Hello World echoTerminal`; |
| 35 | + await validateTerminalResponse(name, text, result); |
| 36 | + }); |
| 37 | + |
| 38 | + test('that we can get a response from each command in the demo tutorial', async () => { |
| 39 | + await commands.executeCommand(START_DIDACT_COMMAND, testMD).then( async () => { |
| 40 | + if (didactManager.active()) { |
| 41 | + |
| 42 | + suite('walk through all the sendNamedTerminalAString commands in the demo', () => { |
| 43 | + const commands : any[] = gatherAllCommandsLinks().filter( (href) => href.match(/=vscode.didact.sendNamedTerminalAString&/g)); |
| 44 | + expect(commands).to.not.be.empty; |
| 45 | + commands.forEach(function(href: string) { |
| 46 | + test(`test terminal command "${href}"`, async () => { |
| 47 | + const ctxt = getContext(); |
| 48 | + if (ctxt) { |
| 49 | + const testUri = new DidactUri(href, ctxt); |
| 50 | + const textToParse = testUri.getText(); |
| 51 | + const userToParse = testUri.getUser(); |
| 52 | + let outputs : string[] = []; |
| 53 | + if (textToParse) { |
| 54 | + handleText(textToParse, outputs); |
| 55 | + } else if (userToParse) { |
| 56 | + handleText(userToParse, outputs); |
| 57 | + } |
| 58 | + expect(outputs).length.to.be.at.least(2); |
| 59 | + const terminalName = outputs[0]; |
| 60 | + const terminalString = outputs[1]; |
| 61 | + await validateSimpleTerminalResponse(terminalName, terminalString); |
| 62 | + } |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | +}); |
| 71 | + |
| 72 | +async function validateSimpleTerminalResponse(terminalName : string, terminalText : string) { |
| 73 | + console.log(`validateSimpleTerminalResponse terminal ${terminalName} executing text ${terminalText}`); |
| 74 | + const term = window.createTerminal(terminalName); |
| 75 | + expect(term).to.not.be.null; |
| 76 | + if (term) { |
| 77 | + console.log(`-current terminal = ${term?.name}`); |
| 78 | + await sendTerminalText(terminalName, terminalText); |
| 79 | + await delay(delayTime); |
| 80 | + const term2 = focusOnNamedTerminal(terminalName); |
| 81 | + await delay(delayTime); |
| 82 | + let result = await getTerminalOutput(terminalName); |
| 83 | + console.log(`-validateSimpleTerminalResponse terminal output = ${result}`); |
| 84 | + |
| 85 | + // we're just making sure we get something back and can see the text we put into the terminal |
| 86 | + expect(result).to.include(terminalText); |
| 87 | + findAndDisposeTerminal(terminalName); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +async function validateTerminalResponse(terminalName : string, terminalText : string, terminalResponse : string) { |
| 92 | + console.log(`validateTerminalResponse terminal ${terminalName} executing text ${terminalText} and looking for response ${terminalResponse}`); |
| 93 | + const term = window.createTerminal(terminalName); |
| 94 | + expect(term).to.not.be.null; |
| 95 | + if (term) { |
| 96 | + console.log(`-current terminal = ${term?.name}`); |
| 97 | + await sendTerminalText(terminalName, terminalText); |
| 98 | + await delay(delayTime); |
| 99 | + const term2 = focusOnNamedTerminal(terminalName); |
| 100 | + await delay(delayTime); |
| 101 | + let result = await getTerminalOutput(terminalName); |
| 102 | + console.log(`-validateTerminalResponse terminal output = ${result}`); |
| 103 | + expect(result).to.include(terminalResponse); |
| 104 | + findAndDisposeTerminal(terminalName); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +async function getTerminalOutput(terminalName : string) : Promise<string> { |
| 109 | + const terminal = getNamedTerminal(terminalName); |
| 110 | + expect(terminal).to.not.be.undefined; |
| 111 | + expect(terminal?.name).to.equal(terminalName); |
| 112 | + focusOnNamedTerminal(terminalName); |
| 113 | + const term = window.activeTerminal; |
| 114 | + console.log(`-current terminal = ${term?.name}`); |
| 115 | + await executeAndWait('workbench.action.terminal.selectAll'); |
| 116 | + await delay(delayTime); |
| 117 | + await executeAndWait('workbench.action.terminal.copySelection'); |
| 118 | + await executeAndWait('workbench.action.terminal.clearSelection'); |
| 119 | + const clipboard_content = await env.clipboard.readText(); |
| 120 | + return clipboard_content.trim();; |
| 121 | +} |
| 122 | + |
| 123 | +function delay(ms: number) { |
| 124 | + return new Promise( resolve => setTimeout(resolve, ms) ); |
| 125 | +} |
| 126 | + |
| 127 | +async function executeAndWait(command: string): Promise<void> { |
| 128 | + await commands.executeCommand(command); |
| 129 | + delay(100); |
| 130 | +} |
| 131 | + |
| 132 | +function getNamedTerminal(terminalName : string): Terminal | undefined { |
| 133 | + return window.terminals.filter(term => term.name === terminalName)[0]; |
| 134 | +} |
| 135 | + |
| 136 | +function findAndDisposeTerminal(terminalName: string) : void { |
| 137 | + const term = getNamedTerminal(terminalName); |
| 138 | + if (term) { |
| 139 | + term.dispose(); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +async function focusOnNamedTerminal(terminalName : string) : Promise<void> { |
| 144 | + let term = window.activeTerminal; |
| 145 | + while (term?.name != terminalName) { |
| 146 | + await commands.executeCommand('workbench.action.terminal.focusNext'); |
| 147 | + term = window.activeTerminal; |
| 148 | + } |
| 149 | +} |
0 commit comments