mirror of
https://github.com/hectorqin/reader.git
synced 2026-11-12 05:50:54 +08:00
两件事:把 PR #8 里那四项合并进来(A 选项),然后把 issue 里一直没落地的 「一次只取一章」从服务端契约一路做到客户端。 ## 一、PR #8 的四项(Android 外壳最值钱) - Android 原生整页图渲染:NativePageView + 桥接 v2(renderPage/hidePage)。 只画整页图,EPUB 永远留在 WebView;画不了就返回 false 回退,不出现空白页。 - 离线队列:脏集合语义固定为契约,补上「请求在途时位置被改写」的三条用例。 - TXT 编码探测:GB18030 与 Big5 都解一遍再按损坏证据评分选优。 原来「先 GB18030 失败再 Big5」永远走不到第二步——两者字节范围重叠, 同一串字节通常两边都能解出合法字符,繁体书会整本乱码。 - 格式嗅探:magic number 优先于扩展名,并明确拒绝非书籍的媒体容器。 ## 二、Android 壳第一次真的编译通过 上一轮说「没有 JDK/SDK,只写了契约」,这一轮补上了环境并真的编译。 编译立刻暴露三处——都是只有编译才能发现的: - colors.xml 的注释里写了 --reader-paper。XML 禁止注释中出现双连字符, AAPT 直接拒绝整个文件。Android 壳此前从未构建过,所以从未发现。 - NativePageView.kt 用了 Uint8Array。那是 JavaScript 类型,Kotlin 里不存在; 桥接的另一侧是 ByteArray。 - AndroidManifest.xml 引用了 @mipmap/ic_launcher,而资源里没有这个文件。 三条都让 :app:assembleDebug 失败,也就是说这个 APK 此前根本产不出来。 现在是 BUILD SUCCESSFUL,产物 3.6MB。 ## 三、分窗读取:从契约做到客户端 服务端: - /manifest 省略 group 现在等价于 group=0。原来省略意味着「发全部条目」, 于是一本 1200 章的书在它最需要快的那个请求(打开书)里收到 1200 条记录, 等于分窗对不传参的客户端完全失效。要完整结构用 ?group=all。 - /toc 在没有 OPF 标题映射时回落到章节自身的第一个标题。手作 EPUB 大量 没有标题映射,原来整本目录会读成「第 1 章 … 第 300 章」,等于没有目录。 - 扫描时的 spine 读取不再解析 NCX 与 nav:扫一遍全库本该只读一次包文档。 - 尾部 Range(bytes=-N)在超过 8MB 时退化为整包响应并撤回 Accept-Ranges。 前向流无法跳到文件末尾,服务这种请求意味着为三个字节读完整个 400MB 漫画包。 客户端: - 新增 formats/remote-zip.ts:走 HTTP Range 的只读 ZIP,让浏览器端也能不下整本书。 - 新增 formats/windowed.ts:分窗文档。分窗是传输边界,目录必须完整,两者分开 —— 合在一起就是原来目录面板显示「第 1 章 – 第 40 章」的原因。 - reader-screen 接上 /toc,并且点击目录项会先取对应窗口再跳。 ## 验证 server: 133/133(typecheck + test + build) web: 179 vitest + 29 node:test(typecheck + test + build) android: BUILD SUCCESSFUL,app-debug.apk 3.6MB shell: 契约双向校验 + 渲染模式词表校验通过
143 lines
5.4 KiB
TypeScript
143 lines
5.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { bookPercentage, formatLocator, parseLocator } from '../src/ui/locator.ts';
|
|
import type { BookDoc, Section } from '../src/formats/types.ts';
|
|
|
|
function doc(sections: Array<Partial<Section>>): BookDoc {
|
|
return {
|
|
format: 'epub',
|
|
layout: 'reflowable',
|
|
render: 'reflowable',
|
|
direction: 'ltr',
|
|
sections: sections.map((section, index) => ({
|
|
id: section.id ?? `s${index}`,
|
|
label: section.label ?? `第 ${index} 章`,
|
|
html: section.html ?? '<p>' + 'x'.repeat(500) + '</p>',
|
|
depth: 0,
|
|
})),
|
|
toc: [],
|
|
styles: [],
|
|
resources: new Map(),
|
|
orderedByBook: true,
|
|
};
|
|
}
|
|
|
|
describe('locator format', () => {
|
|
it('round-trips a position', () => {
|
|
const book = doc([{ id: 'ch1' }, { id: 'ch2' }]);
|
|
const locator = formatLocator('ch2', 0.42);
|
|
const parsed = parseLocator(locator, book);
|
|
expect(parsed?.sectionId).toBe('ch2');
|
|
expect(parsed?.offset).toBeCloseTo(0.42, 3);
|
|
});
|
|
|
|
it('clamps an out-of-range offset instead of trusting it', () => {
|
|
const book = doc([{ id: 'ch1' }]);
|
|
expect(parseLocator(formatLocator('ch1', 5), book)?.offset).toBe(1);
|
|
expect(parseLocator(formatLocator('ch1', -3), book)?.offset).toBe(0);
|
|
});
|
|
|
|
it('rejects a locator for a section that no longer exists', () => {
|
|
// The book changed on disk under the reader. Returning null lets the reader
|
|
// say so instead of silently pretending page one is where they left off.
|
|
const book = doc([{ id: 'ch1' }]);
|
|
expect(parseLocator('gone:0.5', book)).toBeNull();
|
|
});
|
|
|
|
it('does not treat an EPUB CFI from another client as a position it understands', () => {
|
|
// The server stores locators opaquely, so a CFI written by a different
|
|
// client can arrive here. It must be refused, not misparsed as a section id.
|
|
const book = doc([{ id: 'ch1' }]);
|
|
expect(parseLocator('epubcfi(/6/4!/4/2)', book)).toBeNull();
|
|
});
|
|
|
|
it('accepts a bare section id as the start of that section', () => {
|
|
const book = doc([{ id: 's0' }, { id: 'ch2' }]);
|
|
const parsed = parseLocator('ch2', book);
|
|
expect(parsed?.offset).toBe(0);
|
|
expect(parsed?.percentage).toBeCloseTo(0.5, 3);
|
|
});
|
|
|
|
it('rejects an empty locator', () => {
|
|
expect(parseLocator('', doc([{ id: 'a' }]))).toBeNull();
|
|
});
|
|
|
|
it('treats a non-numeric offset as the start of the section', () => {
|
|
// A corrupt offset must not cost the reader the *chapter* as well: the
|
|
// section id is still good information.
|
|
const book = doc([{ id: 'ch1' }]);
|
|
expect(parseLocator('r1:not-a-number:ch1', book)?.offset).toBe(0);
|
|
});
|
|
|
|
it('keeps a section id that itself contains a colon', () => {
|
|
// Windows-authored EPUBs produce hrefs like `OEBPS\text:ch1.xhtml`, and a
|
|
// naive split would truncate the id and lose the position entirely.
|
|
const book = doc([{ id: 'OEBPS/text:ch1.xhtml' }]);
|
|
const parsed = parseLocator(formatLocator('OEBPS/text:ch1.xhtml', 0.5), book);
|
|
expect(parsed?.sectionId).toBe('OEBPS/text:ch1.xhtml');
|
|
expect(parsed?.offset).toBeCloseTo(0.5, 3);
|
|
});
|
|
|
|
it('refuses a versioned locator with no section id', () => {
|
|
const book = doc([{ id: 'ch1' }]);
|
|
expect(parseLocator('r1:0.5:', book)).toBeNull();
|
|
});
|
|
|
|
it('reads a legacy unversioned locator so an upgrade does not lose the place', () => {
|
|
const book = doc([{ id: 'ch1' }, { id: 'ch2' }]);
|
|
const parsed = parseLocator('ch2:0.25', book);
|
|
expect(parsed?.sectionId).toBe('ch2');
|
|
expect(parsed?.offset).toBeCloseTo(0.25, 3);
|
|
});
|
|
|
|
it('treats a bare page number as that page', () => {
|
|
const book = doc([{ id: 'p1' }, { id: 'p2' }]);
|
|
expect(parseLocator('1', book)?.sectionId).toBe('p2');
|
|
});
|
|
});
|
|
|
|
describe('book percentage', () => {
|
|
it('is 0 at the start and approaches 1 at the end', () => {
|
|
const book = doc([{ id: 'a' }, { id: 'b' }, { id: 'c' }]);
|
|
expect(bookPercentage(book, 0, 0)).toBe(0);
|
|
expect(bookPercentage(book, 2, 1)).toBeCloseTo(1, 5);
|
|
});
|
|
|
|
it('weights sections by length so an uneven book does not lie about progress', () => {
|
|
// Three short chapters and one enormous one. At the start of the big chapter
|
|
// a naive index/count calculation would claim two thirds done.
|
|
const short = { html: '<p>' + 'x'.repeat(400) + '</p>' };
|
|
const book = doc([
|
|
short,
|
|
short,
|
|
short,
|
|
{ html: '<p>' + 'x'.repeat(40_000) + '</p>' },
|
|
]);
|
|
const atStartOfBig = bookPercentage(book, 3, 0);
|
|
const naive = 3 / 4;
|
|
expect(atStartOfBig).toBeLessThan(naive);
|
|
expect(atStartOfBig).toBeLessThan(0.2);
|
|
expect(bookPercentage(book, 3, 1)).toBeCloseTo(1, 5);
|
|
});
|
|
|
|
it('gives every page equal weight in a fixed-layout book', () => {
|
|
const book: BookDoc = { ...doc([{ id: 'p1' }, { id: 'p2' }, { id: 'p3' }, { id: 'p4' }]), layout: 'fixed' };
|
|
expect(bookPercentage(book, 2, 0)).toBeCloseTo(0.5, 5);
|
|
});
|
|
|
|
it('returns 0 for a book with no sections rather than NaN', () => {
|
|
expect(bookPercentage(doc([]), 0, 0)).toBe(0);
|
|
});
|
|
|
|
it('is monotonic across a full traversal', () => {
|
|
const book = doc([{ id: 'a' }, { id: 'b', html: '<p>' + 'y'.repeat(5000) + '</p>' }]);
|
|
let previous = -1;
|
|
for (let index = 0; index < book.sections.length; index += 1) {
|
|
for (const within of [0, 0.5, 1]) {
|
|
const value = bookPercentage(book, index, within);
|
|
expect(value).toBeGreaterThanOrEqual(previous - 1e-9);
|
|
previous = value;
|
|
}
|
|
}
|
|
});
|
|
});
|