wrolpi/app/src/components/CbzViewer.test.js
lrnselfreliance 3c362799b5
Resume EPUBs and comic books where the reader left off (#798)
* Resume EPUBs and comic books where the reader left off

The EPUB viewer (epub.html) accepts `?cfi=` and posts its location and
progress to the page that embeds it on every page turn.  Progress comes from
the chapter and the page within it, rather than book.locations.generate(),
which stalls a Raspberry Pi on a large book.  A search result's `loc` still
wins over the saved position.

The comic viewer opens at the saved page and saves each page shown.

Both resume on the doc page and in the file preview.  The resume point is
decided once per opened file, so saves made while reading do not change the
EPUB viewer's URL and reload it.  The doc page tracks only the FileGroup's
primary file; another format beside it is not tracked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* EPUB/comic progress: never save a place the server would discard

The server treats a position at the start (the cover, under 1%) as not
started, and replaces the saved place with progress 0.  Both viewers
reported the place they opened on, so opening a finished book un-finished
it, and peeking at the cover erased the reader's page.

- The comic viewer does not save the page it opens on, nor the cover; a
  peek at the cover keeps the last page saved.
- The EPUB viewer posts nothing at the start of the book.
- Progress before the end is kept between 1% and 94% (resumableProgress), so
  early pages of a long book and the last pages before the end keep their
  position.  Only the last page (comics) or the end (EPUB) is finished.
- Non-finite EPUB progress is ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 18:19:51 -06:00

84 lines
3.8 KiB
JavaScript

import React from 'react';
import {act, fireEvent, screen} from '@testing-library/react';
import {renderWithProviders} from '../test-utils';
import {CbzViewer} from './CbzViewer';
import {getArchiveContents} from '../api';
jest.mock('../api', () => ({
...jest.requireActual('../api'),
getArchiveContents: jest.fn(),
getArchiveMemberUrl: (path, member) => `/member/${member}`,
}));
const PATH = 'comics/issue.cbz';
const PAGES = ['01.jpg', '02.jpg', '03.jpg', '04.jpg'];
const {fetch: realFetch} = global;
const realSendBeacon = navigator.sendBeacon;
beforeEach(() => {
window.sessionStorage.clear();
getArchiveContents.mockResolvedValue({entries: PAGES.map(name => ({name, path: name, is_dir: false}))});
navigator.sendBeacon = jest.fn(() => false);
global.fetch = jest.fn(() => Promise.resolve({ok: true}));
});
afterEach(() => {
global.fetch = realFetch;
navigator.sendBeacon = realSendBeacon;
});
const saves = () => global.fetch.mock.calls.map(([, init]) => JSON.parse(init.body));
describe('CbzViewer progress', () => {
test('opens at the saved page, and saves the page the reader leaves on', async () => {
const fileGroup = {progress: 0.75, position: {kind: 'page', page: 2, updated_at: 1000}};
const {unmount} = renderWithProviders(
<CbzViewer path={PATH} progressPath={PATH} fileGroup={fileGroup}/>);
expect(await screen.findByText('3 / 4')).toBeInTheDocument();
expect(screen.getByAltText('Page 3')).toHaveAttribute('src', '/member/03.jpg');
fireEvent.click(screen.getByLabelText('Next page'));
expect(screen.getByText('4 / 4')).toBeInTheDocument();
unmount();
expect(saves()).toEqual([expect.objectContaining({
file: PATH, progress: 1, final: true, position: expect.objectContaining({kind: 'page', page: 3}),
})]);
});
test('a saved page past the end opens the last page', async () => {
const fileGroup = {progress: 0.5, position: {kind: 'page', page: 40, updated_at: 1000}};
renderWithProviders(<CbzViewer path={PATH} progressPath={PATH} fileGroup={fileGroup}/>);
expect(await screen.findByText('4 / 4')).toBeInTheDocument();
});
test('without a progress path it opens at the first page and saves nothing', async () => {
const fileGroup = {progress: 0.75, position: {kind: 'page', page: 2, updated_at: 1000}};
const {unmount} = renderWithProviders(<CbzViewer path={PATH} fileGroup={fileGroup}/>);
expect(await screen.findByText('1 / 4')).toBeInTheDocument();
await act(async () => fireEvent.click(screen.getByLabelText('Next page')));
unmount();
expect(saves()).toEqual([]);
});
test('opening a comic saves nothing, so a finished comic stays finished', async () => {
const fileGroup = {progress: 1, position: null, viewed: new Date(9000).toISOString()};
const {unmount} = renderWithProviders(<CbzViewer path={PATH} progressPath={PATH} fileGroup={fileGroup}/>);
expect(await screen.findByText('1 / 4')).toBeInTheDocument();
unmount();
expect(saves()).toEqual([]);
});
test('peeking at the cover keeps the reader\'s place', async () => {
const fileGroup = {progress: 0.75, position: {kind: 'page', page: 2, updated_at: 1000}};
const {unmount} = renderWithProviders(<CbzViewer path={PATH} progressPath={PATH} fileGroup={fileGroup}/>);
expect(await screen.findByText('3 / 4')).toBeInTheDocument();
fireEvent.click(screen.getByLabelText('Previous page'));
fireEvent.click(screen.getByLabelText('Previous page'));
expect(screen.getByText('1 / 4')).toBeInTheDocument();
unmount();
expect(saves()).toEqual([expect.objectContaining({position: expect.objectContaining({page: 1})})]);
});
});