Files
dev/third_party/pi/packages/ai/test/google-shared-retry.test.ts
alexion 4def357d09 chore(vendor): import pi 0.85.1
Imported as a linear vendor commit so forge rebases do not replay the upstream root tree.

git-subtree-dir: third_party/pi

git-subtree-split: d981de1229ef899957bbe968bc8dcda02a21f477
2026-09-14 10:48:47 -04:00

41 lines
1.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { retryGoogleRequest } from "../src/api/google-shared.ts";
/** Shaped like @google/genai's ApiError: has `status`, but no `headers`. */
function googleApiError(status: number): Error {
return Object.assign(new Error(`got status: ${status}`), { status });
}
describe("google request retries", () => {
afterEach(() => {
vi.useRealTimers();
});
it("retries a headers-less SDK error with a retryable status", async () => {
vi.useFakeTimers();
const request = vi.fn<() => Promise<string>>().mockRejectedValueOnce(googleApiError(429)).mockResolvedValue("ok");
const result = retryGoogleRequest(request, { maxRetries: 1 });
await vi.advanceTimersByTimeAsync(500);
await expect(result).resolves.toBe("ok");
expect(request).toHaveBeenCalledTimes(2);
});
it("does not retry when maxRetries is unset", async () => {
const error = googleApiError(429);
const request = vi.fn<() => Promise<string>>().mockRejectedValue(error);
await expect(retryGoogleRequest(request)).rejects.toBe(error);
expect(request).toHaveBeenCalledTimes(1);
});
it("does not retry a non-retryable status", async () => {
const error = googleApiError(400);
const request = vi.fn<() => Promise<string>>().mockRejectedValue(error);
await expect(retryGoogleRequest(request, { maxRetries: 2 })).rejects.toBe(error);
expect(request).toHaveBeenCalledTimes(1);
});
});