|
1 | 1 | import { describe, expect, it } from 'bun:test';
|
2 | 2 | import type { NextRequest } from 'next/server';
|
3 | 3 |
|
| 4 | +import type { JwtPayload } from 'jwt-decode'; |
4 | 5 | import {
|
| 6 | + getVisitorAuthCookieMaxAge, |
5 | 7 | getVisitorAuthCookieName,
|
6 | 8 | getVisitorAuthCookieValue,
|
7 | 9 | getVisitorToken,
|
@@ -74,6 +76,47 @@ describe('getVisitorAuthToken', () => {
|
74 | 76 | });
|
75 | 77 | });
|
76 | 78 |
|
| 79 | +describe('getVisitorAuthCookieMaxAge', () => { |
| 80 | + const ONE_MINUTE_IN_SECONDS = 60; |
| 81 | + |
| 82 | + it('returns the max age of 7 days if token expires in 7 days', () => { |
| 83 | + const SEVEN_DAYS_IN_SECONDS = 7 * 24 * 60 * 60; |
| 84 | + const now = Math.floor(Date.now() / 1000); |
| 85 | + const decoded: JwtPayload = { exp: now + SEVEN_DAYS_IN_SECONDS }; |
| 86 | + |
| 87 | + expect(getVisitorAuthCookieMaxAge(decoded)).toBe(SEVEN_DAYS_IN_SECONDS); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns the max age of 30 days if token expires in 30 days', () => { |
| 91 | + const THIRTY_DAYS_IN_SECONDS = 30 * 24 * 60 * 60; |
| 92 | + const now = Math.floor(Date.now() / 1000); |
| 93 | + const decoded: JwtPayload = { exp: now + THIRTY_DAYS_IN_SECONDS }; |
| 94 | + |
| 95 | + expect(getVisitorAuthCookieMaxAge(decoded)).toBe(THIRTY_DAYS_IN_SECONDS); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns the minimum max age of 60 seconds if token has already expired', () => { |
| 99 | + const now = Math.floor(Date.now() / 1000); |
| 100 | + const decoded: JwtPayload = { exp: now - 1000 }; |
| 101 | + |
| 102 | + expect(getVisitorAuthCookieMaxAge(decoded)).toBe(ONE_MINUTE_IN_SECONDS); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns the minimum max age of 60 seconds if token expires in less than 60 seconds', () => { |
| 106 | + const now = Math.floor(Date.now() / 1000); |
| 107 | + const decoded: JwtPayload = { exp: now + 30 }; // Expires in 30 seconds |
| 108 | + |
| 109 | + expect(getVisitorAuthCookieMaxAge(decoded)).toBe(ONE_MINUTE_IN_SECONDS); |
| 110 | + }); |
| 111 | + |
| 112 | + it('returns the correct value if expiry is in the future but more than 60 seconds', () => { |
| 113 | + const now = Math.floor(Date.now() / 1000); |
| 114 | + const decoded: JwtPayload = { exp: now + 300 }; // Expires in 5 minutes |
| 115 | + |
| 116 | + expect(getVisitorAuthCookieMaxAge(decoded)).toBe(300); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
77 | 120 | function assertVisitorAuthCookieValue(
|
78 | 121 | value: unknown
|
79 | 122 | ): asserts value is { source: 'visitor-auth-cookie'; basePath: string; token: string } {
|
|
0 commit comments