# Regression tests for EntityStore and MemoryService SQL persistence.
#
# EntityStore in MemoryService now implements real SQL-backed entity
# persistence via _load_from_persistence() and _persist_if_needed().
# These scenarios verify that entities tracked in one instance survive
# in a fresh instance backed by the same SQLite database.
#
# Originally TDD issue-capture tests for bug #10455 (EntityStore stubs).
# See: https://git.cleverthis.com/cleveragents/cleveragents-core/issues/10455

@mock_only
Feature: EntityStore and MemoryService SQL persistence
  As a developer using MemoryService with a connection_string
  I want entities tracked via track_entity() to survive process restarts
  So that cross-session entity recall works as documented

  Scenario: Entity tracked in one EntityStore instance is visible in a fresh instance
    Given I create an EntityStore with a SQLite connection string and session "entity-persist-test"
    When I track a project entity "my-project" in the first EntityStore instance
    And I create a fresh EntityStore instance with the same connection string and session
    Then the fresh EntityStore instance should contain the entity "my-project"

  Scenario: Entity tracked via MemoryService survives simulated process restart
    Given I create a MemoryService with a SQLite connection string and session "memory-persist-test"
    When I track a plan entity "my-plan" via the MemoryService
    And I create a fresh MemoryService with the same connection string and session
    Then the fresh MemoryService should return the entity "my-plan" when queried

  Scenario: Persistence failure raises an exception rather than silently succeeding
    Given I create an EntityStore with an invalid connection string
    When I attempt to track an entity in the EntityStore with invalid connection
    Then an exception should be raised rather than silently failing

  Scenario: Multiple entities survive a simulated process restart
    Given I create an EntityStore with a SQLite connection string and session "multi-entity-persist"
    When I track multiple entities in the first EntityStore instance
    And I create a fresh EntityStore instance with the same connection string and session
    Then all tracked entities should be present in the fresh EntityStore instance

  Scenario: Entity metadata and mention count survive a simulated process restart
    Given I create an EntityStore with a SQLite connection string and session "entity-metadata-persist"
    When I track a project entity "project-delta" with metadata
      | key   | value |
      | owner | alice |
    And I track the same EntityStore entity "project-delta" again with metadata
      | key    | value  |
      | status | active |
    And I create a fresh EntityStore instance with the same connection string and session
    Then the fresh EntityStore entity "project-delta" should include metadata
      | key   | value |
      | owner | alice |
      | status | active |
    And the fresh EntityStore entity "project-delta" should have mention count 2