@tdd_issue @tdd_issue_987 @tdd_issue_4178
Feature: TDD Bug #987 — AutomationProfileRepository session leak
  As a developer
  I want to verify that AutomationProfileRepository closes sessions
  in auto_commit mode
  So that regressions in session cleanup are caught

  Regression guard for bug #987.  AutomationProfileRepository methods
  previously committed when auto_commit was True but never called
  session.close() in a finally block.  The fix added
  ``finally: if self._auto_commit: session.close()`` to all public
  session-creating methods (get_by_name, list_all, upsert, delete),
  matching the pattern already used by SessionRepository.

  These scenarios verify that sessions are properly closed in
  auto_commit mode for both success and error paths across all
  public methods.

  @tdd_issue @tdd_issue_4274
  Scenario: upsert closes session in auto_commit mode on success
    Given an AutomationProfileRepository with auto_commit enabled and a tracking session factory
    When I upsert a valid automation profile via the repository
    Then the tracking session should have been closed

  @tdd_issue @tdd_issue_4274
  Scenario: delete closes session in auto_commit mode on success
    Given an AutomationProfileRepository with auto_commit enabled and a tracking session factory
    And a persisted automation profile named "local/leak-test-profile"
    When I delete the automation profile "local/leak-test-profile" via the repository
    Then the tracking session should have been closed

  @tdd_issue @tdd_issue_4274
  Scenario: upsert closes session in auto_commit mode on database error
    Given an AutomationProfileRepository with auto_commit enabled and a failing session factory
    When I attempt to upsert a profile that triggers a database error
    Then the tracking session should have been closed despite the error

  @tdd_issue @tdd_issue_4274
  Scenario: delete closes session in auto_commit mode on database error
    Given an AutomationProfileRepository with auto_commit enabled, a pre-populated profile, and a failing-flush tracking session
    When I attempt to delete a profile that triggers a database error
    Then the tracking session should have been closed despite the delete error

  @tdd_issue @tdd_issue_4274
  Scenario: get_by_name closes session in auto_commit mode on success
    Given an AutomationProfileRepository with auto_commit enabled and a tracking session factory
    When I look up a profile by name via the repository
    Then the tracking session should have been closed

  @tdd_issue @tdd_issue_4274
  Scenario: list_all closes session in auto_commit mode on success
    Given an AutomationProfileRepository with auto_commit enabled and a tracking session factory
    When I list all profiles via the repository
    Then the tracking session should have been closed

  @tdd_issue @tdd_issue_4274
  Scenario: get_by_name closes session in auto_commit mode on database error
    Given an AutomationProfileRepository with auto_commit enabled and a failing-query session factory
    When I attempt to look up a profile that triggers a database error
    Then the tracking session should have been closed despite the get_by_name error

  @tdd_issue @tdd_issue_4274
  Scenario: list_all closes session in auto_commit mode on database error
    Given an AutomationProfileRepository with auto_commit enabled and a failing-query session factory
    When I attempt to list profiles that triggers a database error
    Then the tracking session should have been closed despite the list_all error
