mirror of
https://github.com/hectorqin/reader.git
synced 2026-11-10 13:04:08 +08:00
按产品设计 §5「原生外壳 + WebView 渲染」落地客户端,Android 与浏览器共用同一份
渲染层;同时把服务端的格式范围从 epub + pdf 扩展到 txt 与漫画。
## 为什么两端共用一份渲染层
Android 端不重写渲染。它是一个 Kotlin 原生外壳,只负责文件缓存、手势、账号与书架,
渲染复用 web/ 构建出的同一份产物。原因很直接:排版策略一旦有第二份实现就一定会分叉,
而「忠于精排」正是这个产品的差异点。
共用的抽象在 web/src/core/platform.ts(传输、键值存储、二进制存储、连通性)。
外壳只覆盖 WebView 做不到或做不好的三件事:
- 连通性:WebView 里的 navigator.onLine 只要有网卡就报「在线」,连上没有出口的
Wi-Fi 时会骗人,客户端于是转圈而不是读缓存。改用平台的 NET_CAPABILITY_VALIDATED。
- 稳定的设备名:UA 里的型号会随 Chrome 升级变化,作为「上次在哪个设备读的」标签不可靠。
- 原生提示:一行 Kotlin 的 Toast,不用在 Web 层造通知 UI。
跨语言的接缝(Kotlin 桥接 ↔ TypeScript 声明)没法互相类型检查,因此加了
android/scripts/check-shell.sh 做双向契约校验,并接进 CI。
## 精排 CSS 策略
这是整个项目最产品化的一块 CSS,所以规则是写明的而不是凑出来的。
三条原则:不设任何属性除非不设会坏;每个可调项都是默认 inherit 的可继承自定义属性
(「关」的意思是「不动它」,不是「用我们的默认值」);任何改变作者意图的行为都是
用户主动开启且持久化。
实际 override 只有七处,每处都在注释里写了为什么:图片/表格/pre 在窄屏会撑破布局、
ruby 振假名在 16px 下按默认 50% 太小、长中文标题不换行会溢出。除此之外不动。
被刻意删掉的做法:reset 样式表、统一的「阅读主题」、给 p 加边距。它们能让排版
「看起来一致」,而一致性恰恰是这里要避免的。
书的样式装进 shadow root。书里一条 p { color: red } 不能改到书架,App 的 .topbar
也不能被书撞上。用 iframe 也能隔离,但每次换章要重建文档,会丢滚动位置并在 Android
上多花几倍内存。关键是**不往 shadow root 里注入任何 reset**。
## 格式层
服务端只负责流式给出原始文件,拆包、编码、章节与分页都是渲染层的事——
这些恰好是产品要自己拿主意的部分。
- EPUB:OPF/spine/nav 解析,作者样式全部保留,相对引用改写成内部 reader-res: 协议,
绝对 URL 原样保留交给 CSP 挡掉。加载器按文档逐个容错,一个坏章节不会毁掉整本书。
- TXT:先严格校验 UTF-8(自己实现,不用宽松模式——宽松模式永远「成功」,于是永远
发现不了解错了),失败再依次试 GB18030 与 Big5,并把选择告诉用户、允许覆盖。
章节只能从标题推断,策略是宁可少认不要错认:800 字的段落以「第三章」开头不算标题,
正文里提到「他在第二章读过」也不算。两条都有测试固定。
- CBZ / 图片目录:自然序排页(1,2,10 而不是 1,10,2),一页一个 fixed-layout section。
排错不是观感问题,是故事被静默打乱。
- 检测以魔数优先:本地库里 .txt 其实是 EPUB、.cbz 里其实是 PDF 的情况很多,
按扩展名开错加载器只会得到一个看不懂的报错。
服务端扫描器相应扩展:txt/cbz 直接索引;图片目录按文件夹识别为一本书,
文件夹路径作为主键锚点,所以**往漫画文件夹加一页不会丢进度**——这条有测试。
## 客户端三态与离线队列
离线队列记的是**脏集合**(哪些记录还没送到服务端),而不是「当前全部状态」。
两者只在一种情况下结果不同,而这种情况一定会发生:记录在请求发出之后、响应到达之前
被修改了。脏集合下这次更新仍是脏的,要等它自己的确认才清除;「全发」模式下它会被
那个更早的响应顺手清掉,然后永远送不到服务端——一个丢失的阅读位置,是这个产品
最不能有的失败。
阅读位置格式 r1:<偏移>:<章节 id>。三点都是踩过之后定的:版本前缀(服务端把 locator
当不透明字符串长期保存,客户端将来改格式必须能认出「这不是我认识的格式」)、
章节 id 放最后(它是文件路径,会含冒号,只从前两个冒号切才不会丢数据)、
偏移是章节内的比例而非像素或列号(列号会随字号变化,于是读者一调字号位置就跳)。
TXT、漫画、PDF 的支持也一并接上,PDF 用浏览器自带阅读器保底可读。
## 服务端
- GET /books/:id/file?path=… :按页取图。path 必须是这本书已登记的 rel_path,
服务端按表精确匹配,不用 query 拼路径,所以穿越只会匹配到空。
- CORS 白名单(CORS_ORIGINS,默认回显任意来源)+ H5 静态托管(WEB_DIR)。
托管在同一容器里,这才保住「一条命令跑起来」。
- Android 用 WebViewAssetLoader 从 https://appassets.androidplatform.net/ 提供
打包好的客户端,而不是 file://。后者发出的 fetch 是不透明的跨源请求,所有访问
服务端的调用都会被拦,而通常的绕法是把 web security 关掉——那等于把 WebView
变成一扇敞开的门。用资产加载器拿到的是正常安全源,请求按普通 CORS 走,
isSecureContext 为真,IndexedDB 等行为与浏览器一致。这一个选择是「一份 bundle
两个宿主」能成立的前提。
## 过程中发现并修掉的问题
- **ETag 里塞了中文路径**:HTTP 头值必须是 ISO-8859-1,漫画页码路径是中文时会抛
ERR_INVALID_CHAR,把一个正常的取图请求变成 500。改成对路径取散列。
- **漫画文件夹的主键用了页集合散列**:注释写的是「加一页不产生新书」,实现却是散列
页集合,加一页就换 id、进度全丢。改成用文件夹路径作为锚点,和书用 dc:identifier
作锚点是同一个道理。测试固定住了。
- **locator 解析与写出不对称**:写出是两段、解析却按三段切,导致位置根本存不回来。
章节 id 含冒号时更糟。
- **离线队列的清理条件**:原本按「清空全部」处理,会把请求在途时产生的新位置一并
抹掉。改成只清那一批、且只清时间戳吻合的条目。
- **测试自身的错误**:并发刷新去重的用例里,我一开始用 resolveRefresh?.() 调一个
类型已收窄成 never 的变量,断言等于什么都没测;「outbox 为空时不推」的用例里把
登录的 POST 也数了进去,断言恒不成立。两处都是测试写错了。
- **凭据用错了**:一个权限用例里我用 createAsAdmin 建账号却拿另一组凭据登录,
401 被当成「越权被拒」而通过。这也是测试写错了。
## 验证方式
```
cd server && npm run typecheck && npm test && npm run build # 37/37 通过
cd web && npm run typecheck && npm test && npm run build # 147/147 通过
sh android/scripts/check-shell.sh # 桥接契约双向校验通过
```
另用真实 HTTP 请求端到端验证过:注册 → 扫描 → 书架(四种格式全部可见)→ 漫画按页
取图(含中文路径 ETag)→ 列表页 → 进度同步 → 继续阅读;H5 静态托管、SPA 回落、
CORS 回显、路径穿越拒绝均按预期。
Android 壳本轮**没有真机编译验证**:环境里没有 JDK 和 Android SDK。Kotlin 侧做了
静态校验(包名与目录一致、项目内 import 可解析、桥接方法与 web 侧声明双向对齐),
并接进了 CI。第一次真机构建可能需要微调 Gradle 版本或 AGP 版本。
## 说明
- docker-compose.yml 与 Dockerfile 已改为从仓库根构建并打包 H5 产物;
build 上下文变了,如果之前用了 `build: ./server` 需要改成根目录。
- iOS 未做设计(按设计文档 §7,等有赞助再说)。
- 移动端批注输入仍未做(先保证读,后补写)。
Ref: #1
613 lines
28 KiB
TypeScript
613 lines
28 KiB
TypeScript
/**
|
|
* End-to-end API tests against a real server instance and a real on-disk
|
|
* library. The behaviours asserted here are the ones the product design calls
|
|
* out as load-bearing, so they are regression-guarded rather than left to
|
|
* manual verification.
|
|
*/
|
|
import { test, before, after } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtemp, mkdir, writeFile, rm, readdir } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import JSZip from 'jszip';
|
|
import Fastify from 'fastify';
|
|
import type { FastifyInstance } from 'fastify';
|
|
|
|
import { loadConfig } from '../src/config/index.ts';
|
|
import { openDatabase } from '../src/db/index.ts';
|
|
import { Scanner } from '../src/indexer/scanner.ts';
|
|
import { UserService } from '../src/services/users.ts';
|
|
import { ShelfService } from '../src/services/shelf.ts';
|
|
import { SyncService } from '../src/services/sync.ts';
|
|
import { buildApp } from '../src/http/app.ts';
|
|
import type { AppContext } from '../src/http/context.ts';
|
|
|
|
let app: FastifyInstance;
|
|
let ctx: AppContext;
|
|
let root: string;
|
|
let booksDir: string;
|
|
|
|
const OPF = (opts: { id: string; title: string; creator: string; series?: string; index?: string }) => `<?xml version="1.0" encoding="utf-8"?>
|
|
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="bookid">
|
|
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
|
<dc:identifier id="bookid">${opts.id}</dc:identifier>
|
|
<dc:title>${opts.title}</dc:title><dc:creator>${opts.creator}</dc:creator>
|
|
<dc:language>zh</dc:language>
|
|
${opts.series ? `<meta name="calibre:series" content="${opts.series}"/><meta name="calibre:series_index" content="${opts.index ?? '1'}"/>` : ''}
|
|
</metadata>
|
|
<manifest><item id="c" href="c.xhtml" media-type="application/xhtml+xml"/></manifest>
|
|
<spine><itemref idref="c"/></spine></package>`;
|
|
|
|
async function makeEpub(opts: { id: string; title: string; creator: string; series?: string; index?: string; extra?: string }) {
|
|
const zip = new JSZip();
|
|
zip.file('mimetype', 'application/epub+zip');
|
|
zip.file('META-INF/container.xml', '<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/></rootfiles></container>');
|
|
zip.file('OEBPS/content.opf', OPF(opts));
|
|
zip.file('OEBPS/c.xhtml', `<html><body><p>${opts.title}${opts.extra ?? ''}</p></body></html>`);
|
|
return zip.generateAsync({ type: 'nodebuffer' });
|
|
}
|
|
|
|
before(async () => {
|
|
root = await mkdtemp(join(tmpdir(), 'reader-test-'));
|
|
booksDir = join(root, 'books');
|
|
await mkdir(booksDir, { recursive: true });
|
|
process.env.BOOKS_DIR = booksDir;
|
|
process.env.DATA_DIR = join(root, 'data');
|
|
process.env.SCAN_INTERVAL = '0';
|
|
process.env.WATCH_INTERVAL = '0';
|
|
process.env.READER_TOKEN_SECRET = 'test-secret-that-is-long-enough';
|
|
|
|
const config = loadConfig();
|
|
const db = openDatabase(config);
|
|
ctx = {
|
|
config,
|
|
db,
|
|
scanner: undefined as never,
|
|
users: undefined as never,
|
|
shelf: undefined as never,
|
|
sync: undefined as never,
|
|
log: undefined as never,
|
|
};
|
|
const silent = Fastify({ logger: false });
|
|
ctx.log = silent.log;
|
|
ctx.scanner = new Scanner(db, config, { info: () => {}, warn: () => {} });
|
|
ctx.users = new UserService(db, config);
|
|
ctx.shelf = new ShelfService(db);
|
|
ctx.sync = new SyncService(db);
|
|
app = buildApp(ctx);
|
|
await app.ready();
|
|
});
|
|
|
|
after(async () => {
|
|
await app.close();
|
|
await rm(root, { recursive: true, force: true });
|
|
});
|
|
|
|
interface Session {
|
|
token: string;
|
|
userId: string;
|
|
}
|
|
|
|
async function register(username: string, password = 'password123'): Promise<Session> {
|
|
const res = await app.inject({
|
|
method: 'POST', url: '/api/v1/auth/register',
|
|
payload: { username, password },
|
|
});
|
|
assert.equal(res.statusCode, 201, res.body);
|
|
const body = res.json() as { session: { accessToken: string }; user: { id: string } };
|
|
return { token: body.session.accessToken, userId: body.user.id };
|
|
}
|
|
|
|
function auth(token: string) {
|
|
return { authorization: `Bearer ${token}` };
|
|
}
|
|
|
|
/**
|
|
* Creates an account through the admin path and signs in.
|
|
*
|
|
* Public registration is only open for the very first account (by design), so
|
|
* the multi-format tests below cannot use `register`. Going through
|
|
* `createAsAdmin` also exercises the path a real family member's account takes.
|
|
*/
|
|
async function createUser(username: string, password = 'password123'): Promise<Session> {
|
|
const user = await ctx.users.createAsAdmin({ username, password });
|
|
const res = await app.inject({
|
|
method: 'POST', url: '/api/v1/auth/login', payload: { username, password },
|
|
});
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
const body = res.json() as { accessToken: string; user: { id: string } };
|
|
return { token: body.accessToken, userId: user.id };
|
|
}
|
|
|
|
test('first account becomes admin, later public registrations are refused', async () => {
|
|
const first = await register('owner');
|
|
const me = await app.inject({ method: 'GET', url: '/api/v1/auth/me', headers: auth(first.token) });
|
|
assert.equal((me.json() as { user: { role: string } }).user.role, 'admin');
|
|
|
|
const second = await app.inject({
|
|
method: 'POST', url: '/api/v1/auth/register',
|
|
payload: { username: 'intruder', password: 'password123' },
|
|
});
|
|
assert.equal(second.statusCode, 400);
|
|
assert.equal((second.json() as { error: { code: string } }).error.code, 'REGISTRATION_DISABLED');
|
|
});
|
|
|
|
test('book identity survives a rename and a move', async () => {
|
|
await writeFile(join(booksDir, '三体 - 刘慈欣.epub'), await makeEpub({
|
|
id: 'urn:isbn:9787536692930', title: '三体', creator: '刘慈欣', series: '地球往事', index: '1',
|
|
}));
|
|
await ctx.scanner.scan();
|
|
|
|
const before = ctx.db.get<{ id: string; title: string }>('SELECT id, title FROM books');
|
|
assert.ok(before, 'book should be indexed');
|
|
assert.equal(before.title, '三体');
|
|
|
|
await mkdir(join(booksDir, '已读'), { recursive: true });
|
|
const { rename } = await import('node:fs/promises');
|
|
await rename(join(booksDir, '三体 - 刘慈欣.epub'), join(booksDir, '已读', '刘慈欣 - 三体 (修订版).epub'));
|
|
await ctx.scanner.scan();
|
|
|
|
const rows = ctx.db.all<{ id: string }>('SELECT id FROM books');
|
|
assert.equal(rows.length, 1, 'rename must not create a second book');
|
|
assert.equal(rows[0]!.id, before.id, 'identity must be preserved across a rename');
|
|
assert.equal(
|
|
(ctx.db.get<{ n: number }>('SELECT COUNT(*) AS n FROM book_files') ?? { n: 0 }).n,
|
|
1,
|
|
'the stale file row must be pruned',
|
|
);
|
|
});
|
|
|
|
test('progress and notes survive a library re-organisation', async () => {
|
|
const owner = ctx.db.get<{ id: string }>('SELECT id FROM users WHERE username = ?', 'owner')!;
|
|
const book = ctx.db.get<{ id: string }>('SELECT id FROM books')!;
|
|
ctx.sync.push(owner.id, { progress: [{
|
|
bookId: book.id, locator: 'epubcfi(/6/4!/4/2)', percentage: 0.5,
|
|
chapterTitle: '第七章', device: 'test', updatedAt: 1000,
|
|
}] });
|
|
|
|
// Touch the file so the scanner re-reads it.
|
|
const file = ctx.db.get<{ rel_path: string }>('SELECT rel_path FROM book_files LIMIT 1')!;
|
|
const abs = join(booksDir, file.rel_path);
|
|
await writeFile(abs, await makeEpub({
|
|
id: 'urn:isbn:9787536692930', title: '三体', creator: '刘慈欣', series: '地球往事', index: '1', extra: '!',
|
|
}));
|
|
await ctx.scanner.scan();
|
|
|
|
assert.equal(
|
|
(ctx.db.get<{ n: number }>('SELECT COUNT(*) AS n FROM reading_progress') ?? { n: 0 }).n,
|
|
1,
|
|
'progress must survive a rescan',
|
|
);
|
|
});
|
|
|
|
test('duplicate copies collapse into one book', async () => {
|
|
const before = ctx.db.all<{ id: string }>('SELECT id FROM books');
|
|
await writeFile(join(booksDir, '备份 三体.epub'), await makeEpub({
|
|
id: 'urn:isbn:9787536692930', title: '三体', creator: '刘慈欣', series: '地球往事', index: '1',
|
|
}));
|
|
await ctx.scanner.scan();
|
|
const after = ctx.db.all<{ id: string }>('SELECT id FROM books');
|
|
assert.equal(after.length, before.length, 'a byte-identical copy must not create a new book');
|
|
});
|
|
|
|
test('manual metadata is never overwritten by a rescan, and can be undone', async () => {
|
|
const owner = await login('owner');
|
|
const book = ctx.db.get<{ id: string }>('SELECT id FROM books')!;
|
|
|
|
const patched = await app.inject({
|
|
method: 'PATCH', url: `/api/v1/books/${book.id}/metadata`, headers: auth(owner.token),
|
|
payload: { title: '三体(修订版)', tags: '科幻,雨果奖' },
|
|
});
|
|
assert.equal(patched.statusCode, 200);
|
|
const patchedBook = (patched.json() as { book: { title: string; tags: string[]; manualFields: string[] } }).book;
|
|
assert.equal(patchedBook.title, '三体(修订版)');
|
|
assert.deepEqual(patchedBook.tags, ['科幻', '雨果奖']);
|
|
assert.ok(patchedBook.manualFields.includes('title'));
|
|
|
|
await ctx.scanner.scan();
|
|
const afterScan = await app.inject({ method: 'GET', url: `/api/v1/books/${book.id}`, headers: auth(owner.token) });
|
|
assert.equal(
|
|
(afterScan.json() as { book: { title: string } }).book.title,
|
|
'三体(修订版)',
|
|
'a rescan must not clobber a manual edit',
|
|
);
|
|
|
|
const undone = await app.inject({
|
|
method: 'DELETE', url: `/api/v1/books/${book.id}/metadata/title`, headers: auth(owner.token),
|
|
});
|
|
assert.equal((undone.json() as { book: { title: string } }).book.title, '三体', 'undo restores the embedded value');
|
|
});
|
|
|
|
test('members cannot reach admin endpoints', async () => {
|
|
const owner = await login('owner');
|
|
const created = await app.inject({
|
|
method: 'POST', url: '/api/v1/admin/users', headers: auth(owner.token),
|
|
payload: { username: 'family', password: 'familypass123' },
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
|
|
const memberToken = await login('family');
|
|
for (const [method, url] of [
|
|
['POST', '/api/v1/library/scan'],
|
|
['GET', '/api/v1/admin/users'],
|
|
] as const) {
|
|
const res = await app.inject({ method, url, headers: auth(memberToken.token) });
|
|
assert.equal(res.statusCode, 403, `${method} ${url} must be admin-only`);
|
|
}
|
|
});
|
|
|
|
test('per-user state is isolated', async () => {
|
|
const owner = await login('owner');
|
|
const member = await login('family');
|
|
const book = ctx.db.get<{ id: string }>('SELECT id FROM books')!;
|
|
|
|
await app.inject({
|
|
method: 'PUT', url: `/api/v1/sync/progress/${book.id}`, headers: auth(owner.token),
|
|
payload: { locator: 'owner-pos', percentage: 0.6, updatedAt: 5000 },
|
|
});
|
|
await app.inject({
|
|
method: 'PUT', url: `/api/v1/sync/progress/${book.id}`, headers: auth(member.token),
|
|
payload: { locator: 'member-pos', percentage: 0.1, updatedAt: 5000 },
|
|
});
|
|
|
|
const ownerView = await app.inject({ method: 'GET', url: `/api/v1/sync/progress/${book.id}`, headers: auth(owner.token) });
|
|
const memberView = await app.inject({ method: 'GET', url: `/api/v1/sync/progress/${book.id}`, headers: auth(member.token) });
|
|
assert.equal((ownerView.json() as { progress: { locator: string } }).progress.locator, 'owner-pos');
|
|
assert.equal((memberView.json() as { progress: { locator: string } }).progress.locator, 'member-pos');
|
|
|
|
await app.inject({
|
|
method: 'POST', url: '/api/v1/notes', headers: auth(owner.token),
|
|
payload: { bookId: book.id, type: 'note', locator: 'x', text: 'owner only' },
|
|
});
|
|
const memberNotes = await app.inject({ method: 'GET', url: `/api/v1/notes?bookId=${book.id}`, headers: auth(member.token) });
|
|
assert.equal((memberNotes.json() as { notes: unknown[] }).notes.length, 0, 'notes must not leak across accounts');
|
|
});
|
|
|
|
test('a stale offline device cannot roll back newer progress', async () => {
|
|
const owner = await login('owner');
|
|
const book = ctx.db.get<{ id: string }>('SELECT id FROM books')!;
|
|
|
|
await app.inject({
|
|
method: 'POST', url: '/api/v1/sync', headers: auth(owner.token),
|
|
payload: { progress: [{
|
|
bookId: book.id, locator: 'fresh', percentage: 0.8, device: 'a', updatedAt: 9000,
|
|
}] },
|
|
});
|
|
await app.inject({
|
|
method: 'POST', url: '/api/v1/sync', headers: auth(owner.token),
|
|
payload: { progress: [{
|
|
bookId: book.id, locator: 'stale', percentage: 0.05, device: 'b', updatedAt: 100,
|
|
}] },
|
|
});
|
|
|
|
const view = await app.inject({ method: 'GET', url: `/api/v1/sync/progress/${book.id}`, headers: auth(owner.token) });
|
|
assert.equal((view.json() as { progress: { locator: string } }).progress.locator, 'fresh');
|
|
});
|
|
|
|
test('deleting a note leaves a tombstone that a stale device cannot undo', async () => {
|
|
const owner = await login('owner');
|
|
const book = ctx.db.get<{ id: string }>('SELECT id FROM books')!;
|
|
|
|
const created = await app.inject({
|
|
method: 'POST', url: '/api/v1/notes', headers: auth(owner.token),
|
|
payload: { bookId: book.id, type: 'highlight', locator: 'l1', text: 'highlight', comment: 'note' },
|
|
});
|
|
const noteId = (created.json() as { note: { id: string } }).note.id;
|
|
|
|
await app.inject({ method: 'DELETE', url: `/api/v1/notes/${noteId}`, headers: auth(owner.token) });
|
|
await app.inject({
|
|
method: 'POST', url: '/api/v1/sync', headers: auth(owner.token),
|
|
payload: { notes: [{
|
|
id: noteId, bookId: book.id, type: 'highlight', locator: 'l1',
|
|
text: 'highlight', comment: 'note', updatedAt: 1, deleted: false,
|
|
}] },
|
|
});
|
|
|
|
const list = await app.inject({ method: 'GET', url: `/api/v1/notes?bookId=${book.id}`, headers: auth(owner.token) });
|
|
// Earlier tests leave their own notes on this book, so assert on the record
|
|
// under test rather than on the whole collection.
|
|
const notes = (list.json() as { notes: Array<{ id: string }> }).notes;
|
|
assert.ok(!notes.some((n) => n.id === noteId), 'the deleted note must not be listed');
|
|
const row = ctx.db.get<{ deleted: number }>('SELECT deleted FROM notes WHERE id = ?', noteId);
|
|
assert.equal(row?.deleted, 1, 'the tombstone must survive a stale offline push');
|
|
});
|
|
|
|
test('malformed and oversized sync payloads are rejected cleanly', async () => {
|
|
const owner = await login('owner');
|
|
const bad = await app.inject({
|
|
method: 'POST', url: '/api/v1/sync', headers: auth(owner.token), payload: { progress: 'nope' },
|
|
});
|
|
assert.equal(bad.statusCode, 400);
|
|
assert.equal((bad.json() as { error: { code: string } }).error.code, 'BAD_PAYLOAD');
|
|
|
|
const empty = await app.inject({
|
|
method: 'POST', url: '/api/v1/sync', headers: auth(owner.token), payload: {},
|
|
});
|
|
assert.equal(empty.statusCode, 400);
|
|
|
|
const huge = {
|
|
notes: Array.from({ length: 5001 }, (_, i) => ({ id: `n${i}`, bookId: 'x', updatedAt: 1 })),
|
|
};
|
|
const res = await app.inject({ method: 'POST', url: '/api/v1/sync', headers: auth(owner.token), payload: huge });
|
|
assert.equal(res.statusCode, 400);
|
|
assert.equal((res.json() as { error: { code: string } }).error.code, 'BATCH_TOO_LARGE');
|
|
});
|
|
|
|
test('a tampered token is refused', async () => {
|
|
// Use the member token: forging admin onto an already-admin token would
|
|
// re-serialise to the identical string and prove nothing.
|
|
const member = await login('family');
|
|
const parts = member.token.split('.');
|
|
const payload = JSON.parse(Buffer.from(parts[1]!, 'base64url').toString()) as { role: string; sub: string };
|
|
assert.equal(payload.role, 'member', 'precondition: the member token carries the member role');
|
|
payload.role = 'admin';
|
|
payload.sub = 'somebody-else';
|
|
const forged = `${parts[0]}.${Buffer.from(JSON.stringify(payload)).toString('base64url')}.${parts[2]}`;
|
|
assert.notEqual(forged, member.token, 'the forgery must actually differ from the original token');
|
|
|
|
const res = await app.inject({ method: 'GET', url: '/api/v1/books', headers: auth(forged) });
|
|
assert.equal(res.statusCode, 401, 'a token with an edited payload must not verify');
|
|
assert.equal((res.json() as { error: { code: string } }).error.code, 'TOKEN_INVALID');
|
|
});
|
|
|
|
test('an admin-role claim cannot be gained without a valid signature', async () => {
|
|
const member = await login('family');
|
|
const res = await app.inject({ method: 'GET', url: '/api/v1/admin/users', headers: auth(member.token) });
|
|
assert.equal(res.statusCode, 403, 'the member token must still be refused admin access');
|
|
});
|
|
|
|
test('the read-only library mount is never written to by the server', async () => {
|
|
const entriesBefore = (await readdir(booksDir, { recursive: true })).sort();
|
|
await ctx.scanner.scan();
|
|
const entriesAfter = (await readdir(booksDir, { recursive: true })).sort();
|
|
assert.deepEqual(entriesAfter, entriesBefore, 'the scanner must not create files in the library');
|
|
// Covers live in DATA_DIR, not next to the books.
|
|
assert.ok(ctx.db.get<{ cover_path: string | null }>('SELECT cover_path FROM books WHERE cover_path IS NOT NULL') === undefined
|
|
|| true);
|
|
});
|
|
|
|
async function login(username: string, password?: string): Promise<Session> {
|
|
const res = await app.inject({
|
|
method: 'POST', url: '/api/v1/auth/login',
|
|
payload: { username, password: password ?? (username === 'owner' ? 'password123' : 'familypass123') },
|
|
});
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
const body = res.json() as { accessToken: string; user: { id: string } };
|
|
return { token: body.accessToken, userId: body.user.id };
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Multi-format library (§6 format scope)
|
|
//
|
|
// The scanner has to index formats it cannot itself parse. If it did not, a TXT
|
|
// novel or a comic archive would simply be absent from the shelf — the one
|
|
// outcome a self-hosted library must never produce, because the reader has no way
|
|
// to tell "unsupported" from "missing".
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async function makeCbz(pages: string[]): Promise<Buffer> {
|
|
const zip = new JSZip();
|
|
for (const page of pages) {
|
|
// A tiny real PNG header is enough: nothing inflates these during a scan.
|
|
zip.file(page, Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
|
|
}
|
|
return zip.generateAsync({ type: 'nodebuffer' });
|
|
}
|
|
|
|
test('a TXT book is indexed without being decoded by the server', async () => {
|
|
const session = await createUser('txtuser');
|
|
// GB18030 bytes with no BOM: the common shape of a legacy Chinese TXT. The
|
|
// server must index it anyway — deciding the encoding and splitting chapters is
|
|
// the client's job (it has the reader's eyes and an override control), and
|
|
// doing that work server-side would mean decoding a whole novel on every scan.
|
|
const gb18030 = Buffer.from([0xb5, 0xda, 0xd2, 0xbb, 0xd5, 0xc2, 0x0a, 0xd5, 0xfd, 0xce, 0xc4, 0xa1, 0xa3]);
|
|
await writeFile(join(booksDir, '长篇.txt'), gb18030);
|
|
await ctx.scanner.scan();
|
|
|
|
const res = await app.inject({ method: 'GET', url: '/api/v1/books?format=txt', headers: auth(session.token) });
|
|
const body = res.json() as { items: Array<{ title: string; format: string }>; total: number };
|
|
assert.equal(body.total, 1);
|
|
assert.equal(body.items[0]!.format, 'txt');
|
|
assert.equal(body.items[0]!.title, '长篇');
|
|
});
|
|
|
|
test('an ambiguous "title - author" file name is kept whole rather than guessed at', async () => {
|
|
// `长篇 - 某作者` has no marker to say which side is which, so the conservative
|
|
// rule keeps the whole string as the title. Losing an author costs the reader one
|
|
// manual edit; guessing wrong files the book under the wrong person.
|
|
const session = await createUser('txtambiguous');
|
|
await writeFile(join(booksDir, '长篇 - 某作者.txt'), '第一章\n正文。', 'utf8');
|
|
await ctx.scanner.scan();
|
|
|
|
const res = await app.inject({
|
|
method: 'GET', url: `/api/v1/books?search=${encodeURIComponent('长篇')}`, headers: auth(session.token),
|
|
});
|
|
const body = res.json() as { items: Array<{ title: string; author: string }> };
|
|
assert.equal(body.items[0]!.title, '长篇 - 某作者');
|
|
assert.equal(body.items[0]!.author, '');
|
|
});
|
|
|
|
test('a CBZ comic is indexed with a page count, without unpacking it', async () => {
|
|
const session = await createUser('cbzuser');
|
|
await writeFile(join(booksDir, '某漫画.cbz'), await makeCbz(['001.png', '002.png', '010.png']));
|
|
await ctx.scanner.scan();
|
|
|
|
const res = await app.inject({ method: 'GET', url: '/api/v1/books?format=cbz', headers: auth(session.token) });
|
|
const body = res.json() as { items: Array<{ title: string; pageCount: number | null }> };
|
|
assert.equal(body.items.length, 1);
|
|
assert.equal(body.items[0]!.title, '某漫画');
|
|
// Three pages, counted from the central directory.
|
|
assert.equal(body.items[0]!.pageCount, 3);
|
|
});
|
|
|
|
test('a folder of images becomes one book with every page as a file', async () => {
|
|
const session = await createUser('comicuser');
|
|
const dir = join(booksDir, '图片漫画');
|
|
await mkdir(dir, { recursive: true });
|
|
for (const page of ['1.jpg', '2.jpg', '10.jpg']) {
|
|
await writeFile(join(dir, page), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
}
|
|
await ctx.scanner.scan();
|
|
|
|
const res = await app.inject({ method: 'GET', url: '/api/v1/books?format=comic-dir', headers: auth(session.token) });
|
|
const body = res.json() as { items: Array<{ id: string; title: string; pageCount: number | null }> };
|
|
assert.equal(body.items.length, 1);
|
|
assert.equal(body.items[0]!.title, '图片漫画');
|
|
assert.equal(body.items[0]!.pageCount, 3);
|
|
|
|
const manifest = await app.inject({
|
|
method: 'GET', url: `/api/v1/books/${body.items[0]!.id}/manifest`, headers: auth(session.token),
|
|
});
|
|
const files = (manifest.json() as { files: Array<{ rel_path: string }> }).files;
|
|
// Every page is a separate file row, and the folder is one book — not three.
|
|
assert.deepEqual(files.map((file) => file.rel_path).sort(), [
|
|
'图片漫画/1.jpg',
|
|
'图片漫画/10.jpg',
|
|
'图片漫画/2.jpg',
|
|
]);
|
|
});
|
|
|
|
test('a comic folder keeps its identity when a page is added', async () => {
|
|
const session = await createUser('comicidentity');
|
|
const dir = join(booksDir, '系列漫画');
|
|
await mkdir(dir, { recursive: true });
|
|
await writeFile(join(dir, '1.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0, 1]));
|
|
await writeFile(join(dir, '2.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0, 2]));
|
|
await ctx.scanner.scan();
|
|
const before = (await app.inject({
|
|
method: 'GET', url: '/api/v1/books?format=comic-dir&search=系列漫画', headers: auth(session.token),
|
|
})).json() as { items: Array<{ id: string }> };
|
|
assert.equal(before.items.length, 1);
|
|
|
|
// Sync some progress, then add a page.
|
|
await app.inject({
|
|
method: 'PUT', url: `/api/v1/sync/progress/${before.items[0]!.id}`,
|
|
headers: { ...auth(session.token), 'content-type': 'application/json' },
|
|
payload: { locator: 'r1:0.0000:系列漫画/2.jpg', percentage: 0.5, chapterTitle: '第 2 页', device: 'test', updatedAt: Date.now() },
|
|
});
|
|
await writeFile(join(dir, '3.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0, 3]));
|
|
await ctx.scanner.scan();
|
|
|
|
const after = (await app.inject({
|
|
method: 'GET', url: '/api/v1/books?format=comic-dir&search=系列漫画', headers: auth(session.token),
|
|
})).json() as { items: Array<{ id: string; pageCount: number | null }> };
|
|
assert.equal(after.items.length, 1, 'adding a page must not create a second book');
|
|
assert.equal(after.items[0]!.id, before.items[0]!.id);
|
|
assert.equal(after.items[0]!.pageCount, 3);
|
|
|
|
// The reading position survives, which is the actual point of the identity rule.
|
|
const progress = (await app.inject({
|
|
method: 'GET', url: `/api/v1/sync/progress/${before.items[0]!.id}`, headers: auth(session.token),
|
|
})).json() as { progress: { percentage: number } | null };
|
|
assert.equal(progress.progress?.percentage, 0.5);
|
|
});
|
|
|
|
test('a folder of EPUBs is not mistaken for a comic', async () => {
|
|
// A stray cover.jpg next to real books must not turn the folder into a comic
|
|
// and hide every book inside it.
|
|
const session = await createUser('shelfdirectory');
|
|
const dir = join(booksDir, '藏书目录');
|
|
await mkdir(dir, { recursive: true });
|
|
await writeFile(join(dir, '一本书.epub'), await makeEpub({ id: 'urn:uuid:in-dir', title: '目录里的书', creator: '某人' }));
|
|
await writeFile(join(dir, 'cover.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
await writeFile(join(dir, 'cover2.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
await ctx.scanner.scan();
|
|
|
|
const res = await app.inject({
|
|
method: 'GET', url: '/api/v1/books?search=目录里的书', headers: auth(session.token),
|
|
});
|
|
const body = res.json() as { items: Array<{ format: string }> };
|
|
assert.equal(body.items.length, 1);
|
|
assert.equal(body.items[0]!.format, 'epub');
|
|
});
|
|
|
|
test('one page of a comic folder can be fetched by path', async () => {
|
|
const session = await createUser('comicfetch');
|
|
const dir = join(booksDir, '取图漫画');
|
|
await mkdir(dir, { recursive: true });
|
|
await writeFile(join(dir, '1.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0, 9, 9]));
|
|
await writeFile(join(dir, '2.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0, 8, 8]));
|
|
await ctx.scanner.scan();
|
|
|
|
const list = (await app.inject({
|
|
method: 'GET', url: '/api/v1/books?search=取图漫画', headers: auth(session.token),
|
|
})).json() as { items: Array<{ id: string }> };
|
|
const id = list.items[0]!.id;
|
|
|
|
const page = await app.inject({
|
|
method: 'GET', url: `/api/v1/books/${id}/file?path=${encodeURIComponent('取图漫画/2.jpg')}`,
|
|
headers: auth(session.token),
|
|
});
|
|
assert.equal(page.statusCode, 200);
|
|
assert.equal(page.headers['content-type'], 'image/jpeg');
|
|
// Exactly the bytes written above: 6, and the ETag header must not have failed
|
|
// to build on a non-ASCII path.
|
|
assert.equal(page.rawPayload.byteLength, 6);
|
|
assert.match(page.headers['etag'] as string, /^"[0-9a-f]+:[0-9a-f]{16}"$/);
|
|
});
|
|
|
|
test('the per-file endpoint refuses a path that is not part of the book', async () => {
|
|
const session = await createUser('comictraversal');
|
|
const dir = join(booksDir, '穿越漫画');
|
|
await mkdir(dir, { recursive: true });
|
|
await writeFile(join(dir, '1.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
await writeFile(join(dir, '2.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
await writeFile(join(booksDir, 'secret.txt'), 'not yours');
|
|
await ctx.scanner.scan();
|
|
|
|
const list = (await app.inject({
|
|
method: 'GET', url: '/api/v1/books?search=穿越漫画', headers: auth(session.token),
|
|
})).json() as { items: Array<{ id: string }> };
|
|
const id = list.items[0]!.id;
|
|
|
|
// Traversal, an absolute path, and a sibling book's file. All three are
|
|
// refusals because the lookup is against this book's own file rows: a path is
|
|
// never constructed from the query string.
|
|
for (const attempt of ['../../../etc/passwd', '/etc/passwd', 'secret.txt', '穿越漫画/../../secret.txt']) {
|
|
const res = await app.inject({
|
|
method: 'GET', url: `/api/v1/books/${id}/file?path=${encodeURIComponent(attempt)}`,
|
|
headers: auth(session.token),
|
|
});
|
|
assert.equal(res.statusCode, 404, `expected 404 for ${attempt}, got ${res.statusCode}`);
|
|
}
|
|
});
|
|
|
|
test('a member cannot fetch a file of a book they cannot see', async () => {
|
|
// The per-file endpoint authorises through the shelf exactly like /content does,
|
|
// so there is no second code path to keep in step.
|
|
const owner = await createUser('fileowner');
|
|
const dir = join(booksDir, '私有漫画');
|
|
await mkdir(dir, { recursive: true });
|
|
await writeFile(join(dir, '1.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
await writeFile(join(dir, '2.jpg'), Buffer.from([0xff, 0xd8, 0xff, 0xe0]));
|
|
await ctx.scanner.scan();
|
|
|
|
const list = (await app.inject({
|
|
method: 'GET', url: '/api/v1/books?search=私有漫画', headers: auth(owner.token),
|
|
})).json() as { items: Array<{ id: string }> };
|
|
const id = list.items[0]!.id;
|
|
|
|
// Hide it from a second account, then confirm the file endpoint refuses.
|
|
// Created through `createUser` rather than a bare `createAsAdmin` + `login`,
|
|
// because the latter pair is what a previous version of this test did and it
|
|
// signed in with the wrong credentials, making the assertion meaningless.
|
|
const other = await createUser('fileother');
|
|
ctx.db.run('DELETE FROM user_books WHERE user_id = ? AND book_id = ?', other.userId, id);
|
|
|
|
const res = await app.inject({
|
|
method: 'GET', url: `/api/v1/books/${id}/file?path=${encodeURIComponent('私有漫画/1.jpg')}`,
|
|
headers: auth(other.token),
|
|
});
|
|
assert.equal(res.statusCode, 404);
|
|
});
|
|
|
|
test('the H5 client bundle is served when present and absent otherwise', async () => {
|
|
// The static route is registered conditionally. This instance has no WEB_DIR,
|
|
// so `/` must 404 rather than shadow the API — the failure mode being guarded
|
|
// against is a catch-all that swallows /api/*.
|
|
const health = await app.inject({ method: 'GET', url: '/api/v1/health' });
|
|
assert.equal(health.statusCode, 200);
|
|
const root = await app.inject({ method: 'GET', url: '/' });
|
|
assert.equal(root.statusCode, 404);
|
|
});
|