|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { createBlobProgram, runCommand } from "../helpers/program.js"; |
3 | 3 | import { fetchBlobCredentials } from "../../src/commands/blob/credentials.js"; |
| 4 | +import { deleteBlobBucket } from "../../src/commands/blob/delete.js"; |
| 5 | +import { isFreshlyCreated } from "../../src/commands/blob/retry.js"; |
4 | 6 | import type { BlobBucket, BlobS3Credentials } from "../../src/types.js"; |
5 | 7 |
|
6 | 8 | const originalEnv = { ...process.env }; |
@@ -317,6 +319,93 @@ describe("blob credentials command", () => { |
317 | 319 | ).rejects.toThrow(/rejected/); |
318 | 320 | }); |
319 | 321 |
|
| 322 | + it("401 hint mentions provisioning when no retries were allowed", async () => { |
| 323 | + vi.spyOn(globalThis, "fetch").mockResolvedValue( |
| 324 | + new Response('{"error":"unauthorized"}', { status: 401 }), |
| 325 | + ); |
| 326 | + |
| 327 | + await expect(fetchBlobCredentials("token", async () => {})).rejects.toThrow( |
| 328 | + /still be provisioning/, |
| 329 | + ); |
| 330 | + }); |
| 331 | + |
| 332 | + it("retries 401 while a fresh bucket is provisioning, then succeeds", async () => { |
| 333 | + const credentials = makeCredentials(); |
| 334 | + const delays: number[] = []; |
| 335 | + vi.spyOn(globalThis, "fetch") |
| 336 | + .mockResolvedValueOnce(new Response('{"error":"unauthorized"}', { status: 401 })) |
| 337 | + .mockResolvedValueOnce(new Response('{"error":"unauthorized"}', { status: 401 })) |
| 338 | + .mockResolvedValueOnce(new Response(JSON.stringify(credentials), { status: 200 })); |
| 339 | + |
| 340 | + const result = await fetchBlobCredentials( |
| 341 | + "bucket-token", |
| 342 | + async (ms) => { |
| 343 | + delays.push(ms); |
| 344 | + }, |
| 345 | + { unauthorizedRetries: 10 }, |
| 346 | + ); |
| 347 | + |
| 348 | + expect(result).toEqual(credentials); |
| 349 | + expect(delays).toEqual([3000, 3000]); |
| 350 | + }); |
| 351 | + |
| 352 | + it("gives up on 401 after the provisioning retry budget and says how long it waited", async () => { |
| 353 | + const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => |
| 354 | + new Response('{"error":"unauthorized"}', { status: 401 }), |
| 355 | + ); |
| 356 | + |
| 357 | + await expect( |
| 358 | + fetchBlobCredentials("bucket-token", async () => {}, { unauthorizedRetries: 2 }), |
| 359 | + ).rejects.toThrow(/rejected after waiting 6s/); |
| 360 | + expect(fetchSpy).toHaveBeenCalledTimes(3); |
| 361 | + }); |
| 362 | + |
| 363 | + it("401 retries do not consume the throttle retry budget", async () => { |
| 364 | + const credentials = makeCredentials(); |
| 365 | + vi.spyOn(globalThis, "fetch") |
| 366 | + .mockResolvedValueOnce(new Response("", { status: 401 })) |
| 367 | + .mockResolvedValueOnce(new Response("", { status: 503 })) |
| 368 | + .mockResolvedValueOnce(new Response("", { status: 401 })) |
| 369 | + .mockResolvedValueOnce(new Response("", { status: 503 })) |
| 370 | + .mockResolvedValueOnce(new Response("", { status: 503 })) |
| 371 | + .mockResolvedValueOnce(new Response(JSON.stringify(credentials), { status: 200 })); |
| 372 | + |
| 373 | + const result = await fetchBlobCredentials("t", async () => {}, { unauthorizedRetries: 2 }); |
| 374 | + expect(result).toEqual(credentials); |
| 375 | + }); |
| 376 | + |
| 377 | + it("by bucket id does not retry 401 for a bucket that is not freshly created", async () => { |
| 378 | + const bucket = makeBucket({ creation_time: 1 }); |
| 379 | + const fetchSpy = vi |
| 380 | + .spyOn(globalThis, "fetch") |
| 381 | + .mockResolvedValueOnce(new Response(JSON.stringify(bucket), { status: 200 })) |
| 382 | + .mockResolvedValue(new Response('{"error":"unauthorized"}', { status: 401 })); |
| 383 | + |
| 384 | + const program = await createBlobProgram(); |
| 385 | + await expect( |
| 386 | + runCommand(program, ["blob", "credentials", "--bucket-id", "bucket_123"]), |
| 387 | + ).rejects.toThrow(/rejected/); |
| 388 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 389 | + }); |
| 390 | + |
| 391 | + it("by bucket id polls 401 for a freshly created bucket", async () => { |
| 392 | + vi.useFakeTimers(); |
| 393 | + const bucket = makeBucket({ creation_time: Math.floor(Date.now() / 1000) }); |
| 394 | + const credentials = makeCredentials(); |
| 395 | + const fetchSpy = vi |
| 396 | + .spyOn(globalThis, "fetch") |
| 397 | + .mockResolvedValueOnce(new Response(JSON.stringify(bucket), { status: 200 })) |
| 398 | + .mockResolvedValueOnce(new Response('{"error":"unauthorized"}', { status: 401 })) |
| 399 | + .mockResolvedValueOnce(new Response(JSON.stringify(credentials), { status: 200 })); |
| 400 | + |
| 401 | + const program = await createBlobProgram(); |
| 402 | + const pending = runCommand(program, ["blob", "credentials", "--bucket-id", "bucket_123"]); |
| 403 | + await vi.advanceTimersByTimeAsync(3000); |
| 404 | + |
| 405 | + expect(await pending).toEqual(credentials); |
| 406 | + expect(fetchSpy).toHaveBeenCalledTimes(3); |
| 407 | + }); |
| 408 | + |
320 | 409 | it("retries 429 and 503 with retry-after or fallback delays, then succeeds", async () => { |
321 | 410 | const credentials = makeCredentials(); |
322 | 411 | const delays: number[] = []; |
@@ -349,3 +438,70 @@ describe("blob credentials command", () => { |
349 | 438 | expect(delays).toEqual([10000, 2000, 3000]); |
350 | 439 | }); |
351 | 440 | }); |
| 441 | + |
| 442 | +describe("blob provisioning helpers", () => { |
| 443 | + it("treats missing or recent creation times as freshly created", () => { |
| 444 | + expect(isFreshlyCreated(undefined)).toBe(true); |
| 445 | + expect(isFreshlyCreated(1000, 1000 + 60)).toBe(true); |
| 446 | + expect(isFreshlyCreated(1000, 1000 + 5 * 60)).toBe(false); |
| 447 | + }); |
| 448 | +}); |
| 449 | + |
| 450 | +describe("blob delete retries", () => { |
| 451 | + const auth = { email: "user@example.com", apiKey: "api-key" }; |
| 452 | + |
| 453 | + it("retries 5xx while the bucket is provisioning, then succeeds", async () => { |
| 454 | + const delays: number[] = []; |
| 455 | + const fetchSpy = vi |
| 456 | + .spyOn(globalThis, "fetch") |
| 457 | + .mockResolvedValueOnce(new Response('{"error":"internal"}', { status: 500 })) |
| 458 | + .mockResolvedValueOnce(new Response('"OK"', { status: 200 })); |
| 459 | + |
| 460 | + await deleteBlobBucket(auth, "bucket_123", async (ms) => { |
| 461 | + delays.push(ms); |
| 462 | + }); |
| 463 | + |
| 464 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 465 | + expect(delays).toEqual([3000]); |
| 466 | + }); |
| 467 | + |
| 468 | + it("treats a 404 after a failed attempt as already deleted", async () => { |
| 469 | + vi.spyOn(globalThis, "fetch") |
| 470 | + .mockResolvedValueOnce(new Response('{"error":"internal"}', { status: 500 })) |
| 471 | + .mockResolvedValueOnce(new Response('{"error":"resource not found"}', { status: 404 })); |
| 472 | + |
| 473 | + await expect(deleteBlobBucket(auth, "bucket_123", async () => {})).resolves.toBeUndefined(); |
| 474 | + }); |
| 475 | + |
| 476 | + it("does not retry a first-attempt 404 or any 4xx", async () => { |
| 477 | + const fetchSpy = vi.spyOn(globalThis, "fetch") |
| 478 | + .mockResolvedValueOnce(new Response('{"error":"resource not found"}', { status: 404 })); |
| 479 | + await expect(deleteBlobBucket(auth, "bucket_123", async () => {})).rejects.toThrow(/not found/); |
| 480 | + expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 481 | + |
| 482 | + fetchSpy.mockResolvedValueOnce(new Response('{"error":"bad"}', { status: 400 })); |
| 483 | + await expect(deleteBlobBucket(auth, "bucket_123", async () => {})).rejects.toThrow(/bad/); |
| 484 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 485 | + }); |
| 486 | + |
| 487 | + it("gives up after the 5xx retry budget", async () => { |
| 488 | + const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => |
| 489 | + new Response('{"error":"internal"}', { status: 500 }), |
| 490 | + ); |
| 491 | + await expect(deleteBlobBucket(auth, "bucket_123", async () => {})).rejects.toThrow(/internal/); |
| 492 | + expect(fetchSpy).toHaveBeenCalledTimes(6); |
| 493 | + }); |
| 494 | + |
| 495 | + it("delete command surfaces the retried result", async () => { |
| 496 | + vi.useFakeTimers(); |
| 497 | + vi.spyOn(globalThis, "fetch") |
| 498 | + .mockResolvedValueOnce(new Response('{"error":"internal"}', { status: 500 })) |
| 499 | + .mockResolvedValueOnce(new Response('"OK"', { status: 200 })); |
| 500 | + |
| 501 | + const program = await createBlobProgram(); |
| 502 | + const pending = runCommand(program, ["blob", "delete", "--bucket-id", "bucket_123"]); |
| 503 | + await vi.advanceTimersByTimeAsync(3000); |
| 504 | + |
| 505 | + expect(await pending).toEqual({ deleted: true, bucket_id: "bucket_123" }); |
| 506 | + }); |
| 507 | +}); |
0 commit comments