diff --git a/packages/server/lib/modes/interactive-ct.js b/packages/server/lib/modes/interactive-ct.js index b766608f6384..2f675fb0a713 100644 --- a/packages/server/lib/modes/interactive-ct.js +++ b/packages/server/lib/modes/interactive-ct.js @@ -1,13 +1,32 @@ const serverCt = require('@packages/server-ct') +const { getBrowsers } = require('../browsers/utils') -const run = (options) => { - const { projectRoot } = options +const browsersForCtInteractive = ['chrome', 'chromium', 'edge', 'electron', 'firefox'] - options.browser = options.browser || 'chrome' +const returnDefaultBrowser = (browsersByPriority, installedBrowsers) => { + const browserMap = installedBrowsers.reduce((acc, curr) => { + acc[curr.name] = true - return serverCt.start(projectRoot, options) + return acc + }, {}) + + for (const browser of browsersByPriority) { + if (browserMap[browser]) { + return browser + } + } +} + +const run = async (options) => { + const installedBrowsers = await getBrowsers() + + options.browser = options.browser || returnDefaultBrowser(browsersForCtInteractive, installedBrowsers) + + return serverCt.start(options.projectRoot, options) } module.exports = { run, + returnDefaultBrowser, + browsersForCtInteractive, } diff --git a/packages/server/test/unit/modes/interactive-ct_spec.js b/packages/server/test/unit/modes/interactive-ct_spec.js new file mode 100644 index 000000000000..4e0f3db344ac --- /dev/null +++ b/packages/server/test/unit/modes/interactive-ct_spec.js @@ -0,0 +1,35 @@ +require('../../spec_helper') + +const { browsers } = require('@packages/launcher/lib/browsers') +const { + returnDefaultBrowser, + browsersForCtInteractive, +} = require(`${root}../lib/modes/interactive-ct`) + +function filterBrowsers (list) { + return browsers.filter((browser) => list.includes(browser.name)) +} + +describe('returnDefaultBrowser', () => { + it('returns chrome by default is available', async () => { + const installedBrowsers = filterBrowsers(['electron', 'chromium', 'chrome']) + const actual = await returnDefaultBrowser(browsersForCtInteractive, installedBrowsers) + + expect(actual).to.eq('chrome') + }) + + it('returns chromium if chrome is not installed', async () => { + const installedBrowsers = filterBrowsers(['electron', 'edge', 'chromium']) + const actual = await returnDefaultBrowser(browsersForCtInteractive, installedBrowsers) + + expect(actual).to.eq('chromium') + }) + + it('returns undefined if no browser found', async () => { + // error message is handlded further down. + const installedBrowsers = filterBrowsers([]) + const actual = await returnDefaultBrowser(browsersForCtInteractive, installedBrowsers) + + expect(actual).to.eq(undefined) + }) +})