-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.test.ts
More file actions
55 lines (46 loc) · 1.69 KB
/
Copy pathsession.test.ts
File metadata and controls
55 lines (46 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { beforeEach, describe, expect, it } from 'vitest';
import { clearSession, isExpired, loadSession, storeSession } from '../src/lib/session';
import type { TokenResponse } from '../src/lib/auth';
const TOKENS: TokenResponse = {
access_token: 'access-123',
id_token: 'id-456',
token_type: 'Bearer',
expires_in: 3600,
};
beforeEach(() => {
localStorage.clear();
});
describe('storeSession / loadSession', () => {
it('round-trips a session and computes an absolute expiry from expires_in', () => {
const stored = storeSession(TOKENS, 1_000_000);
expect(stored.expiresAt).toBe(1_000_000 + 3600 * 1000);
const loaded = loadSession();
expect(loaded).toEqual(stored);
});
it('returns null when nothing has been stored', () => {
expect(loadSession()).toBeNull();
});
it('returns null for corrupted storage instead of throwing', () => {
localStorage.setItem('ojr.session', 'not json');
expect(loadSession()).toBeNull();
});
it('returns null for a validly-shaped-JSON value missing required fields', () => {
localStorage.setItem('ojr.session', JSON.stringify({ accessToken: 'a' }));
expect(loadSession()).toBeNull();
});
});
describe('clearSession', () => {
it('removes a stored session', () => {
storeSession(TOKENS);
clearSession();
expect(loadSession()).toBeNull();
});
});
describe('isExpired', () => {
it('is false before expiresAt and true at/after it', () => {
const session = storeSession(TOKENS, 1_000_000);
expect(isExpired(session, 1_000_000 + 3600 * 1000 - 1)).toBe(false);
expect(isExpired(session, 1_000_000 + 3600 * 1000)).toBe(true);
expect(isExpired(session, 1_000_000 + 3600 * 1000 + 1)).toBe(true);
});
});