Cleaned up formatting logic

This commit is contained in:
Adam Gastineau 2025-01-10 06:03:13 -08:00
parent 1c3d31ac9c
commit 366dad572f

View file

@ -1,48 +1,116 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { ActionTraceEvent } from '@trace/trace'; import type { ActionTraceEvent } from '@trace/trace';
import { asLocator, type Language } from '@isomorphic/locatorGenerators'; import { asLocator, type Language } from '@isomorphic/locatorGenerators';
export const commandContextString = (action: ActionTraceEvent, sdkLanguage: Language): string | undefined => { const formatClockParams = (params: {
const params = action.params; ticksNumber?: number;
ticksString?: string;
if (action.apiName.startsWith('clock')) { timeNumber?: number;
if ('ticksNumber' in params) { }): string | undefined => {
// clock.fastForward/runFor if (params.ticksNumber !== undefined) {
return `${params.ticksNumber}ms`; // clock.fastForward/runFor
} else if (params.ticksString) { return `${params.ticksNumber}ms`;
// clock.fastForward/runFor } else if (params.ticksString !== undefined) {
return params.ticksString; // clock.fastForward/runFor
} else if ('timeNumber' in params) { return params.ticksString;
// clock.pauseAt/setFixedTime/setSystemTime } else if (params.timeNumber !== undefined) {
try { // clock.pauseAt/setFixedTime/setSystemTime
return new Date(params.timeNumber).toLocaleString(); try {
} catch (e) { return new Date(params.timeNumber).toLocaleString();
return undefined; } catch (e) {
} return undefined;
}
} else if (action.apiName.startsWith('keyboard')) {
if (params.key) {
// keyboard.press/down/up
return params.key;
} else if (params.text) {
// keyboard.type/insertText
return `"${params.text}"`;
}
} else if (action.apiName.startsWith('locator')) {
return asLocator(sdkLanguage, params.selector);
} else if (action.apiName.startsWith('mouse')) {
if ('x' in params && 'y' in params) {
// mouse.click/dblclick/move
return `(${params.x}, ${params.y})`;
} else if ('deltaX' in params && 'deltaY' in params) {
// mouse.wheel
return `(${params.deltaX}, ${params.deltaY})`;
}
} else if (action.apiName.startsWith('touchscreen')) {
if ('x' in params && 'y' in params) {
// touchscreen.tap
return `(${params.x}, ${params.y})`;
} }
} }
return undefined; return undefined;
}; };
const formatLocatorParams = (
sdkLanguage: Language,
params: { selector?: string },
): string | undefined =>
params.selector !== undefined
? asLocator(sdkLanguage, params.selector)
: undefined;
const formatKeyboardParams = (params: {
key?: string;
text?: string;
}): string | undefined => {
if (params.key !== undefined) {
// keyboard.press/down/up
return params.key;
} else if (params.text !== undefined) {
// keyboard.type/insertText
return `"${params.text}"`;
}
return undefined;
};
const formatMouseParams = (params: {
x?: number;
y?: number;
deltaX?: number;
deltaY?: number;
}): string | undefined => {
if (params.x !== undefined && params.y !== undefined) {
// mouse.click/dblclick/move
return `(${params.x}, ${params.y})`;
} else if (params.deltaX !== undefined && params.deltaY !== undefined) {
// mouse.wheel
return `(${params.deltaX}, ${params.deltaY})`;
}
return undefined;
};
const formatTouchscreenParams = (params: {
x?: number;
y?: number;
}): string | undefined => {
if (params.x && params.y) {
// touchscreen.tap
return `(${params.x}, ${params.y})`;
}
return undefined;
};
export const commandContextString = (
action: ActionTraceEvent,
sdkLanguage: Language,
): string | undefined => {
const params = action.params;
const apiName = action.apiName;
switch (true) {
case apiName.startsWith('clock'):
return formatClockParams(params);
case apiName.startsWith('keyboard'):
return formatKeyboardParams(params);
case apiName.startsWith('locator'):
return formatLocatorParams(sdkLanguage, params);
case apiName.startsWith('mouse'):
return formatMouseParams(params);
case apiName.startsWith('touchscreen'):
return formatTouchscreenParams(params);
default:
return undefined;
}
};