# Testing Strategy

This document outlines the testing strategy for the OAuth functionality in the application.

## OAuth Helper Tests

We've implemented several types of tests for the OAuth functionality:

### 1. Unit Tests (`src/lib/oauth.test.ts`)

These tests verify that the OAuth helper functions work correctly in isolation:
- Tests for `getRedirectUri` function.
- Tests for service-specific OAuth configuration functions.
- Tests for environment variable handling and fallbacks.

### 2. OAuth Integration Tests (`src/lib/oauth.integration.test.ts`)

These tests verify that the OAuth helper functions integrate correctly with the Google OAuth client:
- Tests for creating a Google OAuth client with the helper functions.
- Tests for environment-specific redirect URI handling.

### 3. Token Refresh Tests (`src/lib/token-refresh.test.ts`)

These tests verify that the token refresh functionality works correctly:
- Tests for refreshing expired Google OAuth tokens.
- Tests for the token refresh flow.

### 4. API Route Integration Tests (`src/app/api/oauth-integration.test.ts`)

These tests verify that the OAuth helper functions can be used correctly in the API routes:
- Tests for generating auth URLs with the correct redirect URIs.
- Tests for handling custom redirect URIs.

### 5. OAuth Route Integration Tests (`src/lib/oauth-route-integration.test.ts`)

These tests verify that the OAuth helper functions work correctly in a route-like context:
- Tests for all three services (Google, OneDrive, Dropbox).
- Tests for auth URL generation with the correct redirect URIs.

## Testing Approach

We've taken a modular approach to testing the OAuth functionality:

1. **Test the core functionality in isolation**: We test the OAuth helper functions in isolation to ensure they work correctly.

2. **Test the integration with external libraries**: We test that the OAuth helper functions integrate correctly with the Google OAuth client.

3. **Test the token refresh flow**: We test that the token refresh functionality works correctly.

4. **Test the integration with API routes**: We test that the OAuth helper functions can be used correctly in the API routes.

5. **Test the route-like context**: We test that the OAuth helper functions work correctly in a route-like context.

## Why Not Test API Routes Directly?

Testing API routes directly is challenging because:

1. **Mocking dependencies**: It's difficult to mock all the dependencies of the API routes, especially when they use external libraries like Google OAuth.

2. **Environment variables**: The API routes rely on environment variables, which can be difficult to mock in tests.

3. **Next.js API routes**: Next.js API routes have their own context and lifecycle, which can be difficult to simulate in tests.

Instead, we focus on testing the core functionality that the routes depend on, which is a more maintainable approach.

## Running Tests

To run all the OAuth tests:

```bash
pnpm test src/lib/oauth.test.ts src/lib/oauth.integration.test.ts src/lib/token-refresh.test.ts src/app/api/oauth-integration.test.ts src/lib/oauth-route-integration.test.ts
```

To run a specific test file:

```bash
pnpm test src/lib/oauth.test.ts
```
