Skip to content

Commit

Permalink
fix: Update focused activity validation logic (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jan 10, 2025
1 parent 97fabf7 commit c862dbd
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions lib/tools/app-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,31 +947,44 @@ export async function waitForActivityOrNot (pkg, activity, waitForStop, waitMs =
if (!pkg || !activity) {
throw new Error('Package and activity required.');
}
log.debug(`Waiting up to ${waitMs}ms for activity matching pkg: '${pkg}' and ` +
`activity: '${activity}' to${waitForStop ? ' not' : ''} be focused`);

const splitNames = (names) => names.split(',').map((name) => name.trim());
const splitNames = (/** @type {string} */ names) => names.split(',').map(_.trim);
const allPackages = splitNames(pkg);
const allActivities = splitNames(activity);

const possibleActivityNames = [];
const toFullyQualifiedActivityName = (/** @type {string} */ prefix, /** @type {string} */ suffix) =>
`${prefix}${suffix}`.replace(/\/\.?/g, '.').replace(/\.{2,}/g, '.');
/** @type {Set<string>} */
const possibleActivityNamesSet = new Set();
for (const oneActivity of allActivities) {
if (oneActivity.startsWith('.')) {
// add the package name if activity is not full qualified
for (const currentPkg of allPackages) {
possibleActivityNames.push(`${currentPkg}${oneActivity}`.replace(/\.+/g, '.'));
for (const onePkg of allPackages) {
possibleActivityNamesSet.add(toFullyQualifiedActivityName(onePkg, oneActivity));
}
} else {
// accept fully qualified activity name.
possibleActivityNames.push(oneActivity);
possibleActivityNames.push(`${pkg}.${oneActivity}`);
possibleActivityNamesSet.add(toFullyQualifiedActivityName(oneActivity, ''));
const doesIncludePackage = allPackages.some((p) => oneActivity.startsWith(p));
if (!doesIncludePackage) {
for (const onePkg of allPackages) {
possibleActivityNamesSet.add(toFullyQualifiedActivityName(onePkg, `.${oneActivity}`));
}
}
}
}
log.debug(`Possible activities, to be checked: ${possibleActivityNames.map((name) => `'${name}'`).join(', ')}`);

log.debug(
`Expected package names to ${waitForStop ? 'not ' : ''}be focused within ${waitMs}ms: ` +
allPackages.map((name) => `'${name}'`).join(', ')
);
const possibleActivityNames = [...possibleActivityNamesSet];
const possibleActivityPatterns = possibleActivityNames.map(
(actName) => new RegExp(`^${actName.replace(/\./g, '\\.').replace(/\*/g, '.*?').replace(/\$/g, '\\$')}$`)
);
log.debug(
`Expected activity name patterns to ${waitForStop ? 'not ' : ''}be focused within ${waitMs}ms: ` +
possibleActivityPatterns.map((name) => `'${name}'`).join(', ')
);

const conditionFunc = async () => {
let appPackage;
Expand All @@ -983,15 +996,21 @@ export async function waitForActivityOrNot (pkg, activity, waitForStop, waitMs =
return false;
}
if (appActivity && appPackage) {
const fullyQualifiedActivity = appActivity.startsWith('.') ? `${appPackage}${appActivity}` : appActivity;
log.debug(`Found package: '${appPackage}' and fully qualified activity name : '${fullyQualifiedActivity}'`);
const isActivityFound = _.includes(allPackages, appPackage)
log.debug(`Focused package: ${appPackage}`);
const fullyQualifiedActivity = toFullyQualifiedActivityName(
appActivity.startsWith('.') ? appPackage : '',
appActivity
);
log.debug(`Focused fully qualified activity name: ${fullyQualifiedActivity}`);
const isFound = _.includes(allPackages, appPackage)
&& possibleActivityPatterns.some((p) => p.test(fullyQualifiedActivity));
if ((!waitForStop && isActivityFound) || (waitForStop && !isActivityFound)) {
if ((!waitForStop && isFound) || (waitForStop && !isFound)) {
return true;
}
}
log.debug('Incorrect package and activity. Retrying.');
log.debug(
'None of the expected package/activity combinations matched to the currently focused one. Retrying'
);
return false;
};

Expand All @@ -1001,8 +1020,11 @@ export async function waitForActivityOrNot (pkg, activity, waitForStop, waitMs =
intervalMs: 500,
});
} catch {
throw new Error(`${possibleActivityNames.map((name) => `'${name}'`).join(' or ')} never ${waitForStop ? 'stopped' : 'started'}. ` +
`Consider checking the driver's troubleshooting documentation.`);
throw new Error(
`${possibleActivityNames.map((name) => `'${name}'`).join(' or ')} ` +
`never ${waitForStop ? 'stopped' : 'started'}. ` +
`Consider checking the driver's troubleshooting documentation.`
);
}
}

Expand Down

0 comments on commit c862dbd

Please sign in to comment.