The "taz FUTURZWEI im Abo entdecken" box has neither an /abo/ link nor an "Abo" aria-label, so the existing taz rules missed it. Remove its section.article-produktteaser-container before Readability parses the page.
1366 lines
53 KiB
JavaScript
1366 lines
53 KiB
JavaScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
import axios from 'axios'
|
|
import { useFeeds, logout, clearFeedFilter } from '../useFeeds'
|
|
|
|
vi.mock('axios')
|
|
|
|
class FakeIntersectionObserver {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
}
|
|
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver)
|
|
|
|
describe('useFeeds', () => {
|
|
const { feeds, allItems, feedFilter, feedTitles, feedUnreadCounts, unreadCount, displayedFeedUnreadCounts, refreshUnreadDisplay, setFeedFilter, showMessage, message, viewMode, currentIndex, fetchData, sync, getReadable, setInitialLoad, handleIntersection, markAllRead, markRead, markCurrentArticleRead, setupIntersectionObserver, isSyncing } = useFeeds()
|
|
|
|
beforeEach(() => {
|
|
localStorage.setItem('user-token', 'test-token')
|
|
localStorage.setItem('user-id', '7')
|
|
localStorage.removeItem('feedFilter')
|
|
vi.clearAllMocks()
|
|
|
|
feeds.value = []
|
|
allItems.value = []
|
|
feedFilter.value = null
|
|
showMessage.value = false
|
|
message.value = ''
|
|
viewMode.value = 'list'
|
|
})
|
|
|
|
it('fetches and flattens articles for the current user', async () => {
|
|
axios.get.mockResolvedValueOnce({
|
|
data: {
|
|
feeds: [
|
|
{
|
|
title: 'My Feed',
|
|
items: [
|
|
{
|
|
id: 1,
|
|
title: 'Article one',
|
|
content: '<p>hello</p>',
|
|
url: 'https://example.test/1',
|
|
timestamp: '2026-01-01 10:00:00',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(axios.get).toHaveBeenCalledWith('/api/v1/article/get/7', expect.anything())
|
|
expect(feeds.value).toHaveLength(1)
|
|
expect(feeds.value[0]).toMatchObject({ title: 'Article one', feedTitle: 'My Feed' })
|
|
})
|
|
|
|
it('sorts articles by timestamp across feeds, newest first', async () => {
|
|
axios.get.mockResolvedValueOnce({
|
|
data: {
|
|
feeds: [
|
|
{
|
|
title: 'Old Feed',
|
|
items: [
|
|
{ id: 1, title: 'Older article', content: '', url: 'https://example.test/1', timestamp: '2026-01-01 10:00:00' },
|
|
],
|
|
},
|
|
{
|
|
title: 'New Feed',
|
|
items: [
|
|
{ id: 2, title: 'Newer article', content: '', url: 'https://example.test/2', timestamp: '2026-02-01 10:00:00' },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(feeds.value.map(f => f.title)).toEqual(['Newer article', 'Older article'])
|
|
})
|
|
|
|
it('syncs feeds for the current user and refetches', async () => {
|
|
axios.post.mockResolvedValueOnce({ status: 200 })
|
|
axios.get.mockResolvedValue({ data: { feeds: [] } })
|
|
|
|
await sync()
|
|
|
|
expect(axios.post).toHaveBeenCalledWith(
|
|
'/api/v1/article/sync',
|
|
{ user_id: 7 },
|
|
expect.anything(),
|
|
)
|
|
expect(axios.get).toHaveBeenCalledWith('/api/v1/article/get/7', expect.anything())
|
|
})
|
|
|
|
it('marks the correct articles read when several scroll out of view in one batch', async () => {
|
|
feeds.value = [
|
|
{ id: 101, title: 'First' },
|
|
{ id: 102, title: 'Second' },
|
|
{ id: 103, title: 'Third' },
|
|
]
|
|
setInitialLoad(true)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
|
|
// Both the first and second articles scrolled above the viewport in the
|
|
// same IntersectionObserver callback — their `target.id` reflects their
|
|
// original render-time indices (0 and 1).
|
|
await handleIntersection([
|
|
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
|
|
{ isIntersecting: false, boundingClientRect: { y: -5 }, target: { id: '1' } },
|
|
])
|
|
|
|
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/101', null, expect.anything())
|
|
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/102', null, expect.anything())
|
|
expect(axios.put).not.toHaveBeenCalledWith('/api/v1/article/read/103', null, expect.anything())
|
|
expect(feeds.value).toEqual([{ id: 103, title: 'Third' }])
|
|
|
|
setInitialLoad(false)
|
|
})
|
|
|
|
it('refreshes the filter modal\'s displayed per-feed counts when list-view scrolling marks an article read', async () => {
|
|
// displayedFeedUnreadCounts is a frozen snapshot of feedUnreadCounts (see
|
|
// useFeeds.js) — AppNav's filter modal renders it instead of the live
|
|
// computed so paging through article view doesn't force it to repaint.
|
|
// List-view scrolling is a different code path (handleIntersection) and
|
|
// should keep it live regardless.
|
|
allItems.value = [
|
|
{ id: 301, feedTitle: 'Feed A', read: false },
|
|
{ id: 302, feedTitle: 'Feed A', read: false },
|
|
]
|
|
feeds.value = [...allItems.value]
|
|
refreshUnreadDisplay()
|
|
expect(displayedFeedUnreadCounts.value).toEqual({ 'Feed A': 2 })
|
|
|
|
setInitialLoad(true)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
|
|
await handleIntersection([
|
|
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
|
|
])
|
|
|
|
expect(displayedFeedUnreadCounts.value).toEqual({ 'Feed A': 1 })
|
|
|
|
setInitialLoad(false)
|
|
})
|
|
|
|
it('ignores the initial observer snapshot on connect so a scrolled-down reconnect cannot mass-mark', async () => {
|
|
feeds.value = [
|
|
{ id: 201, title: 'First' },
|
|
{ id: 202, title: 'Second' },
|
|
]
|
|
setInitialLoad(true)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
|
|
// A .observe node must exist for setupIntersectionObserver to arm the skip.
|
|
const node = document.createElement('div')
|
|
node.className = 'observe'
|
|
document.body.appendChild(node)
|
|
setupIntersectionObserver()
|
|
|
|
// First callback after connect is the initial snapshot — must be dropped
|
|
// even though both entries look "scrolled past" (above the topbar).
|
|
await handleIntersection([
|
|
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
|
|
{ isIntersecting: false, boundingClientRect: { y: -5 }, target: { id: '1' } },
|
|
])
|
|
expect(axios.put).not.toHaveBeenCalled()
|
|
expect(feeds.value).toHaveLength(2)
|
|
|
|
// A genuine later scroll-past still marks read.
|
|
await handleIntersection([
|
|
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
|
|
])
|
|
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/201', null, expect.anything())
|
|
|
|
document.body.removeChild(node)
|
|
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('isSyncing', () => {
|
|
// Lets a just-resolved deferred promise's `.then` continuations (e.g.
|
|
// sync()'s `await fetchData()`) actually run before the next assertion,
|
|
// without pulling in fake timers (see the CLAUDE.md Vitest gotcha).
|
|
async function flushMicrotasks() {
|
|
for (let i = 0; i < 5; i++) await Promise.resolve()
|
|
}
|
|
|
|
it('is false initially, true while a fetchData() call is in flight, and false again once it resolves', async () => {
|
|
expect(isSyncing.value).toBe(false)
|
|
|
|
let resolveGet
|
|
axios.get.mockReturnValueOnce(new Promise(resolve => { resolveGet = resolve }))
|
|
const fetchPromise = fetchData()
|
|
|
|
expect(isSyncing.value).toBe(true)
|
|
|
|
resolveGet({ data: { feeds: [] } })
|
|
await fetchPromise
|
|
|
|
expect(isSyncing.value).toBe(false)
|
|
})
|
|
|
|
it('stays true for the whole duration of sync(), including its trailing fetchData(), and only clears once sync() fully settles', async () => {
|
|
let resolvePost
|
|
axios.post.mockReturnValueOnce(new Promise(resolve => { resolvePost = resolve }))
|
|
let resolveGet
|
|
axios.get.mockReturnValueOnce(new Promise(resolve => { resolveGet = resolve }))
|
|
|
|
const syncPromise = sync(true)
|
|
expect(isSyncing.value).toBe(true)
|
|
|
|
// The sync POST settles, handing off into sync()'s own trailing
|
|
// fetchData() — isSyncing must not flicker false in between.
|
|
resolvePost({ status: 200 })
|
|
await flushMicrotasks()
|
|
|
|
expect(axios.get).toHaveBeenCalled()
|
|
expect(isSyncing.value).toBe(true)
|
|
|
|
resolveGet({ data: { feeds: [] } })
|
|
await syncPromise
|
|
|
|
expect(isSyncing.value).toBe(false)
|
|
})
|
|
|
|
it('leaves isSyncing true while an independent fetchData() completes during a still in-flight sync(), clearing only once both finish', async () => {
|
|
let resolvePost
|
|
axios.post.mockReturnValueOnce(new Promise(resolve => { resolvePost = resolve }))
|
|
const syncPromise = sync(true)
|
|
expect(isSyncing.value).toBe(true)
|
|
|
|
// An independent fetchData() call runs to completion while sync()'s
|
|
// POST is still pending.
|
|
axios.get.mockResolvedValueOnce({ data: { feeds: [] } })
|
|
await fetchData()
|
|
|
|
// sync() itself hasn't finished yet.
|
|
expect(isSyncing.value).toBe(true)
|
|
|
|
axios.get.mockResolvedValueOnce({ data: { feeds: [] } })
|
|
resolvePost({ status: 200 })
|
|
await syncPromise
|
|
|
|
expect(isSyncing.value).toBe(false)
|
|
})
|
|
|
|
it('clears on a rejected fetchData()', async () => {
|
|
axios.get.mockRejectedValueOnce(new Error('network'))
|
|
|
|
await fetchData() // caught internally — resolves rather than rejecting
|
|
|
|
expect(isSyncing.value).toBe(false)
|
|
})
|
|
|
|
it('clears on a rejected sync()', async () => {
|
|
axios.post.mockRejectedValueOnce(new Error('network'))
|
|
|
|
await sync(true) // caught internally — resolves rather than rejecting
|
|
|
|
expect(isSyncing.value).toBe(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
|
|
// small a value just makes the affected test's `await` hang until it
|
|
// times out, rather than pass incorrectly.
|
|
const RETRY_BACKOFF_SUM_MS = 300 + 1000
|
|
|
|
// A failed assertion before a test's own vi.useRealTimers() call would
|
|
// otherwise leak fake timers into every later test in this file.
|
|
afterEach(() => {
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('retries a failed mark-read PUT before giving up, with a request timeout set', async () => {
|
|
vi.useFakeTimers()
|
|
axios.put
|
|
.mockRejectedValueOnce(new Error('network'))
|
|
.mockRejectedValueOnce(new Error('network'))
|
|
.mockResolvedValueOnce({ status: 200 })
|
|
|
|
const done = markRead(601)
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(await done).toBe(true)
|
|
expect(axios.put).toHaveBeenCalledTimes(3)
|
|
expect(axios.put).toHaveBeenCalledWith(
|
|
'/api/v1/article/read/601',
|
|
null,
|
|
expect.objectContaining({ timeout: expect.any(Number) }),
|
|
)
|
|
expect(showMessage.value).toBe(false)
|
|
})
|
|
|
|
it('reports failure once retries are exhausted, and stops withholding the item from a refetch', async () => {
|
|
vi.useFakeTimers()
|
|
axios.put.mockRejectedValue(new Error('network'))
|
|
|
|
const done = markRead(602)
|
|
// Advance past just the two retry backoffs — not runAllTimersAsync(),
|
|
// which would also fire showMessageForXSeconds's own 5s auto-hide timer
|
|
// and reset showMessage before the assertion below.
|
|
await vi.advanceTimersByTimeAsync(RETRY_BACKOFF_SUM_MS)
|
|
|
|
expect(await done).toBe(false)
|
|
expect(axios.put).toHaveBeenCalledTimes(3)
|
|
expect(showMessage.value).toBe(true)
|
|
expect(message.value).toMatch(/could not mark/i)
|
|
|
|
// The mark never committed, so the item genuinely is still unread
|
|
// server-side — a refetch must be free to show it again, not hide it
|
|
// forever as "pending".
|
|
axios.get.mockResolvedValueOnce({
|
|
data: {
|
|
feeds: [{
|
|
title: 'Feed A',
|
|
items: [{ id: 602, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }],
|
|
}],
|
|
},
|
|
})
|
|
await fetchData()
|
|
expect(feeds.value.map(f => f.id)).toEqual([602])
|
|
})
|
|
|
|
it('does not retry a non-retryable 4xx mark-read failure', async () => {
|
|
const error = new Error('Not Found')
|
|
error.response = { status: 404 }
|
|
axios.put.mockRejectedValueOnce(error)
|
|
|
|
const result = await markRead(605)
|
|
|
|
expect(result).toBe(false)
|
|
expect(axios.put).toHaveBeenCalledTimes(1)
|
|
expect(showMessage.value).toBe(true)
|
|
})
|
|
|
|
it('keeps a still-unconfirmed read article out of a concurrent background refetch', async () => {
|
|
let resolvePut
|
|
axios.put.mockReturnValueOnce(new Promise(resolve => { resolvePut = resolve }))
|
|
|
|
const done = markRead(603) // simulates a page-turn's fire-and-forget mark-read, still in flight
|
|
|
|
// A background sync's trailing fetchData() resolves while that PUT is
|
|
// still pending — the server hasn't committed the read yet, so it's
|
|
// still in the response.
|
|
axios.get.mockResolvedValueOnce({
|
|
data: {
|
|
feeds: [{
|
|
title: 'Feed A',
|
|
items: [{ id: 603, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }],
|
|
}],
|
|
},
|
|
})
|
|
await fetchData()
|
|
|
|
expect(feeds.value).toEqual([])
|
|
|
|
resolvePut({ status: 200 })
|
|
await done
|
|
})
|
|
|
|
it('excludes an item whose mark-read PUT settles between a refetch\'s request and its response', async () => {
|
|
// The PUT commits (and clears the pending id) while the GET below is
|
|
// still in flight — the GET's response nonetheless reflects the DB as
|
|
// of before that commit, still reporting the item unread. A snapshot
|
|
// taken only once the GET resolves would miss this; one taken before
|
|
// it's dispatched (unioned with the live state) catches it too.
|
|
axios.put.mockResolvedValueOnce({ status: 200 })
|
|
const markDone = markRead(604)
|
|
|
|
let resolveGet
|
|
axios.get.mockReturnValueOnce(new Promise(resolve => { resolveGet = resolve }))
|
|
const fetchPromise = fetchData()
|
|
|
|
await markDone // the PUT (and pending-id cleanup) settles first...
|
|
|
|
resolveGet({ // ...then the already-in-flight GET resolves, still unread.
|
|
data: {
|
|
feeds: [{
|
|
title: 'Feed A',
|
|
items: [{ id: 604, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }],
|
|
}],
|
|
},
|
|
})
|
|
await fetchPromise
|
|
|
|
expect(feeds.value).toEqual([])
|
|
})
|
|
|
|
it('keeps an item withheld until every overlapping markRead() call for it settles', async () => {
|
|
// nextArticle()/prevArticle() can both re-mark the same article (page
|
|
// forward, back, forward again) — two overlapping calls for one id.
|
|
let resolveFirst
|
|
let resolveSecond
|
|
axios.put
|
|
.mockReturnValueOnce(new Promise(resolve => { resolveFirst = resolve }))
|
|
.mockReturnValueOnce(new Promise(resolve => { resolveSecond = resolve }))
|
|
|
|
const first = markRead(606)
|
|
const second = markRead(606)
|
|
|
|
resolveSecond({ status: 200 }) // the second (later) call settles first
|
|
await second
|
|
|
|
// The first call is still in flight — a refetch must still withhold it.
|
|
axios.get.mockResolvedValueOnce({
|
|
data: {
|
|
feeds: [{
|
|
title: 'Feed A',
|
|
items: [{ id: 606, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }],
|
|
}],
|
|
},
|
|
})
|
|
await fetchData()
|
|
expect(feeds.value).toEqual([])
|
|
|
|
resolveFirst({ status: 200 })
|
|
await first
|
|
})
|
|
})
|
|
|
|
describe('markAllRead resilience', () => {
|
|
const RETRY_BACKOFF_SUM_MS = 300 + 1000
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('reports a partial failure instead of claiming full success', async () => {
|
|
feeds.value = [
|
|
{ id: 701, title: 'First' },
|
|
{ id: 702, title: 'Second' },
|
|
]
|
|
vi.spyOn(window, 'confirm').mockReturnValue(true)
|
|
axios.put
|
|
.mockResolvedValueOnce({ status: 200 }) // 701 succeeds
|
|
.mockRejectedValue(new Error('network')) // 702 fails every attempt
|
|
|
|
vi.useFakeTimers()
|
|
const done = markAllRead()
|
|
await vi.advanceTimersByTimeAsync(RETRY_BACKOFF_SUM_MS)
|
|
await done
|
|
|
|
expect(message.value).toContain('1 of 2')
|
|
expect(message.value).not.toBe('All articles marked as read.')
|
|
})
|
|
})
|
|
|
|
describe('feed filter', () => {
|
|
const twoFeedsResponse = {
|
|
data: {
|
|
feeds: [
|
|
{
|
|
title: 'Feed A',
|
|
items: [
|
|
{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' },
|
|
{ id: 3, title: 'A2', content: '', url: 'https://example.test/a2', timestamp: '2026-01-03 10:00:00' },
|
|
],
|
|
},
|
|
{
|
|
title: 'Feed B',
|
|
items: [
|
|
{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
}
|
|
|
|
const threeFeedsResponse = {
|
|
data: {
|
|
feeds: [
|
|
{
|
|
title: 'Feed A',
|
|
items: [
|
|
{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' },
|
|
],
|
|
},
|
|
{
|
|
title: 'Feed B',
|
|
items: [
|
|
{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' },
|
|
],
|
|
},
|
|
{
|
|
title: 'Feed C',
|
|
items: [
|
|
{ id: 3, title: 'C1', content: '', url: 'https://example.test/c1', timestamp: '2026-01-03 10:00:00' },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
}
|
|
|
|
it('narrows the displayed list to a single feed and restores it on "All feeds"', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
await fetchData()
|
|
|
|
// Options are the distinct feed titles, sorted.
|
|
expect(feedTitles.value).toEqual(['Feed A', 'Feed B'])
|
|
// Unfiltered: everything, newest first (Jan 3, Jan 2, Jan 1).
|
|
expect(feeds.value.map(f => f.id)).toEqual([3, 2, 1])
|
|
|
|
await setFeedFilter(['Feed A'])
|
|
expect(feeds.value.map(f => f.id)).toEqual([3, 1])
|
|
|
|
await setFeedFilter(null)
|
|
expect(feeds.value.map(f => f.id)).toEqual([3, 2, 1])
|
|
})
|
|
|
|
it('does not resurrect read articles when the filter is cleared', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed A'])
|
|
setInitialLoad(true)
|
|
|
|
// The first Feed A article scrolls above the viewport → marked read and
|
|
// dropped from both the filtered list and the master.
|
|
await handleIntersection([
|
|
{ isIntersecting: false, boundingClientRect: { y: -10 }, target: { id: '0' } },
|
|
])
|
|
setInitialLoad(false)
|
|
|
|
expect(feeds.value.map(f => f.id)).toEqual([1])
|
|
|
|
await setFeedFilter(null)
|
|
// Article 3 stays gone; the still-unread articles remain, newest first.
|
|
expect(feeds.value.map(f => f.id)).toEqual([2, 1])
|
|
})
|
|
|
|
it('does not resurrect an article-view read-in-place article when re-selecting its feed', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed B'])
|
|
expect(feeds.value.map(f => f.id)).toEqual([2])
|
|
|
|
// Article view marks the displayed article read in place (feed.read =
|
|
// true) without removing it, so currentIndex stays valid while paging —
|
|
// see markCurrentArticleRead. It's still in the list at this point.
|
|
markCurrentArticleRead()
|
|
expect(feeds.value.map(f => f.id)).toEqual([2])
|
|
|
|
// Switching to another feed and back must not resurrect it as if it
|
|
// were still unread.
|
|
await setFeedFilter(['Feed A'])
|
|
await setFeedFilter(['Feed B'])
|
|
|
|
expect(feeds.value).toEqual([])
|
|
})
|
|
|
|
it('marks the newly displayed article read when the filter is switched while in article view', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
await fetchData()
|
|
viewMode.value = 'article'
|
|
|
|
// Switching into Feed B while in article view immediately displays its
|
|
// one article's full content — that counts as "viewed", so it's marked
|
|
// read the same way toggleViewMode()/nextArticle()/prevArticle() do.
|
|
await setFeedFilter(['Feed B'])
|
|
|
|
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/2', null, expect.anything())
|
|
expect(feeds.value[0].read).toBe(true)
|
|
|
|
// It doesn't resurface either, once dropped on the next switch.
|
|
await setFeedFilter(['Feed A'])
|
|
await setFeedFilter(['Feed B'])
|
|
expect(feeds.value).toEqual([])
|
|
})
|
|
|
|
it('does not mark the newly displayed article read when the filter is switched in list view', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
await fetchData()
|
|
|
|
// List view only shows titles — switching filters there must not mark
|
|
// anything read; that's still driven solely by scroll (handleIntersection).
|
|
await setFeedFilter(['Feed B'])
|
|
|
|
expect(axios.put).not.toHaveBeenCalled()
|
|
expect(feeds.value[0].read).toBeUndefined()
|
|
})
|
|
|
|
it('keeps an emptied filtered feed selected and selectable, showing "All caught up"', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed B'])
|
|
expect(feeds.value.map(f => f.id)).toEqual([2])
|
|
|
|
// Mark all (visible = just Feed B) read — Feed B has no unread items left.
|
|
vi.spyOn(window, 'confirm').mockReturnValue(true)
|
|
await markAllRead()
|
|
|
|
// The list empties, but the filter stays put and the feed remains a
|
|
// selectable option (so a checked feed never dangles) even though it no
|
|
// longer has unread items.
|
|
expect(feeds.value).toEqual([])
|
|
expect(feedFilter.value).toEqual(['Feed B'])
|
|
expect(feedTitles.value).toEqual(['Feed A', 'Feed B'])
|
|
|
|
// Switching away drops the now-empty feed from the options.
|
|
await setFeedFilter(null)
|
|
expect(feedTitles.value).toEqual(['Feed A'])
|
|
expect(feeds.value.map(f => f.id)).toEqual([3, 1])
|
|
})
|
|
|
|
it('counts unread items in the selected feed when a filter is active, else the global total', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
await fetchData()
|
|
|
|
// No filter: global unread total across both feeds.
|
|
expect(unreadCount.value).toBe(3)
|
|
|
|
// Filtered: only the selected feed's unread items.
|
|
await setFeedFilter(['Feed B'])
|
|
expect(unreadCount.value).toBe(1)
|
|
await setFeedFilter(['Feed A'])
|
|
expect(unreadCount.value).toBe(2)
|
|
|
|
await setFeedFilter(null)
|
|
expect(unreadCount.value).toBe(3)
|
|
})
|
|
|
|
it('exposes per-feed unread counts, excluding items marked read in place', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
await fetchData()
|
|
|
|
expect(feedUnreadCounts.value).toEqual({ 'Feed A': 2, 'Feed B': 1 })
|
|
|
|
// Article view marks items read in place (feed.read = true) without
|
|
// removing them from the master — the counts must not include those.
|
|
allItems.value.find(i => i.id === 3).read = true
|
|
expect(feedUnreadCounts.value).toEqual({ 'Feed A': 1, 'Feed B': 1 })
|
|
})
|
|
|
|
it('filters the displayed list and unread count to the union of several selected feeds', async () => {
|
|
axios.get.mockResolvedValueOnce(threeFeedsResponse)
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed A', 'Feed C'])
|
|
|
|
// Newest first, Feed B (id 2) excluded.
|
|
expect(feeds.value.map(f => f.id)).toEqual([3, 1])
|
|
expect(unreadCount.value).toBe(2)
|
|
})
|
|
|
|
it('keeps every selected feed listed (even at 0 unread) once all of them are emptied', async () => {
|
|
axios.get.mockResolvedValueOnce(threeFeedsResponse)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed A', 'Feed C'])
|
|
vi.spyOn(window, 'confirm').mockReturnValue(true)
|
|
await markAllRead()
|
|
|
|
expect(feeds.value).toEqual([])
|
|
expect(feedFilter.value).toEqual(['Feed A', 'Feed C'])
|
|
// Feed B is still present because it still has an unread item; A and C
|
|
// stay present purely because they're selected — see feedTitles.
|
|
expect(feedTitles.value).toEqual(['Feed A', 'Feed B', 'Feed C'])
|
|
})
|
|
|
|
it('limits mark-all-read to the selected feeds, leaving the rest untouched', async () => {
|
|
axios.get.mockResolvedValueOnce(threeFeedsResponse)
|
|
axios.put.mockResolvedValue({ status: 200 })
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed A', 'Feed C'])
|
|
vi.spyOn(window, 'confirm').mockReturnValue(true)
|
|
await markAllRead()
|
|
|
|
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/1', null, expect.anything())
|
|
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/3', null, expect.anything())
|
|
expect(axios.put).not.toHaveBeenCalledWith('/api/v1/article/read/2', null, expect.anything())
|
|
// Feed B's unread item survives in the master untouched.
|
|
expect(allItems.value.map(f => f.id)).toEqual([2])
|
|
})
|
|
|
|
it('collapses an empty array selection to "All feeds" rather than an empty filter', async () => {
|
|
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
|
await fetchData()
|
|
|
|
await setFeedFilter(['Feed A'])
|
|
await setFeedFilter([])
|
|
|
|
expect(feedFilter.value).toBeNull()
|
|
expect(feeds.value.map(f => f.id)).toEqual([3, 2, 1])
|
|
})
|
|
})
|
|
|
|
describe('feed filter persistence', () => {
|
|
afterEach(() => {
|
|
localStorage.removeItem('feedFilter')
|
|
})
|
|
|
|
it('persists a feed selection to localStorage, and removes the key for "All feeds"', async () => {
|
|
await setFeedFilter(['Feed A', 'Feed B'])
|
|
expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A', 'Feed B']))
|
|
|
|
await setFeedFilter(null)
|
|
expect(localStorage.getItem('feedFilter')).toBeNull()
|
|
})
|
|
|
|
it('clearFeedFilter resets the ref and removes the persisted key', async () => {
|
|
await setFeedFilter(['Feed A'])
|
|
|
|
clearFeedFilter()
|
|
|
|
expect(feedFilter.value).toBeNull()
|
|
expect(localStorage.getItem('feedFilter')).toBeNull()
|
|
})
|
|
|
|
it('logout clears the persisted feed filter along with the session', async () => {
|
|
axios.post.mockResolvedValueOnce({ status: 200 })
|
|
await setFeedFilter(['Feed A'])
|
|
|
|
await logout()
|
|
|
|
expect(feedFilter.value).toBeNull()
|
|
expect(localStorage.getItem('feedFilter')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('pruneFeedFilter (via fetchData)', () => {
|
|
// pruneFeedFilter's own axios.get call is routed by URL, alongside
|
|
// fetchData's — vi.clearAllMocks() (see the outer beforeEach) clears call
|
|
// history but not a mockImplementation, so each test here resets it
|
|
// explicitly rather than leaking a stale router into later tests.
|
|
afterEach(() => {
|
|
axios.get.mockReset()
|
|
})
|
|
|
|
function mockArticleAndFeedsGets({ articleFeeds, filterFeeds, filterFeedsRejects }) {
|
|
axios.get.mockImplementation((url) => {
|
|
if (url.startsWith('/api/v1/article/get/')) {
|
|
return Promise.resolve({ data: { feeds: articleFeeds } })
|
|
}
|
|
if (url.startsWith('/api/v1/article/feeds/')) {
|
|
if (filterFeedsRejects) return Promise.reject(filterFeedsRejects)
|
|
return Promise.resolve({ data: { feeds: filterFeeds } })
|
|
}
|
|
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
|
})
|
|
}
|
|
|
|
async function flushMicrotasks(times = 10) {
|
|
for (let i = 0; i < times; i++) await Promise.resolve()
|
|
}
|
|
|
|
it('does not request the feeds endpoint when no filter is active', async () => {
|
|
feedFilter.value = null
|
|
mockArticleAndFeedsGets({ articleFeeds: [] })
|
|
|
|
await fetchData()
|
|
|
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
|
expect(axios.get).toHaveBeenCalledWith('/api/v1/article/get/7', expect.anything())
|
|
})
|
|
|
|
it('drops a selected title whose feed no longer exists, keeping the one that does', async () => {
|
|
feedFilter.value = ['Feed A', 'Gone']
|
|
mockArticleAndFeedsGets({
|
|
articleFeeds: [
|
|
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
|
{ title: 'Feed B', items: [{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' }] },
|
|
],
|
|
filterFeeds: [{ title: 'Feed A' }],
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(axios.get).toHaveBeenCalledWith('/api/v1/article/feeds/7', expect.anything())
|
|
expect(feedFilter.value).toEqual(['Feed A'])
|
|
expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A']))
|
|
expect(feeds.value.map(f => f.id)).toEqual([1])
|
|
})
|
|
|
|
it('collapses to "All feeds" when every selected title is gone', async () => {
|
|
feedFilter.value = ['Gone']
|
|
mockArticleAndFeedsGets({
|
|
articleFeeds: [
|
|
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
|
],
|
|
filterFeeds: [{ title: 'Feed A' }],
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(feedFilter.value).toBeNull()
|
|
expect(localStorage.getItem('feedFilter')).toBeNull()
|
|
expect(feeds.value.map(f => f.id)).toEqual([1])
|
|
})
|
|
|
|
it('keeps a selected feed with 0 unread items selected, without re-persisting', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
const setItemSpy = vi.spyOn(Storage.prototype, 'setItem')
|
|
mockArticleAndFeedsGets({
|
|
articleFeeds: [], // Feed A has no unread items right now
|
|
filterFeeds: [{ title: 'Feed A' }], // but the feed itself still exists
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(feedFilter.value).toEqual(['Feed A'])
|
|
expect(setItemSpy).not.toHaveBeenCalledWith('feedFilter', expect.anything())
|
|
|
|
setItemSpy.mockRestore()
|
|
})
|
|
|
|
it('leaves the filter untouched and surfaces no user-facing error when the feeds lookup fails', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
localStorage.setItem('feedFilter', JSON.stringify(['Feed A']))
|
|
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
mockArticleAndFeedsGets({
|
|
articleFeeds: [
|
|
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
|
],
|
|
filterFeedsRejects: new Error('network'),
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(feedFilter.value).toEqual(['Feed A'])
|
|
expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A']))
|
|
expect(showMessage.value).toBe(false)
|
|
expect(message.value).toBe('')
|
|
expect(consoleErrorSpy).toHaveBeenCalled()
|
|
|
|
consoleErrorSpy.mockRestore()
|
|
})
|
|
|
|
it('does not request the feeds endpoint when the article fetch itself fails', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
axios.get.mockImplementation((url) => {
|
|
if (url.startsWith('/api/v1/article/get/')) return Promise.reject(new Error('network'))
|
|
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
|
})
|
|
|
|
await fetchData()
|
|
|
|
expect(axios.get).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('does not resurrect the filter if it was cleared while the feeds lookup was still in flight', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
let resolveFeeds
|
|
axios.get.mockImplementation((url) => {
|
|
if (url.startsWith('/api/v1/article/get/')) {
|
|
return Promise.resolve({
|
|
data: { feeds: [{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] }] },
|
|
})
|
|
}
|
|
if (url.startsWith('/api/v1/article/feeds/')) {
|
|
return new Promise(resolve => { resolveFeeds = resolve })
|
|
}
|
|
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
|
})
|
|
|
|
const fetchPromise = fetchData()
|
|
await flushMicrotasks() // let fetchData's own GET resolve and pruneFeedFilter dispatch its GET
|
|
|
|
clearFeedFilter()
|
|
resolveFeeds({ data: { feeds: [{ title: 'Feed A' }] } })
|
|
await fetchPromise
|
|
|
|
expect(feedFilter.value).toBeNull()
|
|
expect(consoleErrorSpy).not.toHaveBeenCalled()
|
|
|
|
consoleErrorSpy.mockRestore()
|
|
})
|
|
|
|
it('keeps a filter selected mid-request rather than reverting it to the prune outcome', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
let resolveFeeds
|
|
axios.get.mockImplementation((url) => {
|
|
if (url.startsWith('/api/v1/article/get/')) {
|
|
return Promise.resolve({
|
|
data: {
|
|
feeds: [
|
|
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
|
{ title: 'Feed B', items: [{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' }] },
|
|
],
|
|
},
|
|
})
|
|
}
|
|
if (url.startsWith('/api/v1/article/feeds/')) {
|
|
return new Promise(resolve => { resolveFeeds = resolve })
|
|
}
|
|
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
|
})
|
|
|
|
const fetchPromise = fetchData()
|
|
await flushMicrotasks()
|
|
|
|
await setFeedFilter(['Feed B'])
|
|
// The (stale, already-in-flight) feeds list still includes Feed B, so the
|
|
// switch survives the prune that resolves afterwards.
|
|
resolveFeeds({ data: { feeds: [{ title: 'Feed A' }, { title: 'Feed B' }] } })
|
|
await fetchPromise
|
|
|
|
expect(feedFilter.value).toEqual(['Feed B'])
|
|
})
|
|
|
|
it('keeps isSyncing true until the feed-filter prune settles, not just the initial fetch', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
let resolveFeeds
|
|
axios.get.mockImplementation((url) => {
|
|
if (url.startsWith('/api/v1/article/get/')) {
|
|
return Promise.resolve({ data: { feeds: [{ title: 'Feed A', items: [] }] } })
|
|
}
|
|
if (url.startsWith('/api/v1/article/feeds/')) {
|
|
return new Promise(resolve => { resolveFeeds = resolve })
|
|
}
|
|
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
|
})
|
|
|
|
const fetchPromise = fetchData()
|
|
await flushMicrotasks()
|
|
|
|
expect(isSyncing.value).toBe(true)
|
|
|
|
resolveFeeds({ data: { feeds: [{ title: 'Feed A' }] } })
|
|
await fetchPromise
|
|
|
|
expect(isSyncing.value).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('setFeedFilter no-op guard', () => {
|
|
it('no-ops on an empty array while already unfiltered', async () => {
|
|
feedFilter.value = null
|
|
const scrollToSpy = vi.spyOn(window, 'scrollTo')
|
|
|
|
await setFeedFilter([])
|
|
|
|
expect(scrollToSpy).not.toHaveBeenCalled()
|
|
expect(feedFilter.value).toBeNull()
|
|
|
|
scrollToSpy.mockRestore()
|
|
})
|
|
|
|
it('does not mark anything read on a no-op call while in article view', async () => {
|
|
feedFilter.value = ['Feed A']
|
|
viewMode.value = 'article'
|
|
feeds.value = [{ id: 1, feedTitle: 'Feed A', title: 'a' }]
|
|
|
|
await setFeedFilter(['Feed A'])
|
|
|
|
expect(axios.put).not.toHaveBeenCalled()
|
|
|
|
viewMode.value = 'list'
|
|
})
|
|
})
|
|
|
|
it('strips leftover embedded-video placeholder headings', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://www.dw.com/en/article-one/a-1',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<h2 aria-label="Eingebettetes Video — Iran-Krieg belastet Wirtschaft und Märkte in Deutschland">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><g fill-rule="evenodd"><path d="M14.114 7.599H13.5l.002 4.706h.601l4.582 3.25-.005-11.11zM11.084 4.444l-9.007.002-1.336.797.002 9.514 1.334.793 9.007.006 1.509-.799-.004-9.516z"></path></g></svg>
|
|
Iran-Krieg belastet Wirtschaft und Märkte in Deutschland
|
|
</h2>
|
|
<p>some article text long enough for readability to keep the paragraph as the main content body, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
expect(feeds.value[0].content).not.toContain('Eingebettetes Video')
|
|
expect(feeds.value[0].content).not.toContain('<svg')
|
|
})
|
|
|
|
it('strips leftover embedded-audio placeholder headings', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://www.dw.com/en/article-one/a-1',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<h2 aria-label="Eingebetteter Audio-Beitrag — Der Gender Pay Gap existiert noch immer">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><g fill-rule="evenodd"><path d="M14.114 7.599H13.5l.002 4.706h.601l4.582 3.25-.005-11.11zM11.084 4.444l-9.007.002-1.336.797.002 9.514 1.334.793 9.007.006 1.509-.799-.004-9.516z"></path></g></svg>
|
|
Der Gender Pay Gap existiert noch immer
|
|
</h2>
|
|
<p>some article text long enough for readability to keep the paragraph as the main content body, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
expect(feeds.value[0].content).not.toContain('Eingebetteter Audio-Beitrag')
|
|
expect(feeds.value[0].content).not.toContain('<svg')
|
|
})
|
|
|
|
it('resolves Deutsche-Welle-style templated image URLs from data-format/data-url', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://www.dw.com/en/article-one/a-1',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<img data-format="MASTER_LANDSCAPE" data-id="76212061"
|
|
data-url="https://static.dw.com/image/76212061_\${formatId}.jpg"
|
|
data-aspect-ratio="16/9" alt="Merz and Trump"
|
|
src="https://static.dw.com/image/76212061_$%7BformatId%7D.jpg">
|
|
<p>some article text long enough for readability to keep the image and paragraph together in the parsed output, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes for the main article body.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
// "MASTER_LANDSCAPE" is a symbolic name from DW's CMS, not a valid value
|
|
// for the CDN's numeric `formatId` — it must be mapped to "6" or the
|
|
// resulting URL 400s and the image fails to load.
|
|
expect(feeds.value[0].content).toContain('src="https://static.dw.com/image/76212061_6.jpg"')
|
|
// The rendered `src` is what matters — `data-url` retaining the raw
|
|
// template is harmless since browsers don't load images from data-* attrs.
|
|
expect(feeds.value[0].content).not.toMatch(/src="[^"]*(\$\{|%7[bB])/)
|
|
})
|
|
|
|
it('resolves templated images regardless of the placeholder name and scrubs other lazy-load attributes', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://www.dw.com/en/article-one/a-1',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<img data-format="MASTER_LANDSCAPE" data-id="76212061"
|
|
data-url="https://static.dw.com/image/76212061_\${size}.jpg"
|
|
data-src="https://static.dw.com/image/76212061_\${size}.jpg"
|
|
alt="Merz and Trump" src="data:image/gif;base64,placeholder">
|
|
<p>some article text long enough for readability to keep the image and paragraph together in the parsed output, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes for the main article body.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
// `${size}` isn't the hardcoded `${formatId}` placeholder, but `data-format`
|
|
// is still the right substitution — and lazy-load attributes like `data-src`
|
|
// (which Readability may promote into `src`) must be cleaned too, not just
|
|
// `data-url`, so a stale template can't resurface in the rendered output.
|
|
expect(feeds.value[0].content).not.toMatch(/src="[^"]*(\$\{|%7[bB])/)
|
|
})
|
|
|
|
it('drops unresolvable templated images instead of leaving a broken src', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://example.test/article-one',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<img data-url="https://example.test/img_\${size}.jpg" src="https://example.test/img_%7Bsize%7D.jpg">
|
|
<p>some article text long enough for readability to keep the paragraph as the main content body, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
expect(feeds.value[0].content).not.toContain('%7Bsize%7D')
|
|
expect(feeds.value[0].content).not.toContain('<img')
|
|
})
|
|
|
|
it('strips taz product-teaser sections (e.g. the FUTURZWEI promo)', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://taz.de/Article-one/!6218657/',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<p>some article text long enough for readability to keep the paragraph as the main content body, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes.</p>
|
|
<section class="module columns article-produktteaser-container"><article class="column">
|
|
<a href="/taz-FUTURZWEI/endlich-mal-ein-magazin-fuer-zukunft/!v=74fecf50/" class="teaser-link">
|
|
<div class="text-column"><h2><span>taz FUTURZWEI im Abo entdecken</span><span>Endlich mal ein Magazin für Zukunft</span></h2>
|
|
<p>taz FUTURZWEI ist unser Magazin für eine bessere Zukunft. Das Abo bietet jährlich vier Ausgaben für nur 38 Euro.</p>
|
|
<ul><li>Jedes Quartal neu in Ihrem Briefkasten</li><li>Nur 38 Euro im Jahr</li></ul></div>
|
|
<p>Jetzt abonnieren</p>
|
|
</a>
|
|
</article></section>
|
|
<p>more article text long enough to survive readability's content-length heuristics as well, since it needs to look like part of the main body too.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
expect(feeds.value[0].readable).toBe(true)
|
|
expect(feeds.value[0].content).toContain('main content body')
|
|
expect(feeds.value[0].content).not.toContain('FUTURZWEI')
|
|
expect(feeds.value[0].content).not.toContain('Jetzt abonnieren')
|
|
})
|
|
|
|
it('strips Al-Jazeera-style "Recommended Stories" sections', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://www.aljazeera.com/news/2026/9/14/article-one',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<p>some article text long enough for readability to keep the paragraph as the main content body, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes.</p>
|
|
<section class="more-on"><h2 class="more-on__heading">Recommended Stories<!-- --> </h2><span>list of 3 items</span><ul>
|
|
<li><span>list 1 of 3</span><a href="https://www.aljazeera.com/a">Unrelated story one</a></li>
|
|
<li><span>list 2 of 3</span><a href="https://www.aljazeera.com/b">Unrelated story two</a></li>
|
|
<li><span>list 3 of 3</span><a href="https://www.aljazeera.com/c">Unrelated story three</a></li>
|
|
</ul><span>end of list</span></section>
|
|
<p>more article text long enough to survive readability's content-length heuristics as well, since it needs to look like part of the main body too.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
expect(feeds.value[0].readable).toBe(true)
|
|
expect(feeds.value[0].content).toContain('main content body')
|
|
expect(feeds.value[0].content).not.toContain('Recommended Stories')
|
|
expect(feeds.value[0].content).not.toContain('Unrelated story')
|
|
})
|
|
|
|
it('keeps other sections that are not the "Recommended Stories" block', async () => {
|
|
feeds.value = [{
|
|
id: 1,
|
|
title: 'Article one',
|
|
url: 'https://www.aljazeera.com/news/2026/9/14/article-one',
|
|
content: '',
|
|
}]
|
|
axios.post.mockResolvedValueOnce({
|
|
data: {
|
|
content: `<html><body><article>
|
|
<section><p>some article text long enough for readability to keep the paragraph as the main content body, padded with extra words to pass the content-length heuristics used by Mozilla Readability when scoring candidate nodes.</p></section>
|
|
<p>more article text long enough to survive readability's content-length heuristics as well, since it needs to look like part of the main body too.</p>
|
|
</article></body></html>`,
|
|
},
|
|
})
|
|
|
|
await getReadable(feeds.value[0], 0)
|
|
|
|
expect(feeds.value[0].readable).toBe(true)
|
|
expect(feeds.value[0].content).toContain('main content body')
|
|
})
|
|
})
|