|
| 1 | +import pytest |
| 2 | + |
| 3 | +from app.database import get_db |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.asyncio |
| 7 | +async def test_same_user_repeat_does_not_increment(auth_client): |
| 8 | + await auth_client.post("/api/pages", json={ |
| 9 | + "title": "View Dedup Same User", |
| 10 | + "content_md": "x", |
| 11 | + "slug": "view-dedup-same", |
| 12 | + }) |
| 13 | + |
| 14 | + first = await auth_client.get("/api/pages/view-dedup-same") |
| 15 | + assert first.status_code == 200 |
| 16 | + count_after_first = first.json()["view_count"] |
| 17 | + |
| 18 | + second = await auth_client.get("/api/pages/view-dedup-same") |
| 19 | + assert second.status_code == 200 |
| 20 | + assert second.json()["view_count"] == count_after_first |
| 21 | + |
| 22 | + third = await auth_client.get("/api/pages/view-dedup-same") |
| 23 | + assert third.json()["view_count"] == count_after_first |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_different_users_each_count(auth_client, admin_client): |
| 28 | + await auth_client.post("/api/pages", json={ |
| 29 | + "title": "View Dedup Two Users", |
| 30 | + "content_md": "x", |
| 31 | + "slug": "view-dedup-two-users", |
| 32 | + }) |
| 33 | + |
| 34 | + r1 = await auth_client.get("/api/pages/view-dedup-two-users") |
| 35 | + first_count = r1.json()["view_count"] |
| 36 | + |
| 37 | + r2 = await admin_client.get("/api/pages/view-dedup-two-users") |
| 38 | + assert r2.json()["view_count"] == first_count + 1 |
| 39 | + |
| 40 | + # Each user's refresh still dedups against their own slot. |
| 41 | + r1b = await auth_client.get("/api/pages/view-dedup-two-users") |
| 42 | + assert r1b.json()["view_count"] == first_count + 1 |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_expired_dedup_counts_again(auth_client): |
| 47 | + await auth_client.post("/api/pages", json={ |
| 48 | + "title": "View Dedup Expired", |
| 49 | + "content_md": "x", |
| 50 | + "slug": "view-dedup-expired", |
| 51 | + }) |
| 52 | + |
| 53 | + r1 = await auth_client.get("/api/pages/view-dedup-expired") |
| 54 | + count_after_first = r1.json()["view_count"] |
| 55 | + |
| 56 | + r2 = await auth_client.get("/api/pages/view-dedup-expired") |
| 57 | + assert r2.json()["view_count"] == count_after_first |
| 58 | + |
| 59 | + # Backdate every dedup row for this page past the cooldown. |
| 60 | + db = await get_db() |
| 61 | + page_rows = await db.execute_fetchall( |
| 62 | + "SELECT id FROM pages WHERE slug = ?", ("view-dedup-expired",) |
| 63 | + ) |
| 64 | + page_id = page_rows[0]["id"] |
| 65 | + await db.execute( |
| 66 | + "UPDATE view_dedup SET last_viewed_at = 0 WHERE page_id = ?", (page_id,) |
| 67 | + ) |
| 68 | + await db.commit() |
| 69 | + |
| 70 | + r3 = await auth_client.get("/api/pages/view-dedup-expired") |
| 71 | + assert r3.json()["view_count"] == count_after_first + 1 |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_dedup_row_is_hashed(auth_client): |
| 76 | + """Row stores a sha256 hex digest, not a raw (user, page) pair.""" |
| 77 | + await auth_client.post("/api/pages", json={ |
| 78 | + "title": "View Dedup Hash", |
| 79 | + "content_md": "x", |
| 80 | + "slug": "view-dedup-hash", |
| 81 | + }) |
| 82 | + await auth_client.get("/api/pages/view-dedup-hash") |
| 83 | + |
| 84 | + db = await get_db() |
| 85 | + page_rows = await db.execute_fetchall( |
| 86 | + "SELECT id FROM pages WHERE slug = ?", ("view-dedup-hash",) |
| 87 | + ) |
| 88 | + page_id = page_rows[0]["id"] |
| 89 | + rows = await db.execute_fetchall( |
| 90 | + "SELECT dedup_key FROM view_dedup WHERE page_id = ?", (page_id,) |
| 91 | + ) |
| 92 | + assert rows, "expected a dedup row after first view" |
| 93 | + key = rows[0]["dedup_key"] |
| 94 | + assert len(key) == 64 |
| 95 | + assert all(c in "0123456789abcdef" for c in key) |
| 96 | + # The structured plaintext form must never hit disk. |
| 97 | + assert "|" not in key and "u:" not in key |
0 commit comments