Skip to content

Add comprehensive unit tests for config, db, and bash tool packages - #1

Draft
ecchanger wants to merge 1 commit into
mainfrom
terragon/complete-unit-tests
Draft

ecchanger wants to merge 1 commit into
mainfrom
terragon/complete-unit-tests

Conversation

@ecchanger

@ecchanger ecchanger commented Jul 17, 2025 •

Copy link
Copy Markdown
Owner

Summary

  • Adds extensive unit tests covering configuration loading, validation, and updates
  • Implements tests for database operations on files, messages, and sessions with SQLite in-memory DB
  • Provides thorough tests for the Bash tool including permission checks, command execution, output truncation, and error handling

Changes

Configuration Package Tests

  • Tests for loading config with and without debug mode
  • Tests for merging local config files and environment variables
  • Validation tests for agents, providers, and LSP configurations
  • Tests for updating agent models and UI themes
  • Concurrent access tests for config retrieval
  • Tests for AWS and Vertex AI credential detection
  • Tests for GitHub token loading from environment and config files

Database Package Tests

  • Setup of in-memory SQLite DB for isolated testing
  • CRUD tests for files, including duplicate handling and session/path queries
  • CRUD tests for messages with role, parts, model, and finished_at fields
  • CRUD tests for sessions including parent-child relationships, token counts, costs, and summaries
  • Tests for listing, updating, and deleting by session or ID
  • Transactional tests verifying rollback behavior

Bash Tool Tests

  • Tests for tool info and parameter parsing
  • Execution tests for valid commands, safe read-only commands, banned commands, and permission denied scenarios
  • Tests for command timeout handling and output truncation
  • Error handling tests for invalid parameters and command failures
  • Tests verifying permission requests and metadata correctness
  • Tests for safe commands that do not require permission

Test plan

  • Run all new unit tests to ensure coverage and correctness
  • Verify no regressions in config loading and validation
  • Confirm database operations behave correctly under various scenarios
  • Validate Bash tool command execution and permission enforcement
  • Ensure concurrent config access is safe and consistent

🌿 Generated by Terry


ℹ️ Tag @terragon-labs to ask questions and address PR feedback

📎 Task: https://www.terragonlabs.com/task/56eadb3e-e1f0-4d66-940c-6bb174be4ca2


Summary by cubic

Added comprehensive unit tests for the config, database, and bash tool packages to ensure correct behavior, edge case handling, and robust coverage.

  • Unit Tests
    • Tested config loading, validation, environment variable overrides, updates, and concurrent access.
    • Verified database CRUD operations for files, messages, and sessions using in-memory SQLite, including transactions and edge cases.
    • Covered bash tool command execution, permission checks, banned commands, timeouts, output truncation, and error handling.

- Add extensive unit tests for internal/config covering config loading, validation, environment variables, and concurrency.
- Add database tests for files, messages, and sessions including CRUD operations, transactions, and edge cases.
- Add tests for internal/llm/tools/bash covering command execution, permissions, banned commands, timeouts, and output truncation.
- Ensure robust coverage and validation for critical internal components.

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jul 17, 2025

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch terragon/complete-unit-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@devloai devloai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

PR Summary:

This PR adds comprehensive unit tests for three critical packages:
• Config package: Tests for configuration loading, validation, environment variable handling, and concurrent access
• Database package: CRUD tests for files, messages, and sessions with SQLite in-memory database and transaction testing
• Bash tool package: Tests for command execution, permission checks, banned commands, timeouts, and error handling

The tests significantly improve code coverage and include both happy path and edge case scenarios.

Review Summary:

I've reviewed the comprehensive test suite and found it to be well-structured with good coverage of core functionality. The tests properly use in-memory databases for isolation, include transaction testing, and cover important edge cases. I identified a few issues that could affect test reliability: SQLite driver inconsistency between test and production code, and flaky timing-dependent tests that use time.Sleep(). The config tests also rely on global state which could cause test pollution. These issues should be addressed to ensure reliable test execution in CI environments.

Please provide feedback on this review which I will incorporate into future reviews.

Follow-up suggestions:

• @devloai fix the SQLite driver inconsistency and flaky timing tests
• @devloai refactor config tests to avoid global state pollution

Comment thread internal/db/files_test.go

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/mattn/go-sqlite3"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The database test files import github.com/mattn/go-sqlite3 but the production code uses github.com/ncruces/go-sqlite3. This inconsistency could lead to subtle differences in behavior between test and production environments. All three test files (files_test.go, messages_test.go, sessions_test.go) should use the same SQLite driver as production code.

Comment thread internal/db/files_test.go
ID: "file-3",
SessionID: "session-1",
Path: "/shared/file.txt",
Content: "Updated content from session 1",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Multiple test files use time.Sleep(1 * time.Millisecond) to ensure different timestamps for ordering tests. This approach is flaky and can cause intermittent test failures, especially in CI environments or under load. Consider using a mock time provider or explicitly setting timestamps in test data to ensure deterministic test behavior.

os.Exit(code)
}

func resetConfig() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The resetConfig() function sets the global cfg variable to nil, but the tests are using a shared global state. This can lead to test pollution where one test affects another. Consider using dependency injection or creating isolated config instances for each test instead of relying on global state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant