|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { isRefreshFailure, isRefreshSuccess, PkAuthRefreshClient } from "../src/refresh"; |
| 4 | + |
| 5 | +function stubFetch(handler: (init: RequestInit) => { status: number; body: string }) { |
| 6 | + return vi.fn(async (_url: RequestInfo | URL, init?: RequestInit) => { |
| 7 | + const { status, body } = handler(init ?? {}); |
| 8 | + return new Response(body, { status }); |
| 9 | + }); |
| 10 | +} |
| 11 | + |
| 12 | +describe("PkAuthRefreshClient", () => { |
| 13 | + it("returns RefreshSuccess on 200", async () => { |
| 14 | + const fetchImpl = stubFetch((init) => { |
| 15 | + expect(init.method).toBe("POST"); |
| 16 | + const body = JSON.parse(String(init.body)); |
| 17 | + expect(body).toEqual({ refreshToken: "old.token" }); |
| 18 | + return { |
| 19 | + status: 200, |
| 20 | + body: JSON.stringify({ |
| 21 | + refreshToken: "new.token", |
| 22 | + accessToken: "jwt-blob", |
| 23 | + expiresAt: "2026-06-01T00:00:00Z", |
| 24 | + }), |
| 25 | + }; |
| 26 | + }); |
| 27 | + |
| 28 | + const client = new PkAuthRefreshClient({ apiBase: "https://x", fetch: fetchImpl } as never); |
| 29 | + const result = await client.refresh("old.token"); |
| 30 | + |
| 31 | + expect(isRefreshSuccess(result)).toBe(true); |
| 32 | + if (isRefreshSuccess(result)) { |
| 33 | + expect(result.accessToken).toBe("jwt-blob"); |
| 34 | + expect(result.refreshToken).toBe("new.token"); |
| 35 | + expect(result.expiresAt).toBe("2026-06-01T00:00:00Z"); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it.each(["expired", "unknown", "replayed", "revoked"] as const)( |
| 40 | + "maps 401 with detail=%s to a typed RefreshFailure", |
| 41 | + async (detail) => { |
| 42 | + const fetchImpl = stubFetch(() => ({ |
| 43 | + status: 401, |
| 44 | + body: JSON.stringify({ detail }), |
| 45 | + })); |
| 46 | + const client = new PkAuthRefreshClient({ apiBase: "https://x", fetch: fetchImpl } as never); |
| 47 | + const result = await client.refresh("old.token"); |
| 48 | + expect(isRefreshFailure(result)).toBe(true); |
| 49 | + if (isRefreshFailure(result)) { |
| 50 | + expect(result.reason).toBe(detail); |
| 51 | + } |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + it("surfaces revokeReason when present", async () => { |
| 56 | + const fetchImpl = stubFetch(() => ({ |
| 57 | + status: 401, |
| 58 | + body: JSON.stringify({ detail: "revoked", reason: "USER_DELETED" }), |
| 59 | + })); |
| 60 | + const client = new PkAuthRefreshClient({ apiBase: "https://x", fetch: fetchImpl } as never); |
| 61 | + const result = await client.refresh("old.token"); |
| 62 | + expect(isRefreshFailure(result)).toBe(true); |
| 63 | + if (isRefreshFailure(result)) { |
| 64 | + expect(result.reason).toBe("revoked"); |
| 65 | + expect(result.revokeReason).toBe("USER_DELETED"); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + it("falls back to 'unknown' on 401 with no/unfamiliar detail", async () => { |
| 70 | + const fetchImpl = stubFetch(() => ({ status: 401, body: "{}" })); |
| 71 | + const client = new PkAuthRefreshClient({ apiBase: "https://x", fetch: fetchImpl } as never); |
| 72 | + const result = await client.refresh("old.token"); |
| 73 | + expect(isRefreshFailure(result)).toBe(true); |
| 74 | + if (isRefreshFailure(result)) { |
| 75 | + expect(result.reason).toBe("unknown"); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it("rethrows non-401 HTTP errors", async () => { |
| 80 | + const fetchImpl = stubFetch(() => ({ status: 500, body: "boom" })); |
| 81 | + const client = new PkAuthRefreshClient({ apiBase: "https://x", fetch: fetchImpl } as never); |
| 82 | + await expect(client.refresh("old.token")).rejects.toThrow(/HTTP 500/); |
| 83 | + }); |
| 84 | +}); |
0 commit comments