verification.spec.js
1 lines
| 1 | import{secondsUntil,getBlockedDuplicateSeconds,resendAnchorFrom,RESEND_COOLDOWN_SECONDS}from"../verification";describe("secondsUntil",(()=>{it("returns 0 for unset values",(()=>{expect(secondsUntil(null)).toBe(0),expect(secondsUntil(void 0)).toBe(0),expect(secondsUntil(0)).toBe(0)})),it("returns 0 for a timestamp in the past",(()=>{expect(secondsUntil(Date.now()-1e4)).toBe(0)})),it("returns the whole seconds remaining for a future timestamp",(()=>{expect(secondsUntil(Date.now()+3e4)).toBeGreaterThan(28),expect(secondsUntil(Date.now()+3e4)).toBeLessThanOrEqual(30)}))})),describe("getBlockedDuplicateSeconds",(()=>{const e=e=>({additional_errors:[{code:"verification_code.email.blocked_duplicate",data:{options:{seconds:e}}}]});it("reads seconds from a blocked_duplicate error",(()=>{expect(getBlockedDuplicateSeconds(e("240"))).toBe(240),expect(getBlockedDuplicateSeconds(e(120))).toBe(120)})),it("returns null when the error has no blocked_duplicate entry",(()=>{expect(getBlockedDuplicateSeconds({additional_errors:[{code:"other"}]})).toBeNull(),expect(getBlockedDuplicateSeconds({})).toBeNull(),expect(getBlockedDuplicateSeconds(null)).toBeNull()})),it("returns null when seconds are missing or zero",(()=>{expect(getBlockedDuplicateSeconds(e("0"))).toBeNull(),expect(getBlockedDuplicateSeconds(e(void 0))).toBeNull()}))})),describe("resendAnchorFrom",(()=>{it("anchors from a provided seconds value",(()=>{const e=resendAnchorFrom(200);expect(secondsUntil(e)).toBeGreaterThan(198),expect(secondsUntil(e)).toBeLessThanOrEqual(200)})),it("falls back to the default window when the value is unknown (null/undefined)",(()=>{for(const e of[null,void 0]){const t=resendAnchorFrom(e);expect(secondsUntil(t)).toBeGreaterThan(RESEND_COOLDOWN_SECONDS-2),expect(secondsUntil(t)).toBeLessThanOrEqual(RESEND_COOLDOWN_SECONDS)}})),it('anchors "now" for a 0 or negative window (resend available immediately)',(()=>{for(const e of[0,-5]){const t=resendAnchorFrom(e);expect(secondsUntil(t)).toBe(0)}}))})); |