nandi/gleanpublic Fork
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/nandi/glean.git
git clone ssh://git@rickub.com/nandi/glean.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
Simplify application structure and remove obsolete code b205012 · on main · nandithebull · Sep 22, 2026
stores_probe.nim · 224 lines · 9.1 KBNim Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
## Exercises the article and feed queries against a real database.
##
##   nim c -r -d:ssl stores_probe.nim
##
## The checks lean on the places these queries can be quietly wrong: a scope
## that clears more than it should, a count that drifts, a listing that
## returns other people's feeds, a NULL that sorts to the top.

import std/[options, os, strformat, strutils, times]
import reader/[articlestore, feedstore, pulseboarddb, sqlite]

var failures = 0

proc report(name: string, ok: bool, detail = "") =
  if not ok: inc failures
  let label = if ok: "PASS" else: "FAIL"
  if detail.len > 0: echo &"  {label}  {name}  -- {detail}"
  else: echo &"  {label}  {name}"

proc cleanup(base: string) =
  for suffix in ["_users", "_articles", "_recs"]:
    for ext in ["", "-wal", "-shm"]:
      removeFile(base & suffix & ext)

const
  alice = "did:plc:alice"
  bob = "did:plc:bob"
  feedA = "https://a.test/feed"
  feedB = "https://b.test/feed"

