Skip to content

test(cli): add vitest infrastructure and tests for auth functionality#24

Draft
romitg2 wants to merge 4 commits intocalcom:mainfrom
romitg2:feat/cli-test-infrastructure
Draft

test(cli): add vitest infrastructure and tests for auth functionality#24
romitg2 wants to merge 4 commits intocalcom:mainfrom
romitg2:feat/cli-test-infrastructure

Conversation

@romitg2
Copy link
Copy Markdown
Member

@romitg2 romitg2 commented Mar 11, 2026

Summary

  • Set up vitest testing infrastructure for the @calcom/cli package
  • Added comprehensive unit tests for authentication and configuration functionality
  • Added CI integration to run tests on every PR/push

Changes

Test Infrastructure

  • Added vitest as dev dependency
  • Created test setup file (src/__tests__/setup.ts) with automatic mock resets
  • Created test helpers:
    • mockFs.ts - File system mocking utilities
    • mockProcess.ts - process.exit and console mocking
    • fixtures.ts - Test data (configs, tokens, error bodies)

Test Coverage

config.test.ts (21 tests)

Function Test Case
readConfig Returns empty object when file doesn't exist
readConfig Parses valid JSON config
readConfig Returns empty object on JSON parse error
readConfig Returns empty object when file read throws
writeConfig Writes config with mode 0o600
writeConfig Creates config directory if missing
getApiUrl Returns env var CAL_API_URL first
getApiUrl Falls back to config.apiUrl
getApiUrl Returns default https://api.cal.com
getAppUrl Returns env var CAL_APP_URL first
getAppUrl Falls back to config.appUrl
getAppUrl Returns default https://app.cal.com
getAuthToken Returns CAL_API_KEY from env
getAuthToken Returns OAuth token when valid
getAuthToken Auto-refreshes expired OAuth token
getAuthToken Falls back to apiKey when no OAuth
getAuthToken Exits with error when no auth configured
getAuthToken Exits with error when token refresh fails
getAuthToken Refreshes token within 60-second buffer before expiry
getAuthToken Persists new tokens to config after refresh
getAuthToken Treats invalid expiry date as expired and refreshes

errors.test.ts (16 tests)

Function Test Case
handleSdkError Handles Error with JSON body containing validation errors
handleSdkError Handles Error with JSON body containing nested validation errors
handleSdkError Handles Error with JSON body containing simple message
handleSdkError Handles Error with JSON body containing string error
handleSdkError Handles SDK error object with body property
handleSdkError Handles SDK error object with status but no body
handleSdkError Handles plain Error with non-JSON message
handleSdkError Handles plain object error with error.message
handleSdkError Handles plain object error with error.details.message
handleSdkError Handles plain object error with top-level message
handleSdkError Handles string error
handleSdkError Handles null/undefined by converting to string
withErrorHandling Returns result on success
withErrorHandling Returns complex objects on success
withErrorHandling Calls exit(1) on error
withErrorHandling Handles SDK errors in wrapped function

auth.test.ts (6 tests)

Function Test Case
ApiKeyAuth.login Saves provided API key
ApiKeyAuth.login Clears existing OAuth config
ApiKeyAuth.login Sets custom apiUrl
OAuthAuth.refreshToken Throws when no OAuth config exists
OAuthAuth.refreshToken Refreshes token and saves new credentials
OAuthAuth.refreshToken Throws when token exchange fails

CI Integration

  • Added CLI - Unit Tests job to .github/workflows/ci.yml
  • Added root-level test script to package.json

Test plan

  • Run bun run test locally - all 43 tests pass
  • CI runs test job successfully on PR

🤖 Generated with Claude Code

…ality

- Add vitest dependency and test scripts to @calcom/cli
- Create test setup file with mock resets
- Create test helpers for fs mocking, process.exit handling, and fixtures
- Add comprehensive tests for config.ts (18 tests)
- Add comprehensive tests for errors.ts (16 tests)
- Add comprehensive tests for auth.ts (6 tests)
- Add test job to CI workflow
- Add root-level test script to run CLI tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@romitg2 romitg2 requested a review from a team as a code owner March 11, 2026 10:34
@github-actions github-actions bot added config Changes to project configuration files ci Changes to CI/CD workflows and GitHub Actions labels Mar 11, 2026
romitg2 and others added 2 commits March 11, 2026 16:08
- Rename test job to "Test CLI" for clarity
- Remove unused consoleLogSpy and consoleErrorSpy variables

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@romitg2 romitg2 force-pushed the feat/cli-test-infrastructure branch from 1542e1d to bba08d9 Compare March 11, 2026 10:40
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 13 files

- Test 60-second buffer pre-expiry refresh
- Test config persistence after token refresh
- Test invalid expiry date handling

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@romitg2 romitg2 marked this pull request as draft March 11, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Changes to CI/CD workflows and GitHub Actions config Changes to project configuration files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant