diff --git a/src/core/component-installer.test.ts b/src/core/component-installer.test.ts index 36c1138a8..435b1db91 100644 --- a/src/core/component-installer.test.ts +++ b/src/core/component-installer.test.ts @@ -1397,6 +1397,49 @@ describe("ComponentInstaller.remove", () => { expect(harness.registry.snapshot.components).toHaveLength(1); }); + it("refuses a record whose version is no longer installed saying none is installed, and a record whose version was installed again saying that one is not Simlock's, in a removal and a dry run alike", async () => { + // 26.5 is on the machine with another receipt; 25.0 is not on the machine at all. + const harness = await createHarness(); + for (const version of ["25.0", "26.5"]) { + await harness.registry.recordComponent({ + installedAt: 1_000, + platform: "ios", + receipt: { install: "1", version }, + version, + }); + } + + for (const dryRun of [false, true]) { + await expect(harness.installer.remove(removeIos("25.0", { dryRun }))).rejects.toMatchObject({ + message: "No ios 25.0 is installed now, so there is nothing to remove", + name: "ComponentNotOwnedError", + }); + await expect(harness.installer.remove(removeIos("26.5", { dryRun }))).rejects.toMatchObject({ + message: + "The ios 26.5 installed now is not the one Simlock installed, so Simlock will not remove it", + name: "ComponentNotOwnedError", + }); + } + expect(removals(harness.ios)).toEqual([]); + }); + + it("removes a component whose receipt matches the record even when the listing names its version differently", async () => { + // The driver lists 26.5 with the receipt Simlock recorded under 26.5.0. + const harness = await createHarness(); + const receipt = { preinstalled: "26.5" }; + await harness.registry.recordComponent({ + installedAt: 1_000, + platform: "ios", + receipt, + version: "26.5.0", + }); + + await expect(harness.installer.remove(removeIos("26.5.0"))).resolves.toMatchObject({ + outcome: "removed", + }); + expect(removals(harness.ios)).toEqual([receipt]); + }); + it.each(["provisioning", "ready", "leased", "reclaiming", "quarantined", "shutdown"] as const)( "refuses a component a %s registry device uses with ComponentInUseError naming the count, and never calls removeComponent", async (state) => { diff --git a/src/core/component-installer.ts b/src/core/component-installer.ts index b4152f306..692dfa4c3 100644 --- a/src/core/component-installer.ts +++ b/src/core/component-installer.ts @@ -388,10 +388,17 @@ export class ComponentInstaller { `Simlock has no record of installing ${platform} ${version}, so it will not remove it`, ); } - const listed = (await this.#listDriver(driver)).find((entry) => - sameReceipt(entry.receipt, record.receipt), - ); + const components = await this.#listDriver(driver); + const listed = components.find((entry) => sameReceipt(entry.receipt, record.receipt)); if (listed === undefined) { + // A record outlives a removal that failed after the driver deleted the component (ADR 0010 + // ยง5): with no such version on the machine, the refusal must not name one. + if (!components.some((entry) => entry.listing.version === version)) { + throw new ComponentNotOwnedError( + platform, + `No ${platform} ${version} is installed now, so there is nothing to remove`, + ); + } throw new ComponentNotOwnedError( platform, `The ${platform} ${version} installed now is not the one Simlock installed, so Simlock ` +