proc main() =
  echo "Article / feed store probe"
  echo ""

  let base = getTempDir() / "pulseboard_stores_probe"
  cleanup(base)
  defer: cleanup(base)

  var g = pulseboarddb.open(base)
  defer: g.close()
  g.migrate()

  for did in [alice, bob]:
    g.db.run("INSERT INTO users (did) VALUES (?)", p(did))

  # --- feeds --------------------------------------------------------------
  block:
    g.upsertFeed(Feed(feedUrl: feedA, title: "Feed A", feedType: "rss",
                      siteUrl: "https://a.test"))
    g.upsertFeed(Feed(feedUrl: feedB, title: "Feed B", feedType: "atom"))
    let f = g.getFeed(feedA)
    report("a feed round-trips", f.isSome and f.get.title == "Feed A",
           f.get(Feed()).title)

    # A metadata refresh must not reset state the fetcher owns.
    g.markFeedFetchError(feedA, "boom")
    g.upsertFeed(Feed(feedUrl: feedA, title: "Feed A renamed", feedType: "rss"))
    let after = g.getFeed(feedA).get
    report("upsert refreshes metadata", after.title == "Feed A renamed")
    report("upsert preserves error state", after.errorCount == 1,
           &"error_count={after.errorCount}")

    g.markFeedFetched(feedA)
    let healed = g.getFeed(feedA).get
    report("a successful fetch clears the error state",
           healed.errorCount == 0 and healed.lastError.len == 0)
    report("last_fetched_at is recorded", healed.lastFetchedAt.isSome)

  # --- subscriptions ------------------------------------------------------
  block:
    g.createSubscription(alice, feedA, "", "news", "", "")
    g.createSubscription(alice, feedB, "", "", "", "")
    g.createSubscription(bob, feedA, "", "", "", "")

    report("subscriber_count tracks subscribers",
           g.getFeed(feedA).get.subscriberCount == 2,
           $g.getFeed(feedA).get.subscriberCount)

    # Re-subscribing is an update, not a second subscriber.
    g.createSubscription(alice, feedA, "My title", "tech", "", "")
    report("re-subscribing does not inflate the count",
           g.getFeed(feedA).get.subscriberCount == 2,
           $g.getFeed(feedA).get.subscriberCount)
    report("re-subscribing updates the row",
           g.getSubscription(alice, feedA).get.category == "tech")

    report("subscription count is per user",
           g.getSubscriptionCount(alice) == 2 and g.getSubscriptionCount(bob) == 1)
    report("categories exclude empties",
           g.getCategories(alice) == @["tech"], g.getCategories(alice).join(","))

    let listed = g.listSubscriptions(alice, "", 10, 0)
    report("listing returns only this user's subscriptions", listed.len == 2,
           $listed.len)
    report("listing falls back to the feed title when none is set",
           g.listSubscriptions(bob, "", 10, 0)[0].feedTitle == "Feed A renamed")

  # --- articles -----------------------------------------------------------
  block:
    let t0 = now().utc - initDuration(days = 2)
    g.batchUpsertArticles(@[
      NewArticle(feedUrl: feedA, guid: "a1", title: "Nim systems programming",
                 url: "https://a.test/1", summary: "about nim",
                 published: some(t0)),
      NewArticle(feedUrl: feedA, guid: "a2", title: "Second post",
                 url: "https://a.test/2", published: some(t0 + initDuration(hours = 1))),
      NewArticle(feedUrl: feedB, guid: "b1", title: "Elsewhere",
                 url: "https://b.test/1", published: some(t0)),
    ])
    let all = g.listArticles(alice, limit = 50)
    report("articles are ingested and listed", all.len == 3, $all.len)

    # Re-ingesting the same guids must not duplicate.
    g.batchUpsertArticles(@[
      NewArticle(feedUrl: feedA, guid: "a1", title: "Nim systems programming",
                 published: some(t0))])
    report("re-ingesting is idempotent",
           g.listArticles(alice, limit = 50).len == 3)

    report("bob only sees his own subscriptions",
           g.listArticles(bob, limit = 50).len == 2,
           $g.listArticles(bob, limit = 50).len)

    # Retention: an article older than the window is not worth ingesting.
    g.batchUpsertArticles(@[
      NewArticle(feedUrl: feedA, guid: "ancient", title: "Old news",
                 published: some(now().utc - initDuration(days = 90)))],
      retentionDays = 30)
    report("articles past the retention window are skipped",
           g.listArticles(alice, limit = 50).len == 3)

    let newest = g.listArticles(alice, feedUrl = feedA, limit = 10)
    report("newest first by default", newest[0].guid == "a2", newest[0].guid)
    let oldest = g.listArticles(alice, feedUrl = feedA, sortOldest = true, limit = 10)
    report("oldest first when asked", oldest[0].guid == "a1", oldest[0].guid)

    report("filtering by feed scopes the listing",
           g.listArticles(alice, feedUrl = feedB, limit = 10).len == 1)
    report("filtering by category scopes the listing",
           g.listArticles(alice, category = "tech", limit = 10).len == 2,
           $g.listArticles(alice, category = "tech", limit = 10).len)

  # --- read state ---------------------------------------------------------
  block:
    let arts = g.listArticles(alice, feedUrl = feedA, limit = 10)
    let first = arts[0]

    report("everything starts unread", g.getUnreadCount(alice, "", "") == 3,
           $g.getUnreadCount(alice, "", ""))

    g.markArticleRead(alice, first.id)
    report("marking read is reflected in the count",
           g.getUnreadCount(alice, "", "") == 2)
    report("read state is per user",
           g.getUnreadCount(bob, "", "") == 2, $g.getUnreadCount(bob, "", ""))
    report("the article reads back as read",
           g.getArticle(alice, first.id).get.isRead)

    report("unread filter excludes it",
           g.listArticles(alice, filter = afUnread, limit = 50).len == 2)
    report("read filter includes only it",
           g.listArticles(alice, filter = afRead, limit = 50).len == 1)

    g.markArticleUnread(alice, first.id)
    report("marking unread reverses it", g.getUnreadCount(alice, "", "") == 3)

    # The scope that matters: one feed, not the account.
    g.markAllRead(alice, feedA)
    report("mark-all-read on a feed clears only that feed",
           g.getUnreadCount(alice, feedA, "") == 0 and
           g.getUnreadCount(alice, feedB, "") == 1,
           &"A={g.getUnreadCount(alice, feedA, \"\")} B={g.getUnreadCount(alice, feedB, \"\")}")
    report("and leaves other users alone",
           g.getUnreadCount(bob, "", "") == 2)

    g.markAllRead(alice, "")
    report("unscoped mark-all-read clears everything subscribed",
           g.getUnreadCount(alice, "", "") == 0)

  # --- search -------------------------------------------------------------
  block:
    let hits = g.searchArticles(alice, "systems")
    report("full-text search finds an article", hits.len == 1,
           if hits.len > 0: hits[0].title else: "no hits")
    report("search is scoped to subscriptions",
           g.searchArticles(bob, "Elsewhere").len == 0,
           "bob is not subscribed to feed B")
    report("an empty query returns nothing", g.searchArticles(alice, "  ").len == 0)

  # --- unsubscribe --------------------------------------------------------
  block:
    g.deleteSubscription(alice, feedA)
    report("unsubscribing decrements the count",
           g.getFeed(feedA).get.subscriberCount == 1,
           $g.getFeed(feedA).get.subscriberCount)
    report("unsubscribing hides the feed's articles",
           g.listArticles(alice, limit = 50).len == 1)

    # The counts are maintained incrementally, so the repair pass must agree.
    g.db.run("UPDATE articles.feeds SET subscriber_count = 99")
    g.recountSubscriberCounts()
    report("the recount repairs drifted counts",
           g.getFeed(feedA).get.subscriberCount == 1,
           $g.getFeed(feedA).get.subscriberCount)

  # --- dead feeds ---------------------------------------------------------
  block:
    for _ in 0 ..< 3:
      g.markFeedFetchError(feedB, "still broken")
    let dead = g.listDeadFeeds(alice, threshold = 3)
    report("dead feeds are listed for the subscriber",
           dead.len == 1 and dead[0].feedUrl == feedB, $dead.len)
    report("a healthy feed is not listed as dead",
           g.listDeadFeeds(bob, threshold = 3).len == 0)

  echo ""
  if failures == 0:
    echo "RESULT: the article and feed queries behave."
  else:
    echo &"RESULT: {failures} check(s) failed."
    quit 1

when isMainModule:
  main()