agnes-the-ai-analyst/tests/test_schema_v46_migration.py
ZdenekSrotyr 9e948abc9c
release(0.54.18): Curated Memory restructure + per-user Dismiss + bundled adversarial-review fixes (#316/#320/#322) (#324)
* feat(web): Curated Memory restructure + per-user Dismiss + filter-state utility

Squashed from cvrysanek/zsrotyr's 4-commit PR branch + rebased onto
current main + CHANGELOG bullets spliced into [Unreleased] (preserves
existing #316/#320/#322 entries that landed on main since the branch
was authored).

Routes + access:
- /corporate-memory now user-facing (get_current_user), in primary
  nav next to "Data Packages" — same gate as /api/memory/*.
- /admin/corporate-memory is the new admin review queue location
  (was /corporate-memory/admin); reached via Admin dropdown. Template
  renamed: corporate_memory_admin.html → admin_corporate_memory.html.

Visual chrome:
- Both pages migrate to shared _page_hero.html blue hero band.

Per-user Dismiss (new feature, schema v46):
- knowledge_item_user_dismissed(user_id, item_id, dismissed_at) + index.
- POST /api/memory/{id}/dismiss + DELETE (idempotent).
- Mandatory items can never be dismissed — enforced at 2 layers.
- GET /api/memory: hide_dismissed=false default + dismissed_by_me flag.
- GET /api/memory/bundle: always excludes dismissed for the caller.
- UI: Dismiss/Undismiss button per item (hidden for mandatory),
  gray-out + line-through for dismissed rows, Hide-dismissed toggle.

Admin edit modal:
- Category as <select> + "Add new category…" reveal.
- Audience as <select> with (unset)/all/group:<name> from RBAC.
- Tags: full tag-input widget (pills, ×-remove, Backspace pop,
  Enter/comma to add, ↑/↓ typeahead from EXISTING_TAGS).

Bulk-edit modal pickers (closes #128):
- Move-to-category / Add-tag: <select> + add-new.
- Set-audience: <select> (no more typo-able 'gourp:eng').
- Remove-tag: closed-set picker.

FilterState utility:
- app/web/static/js/filter-state.js — save/load/clear/bindInputs
  for per-page localStorage filter state. Adopted on /corporate-memory.

E2E verified live on a real VM through the API + browser flow.

* release: 0.54.18 — Curated Memory restructure + 4 adversarial-review fixes

Bundles together:
- #316  fix(store): surface review failures + harden publish gate
        (BREAKING fail-CLOSED guardrail, override v2+ promote, restore guard,
        retry/rescan staged-bundle, banner widening, LLM truncation retry)
- #320  fix(store): C2 bundle export RBAC + H2 per-entity write lock +
        H3 update_status compare-and-swap with bg_verdict_skipped audit
- #322  fix(store): M1 prompt sentinel filename escape + M2 atomic
        promote_to_version helper + L1 admin forensic download per-version
- #324  Curated Memory restructure + per-user Dismiss + FilterState utility

Bump from 0.54.17 → 0.54.18 (patch — pre-1.0 policy: every cycle is patch).
2026-05-15 18:51:05 +02:00

133 lines
4.6 KiB
Python

"""v45 → v46 migration: per-user opt-out (dismiss) for curated memory items.
Adds ``knowledge_item_user_dismissed`` ((user_id, item_id) PK,
dismissed_at) plus an index on ``user_id`` to support the EXISTS subquery
used by list_items / search / count_items / bundle. Mandatory items are
governance-protected: the API rejects POSTs against them, and the SQL
filter exempts ``status = 'mandatory'`` so any stale row from before an
item was mandated is silently ignored.
"""
import duckdb
from src.db import SCHEMA_VERSION, _ensure_schema, _v45_to_v46, get_schema_version
def test_schema_version_is_46():
assert SCHEMA_VERSION == 46
def test_fresh_install_creates_dismissed_table(tmp_path):
"""A brand-new DB ends at v46 with the dismiss table + index in place."""
db_path = tmp_path / "system.duckdb"
conn = duckdb.connect(str(db_path))
_ensure_schema(conn)
assert get_schema_version(conn) == SCHEMA_VERSION
tables = {
r[0]
for r in conn.execute(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'main'"
).fetchall()
}
assert "knowledge_item_user_dismissed" in tables, (
f"knowledge_item_user_dismissed missing from {tables}"
)
cols = {
r[0]
for r in conn.execute(
"SELECT column_name FROM information_schema.columns "
"WHERE table_name = 'knowledge_item_user_dismissed'"
).fetchall()
}
assert {"user_id", "item_id", "dismissed_at"} <= cols, (
f"missing columns on knowledge_item_user_dismissed: {cols}"
)
idx_names = {
r[0]
for r in conn.execute(
"SELECT index_name FROM duckdb_indexes "
"WHERE table_name = 'knowledge_item_user_dismissed'"
).fetchall()
}
assert "idx_knowledge_item_user_dismissed_user" in idx_names, (
f"index on user_id missing: {idx_names}"
)
conn.close()
def test_v45_db_migrates_cleanly_to_v46(tmp_path):
"""A pre-existing v45 DB (no dismiss table) climbs to v46 without error."""
db_path = tmp_path / "v45.duckdb"
conn = duckdb.connect(str(db_path))
# Stand up a minimal v45-shape: schema_version row pinned at 45 plus a
# knowledge_items table with one survivor row that must come through
# the migration intact.
conn.execute(
"CREATE TABLE schema_version (version INTEGER, applied_at TIMESTAMP DEFAULT current_timestamp)"
)
conn.execute("INSERT INTO schema_version (version) VALUES (45)")
conn.execute(
"""CREATE TABLE knowledge_items (
id VARCHAR PRIMARY KEY,
title VARCHAR NOT NULL,
content TEXT,
category VARCHAR,
status VARCHAR DEFAULT 'pending',
created_at TIMESTAMP DEFAULT current_timestamp,
updated_at TIMESTAMP
)"""
)
conn.execute(
"INSERT INTO knowledge_items (id, title, content, category, status) "
"VALUES ('legacy', 'Legacy', 'still here', 'engineering', 'approved')"
)
_ensure_schema(conn)
assert get_schema_version(conn) == SCHEMA_VERSION
tables = {
r[0]
for r in conn.execute(
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'main'"
).fetchall()
}
assert "knowledge_item_user_dismissed" in tables
# Pre-existing knowledge_items row survived the migration.
row = conn.execute(
"SELECT id, title, status FROM knowledge_items WHERE id = 'legacy'"
).fetchone()
assert row == ("legacy", "Legacy", "approved")
# Inserts work, ON CONFLICT path is idempotent.
conn.execute(
"INSERT INTO knowledge_item_user_dismissed (user_id, item_id) VALUES ('u1', 'legacy')"
)
conn.execute(
"INSERT INTO knowledge_item_user_dismissed (user_id, item_id) VALUES ('u1', 'legacy') "
"ON CONFLICT (user_id, item_id) DO NOTHING"
)
cnt = conn.execute(
"SELECT COUNT(*) FROM knowledge_item_user_dismissed WHERE user_id = 'u1' AND item_id = 'legacy'"
).fetchone()[0]
assert cnt == 1, "primary key + ON CONFLICT DO NOTHING must collapse duplicate inserts"
conn.close()
def test_v45_to_v46_function_is_idempotent(tmp_path):
"""Calling ``_v45_to_v46`` twice on the same DB is a no-op the second time."""
db_path = tmp_path / "twice.duckdb"
conn = duckdb.connect(str(db_path))
_ensure_schema(conn)
# Re-running the migration step directly must not error — CREATE TABLE
# IF NOT EXISTS / CREATE INDEX IF NOT EXISTS are idempotent by design.
_v45_to_v46(conn)
_v45_to_v46(conn)
assert get_schema_version(conn) == SCHEMA_VERSION
conn.close()