Disable mark-as-read while a sync or reload is in progress

useFeeds.js had no concept of a sync/reload being in flight, so two
passive mark-as-read paths could race a wholesale fetchData()/sync()
list replacement:

- handleIntersection() (list-view scroll marking)
- markCurrentArticleRead() (article-view display/paging marking)

Both are now gated by a refcount (activeSyncCount/beginSync/endSync/
isSyncing), mirroring the existing pendingReadCounts pattern for the
same class of overlapping-async problem. A refcount rather than a
boolean because sync() nests fetchData() inside it, and fetchData()
can also run independently while a sync() elsewhere is still in
flight.

Reads suppressed during a sync/reload aren't dropped: their ids are
queued and flushed (marked read, and swept out of the list) once
every in-flight fetchData()/sync() call has finished. The explicit
"Mark all read" action is left ungated, since it's a deliberate,
already-confirmed action rather than a passive side effect.

87/87 frontend tests passing.
This commit is contained in:
mathias committed 2026-09-13 12:11:14 +02:00
1 parent c290513ed1
commit 4d9e899519
2 files changed
+278 -7

No files matched your search

+190 -1
View File
@@ -12,7 +12,7 @@ class FakeIntersectionObserver {
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver)
describe('useFeeds', () => {
const { feeds, allItems, feedFilter, feedTitles, feedUnreadCounts, unreadCount, displayedFeedUnreadCounts, refreshUnreadDisplay, setFeedFilter, showMessage, message, viewMode, fetchData, sync, getReadable, setInitialLoad, handleIntersection, markAllRead, markRead, markCurrentArticleRead, setupIntersectionObserver } = useFeeds()
const { feeds, allItems, feedFilter, feedTitles, feedUnreadCounts, unreadCount, displayedFeedUnreadCounts, refreshUnreadDisplay, setFeedFilter, showMessage, message, viewMode, currentIndex, fetchData, sync, getReadable, setInitialLoad, handleIntersection, markAllRead, markRead, markCurrentArticleRead, setupIntersectionObserver } = useFeeds()
beforeEach(() => {
localStorage.setItem('user-token', 'test-token')
@@ -177,6 +177,195 @@ describe('useFeeds', () => {
setInitialLoad(false)
})
describe('mark-as-read suppressed while syncing', () => {
it('queues list-view scroll marking while a reload (fetchData) is in flight, and flushes it automatically once it finishes', async () => {
feeds.value = [{ id: 701, title: 'First' }]
setInitialLoad(true)
// No .observe nodes exist here, so this deterministically clears
// skipNextObservation rather than relying on whatever an earlier test
// left it as.
setupIntersectionObserver()
axios.put.mockResolvedValue({ status: 200 })
let resolveGet
axios.get.mockReturnValueOnce(new Promise(resolve => { resolveGet = resolve }))
const fetchPromise = fetchData()
await handleIntersection([
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
])
expect(axios.put).not.toHaveBeenCalled()
// Queued, not dropped — untouched while the reload is in flight, unlike
// a normal scroll-past which removes it immediately.
expect(feeds.value.map(f => f.id)).toEqual([701])
// The refetch's response still lists it (its read never committed) —
// the flush that follows must find and remove it by id.
resolveGet({
data: {
feeds: [{
title: 'Feed A',
items: [{ id: 701, title: 'First', content: '', url: 'https://example.test/701', timestamp: '2026-01-01 10:00:00' }],
}],
},
})
await fetchPromise
// Flushed automatically once the reload finished — no second scroll needed.
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/701', null, expect.anything())
expect(feeds.value).toEqual([])
setInitialLoad(false)
})
it('queues list-view scroll marking for the whole duration of sync(), and flushes it once sync finishes', async () => {
feeds.value = [{ id: 702, title: 'First' }]
setInitialLoad(true)
setupIntersectionObserver() // deterministically clears skipNextObservation — see the previous test
axios.put.mockResolvedValue({ status: 200 })
let resolvePost
axios.post.mockReturnValueOnce(new Promise(resolve => { resolvePost = resolve }))
const syncPromise = sync(true)
// Still mid network round-trip to /api/v1/article/sync — fetchData()
// hasn't even started yet, but scroll-marking must already be queued.
await handleIntersection([
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
])
expect(axios.put).not.toHaveBeenCalled()
expect(feeds.value.map(f => f.id)).toEqual([702])
axios.get.mockResolvedValueOnce({ data: { feeds: [] } })
resolvePost({ status: 200 })
await syncPromise
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/702', null, expect.anything())
expect(feeds.value).toEqual([])
setInitialLoad(false)
})
it('queues article-view display marking while a reload is in flight, and flushes it in place once the reload finishes', async () => {
viewMode.value = 'article' // still on screen when the flush runs — must not be removed out from under it
feeds.value = [{ id: 703, title: 'First' }]
axios.put.mockResolvedValue({ status: 200 })
let resolveGet
axios.get.mockReturnValueOnce(new Promise(resolve => { resolveGet = resolve }))
const fetchPromise = fetchData()
markCurrentArticleRead()
expect(axios.put).not.toHaveBeenCalled()
expect(feeds.value[0].read).toBeFalsy()
// The refetch's response still lists it (its read never committed), same
// id, now at whatever index it re-sorts to — the flush must find it by
// id, not by the currentIndex it was queued under.
resolveGet({
data: {
feeds: [{
title: 'Feed A',
items: [{ id: 703, title: 'First', content: '', url: 'https://example.test/703', timestamp: '2026-01-01 10:00:00' }],
}],
},
})
await fetchPromise
// Flushed automatically once the reload finished — no second call needed.
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/703', null, expect.anything())
// Still in article view — marked in place, not removed (removing it here
// would yank whatever's on screen out from under currentIndex; the same
// invariant markCurrentArticleRead() already keeps while paging).
expect(feeds.value.find(f => f.id === 703)).toMatchObject({ read: true })
})
it('drops a queued article-view read once flushed, if the user already left article view before the reload finished', async () => {
feeds.value = [{ id: 707, title: 'First' }]
viewMode.value = 'article'
axios.put.mockResolvedValue({ status: 200 })
let resolveGet
axios.get.mockReturnValueOnce(new Promise(resolve => { resolveGet = resolve }))
const fetchPromise = fetchData()
markCurrentArticleRead() // queued: isSyncing() is true
// The user backs out to list view before the reload finishes —
// leaveArticleView()'s dropReadArticles() runs here, but this article
// isn't flagged read yet (it's only queued), so it survives that pass.
viewMode.value = 'list'
expect(feeds.value.map(f => f.id)).toEqual([707])
resolveGet({ data: { feeds: [] } })
await fetchPromise
// Now in list view with no further transition to rely on — the flush
// must sweep it out itself instead of leaving it stuck looking unread.
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/707', null, expect.anything())
expect(feeds.value).toEqual([])
})
it('queues every article paged past during a sync, flushing all of them once it ends', async () => {
feeds.value = [
{ id: 704, title: 'First' },
{ id: 705, title: 'Second' },
]
axios.put.mockResolvedValue({ status: 200 })
let resolvePost
axios.post.mockReturnValueOnce(new Promise(resolve => { resolvePost = resolve }))
const syncPromise = sync(true)
markCurrentArticleRead() // pages onto article 704
currentIndex.value = 1
markCurrentArticleRead() // pages onto article 705
expect(axios.put).not.toHaveBeenCalled()
axios.get.mockResolvedValueOnce({ data: { feeds: [] } })
resolvePost({ status: 200 })
await syncPromise
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/704', null, expect.anything())
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/705', null, expect.anything())
currentIndex.value = 0
})
it('does not reopen the mark-as-read window when an independent fetchData() resolves while a sync() is still in flight', async () => {
feeds.value = [{ id: 706, title: 'First' }]
setInitialLoad(true)
setupIntersectionObserver() // deterministically clears skipNextObservation — see above
axios.put.mockResolvedValue({ status: 200 })
// sync() starts and is left mid network round-trip.
let resolvePost
axios.post.mockReturnValueOnce(new Promise(resolve => { resolvePost = resolve }))
const syncPromise = sync(true)
// An independent fetchData() call (e.g. a separate reload) runs to
// completion while sync()'s POST is still pending.
axios.get.mockResolvedValueOnce({ data: { feeds: [] } })
await fetchData()
// sync() itself hasn't finished — scroll-marking must still be
// suppressed, not reopened by the independent call settling first.
feeds.value = [{ id: 706, title: 'First' }]
await handleIntersection([
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
])
expect(axios.put).not.toHaveBeenCalled()
axios.get.mockResolvedValueOnce({ data: { feeds: [] } })
resolvePost({ status: 200 })
await syncPromise
// Only now, with sync() truly finished, does the queued entry flush.
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/706', null, expect.anything())
setInitialLoad(false)
})
})
describe('markRead resilience', () => {
// Mirrors useFeeds.js's MARK_READ_RETRY_DELAYS_MS (not exported). Kept in
// sync via this comment: if that backoff changes, update this too — too