Skip to content

Commit

Permalink
cli(fr): add --fraggle-rock flag (#12805)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored Jul 20, 2021
1 parent 52287af commit 1abc363
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ function getFlags(manualArgv, options = {}) {
default: false,
describe: 'Print the normalized config for the given config and options, then exit.',
},
'fraggle-rock': {
type: 'boolean',
default: false,
describe: '[EXPERIMENTAL] Use the new Fraggle Rock navigation runner to gather results.',
},
'additional-trace-categories': {
type: 'string',
describe: 'Additional categories to capture with the trace (comma-delimited).',
Expand Down
23 changes: 22 additions & 1 deletion lighthouse-cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ async function potentiallyKillChrome(launchedChrome) {
});
}

/**
* @param {string} url
* @param {LH.CliFlags} flags
* @param {LH.Config.Json|undefined} config
* @param {ChromeLauncher.LaunchedChrome} launchedChrome
* @return {Promise<LH.RunnerResult|undefined>}
*/
async function runLighthouseWithFraggleRock(url, flags, config, launchedChrome) {
const fraggleRock = require('../lighthouse-core/fraggle-rock/api.js');
const puppeteer = require('puppeteer');
// @ts-expect-error - FIXME: lighthouse-logger is greedy and takes over `debug` for all packages
require('debug').enable('-puppeteer:*');
const browser = await puppeteer.connect({browserURL: `http://localhost:${launchedChrome.port}`});
const page = await browser.newPage();
flags.channel = 'fraggle-rock-cli';
const configContext = {configPath: flags.configPath, settingsOverrides: flags};
return fraggleRock.navigation({url, page, config, configContext});
}

/**
* @param {string} url
* @param {LH.CliFlags} flags
Expand Down Expand Up @@ -223,7 +242,9 @@ async function runLighthouse(url, flags, config) {
flags.port = launchedChrome.port;
}

const runnerResult = await lighthouse(url, flags, config);
const runnerResult = flags.fraggleRock && launchedChrome ?
await runLighthouseWithFraggleRock(url, flags, config, launchedChrome) :
await lighthouse(url, flags, config);

// If in gatherMode only, there will be no runnerResult.
if (runnerResult) {
Expand Down
1 change: 1 addition & 0 deletions lighthouse-cli/test/cli/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ beforeEach(() => {
view: false,
verbose: false,
quiet: false,
fraggleRock: false,
port: 0,
hostname: '',
// Command modes
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/fraggle-rock/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function resolveNavigationsToDefns(navigations, artifactDefns) {

/**
* @param {LH.Config.Json|undefined} configJSON
* @param {{gatherMode: LH.Gatherer.GatherMode, configPath?: string, settingsOverrides?: LH.SharedFlagsSettings}} context
* @param {Omit<LH.Config.FRContext, 'gatherMode'> & {gatherMode: LH.Gatherer.GatherMode}} context
* @return {{config: LH.Config.FRConfig, warnings: string[]}}
*/
function initializeConfig(configJSON, context) {
Expand Down
6 changes: 3 additions & 3 deletions lighthouse-core/fraggle-rock/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ async function _cleanup({requestedUrl, driver, config}) {
}

/**
* @param {{url: string, page: import('puppeteer').Page, config?: LH.Config.Json}} options
* @param {{url: string, page: import('puppeteer').Page, config?: LH.Config.Json, configContext?: LH.Config.FRContext}} options
* @return {Promise<LH.RunnerResult|undefined>}
*/
async function navigation(options) {
const {url: requestedUrl, page} = options;
const {config} = initializeConfig(options.config, {gatherMode: 'navigation'});
const {url: requestedUrl, page, configContext = {}} = options;
const {config} = initializeConfig(options.config, {...configContext, gatherMode: 'navigation'});
const computedCache = new Map();

return Runner.run(
Expand Down
5 changes: 3 additions & 2 deletions lighthouse-core/fraggle-rock/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const {
const {initializeConfig} = require('../config/config.js');
const {getBaseArtifacts, finalizeArtifacts} = require('./base-artifacts.js');

/** @param {{page: import('puppeteer').Page, config?: LH.Config.Json}} options */
/** @param {{page: import('puppeteer').Page, config?: LH.Config.Json, configContext?: LH.Config.FRContext}} options */
async function snapshot(options) {
const {config} = initializeConfig(options.config, {gatherMode: 'snapshot'});
const {configContext = {}} = options;
const {config} = initializeConfig(options.config, {...configContext, gatherMode: 'snapshot'});
const driver = new Driver(options.page);
await driver.connect();

Expand Down
5 changes: 3 additions & 2 deletions lighthouse-core/fraggle-rock/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ const {initializeConfig} = require('../config/config.js');
const {getBaseArtifacts, finalizeArtifacts} = require('./base-artifacts.js');

/**
* @param {{page: import('puppeteer').Page, config?: LH.Config.Json}} options
* @param {{page: import('puppeteer').Page, config?: LH.Config.Json, configContext?: LH.Config.FRContext}} options
* @return {Promise<{endTimespan(): Promise<LH.RunnerResult|undefined>}>}
*/
async function startTimespan(options) {
const {config} = initializeConfig(options.config, {gatherMode: 'timespan'});
const {configContext = {}} = options;
const {config} = initializeConfig(options.config, {...configContext, gatherMode: 'timespan'});
const driver = new Driver(options.page);
await driver.connect();

Expand Down
34 changes: 32 additions & 2 deletions lighthouse-core/test/fraggle-rock/gather/navigation-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

/* eslint-env jest */

const {createMockDriver, mockDriverSubmodules} = require('./mock-driver.js');
const {createMockDriver, mockDriverSubmodules, mockRunnerModule} = require('./mock-driver.js');
const mocks = mockDriverSubmodules();
const runner = require('../../../fraggle-rock/gather/navigation-runner.js');
const {initializeConfig} = require('../../../fraggle-rock/config/config.js');
const {defaultNavigationConfig} = require('../../../config/constants.js');
const LighthouseError = require('../../../lib/lh-error.js');
const DevtoolsLogGatherer = require('../../../gather/gatherers/devtools-log.js');
const toDevtoolsLog = require('../../network-records-to-devtools-log.js');

// Establish the mocks before we require our file under test.
let mockRunnerRun = jest.fn();

jest.mock('../../../runner.js', () => mockRunnerModule(() => mockRunnerRun));

const runner = require('../../../fraggle-rock/gather/navigation-runner.js');

/** @typedef {{meta: LH.Gatherer.GathererMeta<'Accessibility'>, getArtifact: jest.Mock<any, any>, startInstrumentation:jest.Mock<any, any>, stopInstrumentation: jest.Mock<any, any>, startSensitiveInstrumentation:jest.Mock<any, any>, stopSensitiveInstrumentation: jest.Mock<any, any>}} MockGatherer */

describe('NavigationRunner', () => {
Expand Down Expand Up @@ -79,6 +85,7 @@ describe('NavigationRunner', () => {

beforeEach(() => {
requestedUrl = 'http://example.com';
mockRunnerRun = jest.fn();
config = initializeConfig(undefined, {gatherMode: 'navigation'}).config;
navigation = createNavigation().navigation;
computedCache = new Map();
Expand Down Expand Up @@ -381,4 +388,27 @@ describe('NavigationRunner', () => {
expect(mocks.storageMock.clearDataForOrigin).not.toHaveBeenCalled();
});
});

describe('navigation', () => {
it('should initialize config', async () => {
const settingsOverrides = {
formFactor: /** @type {'desktop'} */ ('desktop'),
maxWaitForLoad: 1234,
screenEmulation: {mobile: false},
};

const configContext = {settingsOverrides};
await runner.navigation({
url: 'http://example.com',
page: mockDriver._page.asPage(),
configContext,
});

expect(mockRunnerRun.mock.calls[0][1]).toMatchObject({
config: {
settings: settingsOverrides,
},
});
});
});
});
18 changes: 18 additions & 0 deletions lighthouse-core/test/fraggle-rock/gather/snapshot-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ describe('Snapshot Runner', () => {
expect(gathererB.getArtifact).toHaveBeenCalled();
});


it('should use configContext', async () => {
const settingsOverrides = {
formFactor: /** @type {'desktop'} */ ('desktop'),
maxWaitForLoad: 1234,
screenEmulation: {mobile: false},
};

const configContext = {settingsOverrides};
await snapshot({page, config, configContext});

expect(mockRunnerRun.mock.calls[0][1]).toMatchObject({
config: {
settings: settingsOverrides,
},
});
});

it('should not invoke instrumentation methods', async () => {
await snapshot({page, config});
await mockRunnerRun.mock.calls[0][0]();
Expand Down
18 changes: 18 additions & 0 deletions lighthouse-core/test/fraggle-rock/gather/timespan-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ describe('Timespan Runner', () => {
});
});

it('should use configContext', async () => {
const settingsOverrides = {
formFactor: /** @type {'desktop'} */ ('desktop'),
maxWaitForLoad: 1234,
screenEmulation: {mobile: false},
};

const configContext = {settingsOverrides};
const timespan = await startTimespan({page, config, configContext});
await timespan.endTimespan();

expect(mockRunnerRun.mock.calls[0][1]).toMatchObject({
config: {
settings: settingsOverrides,
},
});
});

it('should invoke stop instrumentation', async () => {
const timespan = await startTimespan({page, config});
await timespan.endTimespan();
Expand Down
10 changes: 10 additions & 0 deletions types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ declare global {
groups: Record<string, Group> | null;
}

/**
* Additional information about the context in which a Fraggle Rock config should be interpreted.
* This information is typically set by the CLI or other channel integrations.
*/
export interface FRContext {
gatherMode?: LH.Gatherer.GatherMode;
configPath?: string;
settingsOverrides?: LH.SharedFlagsSettings;
}

interface SharedPassNavigationJson {
/**
* Controls the behavior when the navigation fails to complete (due to server error, no FCP, etc).
Expand Down
2 changes: 2 additions & 0 deletions types/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ declare global {
quiet: boolean;
/** A flag to print the normalized config for the given config and options, then exit. */
printConfig: boolean;
/** Use the new Fraggle Rock navigation runner to gather CLI results. */
fraggleRock: boolean;
/** Path to the file where precomputed lantern data should be read from. */
precomputedLanternDataPath?: string;
/** Path to the file where precomputed lantern data should be written to. */
Expand Down

0 comments on commit 1abc363

Please sign in to comment.