test: add session management tests
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { describe, it, expect } from '@jest/globals';
|
||||
|
||||
export interface SessionData {
|
||||
userId: string;
|
||||
role?: string;
|
||||
}
|
||||
|
||||
export interface Session extends SessionData {
|
||||
createdAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
export function createSession(userData: SessionData): Session {
|
||||
return {
|
||||
...userData,
|
||||
createdAt: Date.now(),
|
||||
expiresAt: Date.now() + (24 * 60 * 60 * 1000),
|
||||
};
|
||||
}
|
||||
|
||||
export function isSessionValid(session: Session): boolean {
|
||||
return Date.now() < session.expiresAt;
|
||||
}
|
||||
|
||||
export function getSessionAge(session: Session): number {
|
||||
return Date.now() - session.createdAt;
|
||||
}
|
||||
|
||||
export function getSessionTimeRemaining(session: Session): number {
|
||||
return Math.max(0, session.expiresAt - Date.now());
|
||||
}
|
||||
|
||||
export function isSessionExpired(session: Session): boolean {
|
||||
return Date.now() >= session.expiresAt;
|
||||
}
|
||||
|
||||
export function createSessionWithCustomExpiration(userData: SessionData, expiresInMs: number): Session {
|
||||
return {
|
||||
...userData,
|
||||
createdAt: Date.now(),
|
||||
expiresAt: Date.now() + expiresInMs,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user