Skip to content

Commit

Permalink
fix(cli): fixes gh issue 77
Browse files Browse the repository at this point in the history
  • Loading branch information
lynchbomb committed Oct 11, 2019
1 parent 0b37383 commit 37e3a09
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/command-config/default-flag-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const defaultFlagArgs: ITBConfig = {
emulateDevice: '',
emulateDeviceOrientation: 'vertical',
regressionThreshold: '0ms',
cookie: '',
};

// specify with --headless flag
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/command-config/tb-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface ITBConfig {
[CONTROL_ENV_OVERRIDE_ATTR]?: IBenchmarkEnvironmentOverride;
[EXPERIMENT_ENV_OVERRIDE_ATTR]?: IBenchmarkEnvironmentOverride;
[key: string]: any;
cookie?: string;
}

export interface IHARServer {
Expand Down
20 changes: 12 additions & 8 deletions packages/cli/src/commands/compare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,12 @@ export default class Compare extends Command {
this.parsedConfig
),
delay,
emulateDeviceSettings: getEmulateDeviceSettingForKeyAndOrientation(
controlEmulateDevice,
controlEmulateDeviceOrientation
),
emulateDeviceSettings: controlEmulateDevice
? getEmulateDeviceSettingForKeyAndOrientation(
controlEmulateDevice,
controlEmulateDeviceOrientation
)
: undefined,
markers: this.compareFlags.markers,
networkConditions: controlNetwork
? networkConditions[controlNetwork as keyof typeof networkConditions]
Expand All @@ -359,10 +361,12 @@ export default class Compare extends Command {
this.parsedConfig
),
delay,
emulateDeviceSettings: getEmulateDeviceSettingForKeyAndOrientation(
experimentEmulateDevice,
experimentEmulateDeviceOrientation
),
emulateDeviceSettings: experimentEmulateDevice
? getEmulateDeviceSettingForKeyAndOrientation(
experimentEmulateDevice,
experimentEmulateDeviceOrientation
)
: undefined,
markers: this.compareFlags.markers,
networkConditions: experimentNetwork
? networkConditions[experimentNetwork as keyof typeof networkConditions]
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/helpers/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
getDefaultValue,
} from '../command-config/default-flag-args';
import { parseMarkers } from './utils';
import deviceSettings from './simulate-device-options';
import deviceSettings, {
EmulateDeviceSettingCliOption,
} from './simulate-device-options';
/*
! oclif flags.build#parse will only execute when the flag:string is passed directly
! from the cli. thus when passed via the tbconfig.json or the defaultFlagArgs
Expand Down Expand Up @@ -200,7 +202,9 @@ export const socksPorts = flags.build({
export const emulateDevice = flags.build({
default: () => getDefaultValue('emulateDevice'),
description: `Emulate a mobile device screen size.`,
options: deviceSettings.map(setting => `${setting.typeable}`),
options: deviceSettings.map(
(setting: EmulateDeviceSettingCliOption) => `${setting.typeable}`
),
});

export const emulateDeviceOrientation = flags.build({
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import deviceSettings, {
EmulateDeviceSetting,
getEmulateDeviceSettingForKeyAndOrientation,
} from './simulate-device-options';
import { Stats } from './statistics/stats';
import { getWilcoxonRankSumTest } from './statistics/wilcoxon-rank-sum';
import { getWilcoxonSignedRankTest } from './statistics/wilcoxon-signed-rank';
import createConsumeableHTML, { ITracerBenchTraceResult } from './create-consumable-html';
import createConsumeableHTML, {
ITracerBenchTraceResult,
} from './create-consumable-html';

export {
deviceSettings,
Expand All @@ -14,4 +17,5 @@ export {
getWilcoxonSignedRankTest,
createConsumeableHTML,
ITracerBenchTraceResult,
getEmulateDeviceSettingForKeyAndOrientation,
};
15 changes: 8 additions & 7 deletions packages/cli/src/helpers/simulate-device-options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CLIError } from '@oclif/errors';
import Protocol from 'devtools-protocol';
import { convertToTypable } from './utils';
import { deviceLookup } from './device-lookup';
Expand All @@ -21,11 +22,9 @@ export interface EmulateDeviceSettingBase {
}

export interface EmulateDeviceSetting
extends EmulateDeviceSettingBase,
Protocol.Emulation.SetDeviceMetricsOverrideRequest,
extends Protocol.Emulation.SetDeviceMetricsOverrideRequest,
Protocol.Emulation.SetUserAgentOverrideRequest {
width: number;
height: number;
typeable: string;
}

export interface EmulateDeviceSettingCliOption
Expand Down Expand Up @@ -56,14 +55,14 @@ const deviceSettings: EmulateDeviceSettingCliOption[] = deviceLookup.map(
export function getEmulateDeviceSettingForKeyAndOrientation(
key: string,
orientation: string = 'vertical'
): EmulateDeviceSetting | undefined {
): EmulateDeviceSetting {
let deviceSetting;

for (deviceSetting of deviceSettings) {
if (key === deviceSetting.typeable) {
if (!deviceSetting.screens[orientation!]) {
throw new Error(
`${orientation} orientation for ${key} does not exist.`
throw new CLIError(
`${orientation} orientation for ${key} does not exist`
);
}
return {
Expand All @@ -76,6 +75,8 @@ export function getEmulateDeviceSettingForKeyAndOrientation(
};
}
}

throw new CLIError(`Device emulation settings not found for device ${key}`);
}

export default deviceSettings;
3 changes: 3 additions & 0 deletions packages/cli/tb-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
"controlURL": {
"type": "string"
},
"cookie": {
"type": "string"
},
"cpuThrottleRate": {
"type": [
"string",
Expand Down
15 changes: 9 additions & 6 deletions packages/cli/test/helpers/simulate-device-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
getEmulateDeviceSettingForKeyAndOrientation
} from '../../src/helpers/simulate-device-options';
import { getEmulateDeviceSettingForKeyAndOrientation } from '../../src/helpers/simulate-device-options';
import { expect } from 'chai';


describe('simulate-device-options', () => {
it(`getEmulateDeviceSettingForKeyAndOrientation() with non-existent device`, () => {
const result = getEmulateDeviceSettingForKeyAndOrientation('not-exist');
expect(result).to.equal(undefined);
const device = 'not-exist';
try {
getEmulateDeviceSettingForKeyAndOrientation(device);
} catch (error) {
expect(error.message).to.equal(
`Device emulation settings not found for device ${device}`
);
}
});

it(`getEmulateDeviceSettingForKeyAndOrientation() success path`, () => {
Expand Down
15 changes: 11 additions & 4 deletions packages/tracerbench/src/trace/archive_trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export interface IEntry {
response: IResponse;
}

export async function harTrace(url: string, additionalBrowserArgs: string[] = [], cookies: any = null) {

export async function harTrace(
url: string,
additionalBrowserArgs: string[] = [],
cookies: any = null
) {
// the saving of the cookies should be a dif command
// spawn browser > sign-in > done > save cookies

Expand Down Expand Up @@ -66,7 +69,9 @@ export async function harTrace(url: string, additionalBrowserArgs: string[] = []

await client.send('Network.enable');

cookies = cookies ? cookies : await client.send('Network.getCookies', { urls });
cookies = cookies
? cookies
: await client.send('Network.getCookies', { urls });
await setCookies(client, cookies);

await client.send('Page.enable');
Expand All @@ -79,7 +84,9 @@ export async function harTrace(url: string, additionalBrowserArgs: string[] = []
for (let i = 0; i < requestIds.length; i++) {
const requestId = requestIds[i];
const response = responses[i];
const responseBody = await client.send('Network.getResponseBody', { requestId });
const responseBody = await client.send('Network.getResponseBody', {
requestId,
});
const entry: IEntry = {
request: { url: response.url },
response: { content: { text: responseBody.body } },
Expand Down

0 comments on commit 37e3a09

Please sign in to comment.