|
| 1 | +import { vscode } from '../mockVscode.spec'; |
| 2 | +import { createSandbox } from 'sinon'; |
| 3 | +import { CaptureScreenshotCommand } from './CaptureScreenshotCommand'; |
| 4 | +import { BrightScriptCommands } from '../BrightScriptCommands'; |
| 5 | +import * as rokuDeploy from 'roku-deploy'; |
| 6 | +import { expect } from 'chai'; |
| 7 | +import URI from 'vscode-uri'; |
| 8 | +import { standardizePath as s } from 'brighterscript'; |
| 9 | + |
| 10 | +const cwd = s`${process.cwd()}`; |
| 11 | + |
| 12 | +const sinon = createSandbox(); |
| 13 | + |
| 14 | +describe('CaptureScreenshotCommand', () => { |
| 15 | + let brightScriptCommands: BrightScriptCommands; |
| 16 | + let command: CaptureScreenshotCommand; |
| 17 | + let context = vscode.context; |
| 18 | + let workspace = vscode.workspace; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + command = new CaptureScreenshotCommand(); |
| 22 | + brightScriptCommands = new BrightScriptCommands({} as any, {} as any, {} as any, {} as any, {} as any, {} as any); |
| 23 | + command.register(context, brightScriptCommands); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + sinon.restore(); |
| 28 | + workspace.workspaceFolders = []; |
| 29 | + }); |
| 30 | + |
| 31 | + it('gets remoteHost and remotePassword when hostParam is not provided', async () => { |
| 32 | + sinon.stub(brightScriptCommands, 'getRemoteHost').resolves('1.1.1.1'); |
| 33 | + sinon.stub(brightScriptCommands, 'getRemotePassword').resolves('password'); |
| 34 | + |
| 35 | + const { host, password } = await command['getHostAndPassword'](); |
| 36 | + |
| 37 | + expect(host).to.eql('1.1.1.1'); |
| 38 | + expect(password).to.eql('password'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('gets remotePassword when hostParam matches remoteHost', async () => { |
| 42 | + await context.workspaceState.update('remoteHost', '1.1.1.1'); |
| 43 | + await context.workspaceState.update('remotePassword', 'password'); |
| 44 | + |
| 45 | + const { host, password } = await command['getHostAndPassword']('1.1.1.1'); |
| 46 | + |
| 47 | + expect(host).to.eql('1.1.1.1'); |
| 48 | + expect(password).to.eql('password'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('prompts for password when hostParam does not match remoteHost', async () => { |
| 52 | + sinon.stub(vscode.window, 'showInputBox').resolves('password'); |
| 53 | + |
| 54 | + const { host, password } = await command['getHostAndPassword']('1.1.1.1'); |
| 55 | + |
| 56 | + expect(host).to.eql('1.1.1.1'); |
| 57 | + expect(password).to.eql('password'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('shows error message when captureScreenshot fails', async () => { |
| 61 | + const stub = sinon.stub(command as any, 'getHostAndPassword').callsFake(() => Promise.resolve({ host: '1.1.1.1', password: 'password' })); |
| 62 | + sinon.stub(rokuDeploy, 'takeScreenshot').rejects(new Error('Screenshot failed')); |
| 63 | + const stubError = sinon.stub(vscode.window, 'showErrorMessage'); |
| 64 | + |
| 65 | + await command['captureScreenshot']('1.1.1.1'); |
| 66 | + |
| 67 | + expect(stub.getCall(0).args[0]).to.eql('1.1.1.1'); |
| 68 | + expect(stubError.calledOnce).to.be.true; |
| 69 | + }); |
| 70 | + |
| 71 | + it('uses temp dir when screenshotDir is not defined', async () => { |
| 72 | + sinon.stub(command as any, 'getHostAndPassword').callsFake(() => Promise.resolve({ host: '1.1.1.1', password: 'password' })); |
| 73 | + const stub = sinon.stub(rokuDeploy, 'takeScreenshot').returns(Promise.resolve('screenshot.png')); |
| 74 | + |
| 75 | + await command['captureScreenshot'](); |
| 76 | + |
| 77 | + expect(stub.getCall(0).args[0]).to.eql({ host: '1.1.1.1', password: 'password' }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('uses screenshotDir with single workspace', async () => { |
| 81 | + sinon.stub(command as any, 'getHostAndPassword').callsFake(() => Promise.resolve({ host: '1.1.1.1', password: 'password' })); |
| 82 | + const stub = sinon.stub(rokuDeploy, 'takeScreenshot').returns(Promise.resolve('screenshot.png')); |
| 83 | + workspace._configuration = { |
| 84 | + 'brightscript.screenshotDir': '${workspaceFolder}/screenshots' |
| 85 | + }; |
| 86 | + workspace.workspaceFolders = [ |
| 87 | + { |
| 88 | + uri: URI.file(s`${cwd}/workspace`), |
| 89 | + name: 'test-workspace', |
| 90 | + index: 0 |
| 91 | + } |
| 92 | + ]; |
| 93 | + |
| 94 | + await command['captureScreenshot'](); |
| 95 | + |
| 96 | + expect(stub.getCall(0).args[0]).to.eql({ host: '1.1.1.1', password: 'password', outDir: s`${cwd}/workspace/screenshots` }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('uses relative screenshotDir with single workspace', async () => { |
| 100 | + sinon.stub(command as any, 'getHostAndPassword').callsFake(() => Promise.resolve({ host: '1.1.1.1', password: 'password' })); |
| 101 | + const stub = sinon.stub(rokuDeploy, 'takeScreenshot').returns(Promise.resolve('screenshot.png')); |
| 102 | + workspace._configuration = { |
| 103 | + 'brightscript.screenshotDir': 'screenshots' |
| 104 | + }; |
| 105 | + workspace.workspaceFolders = [ |
| 106 | + { |
| 107 | + uri: URI.file(s`${cwd}/workspace`), |
| 108 | + name: 'test-workspace', |
| 109 | + index: 0 |
| 110 | + } |
| 111 | + ]; |
| 112 | + |
| 113 | + await command['captureScreenshot'](); |
| 114 | + |
| 115 | + expect(stub.getCall(0).args[0]).to.eql({ host: '1.1.1.1', password: 'password', outDir: s`${cwd}/workspace/screenshots` }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('uses screenshotDir with multiple workspace', async () => { |
| 119 | + sinon.stub(command as any, 'getHostAndPassword').callsFake(() => Promise.resolve({ host: '1.1.1.1', password: 'password' })); |
| 120 | + const stub = sinon.stub(rokuDeploy, 'takeScreenshot').returns(Promise.resolve('screenshot.png')); |
| 121 | + const workspaceFolders = [ |
| 122 | + { |
| 123 | + uri: URI.file(s`${cwd}/workspace1`), |
| 124 | + name: 'test-workspace', |
| 125 | + index: 0 |
| 126 | + }, |
| 127 | + { |
| 128 | + uri: URI.file(s`${cwd}/workspace2`), |
| 129 | + name: 'test-workspace2', |
| 130 | + index: 1 |
| 131 | + } |
| 132 | + ]; |
| 133 | + workspace.workspaceFolders = workspaceFolders; |
| 134 | + workspace._configuration = { |
| 135 | + 'brightscript.screenshotDir': '${workspaceFolder}/screenshots' |
| 136 | + }; |
| 137 | + sinon.stub(vscode.window, 'showWorkspaceFolderPick').resolves(workspaceFolders[1]); |
| 138 | + |
| 139 | + await command['captureScreenshot'](); |
| 140 | + |
| 141 | + expect(stub.getCall(0).args[0]).to.eql({ host: '1.1.1.1', password: 'password', outDir: s`${cwd}/workspace2/screenshots` }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('uses relative screenshotDir with multiple workspace', async () => { |
| 145 | + sinon.stub(command as any, 'getHostAndPassword').callsFake(() => Promise.resolve({ host: '1.1.1.1', password: 'password' })); |
| 146 | + const stub = sinon.stub(rokuDeploy, 'takeScreenshot').returns(Promise.resolve('screenshot.png')); |
| 147 | + const workspaceFolders = [ |
| 148 | + { |
| 149 | + uri: URI.file(s`${cwd}/workspace1`), |
| 150 | + name: 'test-workspace', |
| 151 | + index: 0 |
| 152 | + }, |
| 153 | + { |
| 154 | + uri: URI.file(s`${cwd}/workspace2`), |
| 155 | + name: 'test-workspace2', |
| 156 | + index: 1 |
| 157 | + } |
| 158 | + ]; |
| 159 | + workspace.workspaceFolders = workspaceFolders; |
| 160 | + workspace._configuration = { |
| 161 | + 'brightscript.screenshotDir': 'screenshots' |
| 162 | + }; |
| 163 | + sinon.stub(vscode.window, 'showWorkspaceFolderPick').resolves(workspaceFolders[1]); |
| 164 | + |
| 165 | + await command['captureScreenshot'](); |
| 166 | + |
| 167 | + expect(stub.getCall(0).args[0]).to.eql({ host: '1.1.1.1', password: 'password', outDir: s`${cwd}/workspace2/screenshots` }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments