# TDD issue-capture test for bug #1022 — InvariantService in-memory storage only.
#
# InvariantService stores invariants in an in-memory dict (self._invariants)
# with no database persistence layer.  Each CLI invocation spawns a fresh
# process with a new InvariantService() instance, so all invariants added in
# one invocation are lost when the process exits.
#
# These scenarios prove the bug exists by simulating separate CLI invocations
# (fresh InvariantService instances) and asserting that data added in one
# invocation is visible in the next.  They FAIL until the bug is fixed.
# The @tag inverts the result so CI passes.
#
# See: https://git.cleverthis.com/cleveragents/cleveragents-core/issues/1022

@tdd_issue @tdd_issue_1022 @mock_only
Feature: TDD Issue #1022 — InvariantService invariants lost across process restarts
  As a developer using the agents CLI
  I want invariants added via "agents invariant add" to persist across CLI invocations
  So that "agents invariant list" in a subsequent invocation returns previously added invariants

  InvariantService uses in-memory dict storage only.  Each CLI invocation
  creates a fresh InvariantService() instance, so invariants are lost when
  the process exits.  These tests simulate separate process invocations by
  creating fresh service instances and verifying cross-instance data visibility.

  @tdd_issue @tdd_issue_4283 @tdd_expected_fail
  Scenario: Invariant added in one service instance is visible in a fresh instance
    Given I add a project invariant "All APIs must validate auth tokens" to project "local/api-service" via invariant service instance A
    When I create a fresh invariant service instance B
    And I list project invariants for "local/api-service" via instance B
    Then the invariant list from instance B should contain "All APIs must validate auth tokens"

  @tdd_issue @tdd_issue_4283 @tdd_expected_fail
  Scenario: Global invariant persists across simulated process restarts
    Given I add a global invariant "Never delete production data" via invariant service instance A
    When I create a fresh invariant service instance B
    And I list global invariants via instance B
    Then the invariant list from instance B should contain "Never delete production data"

  @tdd_issue @tdd_issue_4283 @tdd_expected_fail
  Scenario: Invariant added via CLI add is visible via CLI list in a new invocation
    Given I invoke invariant add via CLI with "--project local/webapp" and text "All changes need tests" using service invocation 1
    When I invoke invariant list via CLI with "--project local/webapp" using service invocation 2
    Then the CLI list output from invocation 2 should contain "All changes need tests"

  @tdd_issue @tdd_issue_4283 @tdd_expected_fail
  Scenario: Invariant soft-deleted in a fresh instance after being added in another
    Given I add a project invariant "Temporary constraint" to project "local/temp" via invariant service instance A
    And I capture the invariant ID from instance A
    When I create a fresh invariant service instance B
    And I attempt to remove the captured invariant ID via instance B
    Then the remove operation via instance B should succeed without NotFoundError
