Files
gridpilot.gg/core/identity/application/use-cases/GetCurrentUserSessionUseCase.test.ts
2025-12-20 12:55:07 +01:00

55 lines
1.5 KiB
TypeScript

import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetCurrentUserSessionUseCase } from './GetCurrentUserSessionUseCase';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
describe('GetCurrentUserSessionUseCase', () => {
let sessionPort: {
getCurrentSession: Mock;
createSession: Mock;
clearSession: Mock;
};
let useCase: GetCurrentUserSessionUseCase;
beforeEach(() => {
sessionPort = {
getCurrentSession: vi.fn(),
createSession: vi.fn(),
clearSession: vi.fn(),
};
useCase = new GetCurrentUserSessionUseCase(sessionPort as unknown as IdentitySessionPort);
});
it('returns the current auth session when one exists', async () => {
const session: AuthSessionDTO = {
user: {
id: 'user-1',
email: 'test@example.com',
displayName: 'Test User',
primaryDriverId: 'driver-1',
},
issuedAt: Date.now(),
expiresAt: Date.now() + 1000,
token: 'token-123',
};
sessionPort.getCurrentSession.mockResolvedValue(session);
const result = await useCase.execute();
expect(sessionPort.getCurrentSession).toHaveBeenCalledTimes(1);
expect(result).toEqual(session);
});
it('returns null when there is no active session', async () => {
sessionPort.getCurrentSession.mockResolvedValue(null);
const result = await useCase.execute();
expect(sessionPort.getCurrentSession).toHaveBeenCalledTimes(1);
expect(result).toBeNull();
});
});