fedify icon indicating copy to clipboard operation
fedify copied to clipboard

Add automated tests for CLI commands

Open dahlia opened this issue 5 months ago • 2 comments

Description

The Fedify CLI currently has no automated tests (0% test coverage), while the main library has comprehensive test coverage. This makes it difficult to ensure CLI commands work correctly and prevents regression testing when making changes.

Current State

  • CLI directory (cli/) has no unit tests or integration tests
  • Main library has extensive test coverage using Deno's test framework
  • CLI commands are only manually tested
  • No CI/CD validation for CLI functionality
  • cli/deno.json has no test task defined

Proposed Solution

Add comprehensive test coverage for CLI commands using Deno.test() directly, including:

  1. Unit tests for individual CLI modules:

    • lookup.ts - Test command parsing, option validation, error handling
    • inbox.tsx - Test server setup, request handling
    • node.ts - Test NodeInfo command functionality
    • tunnel.ts - Test tunnel creation logic
    • init.ts - Test project initialization
    • Helper modules (docloader.ts, utils.ts, cache.ts, etc.)
  2. Integration tests for CLI commands:

    • Test actual command execution with various options
    • Test error scenarios and edge cases
    • Test CLI output formatting and messaging
  3. Test infrastructure:

    • Add test task to cli/deno.json
    • Set up test utilities and mocks
    • Add test coverage reporting

Implementation Approach

Phase 1: Test Infrastructure Setup

  • [ ] Add test task to cli/deno.json
  • [ ] Create test utilities and helper functions
  • [ ] Set up mocking for external dependencies (HTTP requests, file system, etc.)

Phase 2: Unit Tests

  • [ ] utils.test.ts - Test utility functions
  • [ ] cache.test.ts - Test cache operations
  • [ ] docloader.test.ts - Test document loader configurations
  • [ ] table.test.ts - Test table formatting utilities

Phase 3: Command Tests

  • [ ] lookup.test.ts - Test lookup command logic
  • [ ] inbox.test.ts - Test inbox command functionality
  • [ ] node.test.ts - Test node command
  • [ ] tunnel.test.ts - Test tunnel command
  • [ ] init.test.ts - Test init command

Phase 4: Integration Tests

  • [ ] End-to-end CLI command execution tests
  • [ ] CLI option parsing and validation tests
  • [ ] Error message and help text tests

Example Test Structure

Using Deno's built-in testing framework:

// cli/lookup.test.ts
import { assertEquals, assertRejects } from "@std/assert";
import { command as lookupCommand } from "./lookup.ts";

Deno.test("lookup command - should validate URLs", async () => {
  // Test URL validation logic
});

Deno.test("lookup command - should handle timeout options", async () => {
  // Test timeout option parsing
});

Deno.test("lookup command - should format output correctly", async () => {
  // Test output formatting
});

Testing Challenges & Considerations

  1. External Dependencies: Mock HTTP requests, file system operations, tunnel services
  2. CLI Interaction: Test command-line argument parsing and validation
  3. Async Operations: Handle async operations like network requests and server startup
  4. Output Testing: Validate console output, colors, and formatting
  5. Environment Isolation: Ensure tests don't interfere with each other

Benefits

  • Regression Prevention: Catch breaking changes before they reach users
  • Documentation: Tests serve as examples of how CLI commands should work
  • Confidence: Make changes to CLI code with confidence
  • Code Quality: Improve code structure through testability requirements
  • Contributor Onboarding: Help new contributors understand CLI functionality

Getting Started

This is an excellent issue for new contributors because:

  • Testing provides a great way to understand the codebase
  • You can start with simple utility function tests
  • Each CLI command can be tested independently
  • No complex domain knowledge required initially
  • Clear success criteria (tests pass/fail)

Acceptance Criteria

  • [ ] All CLI modules have unit tests with good coverage
  • [ ] Integration tests cover main CLI workflows
  • [ ] Test task added to cli/deno.json (e.g., "test": "deno test **/*.test.ts")
  • [ ] Tests run successfully in CI/CD pipeline
  • [ ] Test coverage reporting is available
  • [ ] Documentation includes testing guidelines for future CLI features

Suggested First Steps

  1. Start by adding a test task to cli/deno.json
  2. Create tests for utility functions in utils.ts
  3. Add tests for simple modules like cache.ts
  4. Gradually work up to testing CLI commands themselves

Contributors can work on different test files in parallel, making this a great collaborative issue.

dahlia avatar Jun 26 '25 03:06 dahlia

I will add test task for fedify node first.

dodok8 avatar Jul 15 '25 08:07 dodok8

I'll work on fedify tunnel

dodok8 avatar Aug 20 '25 11:08 dodok8