Repository Public
1import assert from "node:assert/strict"2import { test } from "node:test"3import { ReauthCancelled, isReauthRequired, retryWithReauth } from "./reauth"45const reauthError = () => Object.assign(new Error("reauth_required"), { status: 403, code: "reauth_required" })67test("réauth : action réussie du premier coup → aucune invite", async () => {8 let prompts = 09 const r = await retryWithReauth(async () => "ok", async () => void prompts++)10 assert.equal(r, "ok")11 assert.equal(prompts, 0)12})1314test("réauth : 403 reauth_required → invite, puis l'action est rejouée une fois", async () => {15 let calls = 016 let prompts = 017 const r = await retryWithReauth(18 async () => {19 if (++calls === 1) throw reauthError()20 return calls21 },22 async () => void prompts++,23 )24 assert.equal(r, 2)25 assert.equal(prompts, 1)26})2728test("réauth : jamais de boucle — un second reauth_required est rendu à l'appelant", async () => {29 let calls = 030 let prompts = 031 await assert.rejects(32 retryWithReauth(33 async () => {34 calls++35 throw reauthError()36 },37 async () => void prompts++,38 ),39 (e) => isReauthRequired(e),40 )41 assert.equal(calls, 2)42 assert.equal(prompts, 1)43})4445test("réauth : invite refermée → l'action n'est pas rejouée", async () => {46 let calls = 047 await assert.rejects(48 retryWithReauth(49 async () => {50 calls++51 throw reauthError()52 },53 async () => {54 throw new ReauthCancelled()55 },56 ),57 ReauthCancelled,58 )59 assert.equal(calls, 1)60})6162test("réauth : les autres erreurs (403 forbidden, 500) ne déclenchent pas l'invite", async () => {63 let prompts = 064 for (const err of [Object.assign(new Error("x"), { status: 403, code: "forbidden" }), Object.assign(new Error("y"), { status: 500, code: "internal" }), new Error("z")]) {65 await assert.rejects(retryWithReauth(async () => Promise.reject(err), async () => void prompts++), (e) => e === err)66 assert.equal(isReauthRequired(err), false)67 }68 assert.equal(prompts, 0)69})70