diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 5e0232047..61a533980 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -4668,6 +4668,16 @@ const HOST_IMAGES: readonly FitImage[] = [ ["34", "android-tv", "arm64-v8a"], ]; +// why: the verbatim source.properties of the real android-Baklava image installed beside +// android-36 and android-37.0 on a reporting host, so the fixture holds what the SDK really stores. +const BAKLAVA_SOURCE_PROPERTIES = + "Pkg.Desc=System Image arm64-v8a with Google APIs.\n" + + "Pkg.Revision=4\n" + + "AndroidVersion.ApiLevel=35\n" + + "AndroidVersion.CodeName=Baklava\n" + + "SystemImage.Abi=arm64-v8a\n" + + "SystemImage.TagId=google_apis\n"; + describe("AndroidDriver image fit", () => { describe("each row of the In short table", () => { interface Row { @@ -4886,6 +4896,60 @@ describe("AndroidDriver image fit", () => { expect(await resolve("Pixel 9")).toMatchObject({ osVersion: "Baklava" }); }); + it("ranks a preview image by the API level it records, so a newer numeric level stays the newest", async () => { + const { driver, resolve } = await fitHarness([ + ["35", "google_apis", "arm64-v8a"], + ["36", "google_apis", "arm64-v8a"], + ["37.0", "google_apis", "arm64-v8a"], + ["Baklava", "google_apis", "arm64-v8a", BAKLAVA_SOURCE_PROPERTIES], + ]); + + const catalog = await driver.listCatalog(); + + // why: stated first so the assertions below can only fail on the *ranking* — the preview is + // genuinely a listed candidate, not something the catalog left out. + expect(catalog.runtimes).toContain("Baklava"); + expect(catalog.modelRuntimes["Pixel 9"]).toContain("Baklava"); + expect(catalog.defaultRuntime).toBe("37.0"); + expect(await resolve("Pixel 9")).toMatchObject({ osVersion: "37.0" }); + }); + + it("ranks a preview image above the numeric levels when the level it records is higher", async () => { + const { driver } = await fitHarness([ + ["37.0", "google_apis", "arm64-v8a"], + ["Baklava", "google_apis", "arm64-v8a", "AndroidVersion.ApiLevel=38\n"], + ]); + + expect((await driver.listCatalog()).defaultRuntime).toBe("Baklava"); + }); + + it("ranks a preview folder by the highest level any of its images records, so an image recording none does not raise it", async () => { + const { driver } = await fitHarness([ + ["36", "google_apis", "arm64-v8a"], + ["37.0", "google_apis", "arm64-v8a"], + ["Baklava", "google_apis", "arm64-v8a", BAKLAVA_SOURCE_PROPERTIES], + ["Baklava", "google_apis_playstore", "arm64-v8a"], + ]); + + expect((await driver.listCatalog()).defaultRuntime).toBe("37.0"); + }); + + it.each([ + ["lower first", "35", "38"], + ["higher first", "38", "35"], + ])( + "ranks a preview folder by the highest level among its images, whichever is read first (%s)", + async (_order, first, second) => { + const { driver } = await fitHarness([ + ["37.0", "google_apis", "arm64-v8a"], + ["Baklava", "google_apis", "arm64-v8a", `AndroidVersion.ApiLevel=${first}\n`], + ["Baklava", "google_apis_playstore", "arm64-v8a", `AndroidVersion.ApiLevel=${second}\n`], + ]); + + expect((await driver.listCatalog()).defaultRuntime).toBe("Baklava"); + }, + ); + it("uses the folder name for --image-tag and the catalog's images[].tag when SystemImage.TagId differs", async () => { const { driver, refusal, resolve } = await fitHarness([ ["35", "custom-folder", "arm64-v8a", "SystemImage.TagId=google_apis_playstore\n"], diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index f28da9f4c..f17363f1e 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -2258,7 +2258,21 @@ class AndroidImageTagMissingError extends RuntimeMissingError { } function installedApiLevels(images: readonly SystemImage[]): string[] { - return [...new Set(images.map((image) => image.apiLevel))].sort(compareApiLevels); + // why: a preview folder (`Baklava`) ranks by the highest level any of its images records, so an + // image that records none cannot make the preview outrank a numeric level. + const rank = new Map(); + for (const image of images) { + const level = /^\d/.test(image.apiLevel) ? image.apiLevel : image.sourceApiLevel; + const current = rank.get(image.apiLevel); + if (level !== undefined && (current === undefined || compareApiLevels(level, current) > 0)) { + rank.set(image.apiLevel, level); + } + } + return [...new Set(images.map((image) => image.apiLevel))].sort( + (left, right) => + compareApiLevels(rank.get(left) ?? left, rank.get(right) ?? right) || + left.localeCompare(right), + ); } function fittingApiLevels(profile: DeviceProfile, images: readonly SystemImage[]): string[] {