Skip to content

Add comprehensive unit tests #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
496 changes: 496 additions & 0 deletions __tests__/api/analysis.test.ts

Large diffs are not rendered by default.

222 changes: 222 additions & 0 deletions __tests__/api/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import {
getAccount,
logoutAndGetAccount,
getLeaderboard,
getGlobalStats,
} from '../../src/api/auth/auth'

// Mock the buildUrl function
jest.mock('../../src/api', () => ({
buildUrl: jest.fn((path: string) => `/api/v1/${path}`),
}))

// Mock fetch
global.fetch = jest.fn()

describe('Auth API', () => {
const mockFetch = fetch as jest.MockedFunction<typeof fetch>

beforeEach(() => {
jest.clearAllMocks()
})

describe('getAccount', () => {
it('should fetch and parse account information', async () => {
const mockResponse = {
client_id: 'test-client-123',
display_name: 'Test User',
lichess_id: 'testuser',
extra_field: 'ignored',
}

mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(mockResponse),
} as any)

const result = await getAccount()

expect(mockFetch).toHaveBeenCalledWith('/api/v1/auth/account')
expect(result).toEqual({
clientId: 'test-client-123',
displayName: 'Test User',
lichessId: 'testuser',
})
})

it('should handle missing fields gracefully', async () => {
const mockResponse = {
client_id: 'test-client-123',
// missing display_name and lichess_id
}

mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(mockResponse),
} as any)

const result = await getAccount()

expect(result).toEqual({
clientId: 'test-client-123',
displayName: undefined,
lichessId: undefined,
})
})

it('should handle empty response', async () => {
mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue({}),
} as any)

const result = await getAccount()

expect(result).toEqual({
clientId: undefined,
displayName: undefined,
lichessId: undefined,
})
})

it('should handle fetch errors', async () => {
mockFetch.mockRejectedValueOnce(new Error('Network error'))

await expect(getAccount()).rejects.toThrow('Network error')
})

it('should handle JSON parsing errors', async () => {
mockFetch.mockResolvedValueOnce({
json: jest.fn().mockRejectedValue(new Error('Invalid JSON')),
} as any)

await expect(getAccount()).rejects.toThrow('Invalid JSON')
})
})

describe('logoutAndGetAccount', () => {
it('should call logout endpoint then get account', async () => {
const mockAccountResponse = {
client_id: 'test-client-123',
display_name: 'Test User',
lichess_id: 'testuser',
}

// Mock logout call
mockFetch
.mockResolvedValueOnce({
json: jest.fn(),
} as any)
// Mock getAccount call
.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(mockAccountResponse),
} as any)

const result = await logoutAndGetAccount()

expect(mockFetch).toHaveBeenCalledTimes(2)
expect(mockFetch).toHaveBeenNthCalledWith(1, '/api/v1/auth/logout')
expect(mockFetch).toHaveBeenNthCalledWith(2, '/api/v1/auth/account')
expect(result).toEqual({
clientId: 'test-client-123',
displayName: 'Test User',
lichessId: 'testuser',
})
})

it('should handle logout endpoint errors', async () => {
mockFetch.mockRejectedValueOnce(new Error('Logout failed'))

await expect(logoutAndGetAccount()).rejects.toThrow('Logout failed')
})

it('should handle account fetch errors after successful logout', async () => {
mockFetch
.mockResolvedValueOnce({
json: jest.fn(),
} as any)
.mockRejectedValueOnce(new Error('Account fetch failed'))

await expect(logoutAndGetAccount()).rejects.toThrow(
'Account fetch failed',
)
})
})

describe('getLeaderboard', () => {
it('should fetch leaderboard data', async () => {
const mockLeaderboard = [
{ rank: 1, username: 'player1', rating: 2000 },
{ rank: 2, username: 'player2', rating: 1950 },
]

mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(mockLeaderboard),
} as any)

const result = await getLeaderboard()

expect(mockFetch).toHaveBeenCalledWith('/api/v1/auth/leaderboard')
expect(result).toEqual(mockLeaderboard)
})

it('should handle empty leaderboard', async () => {
mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue([]),
} as any)

const result = await getLeaderboard()

expect(result).toEqual([])
})

it('should handle leaderboard fetch errors', async () => {
mockFetch.mockRejectedValueOnce(new Error('Leaderboard unavailable'))

await expect(getLeaderboard()).rejects.toThrow('Leaderboard unavailable')
})
})

describe('getGlobalStats', () => {
it('should fetch global statistics', async () => {
const mockStats = {
totalUsers: 50000,
totalGames: 1000000,
averageRating: 1500,
activeUsers: 5000,
}

mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(mockStats),
} as any)

const result = await getGlobalStats()

expect(mockFetch).toHaveBeenCalledWith('/api/v1/auth/global_stats')
expect(result).toEqual(mockStats)
})

it('should handle empty stats response', async () => {
mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue({}),
} as any)

const result = await getGlobalStats()

expect(result).toEqual({})
})

it('should handle global stats fetch errors', async () => {
mockFetch.mockRejectedValueOnce(new Error('Stats unavailable'))

await expect(getGlobalStats()).rejects.toThrow('Stats unavailable')
})

it('should handle malformed stats response', async () => {
mockFetch.mockResolvedValueOnce({
json: jest.fn().mockResolvedValue(null),
} as any)

const result = await getGlobalStats()

expect(result).toBeNull()
})
})
})
45 changes: 45 additions & 0 deletions __tests__/api/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { buildUrl, connectLichessUrl } from '../../src/api/utils'

describe('API Utils', () => {
describe('buildUrl', () => {
it('should build URL with base path', () => {
const result = buildUrl('games/123')
expect(result).toBe('/api/v1/games/123')
})

it('should handle empty path', () => {
const result = buildUrl('')
expect(result).toBe('/api/v1/')
})

it('should handle path with leading slash', () => {
const result = buildUrl('/users/profile')
expect(result).toBe('/api/v1//users/profile')
})

it('should handle complex paths', () => {
const result = buildUrl('analysis/game/456/moves')
expect(result).toBe('/api/v1/analysis/game/456/moves')
})

it('should handle paths with query parameters', () => {
const result = buildUrl('games?limit=10&offset=0')
expect(result).toBe('/api/v1/games?limit=10&offset=0')
})

it('should handle paths with special characters', () => {
const result = buildUrl('search/players/test%20user')
expect(result).toBe('/api/v1/search/players/test%20user')
})
})

describe('connectLichessUrl', () => {
it('should return correct lichess connection URL', () => {
expect(connectLichessUrl).toBe('/api/v1/auth/lichess_login')
})

it('should be a string', () => {
expect(typeof connectLichessUrl).toBe('string')
})
})
})
Loading
Loading