Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scripts/verify-cli-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ describe("verify-cli-update", () => {

it("bootstraps an exact candidate from a legacy v1 update state", async () => {
const deps = createDeps();
let startInstallAttempts = 0;
vi.mocked(deps.callWs).mockImplementation(async ({ op }) => {
if (op === "updates.getState") {
return { version: 1, currentVersion: "0.5.6", updateStatus: "idle" };
Expand All @@ -274,6 +275,10 @@ describe("verify-cli-update", () => {
return { activity: { hasActiveWork: false } };
}
if (op === "updates.startInstall") {
startInstallAttempts += 1;
if (startInstallAttempts === 1) {
throw new Error("update_busy: Update check is already in progress");
}
return {
version: 1,
currentVersion: "0.5.6",
Expand Down Expand Up @@ -323,6 +328,8 @@ describe("verify-cli-update", () => {
args: { targetVersion: "0.5.7", force: false },
})
);
expect(deps.wait).toHaveBeenCalledWith(500);
expect(startInstallAttempts).toBe(2);
});

it("waits for a just-written candidate dist-tag to propagate", async () => {
Expand Down
37 changes: 34 additions & 3 deletions scripts/verify-cli-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,37 @@ async function waitForCandidateRelease(input: {
);
}

async function startInstallAfterBackgroundCheck(input: {
deps: Pick<VerifyCliUpdateDeps, "callWs" | "wait">;
apiUrl: string;
candidateVersion: string;
maxAttempts?: number;
}): Promise<UpdateStateSnapshot> {
const maxAttempts = input.maxAttempts ?? 60;
let lastError: unknown;

for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await input.deps.callWs<UpdateStateSnapshot>({
apiUrl: input.apiUrl,
op: "updates.startInstall",
args: { targetVersion: input.candidateVersion, force: false },
});
} catch (startError) {
lastError = startError;
const message = startError instanceof Error ? startError.message : String(startError);
if (!message.startsWith("update_busy: Update check is already in progress")) {
throw startError;
}
if (attempt < maxAttempts) await input.deps.wait(500);
}
}

throw new Error(
`CLI updater remained busy while waiting for its startup update check: ${lastError instanceof Error ? lastError.message : String(lastError)}`
);
}

export async function verifyCliUpdate(
options: VerifyCliUpdateOptions,
deps: VerifyCliUpdateDeps = defaultDeps
Expand Down Expand Up @@ -767,10 +798,10 @@ export async function verifyCliUpdate(
});
if (prepared.activity.hasActiveWork)
throw new Error("CLI acceptance prefix unexpectedly has active work");
const started = await deps.callWs<UpdateStateSnapshot>({
const started = await startInstallAfterBackgroundCheck({
deps,
apiUrl: server.apiUrl,
op: "updates.startInstall",
args: { targetVersion: candidateVersion, force: false },
candidateVersion,
});
if (
started.targetVersion !== candidateVersion ||
Expand Down
Loading