-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refactor dataconnect to use the common appUtils models #9286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
// Detect what platform based on current user | ||
let targetPlatform = await getPlatformFromFolder(appDir); | ||
if (targetPlatform === Platform.NONE) { | ||
let targetPlatforms = await getPlatformsFromFolder(appDir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maneesht is the right person to take a look at management/apps.ts
~
I took a glance. Don't see any issues.
b6bdceb
to
79b8215
Compare
79b8215
to
0eca209
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request does a great job of refactoring the app detection logic into a common appUtils
module, which improves code organization and reusability. The increased test coverage is also a welcome addition. I've found a critical issue with a new deduplication function and a couple of other points that I've detailed in the specific comments. Once these are addressed, this will be a solid improvement.
function dedupeAppsByPlatformAndDirectory(apps: App[]): App[] { | ||
// detectApps creates unique apps by appId and bundleId, but this method operates | ||
// on platform, directory, and frameworks alone. Deduping here to retain the | ||
// same behavior | ||
return [ | ||
...new Set( | ||
apps.map( | ||
(app) => | ||
({ | ||
platform: app.platform, | ||
directory: app.directory, | ||
frameworks: app.frameworks, | ||
}) as App, | ||
), | ||
), | ||
]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dedupeAppsByPlatformAndDirectory
function is implemented using new Set()
on newly created objects inside a map
. This will not correctly deduplicate the apps because objects in JavaScript are compared by reference, and each iteration of map
creates a new object instance. This can lead to duplicate app configurations being processed downstream.
To fix this, you should generate a unique key for each app based on its properties and use a Map
to ensure uniqueness.
function dedupeAppsByPlatformAndDirectory(apps: App[]): App[] { | |
// detectApps creates unique apps by appId and bundleId, but this method operates | |
// on platform, directory, and frameworks alone. Deduping here to retain the | |
// same behavior | |
return [ | |
...new Set( | |
apps.map( | |
(app) => | |
({ | |
platform: app.platform, | |
directory: app.directory, | |
frameworks: app.frameworks, | |
}) as App, | |
), | |
), | |
]; | |
} | |
function dedupeAppsByPlatformAndDirectory(apps: App[]): App[] { | |
// detectApps creates unique apps by appId and bundleId, but this method operates | |
// on platform, directory, and frameworks alone. Deduping here to retain the | |
// same behavior | |
const uniqueApps = new Map<string, App>(); | |
for (const app of apps) { | |
// Sorting frameworks for consistent key generation | |
const frameworkKey = app.frameworks ? [...app.frameworks].sort().join(",") : ""; | |
const key = `${app.platform}:${app.directory}:${frameworkKey}`; | |
if (!uniqueApps.has(key)) { | |
// Create a new object with only the desired properties to avoid carrying over others like appId | |
uniqueApps.set(key, { | |
platform: app.platform, | |
directory: app.directory, | |
frameworks: app.frameworks, | |
}); | |
} | |
} | |
return Array.from(uniqueApps.values()); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does seem legit and worth fixing.
it("should deduplicate apps with the same platform and directory", async () => { | ||
const apps: App[] = [ | ||
{ platform: Platform.WEB, directory: "web", frameworks: [Framework.REACT], appId: "app1" }, | ||
{ platform: Platform.WEB, directory: "web", frameworks: [Framework.REACT], appId: "app2" }, | ||
{ platform: Platform.ANDROID, directory: "android" }, | ||
]; | ||
detectAppsStub.resolves(apps); | ||
promptStub.resolves([ | ||
{ platform: Platform.WEB, directory: "web", frameworks: [Framework.REACT] }, | ||
]); | ||
|
||
const selectedApps = await chooseApp(); | ||
|
||
expect(selectedApps).to.deep.equal([ | ||
{ platform: Platform.WEB, directory: "web", frameworks: [Framework.REACT] }, | ||
]); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case should deduplicate apps with the same platform and directory
doesn't properly validate the deduplication logic. It mocks the result of the user prompt, which hides the fact that chooseApp
would present duplicated choices to the user due to the faulty dedupeAppsByPlatformAndDirectory
function.
To make this test more robust, you should assert that the choices
argument passed to the prompt.checkbox
stub contains deduplicated entries.
|
||
if (targetPlatforms.length !== 1) { | ||
if (targetPlatforms.length === 0) { | ||
logBullet(`Couldn't automatically detect app your in directory ${appDir}.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Check for environment variables override. | ||
const envAppFolder = envOverride(FDC_APP_FOLDER, ""); | ||
const envPlatform = envOverride(FDC_SDK_PLATFORM_ENV, Platform.NONE) as Platform; | ||
const envPlatform: Platform | undefined = envOverride(FDC_SDK_PLATFORM_ENV, "") as Platform; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This typing seems a little wonky to me? I believe this will either be a string from the env var, or "", neither of which are undefined.
Description
Scenarios Tested
Sample Commands