Feature: Database CLI commands coverage
  Exercise uncovered lines in cleveragents.cli.commands.db module
  by invoking each Typer command with mocked infrastructure.

  Background:
    Given the db commands module is imported

  Scenario: migrate command generates a new revision
    Given the migration runner is mocked
    And alembic command module is mocked
    When I invoke the db migrate command with message "add_users_table"
    Then alembic revision should have been called with message "add_users_table" and autogenerate True
    And typer should echo a db message containing "Migration revision created: add_users_table"

  Scenario: migrate command uses default message
    Given the migration runner is mocked
    And alembic command module is mocked
    When I invoke the db migrate command with default message
    Then alembic revision should have been called with message "auto" and autogenerate True

  Scenario: upgrade command with specific non-head revision
    Given the migration runner is mocked
    And alembic command module is mocked
    And the db command parameters are revision "abc123" and format "rich"
    When I invoke the db upgrade command with stored params
    Then alembic upgrade should have been called with revision "abc123"
    And the runner get_current_revision should have been called

  Scenario: upgrade command with head revision uses init_or_upgrade
    Given the migration runner is mocked
    And the db command parameters are revision "head" and format "rich"
    When I invoke the db upgrade command with stored params
    Then the runner init_or_upgrade should have been called
    And typer should echo a db message containing "Database upgraded to revision"

  Scenario: upgrade command with json format outputs formatted data
    Given the migration runner is mocked
    And format_output is mocked
    And the db command parameters are revision "head" and format "json"
    When I invoke the db upgrade command with stored params
    Then format_output should have been called with status ok data
    And the formatted output should have been echoed

  Scenario: downgrade command invokes alembic downgrade
    Given the migration runner is mocked
    And alembic command module is mocked
    And the db command parameters are revision "-1" and format "rich"
    When I invoke the db downgrade command with stored params
    Then alembic downgrade should have been called with revision "-1"
    And typer should echo a db message containing "Database downgraded to revision"

  Scenario: downgrade command with json format outputs formatted data
    Given the migration runner is mocked
    And alembic command module is mocked
    And format_output is mocked
    And the db command parameters are revision "base" and format "json"
    When I invoke the db downgrade command with stored params
    Then format_output should have been called with status ok data
    And the formatted output should have been echoed

  Scenario: current command shows revision in rich format
    Given the migration runner is mocked with pending migrations
    When I invoke the db current command in rich format
    Then typer should echo a db message containing "Current revision"
    And typer should echo a db message containing "Pending migrations"

  Scenario: current command with json format outputs formatted data
    Given the migration runner is mocked with pending migrations
    And format_output is mocked
    And the db command format is "json"
    When I invoke the db current command with stored format
    Then format_output should have been called with current revision data

  Scenario: current command shows none revision when no migrations applied
    Given the migration runner is mocked with no current revision
    When I invoke the db current command in rich format
    Then typer should echo a db message containing "(none)"

  Scenario: history command in rich format calls alembic history
    Given the migration runner is mocked
    And alembic command module is mocked
    When I invoke the db history command in rich format
    Then alembic history should have been called with the runner config

  Scenario: history command with json format walks script revisions
    Given the migration runner is mocked
    And alembic ScriptDirectory is mocked with sample revisions
    And format_output is mocked
    And the db command format is "json"
    When I invoke the db history command with stored format
    Then format_output should have been called with revision list data
    And the formatted output should have been echoed
