Files
FabledCurator/frontend/test/seriesReader.spec.js
T
bvandeusenandClaude Opus 5.5 ff70f837d0
CI and images / lint (push) Successful in 2s
CI and images / extension-version (push) Successful in 2s
CI and images / frontend-build (push) Successful in 20s
CI and images / backend-lint-and-test (push) Successful in 30s
CI and images / integration (push) Successful in 2m18s
CI and images / sign-extension (push) Successful in 3s
CI and images / build-agent (push) Successful in 6s
CI and images / build-web (push) Successful in 1m44s
CI and images / smoke-web (push) Successful in 56s
CI and images / promote (push) Skipped
refactor: test row factories and the fetch stub have one copy each (3109)
tests/factories.py holds image_row/make_image/make_image_async/make_tag. The
17 byte-identical _img/_tag helpers (15 modules) now import them under their
old names, so no call site changed. frontend/test/support/stubFetch.js
replaces 15 copies that differed only in formatting. Copies whose bodies
differ (other defaults, other columns, a url-only stub) are left as they
are; folding those needs a look at each caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LVjrnpQjRgHdvq95rASoiR
2026-09-24 16:39:38 -04:00

72 lines
2.4 KiB
JavaScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import {
useSeriesReaderStore,
currentPageFromScroll,
progressPct,
clampPage
} from '../src/stores/seriesReader.js'
import { stubFetch } from './support/stubFetch.js'
const M = [
{ page_number: 1, top: 0, height: 100 },
{ page_number: 2, top: 100, height: 100 },
{ page_number: 3, top: 200, height: 100 }
]
describe('seriesReader pure helpers', () => {
it('currentPageFromScroll: top of doc → page 1', () => {
expect(currentPageFromScroll(M, 0, 90)).toBe(1) // target 30
})
it('currentPageFromScroll: boundary crosses into page 2', () => {
expect(currentPageFromScroll(M, 80, 90)).toBe(2) // target 110
})
it('currentPageFromScroll: scrolled past last → last page', () => {
expect(currentPageFromScroll(M, 5000, 300)).toBe(3)
})
it('currentPageFromScroll: empty metrics → 1', () => {
expect(currentPageFromScroll([], 0, 90)).toBe(1)
})
it('progressPct: 0 at top, ~50 mid, clamped 100, zero-guard', () => {
expect(progressPct(0, 1000, 500)).toBe(0)
expect(progressPct(250, 1000, 500)).toBe(50)
expect(progressPct(9999, 1000, 500)).toBe(100)
expect(progressPct(0, 100, 100)).toBe(0) // denom 0 → 0
})
it('clampPage: below→1, above→total, valid passthrough, NaN→1', () => {
expect(clampPage(0, 5)).toBe(1)
expect(clampPage(99, 5)).toBe(5)
expect(clampPage(3, 5)).toBe(3)
expect(clampPage(NaN, 5)).toBe(1)
expect(clampPage(2, 0)).toBe(1)
})
})
describe('seriesReader store', () => {
beforeEach(() => setActivePinia(createPinia()))
afterEach(() => vi.restoreAllMocks())
it('load() stores series + pages', async () => {
const s = useSeriesReaderStore()
stubFetch(() => ({
status: 200,
body: {
series: { id: 7, name: 'V' },
pages: [{ image_id: 1, page_number: 1, thumbnail_url: 't', image_url: 'i' }]
}
}))
await s.load(7)
expect(s.series).toEqual({ id: 7, name: 'V' })
expect(s.pages[0].image_url).toBe('i')
expect(s.error).toBe(null)
})
it('load() on 404 sets error, no throw', async () => {
const s = useSeriesReaderStore()
stubFetch(() => ({ status: 404, body: { error: 'not a series' } }))
await s.load(9)
expect(s.error).toBe('not a series')
expect(s.pages).toEqual([])
})
})