@phase1 @domain @repository @coverage_boost
Feature: Repository coverage boost for actions, plans, and resources
  As a developer ensuring high test coverage
  I want to exercise uncovered repository branches
  So that the persistence layer is thoroughly tested

  Background:
    Given a fresh in-memory database with full lifecycle schema
    And an action repository using the session factory
    And a lifecycle plan repository using the session factory

  # ---------------------------------------------------------------------------
  # ActionRepository.update – non-existent action raises DatabaseError
  # ---------------------------------------------------------------------------

  @action_update @error_handling
  Scenario: Updating a non-existent action raises DatabaseError with "not found for update"
    Given a valid action object named "local/ghost-action"
    When the action is updated without being persisted first
    Then a DatabaseError mentioning "not found for update" should be raised

  # ---------------------------------------------------------------------------
  # ActionRepository.update – with arguments (including default_value) and invariants
  # ---------------------------------------------------------------------------

  @action_update
  Scenario: Updating an action replaces its child arguments and invariants
    Given a valid action object named "local/updatable-with-children"
    And the action has been persisted in the database
    When the action arguments are replaced with new arguments including defaults
    And the action invariants are set to new invariant texts
    And the action is updated via the repository
    Then the update should succeed without error
    And retrieving the action should show the new arguments
    And retrieving the action should show the new invariants

  # ---------------------------------------------------------------------------
  # LifecyclePlanRepository.get_by_name
  # ---------------------------------------------------------------------------

  @plan_read
  Scenario: A lifecycle plan is retrieved by its namespaced name
    Given a valid action object named "local/plan-action-lookup"
    And the action has been persisted in the database
    And a lifecycle plan domain object linked to "local/plan-action-lookup"
    And the lifecycle plan has been persisted in the database
    When the lifecycle plan is looked up by namespaced name
    Then the returned plan should match the original plan identity

  @plan_read
  Scenario: Looking up a non-existent plan by name returns nothing
    When a lifecycle plan is looked up by name "local/nonexistent-plan"
    Then no lifecycle plan should be returned

  # ---------------------------------------------------------------------------
  # LifecyclePlanRepository.update – PlanNotFoundError
  # ---------------------------------------------------------------------------

  @plan_update @error_handling
  Scenario: Updating a non-existent lifecycle plan raises PlanNotFoundError
    Given a valid action object named "local/plan-action-missing"
    And the action has been persisted in the database
    And a lifecycle plan domain object linked to "local/plan-action-missing"
    When the lifecycle plan is updated without being persisted first
    Then a PlanNotFoundError should be raised for the repository boost

  # ---------------------------------------------------------------------------
  # LifecyclePlanRepository.update – with project_links, arguments, invariants
  # ---------------------------------------------------------------------------

  @plan_update
  Scenario: Updating a plan replaces project links, arguments, and invariants
    Given a valid action object named "local/plan-action-update"
    And the action has been persisted in the database
    And a lifecycle plan domain object linked to "local/plan-action-update"
    And the lifecycle plan has been persisted in the database
    When the plan is updated with project links, arguments, and invariants
    Then the plan update should succeed without error
    And retrieving the plan should show the new project links
    And retrieving the plan should show the new plan arguments
    And retrieving the plan should show the new plan invariants

  @plan_update
  Scenario: Updating a plan argument with the same name does not hit UNIQUE constraints
    Given a valid action object named "local/plan-action-arg-regression"
    And the action has been persisted in the database
    And a lifecycle plan domain object linked to "local/plan-action-arg-regression"
    And the lifecycle plan has argument "x" set to "v1"
    And the lifecycle plan has been persisted in the database
    When the lifecycle plan argument "x" is updated to "v2"
    Then the plan update should succeed without error
    And retrieving the plan should show argument "x" value "v2"

  # ---------------------------------------------------------------------------
  # LifecyclePlanRepository.list_plans filtered by phase
  # ---------------------------------------------------------------------------

  @plan_list
  Scenario: Listing plans filtered by phase returns only matching plans
    Given a valid action object named "local/plan-action-phase"
    And the action has been persisted in the database
    And lifecycle plans in different phases linked to "local/plan-action-phase"
    When plans are listed filtered by phase "strategize"
    Then only the strategize-phase plans should be returned

  # ---------------------------------------------------------------------------
  # ResourceTypeRepository – create, get, list_types
  # ---------------------------------------------------------------------------

  @resource_type
  Scenario: A resource type is created and retrieved by name
    Given a resource type repository using the session factory
    And a valid resource type domain object named "test/my-git-checkout"
    When the resource type is created through the repository
    Then the resource type should be retrievable by name "test/my-git-checkout"

  @resource_type @error_handling
  Scenario: Creating a duplicate resource type raises DuplicateResourceTypeError
    Given a resource type repository using the session factory
    And a valid resource type domain object named "test/dup-type"
    And the resource type has been persisted in the database
    When the same resource type is created again
    Then a DuplicateResourceTypeError should be raised

  # ---------------------------------------------------------------------------
  # ResourceRepository – create, get, get_by_name, list_resources by type
  # ---------------------------------------------------------------------------

  @resource
  Scenario: A resource is created and retrieved by ID and name
    Given a resource type repository using the session factory
    And a resource repository using the session factory
    And a valid resource type domain object named "test/res-type-a"
    And the resource type has been persisted in the database
    And a valid resource domain object of type "test/res-type-a"
    When the resource is created through the repository
    Then the resource should be retrievable by its ID
    And the resource should be retrievable by its namespaced name

  @resource
  Scenario: Resources are listed by type name
    Given a resource type repository using the session factory
    And a resource repository using the session factory
    And a valid resource type domain object named "test/list-type"
    And the resource type has been persisted in the database
    And multiple resources of type "test/list-type" have been created
    When resources are listed by type "test/list-type"
    Then all resources of that type should be returned
