fix(memory): implement entity persistence in MemoryService or remove stub
Added Behave scenario to ensure EntityStore metadata and mention counts persist across restarts. Implemented new step definitions to track metadata updates and assert mention counts. Kept existing persistence behavior unchanged while expanding TDD coverage to validate regression safety. ISSUES CLOSED: #10455
This commit is contained in:
@@ -195,3 +195,78 @@ def step_all_entities_in_fresh_store(context: Any) -> None:
|
||||
assert entity.name == name, (
|
||||
f"Expected entity name '{name}' but got '{entity.name}'"
|
||||
)
|
||||
|
||||
|
||||
def _table_to_metadata(table: Any) -> dict[str, str]:
|
||||
"""Convert a Behave table of key/value rows into a metadata dictionary."""
|
||||
if table is None:
|
||||
return {}
|
||||
metadata: dict[str, str] = {}
|
||||
for row in table:
|
||||
key = row.get("key")
|
||||
value = row.get("value")
|
||||
if key is None:
|
||||
raise AssertionError("Metadata table is missing a 'key' column entry")
|
||||
if value is None:
|
||||
raise AssertionError(
|
||||
f"Metadata row for key '{key}' is missing a 'value' column entry"
|
||||
)
|
||||
metadata[key] = value
|
||||
return metadata
|
||||
|
||||
|
||||
@when('I track a project entity "{name}" with metadata')
|
||||
def step_track_project_entity_with_metadata(context: Any, name: str) -> None:
|
||||
"""Track a project entity with provided metadata in the first EntityStore."""
|
||||
metadata = _table_to_metadata(context.table)
|
||||
context.entity_store_instance_a.track(name, EntityType.PROJECT, metadata=metadata)
|
||||
context.tracked_entity_name = name
|
||||
context.tracked_entity_type = EntityType.PROJECT
|
||||
context.tracked_metadata = metadata.copy()
|
||||
|
||||
|
||||
@when(
|
||||
'I track the same EntityStore entity "{name}" again with metadata'
|
||||
)
|
||||
def step_track_same_entity_again_with_metadata(context: Any, name: str) -> None:
|
||||
"""Track the same entity again with additional metadata to update persistence."""
|
||||
metadata = _table_to_metadata(context.table)
|
||||
if getattr(context, "tracked_entity_name", None) != name:
|
||||
raise AssertionError(
|
||||
"Scenario setup error: attempting to update metadata for a different entity"
|
||||
)
|
||||
context.entity_store_instance_a.track(name, context.tracked_entity_type, metadata)
|
||||
context.tracked_metadata.update(metadata)
|
||||
|
||||
|
||||
@then(
|
||||
'the fresh EntityStore entity "{name}" should include metadata'
|
||||
)
|
||||
def step_fresh_entity_should_include_metadata(context: Any, name: str) -> None:
|
||||
"""Assert that the fresh EntityStore entity has the expected metadata entries."""
|
||||
entity = context.entity_store_instance_b.get(name, context.tracked_entity_type)
|
||||
assert entity is not None, (
|
||||
f"Expected entity '{name}' to be present after restart, but it was missing."
|
||||
)
|
||||
expected_metadata = _table_to_metadata(context.table)
|
||||
for key, value in expected_metadata.items():
|
||||
actual_value = entity.metadata.get(key)
|
||||
assert actual_value == value, (
|
||||
f"Expected metadata key '{key}' to equal '{value}', got '{actual_value}'"
|
||||
)
|
||||
|
||||
|
||||
@then(
|
||||
'the fresh EntityStore entity "{name}" should have mention count {count:d}'
|
||||
)
|
||||
def step_fresh_entity_should_have_mention_count(
|
||||
context: Any, name: str, count: int
|
||||
) -> None:
|
||||
"""Assert that the fresh EntityStore entity has the expected mention count."""
|
||||
entity = context.entity_store_instance_b.get(name, context.tracked_entity_type)
|
||||
assert entity is not None, (
|
||||
f"Expected entity '{name}' to be present after restart, but it was missing."
|
||||
)
|
||||
assert (
|
||||
entity.mention_count == count
|
||||
), f"Expected mention count {count}, got {entity.mention_count}"
|
||||
|
||||
@@ -55,3 +55,19 @@ Feature: TDD Issue #10455 — EntityStore entity data lost across process restar
|
||||
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
|
||||
|
||||
@tdd_issue @tdd_issue_10455
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user