@gongfpp/dsh-novel-fish 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/README.md +47 -35
- package/docs/evidence/reading-box-121.png +0 -0
- package/docs/evidence/settings-121.png +0 -0
- package/index.json +24 -0
- package/lib/client.js +924 -323
- package/locale/en.json +22 -10
- package/locale/zh.json +22 -10
- package/package.json +5 -7
- package/samples/ebooks/{aq-zheng-zhuan-wikisource.md → aq-zheng-zhuan.md} +9 -360
- package/samples/ebooks/{million-pound-bank-note-pg61522.txt → million-pound-bank-note.txt} +6476 -6970
- package/samples/ebooks/sources.json +13 -49
- package/screenshots.json +6 -0
- package/docs/acceptance.md +0 -42
- package/docs/community-markets.md +0 -88
- package/docs/compatibility-017.md +0 -70
- package/docs/compatibility-101.md +0 -41
- package/docs/continuous-reading-020.md +0 -46
- package/docs/ebook-tests.md +0 -99
- package/docs/evidence/abc-result.json +0 -9
- package/docs/evidence/banner-100.png +0 -0
- package/docs/evidence/community-market-014-result.json +0 -170
- package/docs/evidence/compatibility-017-result.json +0 -65
- package/docs/evidence/continuous-reading-020-result.json +0 -72
- package/docs/evidence/formats-result.json +0 -12
- package/docs/evidence/live-ab.png +0 -0
- package/docs/evidence/live-collapsed.png +0 -0
- package/docs/evidence/live-disabled.png +0 -0
- package/docs/evidence/live-epub.png +0 -0
- package/docs/evidence/live-market.png +0 -0
- package/docs/evidence/live-resume.png +0 -0
- package/docs/evidence/live-unloaded.png +0 -0
- package/docs/evidence/long-result.json +0 -11
- package/docs/evidence/multi-region-014-result.json +0 -47
- package/docs/evidence/native-disclosure-013-result.json +0 -36
- package/docs/evidence/native-disclosures-013.png +0 -0
- package/docs/evidence/native-open-013.png +0 -0
- package/docs/evidence/native-settings-sample.md +0 -11
- package/docs/evidence/native-settings-types-015.png +0 -0
- package/docs/evidence/npm-market-014-result.json +0 -132
- package/docs/evidence/plugin-settings-details.png +0 -0
- package/docs/evidence/public-release-014-result.json +0 -51
- package/docs/evidence/public-release-015-result.json +0 -36
- package/docs/evidence/public-release-result.json +0 -48
- package/docs/evidence/reading-demo-100.png +0 -0
- package/docs/evidence/reading-demo-110.png +0 -0
- package/docs/evidence/reading-two-expansions-110.gif +0 -0
- package/docs/evidence/reading-two-expansions-110.json +0 -89
- package/docs/evidence/samples-110-capture.json +0 -49
- package/docs/evidence/settings-011-result.json +0 -28
- package/docs/evidence/settings-011.png +0 -0
- package/docs/evidence/settings-100.png +0 -0
- package/docs/evidence/settings-110.png +0 -0
- package/docs/evidence/settings-chat-011.png +0 -0
- package/docs/evidence/type-append-012-result.json +0 -30
- package/docs/evidence/type-append-012.png +0 -0
- package/docs/evidence/type-settings-012.png +0 -0
- package/docs/evidence/unload-result.json +0 -7
- package/docs/host-adapter.md +0 -43
- package/docs/icon-metadata-021.md +0 -40
- package/docs/interaction-paths-020.md +0 -39
- package/docs/marketplace.md +0 -42
- package/docs/multi-region-update.md +0 -53
- package/docs/native-disclosure-update.md +0 -39
- package/docs/native-settings-update.md +0 -48
- package/docs/plugin-settings-update.md +0 -61
- package/docs/release.md +0 -151
- package/docs/settings-update.md +0 -49
- package/docs/type-append-update.md +0 -50
- package/pnpm-workspace.yaml +0 -5
- package/samples/ebooks/LICENSE.md +0 -355
- package/samples/ebooks/NOTICE.md +0 -33
- package/scripts/build.mjs +0 -70
- package/scripts/check-host-contracts.mjs +0 -247
- package/scripts/licenses/path-webpack.txt +0 -28
- package/scripts/make-readme-gif.mjs +0 -69
- package/scripts/test-browser-samples.mjs +0 -665
- package/scripts/test-candidate-books.mjs +0 -71
- package/scripts/test-ebook-corpus.mjs +0 -161
- package/src/bundled-books.js +0 -42
- package/src/chat-adapter.js +0 -278
- package/src/client.js +0 -61
- package/src/epub.js +0 -166
- package/src/novel-content.js +0 -28
- package/src/persistence.js +0 -271
- package/src/pg-notice.js +0 -19
- package/src/segments.js +0 -185
- package/src/settings.js +0 -243
- package/src/store.js +0 -429
- package/src/text-decoding.js +0 -28
- package/test/bundled-library.test.js +0 -526
- package/test/chat-adapter.test.js +0 -518
- package/test/client.test.js +0 -124
- package/test/ebooks.test.js +0 -192
- package/test/epub.test.js +0 -185
- package/test/helpers/host-primitives.js +0 -28
- package/test/helpers/memory-storage.js +0 -48
- package/test/host-contracts.test.js +0 -32
- package/test/import.test.js +0 -102
- package/test/persistence.test.js +0 -27
- package/test/pg-notice.test.js +0 -109
- package/test/release.test.js +0 -87
- package/test/segments.test.js +0 -182
- package/test/settings.test.js +0 -312
- package/test/store.test.js +0 -617
- package/test/text-decoding.test.js +0 -49
package/test/ebooks.test.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { readFile } from 'node:fs/promises'
|
|
4
|
-
import { createHash } from 'node:crypto'
|
|
5
|
-
import { NovelStore } from '../src/store.js'
|
|
6
|
-
import { MemoryStorage } from './helpers/memory-storage.js'
|
|
7
|
-
|
|
8
|
-
// Ordinary tests use only committed samples: no dist, temporary downloads or network.
|
|
9
|
-
const samples = new URL('../samples/ebooks/', import.meta.url)
|
|
10
|
-
const { books } = JSON.parse(await readFile(new URL('sources.json', samples), 'utf8'))
|
|
11
|
-
const pinned = {
|
|
12
|
-
'million-pound-bank-note': { file: 'million-pound-bank-note-pg61522.txt', format: 'txt', bytes: 394538, sha256: '4f36f8ce3837afd6c28a23684153a946e39c8970a27486d36f6f4c64b92bb0d2' },
|
|
13
|
-
'aq-zheng-zhuan': { file: 'aq-zheng-zhuan-wikisource.md', format: 'md', bytes: 68170, sha256: '228f5e9504caecc97460e815461df7ac60de80ea311f289f7de23760c69c9643' },
|
|
14
|
-
}
|
|
15
|
-
const aqChapters = ['第一章 序', '第二章 優勝記略', '第三章 續優勝記略', '第四章 戀愛的悲劇', '第五章 生計問題', '第六章 從中興到末路', '第七章 革命', '第八章 不准革命', '第九章 大團圓']
|
|
16
|
-
const otherStories = ['MENTAL TELEGRAPHY', 'A CURE FOR THE BLUES', 'THE ENEMY CONQUERED; OR, LOVE TRIUMPHANT', 'ABOUT ALL KINDS OF SHIPS', 'PLAYING COURIER', 'THE GERMAN CHICAGO', 'A PETITION TO THE QUEEN OF ENGLAND', 'A MAJESTIC LITERARY FOSSIL']
|
|
17
|
-
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex')
|
|
18
|
-
const compact = text => text.replace(/\s+/gu, '')
|
|
19
|
-
const asFile = (name, bytes) => ({ name, arrayBuffer: async () => Uint8Array.from(bytes).buffer })
|
|
20
|
-
const byId = id => books.find(book => book.id === id)
|
|
21
|
-
const segmentsFor = (storage, book) => Promise.all(Array.from({ length: book.segmentCount }, (_, index) => storage.getSegment(book.id, index)))
|
|
22
|
-
const storedBody = async (storage, book) => (await segmentsFor(storage, book)).map(segment => segment.text).join('\n')
|
|
23
|
-
|
|
24
|
-
function assertReadableBody(id, body) {
|
|
25
|
-
assert.doesNotMatch(body, /\uFFFD/u)
|
|
26
|
-
const text = compact(body)
|
|
27
|
-
if (id === 'million-pound-bank-note') {
|
|
28
|
-
assert.ok(text.includes(compact('When I was twenty-seven years old, I was a mining-broker’s clerk in San Francisco')), 'English narrative, not just PG wrapper')
|
|
29
|
-
assert.ok(text.includes(compact('My Portia’s papa took that friendly and hospitable bill back to the Bank of England and cashed it')), 'title story reaches its ending')
|
|
30
|
-
for (const title of otherStories) assert.ok(text.includes(compact(`_${title}_`)), title)
|
|
31
|
-
} else {
|
|
32
|
-
assert.equal(id, 'aq-zheng-zhuan')
|
|
33
|
-
assert.ok(text.includes('我要給阿Q做正傳,已經不止一兩年了。'))
|
|
34
|
-
assert.ok(text.includes('他們白跟一趟了。'), 'AQ final paragraph survived import')
|
|
35
|
-
assert.ok(text.includes('(一九二一年十二月。)'))
|
|
36
|
-
assert.match(body, /About this digital edition/u)
|
|
37
|
-
assert.match(body, /The following users contributed to this book:/u)
|
|
38
|
-
assert.match(body, /Dbenbenn/u)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function seededStorage() {
|
|
43
|
-
return new MemoryStorage({
|
|
44
|
-
books: [{ id: 'existing', title: '已有的书', format: 'txt', progress: 1, segmentCount: 3, importedAt: 1 }],
|
|
45
|
-
settings: { currentBookId: 'existing', mainTitle: '原来的标题' },
|
|
46
|
-
passages: { existing: [{ text: '已经读过。' }, { text: '保留现有阅读位置。' }, { text: '最后一段。' }] },
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
test('confirmed two-book offline corpus pins original English TXT and complete derived AQ Markdown bytes', async () => {
|
|
51
|
-
assert.deepEqual(books.map(book => book.id), Object.keys(pinned))
|
|
52
|
-
let totalBytes = 0
|
|
53
|
-
for (const source of books) {
|
|
54
|
-
for (const [key, value] of Object.entries(pinned[source.id])) assert.equal(source[key], value, `${source.id}.${key}`)
|
|
55
|
-
const bytes = await readFile(new URL(source.file, samples))
|
|
56
|
-
assert.equal(bytes.length, source.bytes, source.file)
|
|
57
|
-
assert.equal(sha256(bytes), source.sha256, source.file)
|
|
58
|
-
assert.equal(source.redistribution.status, 'permitted-with-conditions')
|
|
59
|
-
assert.equal(source.redistribution.packageByDefault, true)
|
|
60
|
-
assert.match(source.redistribution.conditions, /Retain|Keep/u)
|
|
61
|
-
totalBytes += bytes.length
|
|
62
|
-
}
|
|
63
|
-
assert.equal(totalBytes, 462708)
|
|
64
|
-
assert.ok(totalBytes < 600 * 1024, `Bundled corpus grew to ${totalBytes} bytes`)
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('manifest and combined notices distinguish collection, complete Markdown derivative and digital licenses', async () => {
|
|
68
|
-
const english = byId('million-pound-bank-note')
|
|
69
|
-
const aq = byId('aq-zheng-zhuan')
|
|
70
|
-
assert.equal(english.source, 'https://www.gutenberg.org/ebooks/61522')
|
|
71
|
-
assert.equal(english.redistribution.authorDeathYear, 1910)
|
|
72
|
-
assert.match(english.edition, /Unmodified original download.*collection/u)
|
|
73
|
-
assert.match(english.edition, /full title story and eight other stories; not a standalone/u)
|
|
74
|
-
assert.match(english.redistribution.conditions, /1\.E\.1.*full-license.*Fee-bearing/u)
|
|
75
|
-
assert.equal(aq.redistribution.authorDeathYear, 1936)
|
|
76
|
-
assert.match(aq.download, /^https:\/\/ws-export\.wmcloud\.org\/\?lang=zh&format=txt&page=/u)
|
|
77
|
-
assert.match(aq.edition, /Local Markdown derivative.*complete nine chapters/u)
|
|
78
|
-
assert.match(aq.redistribution.license, /CC BY-SA 3\.0 Unported or GFDL/u)
|
|
79
|
-
const notices = await readFile(new URL('LICENSE.md', samples), 'utf8')
|
|
80
|
-
for (const source of books) {
|
|
81
|
-
assert.ok(notices.includes(source.source), `${source.id} source attribution`)
|
|
82
|
-
assert.ok(notices.includes(source.redistribution.licenseUrl), `${source.id} license access`)
|
|
83
|
-
}
|
|
84
|
-
assert.match(notices, /NOT covered by the plugin's MIT/u)
|
|
85
|
-
assert.match(notices, /NOT a standalone/u)
|
|
86
|
-
assert.doesNotMatch(notices, /山海經|南山經|shanhaijing/u)
|
|
87
|
-
assert.match(notices, /CC BY-SA 3\.0.*GFDL/su)
|
|
88
|
-
const noticeIndex = await readFile(new URL('NOTICE.md', samples), 'utf8')
|
|
89
|
-
assert.match(noticeIndex, /^# 内置两本样书/mu)
|
|
90
|
-
assert.ok(noticeIndex.includes('462708 bytes'))
|
|
91
|
-
for (const source of books) assert.ok(noticeIndex.includes(source.file))
|
|
92
|
-
for (const [, link] of noticeIndex.matchAll(/\]\(<?([^)>]+)>?\)/gu)) {
|
|
93
|
-
if (!/^https?:/u.test(link)) await readFile(new URL(link, samples))
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const original = (await readFile(new URL(english.file, samples), 'utf8')).replace(/\r\n?/gu, '\n')
|
|
97
|
-
assert.match(original, /Credits: Produced by Richard Tonsing/u)
|
|
98
|
-
const licenseStart = original.indexOf('START: FULL LICENSE')
|
|
99
|
-
assert.ok(licenseStart > 0)
|
|
100
|
-
const fullLicense = original.slice(licenseStart).trim()
|
|
101
|
-
assert.match(fullLicense, /1\.E\.1\./u)
|
|
102
|
-
assert.match(fullLicense, /Section 5\. General Information/u)
|
|
103
|
-
assert.ok(notices.replace(/\r\n?/gu, '\n').includes(fullLicense), 'combined LICENSE retains the entire original PG license verbatim')
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
test('official AQ export has all nine ordered chapters, ending, notices and complete credit tail', async () => {
|
|
107
|
-
const original = await readFile(new URL(byId('aq-zheng-zhuan').file, samples), 'utf8')
|
|
108
|
-
assert.deepEqual([...original.matchAll(/^第[一二三四五六七八九]章[\t \u3000]+.+$/gmu)].map(match => match[0].replace(/\s+/gu, ' ')), aqChapters)
|
|
109
|
-
assertReadableBody('aq-zheng-zhuan', original)
|
|
110
|
-
assert.ok(original.indexOf('他們白跟一趟了。') < original.indexOf('About this digital edition'))
|
|
111
|
-
assert.match(original, /Creative Commons Attribution-ShareAlike 3\.0 Unported.*GNU FDL/u)
|
|
112
|
-
assert.match(original, /https:\/\/www\.creativecommons\.org\/licenses\/by-sa\/3\.0/u)
|
|
113
|
-
assert.match(original, /https:\/\/www\.gnu\.org\/copyleft\/fdl\.html/u)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
for (const source of books) {
|
|
117
|
-
test(`real ${source.format.toUpperCase()} ${source.title}: body, selection safety, deduplicated reads and persisted progress`, async () => {
|
|
118
|
-
const storage = seededStorage()
|
|
119
|
-
const original = await storage.load()
|
|
120
|
-
const originalPassages = structuredClone([...storage.passages.entries()])
|
|
121
|
-
const store = new NovelStore(storage)
|
|
122
|
-
await store.ready
|
|
123
|
-
let restored
|
|
124
|
-
try {
|
|
125
|
-
const book = await store.importFile(asFile(source.file, await readFile(new URL(source.file, samples))))
|
|
126
|
-
assert.equal(book.format, source.format)
|
|
127
|
-
assert.ok(book.segmentCount > 1)
|
|
128
|
-
assert.equal(book.progress, 0)
|
|
129
|
-
assertReadableBody(source.id, await storedBody(storage, book))
|
|
130
|
-
if (source.id === 'aq-zheng-zhuan') {
|
|
131
|
-
const segments = await segmentsFor(storage, book)
|
|
132
|
-
assert.equal(segments[0].chapterTitle, '阿Q正傳')
|
|
133
|
-
const body = segments.map(segment => segment.text).join('\n')
|
|
134
|
-
for (const title of aqChapters) assert.ok(compact(body).includes(compact(title)), title)
|
|
135
|
-
}
|
|
136
|
-
assert.equal(store.getSettingsSnapshot().currentBookId, 'existing')
|
|
137
|
-
assert.equal(store.getSettingsSnapshot().mainTitle, '原来的标题')
|
|
138
|
-
assert.deepEqual(storage.books.get('existing'), original.books[0])
|
|
139
|
-
for (const [key, passage] of originalPassages) assert.deepEqual(storage.passages.get(key), passage)
|
|
140
|
-
|
|
141
|
-
await store.selectBook(book.id)
|
|
142
|
-
const [first, duplicate] = await Promise.all([store.claimPassage(`real-${source.id}`), store.claimPassage(`real-${source.id}`)])
|
|
143
|
-
assert.equal(first.kind, 'ready')
|
|
144
|
-
assert.equal(first.index, 0)
|
|
145
|
-
assert.ok(first.passage.text.trim())
|
|
146
|
-
assert.deepEqual(duplicate, first)
|
|
147
|
-
assert.deepEqual(await store.claimPassage(`real-${source.id}`), first, 'reopening must not consume another passage')
|
|
148
|
-
assert.equal(storage.books.get(book.id).progress, 1)
|
|
149
|
-
store.dispose()
|
|
150
|
-
|
|
151
|
-
restored = new NovelStore(storage)
|
|
152
|
-
await restored.ready
|
|
153
|
-
assert.equal(restored.getSettingsSnapshot().currentBookId, book.id)
|
|
154
|
-
assert.equal(restored.getSnapshot().books.find(item => item.id === book.id).progress, 1)
|
|
155
|
-
const next = await restored.claimPassage(`restored-${source.id}`)
|
|
156
|
-
assert.equal(next.kind, 'ready')
|
|
157
|
-
assert.equal(next.index, 1)
|
|
158
|
-
assert.equal(storage.books.get(book.id).progress, 2)
|
|
159
|
-
assert.deepEqual(storage.books.get('existing'), original.books[0])
|
|
160
|
-
for (const [key, passage] of originalPassages) assert.deepEqual(storage.passages.get(key), passage)
|
|
161
|
-
} finally {
|
|
162
|
-
store.dispose()
|
|
163
|
-
restored?.dispose()
|
|
164
|
-
}
|
|
165
|
-
})
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
test('canonical AQ Markdown retains the entire original official export byte-for-byte after its sole heading', async () => {
|
|
169
|
-
const source = byId('aq-zheng-zhuan')
|
|
170
|
-
const bytes = await readFile(new URL(source.file, samples))
|
|
171
|
-
const prefix = Buffer.from(source.derivation.prefix, 'utf8')
|
|
172
|
-
assert.deepEqual(bytes.subarray(0, prefix.length), prefix)
|
|
173
|
-
const original = bytes.subarray(prefix.length)
|
|
174
|
-
assert.equal(original.length, 68156)
|
|
175
|
-
assert.equal(sha256(original), '15a60f2d6e105c0869c4a11516eaba0d6726e6df68a91c40836dad51c1016b51')
|
|
176
|
-
const text = original.toString('utf8')
|
|
177
|
-
const storage = new MemoryStorage()
|
|
178
|
-
const store = new NovelStore(storage)
|
|
179
|
-
await store.ready
|
|
180
|
-
try {
|
|
181
|
-
const book = await store.importFile(asFile(source.file, bytes))
|
|
182
|
-
assert.equal(book.format, 'md')
|
|
183
|
-
const body = await storedBody(storage, book)
|
|
184
|
-
assertReadableBody(source.id, body)
|
|
185
|
-
for (const title of aqChapters) assert.ok(compact(body).includes(compact(title)), title)
|
|
186
|
-
assert.equal(compact(body), compact(text), 'all original exported text survives the Markdown import')
|
|
187
|
-
assert.equal((await storage.getSegment(book.id, 0)).chapterTitle, '阿Q正傳')
|
|
188
|
-
assert.match(body, /Creative Commons Attribution-ShareAlike 3\.0 Unported.*GNU FDL/u)
|
|
189
|
-
} finally {
|
|
190
|
-
store.dispose()
|
|
191
|
-
}
|
|
192
|
-
})
|
package/test/epub.test.js
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import JSZip from 'jszip'
|
|
4
|
-
import { parseEpub } from '../src/epub.js'
|
|
5
|
-
|
|
6
|
-
async function epubFile({ malformed = false, missingNav = false, missingChapter = false, injected = false, edit, compression = 'STORE' } = {}) {
|
|
7
|
-
const zip = new JSZip()
|
|
8
|
-
if (malformed) {
|
|
9
|
-
zip.file('not-an-epub.txt', 'no package document')
|
|
10
|
-
} else {
|
|
11
|
-
zip.file('mimetype', 'application/epub+zip', { compression: 'STORE' })
|
|
12
|
-
zip.file('META-INF/container.xml', `<?xml version="1.0"?>
|
|
13
|
-
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
|
14
|
-
<rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/></rootfiles>
|
|
15
|
-
</container>`)
|
|
16
|
-
zip.file('OEBPS/content.opf', `<?xml version="1.0"?>
|
|
17
|
-
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="book-id">
|
|
18
|
-
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
|
19
|
-
<dc:identifier id="book-id">sample-id</dc:identifier>
|
|
20
|
-
<dc:title>Sample Novel</dc:title><dc:language>en</dc:language>
|
|
21
|
-
</metadata>
|
|
22
|
-
<manifest>
|
|
23
|
-
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
|
|
24
|
-
<item id="one" href="chapter-one.xhtml" media-type="application/xhtml+xml"/>
|
|
25
|
-
<item id="two" href="chapter-two.xhtml" media-type="application/xhtml+xml"/>
|
|
26
|
-
<item id="appendix" href="appendix.xhtml" media-type="application/xhtml+xml"/>
|
|
27
|
-
</manifest>
|
|
28
|
-
<spine>
|
|
29
|
-
<itemref idref="one"/><itemref idref="two"/><itemref idref="appendix" linear="no"/>
|
|
30
|
-
</spine>
|
|
31
|
-
</package>`)
|
|
32
|
-
zip.file('OEBPS/nav.xhtml', `<?xml version="1.0"?>
|
|
33
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
|
|
34
|
-
<head></head><body><nav epub:type="toc"><ol>
|
|
35
|
-
<li><a href="chapter-two.xhtml">TOC Two</a></li>
|
|
36
|
-
<li><a href="chapter-one.xhtml">TOC One</a></li>
|
|
37
|
-
</ol></nav></body></html>`)
|
|
38
|
-
zip.file('OEBPS/chapter-one.xhtml', `<?xml version="1.0"?>
|
|
39
|
-
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><section>
|
|
40
|
-
<h1>Chapter One</h1><p>First passage.</p><p>Second paragraph.</p>
|
|
41
|
-
</section></body></html>`)
|
|
42
|
-
zip.file('OEBPS/chapter-two.xhtml', `<?xml version="1.0"?>
|
|
43
|
-
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>
|
|
44
|
-
<h2>Chapter Two</h2><p>Third passage.</p>
|
|
45
|
-
</body></html>`)
|
|
46
|
-
zip.file('OEBPS/appendix.xhtml', `<?xml version="1.0"?>
|
|
47
|
-
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><h1>Appendix</h1><p>Must be excluded.</p></body></html>`)
|
|
48
|
-
}
|
|
49
|
-
if (missingNav) zip.remove('OEBPS/nav.xhtml')
|
|
50
|
-
if (missingChapter) zip.remove('OEBPS/chapter-two.xhtml')
|
|
51
|
-
if (injected) zip.file('OEBPS/chapter-one.xhtml', '<html xmlns="http://www.w3.org/1999/xhtml"><head/><body><h1>Chapter One</h1><script>evil()</script><style>.evil{}</style><p hidden="hidden">hidden text</p><p>Visible<script>nested()</script> words.</p><div aria-hidden="true">invisible</div></body></html>')
|
|
52
|
-
if (edit) await edit(zip)
|
|
53
|
-
const bytes = await zip.generateAsync({ type: 'uint8array', compression })
|
|
54
|
-
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength)
|
|
55
|
-
return { name: 'sample.epub', arrayBuffer: async () => buffer }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
test('EPUB content follows linear spine order, extracts chapter text, and ignores non-linear content', async () => {
|
|
59
|
-
const parsed = await parseEpub(await epubFile(), 600)
|
|
60
|
-
assert.equal(parsed.title, 'Sample Novel')
|
|
61
|
-
assert.deepEqual(parsed.chapters.map(chapter => chapter.title), ['Chapter One', 'Chapter Two'])
|
|
62
|
-
assert.deepEqual(parsed.segments.map(segment => segment.text), ['First passage.\n\nSecond paragraph.', 'Third passage.'])
|
|
63
|
-
assert.equal(parsed.segments.some(segment => segment.text.includes('Must be excluded.')), false)
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
test('malformed EPUB fails with an explicit parsing error', async () => {
|
|
67
|
-
await assert.rejects(parseEpub(await epubFile({ malformed: true })), /EPUB 无法解析/u)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
test('missing declared navigation and chapters reject instead of hanging', { timeout: 2000 }, async () => {
|
|
71
|
-
await assert.rejects(parseEpub(await epubFile({ missingNav: true })), /文件缺失.*nav.xhtml/u)
|
|
72
|
-
await assert.rejects(parseEpub(await epubFile({ missingChapter: true })), /文件缺失.*chapter-two.xhtml/u)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('script, style, and hidden descendants never become book text', async () => {
|
|
76
|
-
const result = await parseEpub(await epubFile({ injected: true }))
|
|
77
|
-
assert.equal(result.segments[0].text, 'Visible words.')
|
|
78
|
-
assert.doesNotMatch(result.segments.map(x => x.text).join(''), /evil|nested|hidden|invisible/u)
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
function utf16(text, bigEndian = false, bom = true) {
|
|
82
|
-
const bytes = new Uint8Array(text.length * 2 + (bom ? 2 : 0))
|
|
83
|
-
const view = new DataView(bytes.buffer)
|
|
84
|
-
if (bom) view.setUint16(0, 0xfeff, !bigEndian)
|
|
85
|
-
for (let i = 0; i < text.length; i++) view.setUint16(i * 2 + (bom ? 2 : 0), text.charCodeAt(i), !bigEndian)
|
|
86
|
-
return bytes
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
test('namespace-prefixed XHTML keeps nested paragraphs separate and inline text continuous', async () => {
|
|
90
|
-
const file = await epubFile({ edit(zip) {
|
|
91
|
-
zip.file('OEBPS/chapter-one.xhtml', `<x:html xmlns:x="http://www.w3.org/1999/xhtml"><x:head/><x:body>
|
|
92
|
-
<x:h1>命名空间标题</x:h1><x:blockquote><x:p>第一段</x:p><x:p>第二段</x:p></x:blockquote>
|
|
93
|
-
<x:div>段首<x:span>连续<x:em>内联</x:em></x:span>段末<x:br/>换行</x:div>
|
|
94
|
-
<x:div>分块一</x:div><x:div>分块二</x:div>
|
|
95
|
-
</x:body></x:html>`)
|
|
96
|
-
} })
|
|
97
|
-
const result = await parseEpub(file)
|
|
98
|
-
assert.equal(result.chapters[0].title, '命名空间标题')
|
|
99
|
-
assert.equal(result.chapters[0].text, '第一段\n\n第二段\n\n段首连续内联段末\n换行\n\n分块一\n\n分块二')
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
test('无 xmlns 的 XHTML 和 text/html 章节保持可读取', async () => {
|
|
103
|
-
for (const type of ['application/xhtml+xml', 'text/html']) {
|
|
104
|
-
const file = await epubFile({ async edit(zip) {
|
|
105
|
-
const opf = await zip.file('OEBPS/content.opf').async('string')
|
|
106
|
-
zip.file('OEBPS/content.opf', opf.replace('href="chapter-one.xhtml" media-type="application/xhtml+xml"', `href="chapter-one.xhtml" media-type="${type}"`))
|
|
107
|
-
zip.file('OEBPS/chapter-one.xhtml', '<html><head/><body><h1>无命名空间</h1><p>仍可读取。</p></body></html>')
|
|
108
|
-
} })
|
|
109
|
-
const result = await parseEpub(file)
|
|
110
|
-
assert.equal(result.chapters[0].title, '无命名空间')
|
|
111
|
-
assert.equal(result.chapters[0].text, '仍可读取。')
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
test('UTF-16 container, package, navigation and XHTML are decoded before parsing', async () => {
|
|
116
|
-
for (const bigEndian of [false, true]) {
|
|
117
|
-
for (const bom of [false, true]) {
|
|
118
|
-
const file = await epubFile({ async edit(zip) {
|
|
119
|
-
for (const name of ['META-INF/container.xml', 'OEBPS/content.opf', 'OEBPS/nav.xhtml', 'OEBPS/chapter-one.xhtml', 'OEBPS/chapter-two.xhtml']) {
|
|
120
|
-
const xml = (await zip.file(name).async('string')).replace('<?xml version="1.0"?>', '<?xml version="1.0" encoding="UTF-16"?>')
|
|
121
|
-
zip.file(name, utf16(xml, bigEndian, bom))
|
|
122
|
-
}
|
|
123
|
-
} })
|
|
124
|
-
assert.equal((await parseEpub(file)).chapters[0].text, 'First passage.\n\nSecond paragraph.')
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
test('invalid UTF-8 and unsupported XML declarations reject instead of corrupting text', async () => {
|
|
130
|
-
for (const bytes of [Uint8Array.of(0xff), new TextEncoder().encode('<?xml version="1.0" encoding="GBK"?><html/>')]) {
|
|
131
|
-
await assert.rejects(parseEpub(await epubFile({ edit(zip) { zip.file('OEBPS/chapter-one.xhtml', bytes) } })), /编码/u)
|
|
132
|
-
}
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
function corruptCrc(buffer, name) {
|
|
136
|
-
const bytes = new Uint8Array(buffer.slice(0))
|
|
137
|
-
const view = new DataView(bytes.buffer)
|
|
138
|
-
for (let i = 0; i + 46 <= bytes.length; i++) {
|
|
139
|
-
if (view.getUint32(i, true) !== 0x02014b50) continue
|
|
140
|
-
const length = view.getUint16(i + 28, true)
|
|
141
|
-
if (new TextDecoder().decode(bytes.subarray(i + 46, i + 46 + length)) === name) {
|
|
142
|
-
view.setUint32(i + 16, view.getUint32(i + 16, true) ^ 1, true)
|
|
143
|
-
return bytes.buffer
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
throw new Error(`未找到 ZIP 中央目录项:${name}`)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
test('CRC is verified for used entries but unused resources are not decompressed', async () => {
|
|
150
|
-
const file = await epubFile({ edit(zip) { zip.file('unused.bin', 'unused resource') } })
|
|
151
|
-
const bytes = await file.arrayBuffer()
|
|
152
|
-
assert.equal((await parseEpub({ arrayBuffer: async () => corruptCrc(bytes, 'unused.bin') })).title, 'Sample Novel')
|
|
153
|
-
await assert.rejects(parseEpub({ arrayBuffer: async () => corruptCrc(bytes, 'OEBPS/chapter-one.xhtml') }), /CRC.*chapter-one.xhtml/u)
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
test('large unused content is skipped and actual streamed output is capped despite forged size metadata', { timeout: 30000 }, async t => {
|
|
157
|
-
const file = await epubFile({ compression: 'DEFLATE', edit(zip) {
|
|
158
|
-
zip.file('unused-large.bin', new Uint8Array(129 * 1024 * 1024).fill(0x61))
|
|
159
|
-
} })
|
|
160
|
-
assert.ok((await file.arrayBuffer()).byteLength < 1024 * 1024)
|
|
161
|
-
assert.equal((await parseEpub(file)).title, 'Sample Novel')
|
|
162
|
-
|
|
163
|
-
const loadAsync = JSZip.loadAsync.bind(JSZip)
|
|
164
|
-
let paused = false
|
|
165
|
-
let emittedAtPause = 0
|
|
166
|
-
t.mock.method(JSZip, 'loadAsync', async (...args) => {
|
|
167
|
-
const zip = await loadAsync(...args)
|
|
168
|
-
const chapter = zip.file('OEBPS/chapter-one.xhtml')
|
|
169
|
-
chapter._data = zip.file('unused-large.bin')._data
|
|
170
|
-
chapter._data.uncompressedSize = 1
|
|
171
|
-
const internalStream = chapter.internalStream.bind(chapter)
|
|
172
|
-
chapter.internalStream = (...streamArgs) => {
|
|
173
|
-
const stream = internalStream(...streamArgs)
|
|
174
|
-
let emitted = 0
|
|
175
|
-
stream.on('data', chunk => { emitted += chunk.byteLength })
|
|
176
|
-
const pause = stream.pause.bind(stream)
|
|
177
|
-
stream.pause = () => { paused = true; emittedAtPause = emitted; return pause() }
|
|
178
|
-
return stream
|
|
179
|
-
}
|
|
180
|
-
return zip
|
|
181
|
-
})
|
|
182
|
-
await assert.rejects(parseEpub(file), /解压正文超过 128 MiB/u)
|
|
183
|
-
assert.equal(paused, true)
|
|
184
|
-
assert.ok(emittedAtPause <= 128 * 1024 * 1024 + 16 * 1024)
|
|
185
|
-
})
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
|
-
|
|
3
|
-
// Contract doubles only. Appearance, focus, portals and keyboard behavior are
|
|
4
|
-
// verified against the actual host; these doubles must not claim to test them.
|
|
5
|
-
export function HostSwitch({ checked, disabled, label, onChange }) {
|
|
6
|
-
return React.createElement('button', { role: 'switch', 'aria-checked': checked, 'aria-label': label, disabled, onClick: () => onChange(!checked) })
|
|
7
|
-
}
|
|
8
|
-
export const HostButton = React.forwardRef(function HostButton({ variant, size, children, ...props }, ref) {
|
|
9
|
-
return React.createElement('button', { type: 'button', ...props, ref, 'data-host-variant': variant }, children)
|
|
10
|
-
})
|
|
11
|
-
export const HostInput = React.forwardRef(function HostInput(props, ref) {
|
|
12
|
-
return React.createElement('input', { ...props, ref })
|
|
13
|
-
})
|
|
14
|
-
export function HostMenu({ anchor, open, items, footer = [], selectedId, selectedIds = [], onSelect }) {
|
|
15
|
-
return React.createElement('div', null, anchor, open ? React.createElement('div', { role: 'menu' },
|
|
16
|
-
[...items, ...footer].map(item => React.createElement('button', {
|
|
17
|
-
key: item.id, role: 'menuitem', disabled: item.disabled,
|
|
18
|
-
'data-selected': selectedId === item.id || selectedIds.includes(item.id),
|
|
19
|
-
onClick: () => onSelect(item.id),
|
|
20
|
-
}, item.label)),
|
|
21
|
-
) : null)
|
|
22
|
-
}
|
|
23
|
-
export function HostModal({ open, title, description, children, footer }) {
|
|
24
|
-
return open ? React.createElement('div', { role: 'dialog', 'aria-label': title }, description, children, footer) : null
|
|
25
|
-
}
|
|
26
|
-
function Chevron() { return React.createElement('svg', { 'aria-hidden': true }) }
|
|
27
|
-
|
|
28
|
-
export const hostPrimitives = { Switch: HostSwitch, Button: HostButton, Input: HostInput, Menu: HostMenu, Modal: HostModal, IconChevronDownOutlineRegular: Chevron }
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
export class MemoryStorage {
|
|
2
|
-
constructor({ books = [], settings = {}, passages = {}, delayReads = 0 } = {}) {
|
|
3
|
-
this.books = new Map(books.map(book => [book.id, { ...book }]))
|
|
4
|
-
this.settings = { ...settings }
|
|
5
|
-
this.passages = new Map()
|
|
6
|
-
this.delayReads = delayReads
|
|
7
|
-
this.failNextRead = false
|
|
8
|
-
this.reads = 0
|
|
9
|
-
for (const [bookId, segments] of Object.entries(passages)) {
|
|
10
|
-
segments.forEach((segment, index) => this.passages.set(`${bookId}:${index}`, { ...segment }))
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async load() {
|
|
15
|
-
return { books: [...this.books.values()].map(book => ({ ...book })), settings: { ...this.settings } }
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async saveSettings(settings) {
|
|
19
|
-
this.settings = { ...settings }
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async addBook(book, segments) {
|
|
23
|
-
this.books.set(book.id, { ...book })
|
|
24
|
-
segments.forEach((segment, index) => this.passages.set(`${book.id}:${index}`, { ...segment }))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async updateBook(book) {
|
|
28
|
-
this.books.set(book.id, { ...book })
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async getSegment(bookId, index) {
|
|
32
|
-
this.reads += 1
|
|
33
|
-
if (this.delayReads) await new Promise(resolve => setTimeout(resolve, this.delayReads))
|
|
34
|
-
if (this.failNextRead) {
|
|
35
|
-
this.failNextRead = false
|
|
36
|
-
throw new Error('synthetic storage failure')
|
|
37
|
-
}
|
|
38
|
-
const segment = this.passages.get(`${bookId}:${index}`)
|
|
39
|
-
return segment ? { ...segment } : null
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async deleteBook(bookId) {
|
|
43
|
-
this.books.delete(bookId)
|
|
44
|
-
for (const key of this.passages.keys()) if (key.startsWith(`${bookId}:`)) this.passages.delete(key)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
close() {}
|
|
48
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { spawnSync } from 'node:child_process'
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
|
-
|
|
6
|
-
const script = fileURLToPath(new URL('../scripts/check-host-contracts.mjs', import.meta.url))
|
|
7
|
-
const run = args => spawnSync(process.execPath, [script, ...args], {
|
|
8
|
-
encoding: 'utf8', timeout: 30000, env: { ...process.env, HOST_ROOT: '' },
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
test('host-contract probe rejects relative installation paths with a JSON failure', () => {
|
|
12
|
-
const result = run(['relative/host'])
|
|
13
|
-
assert.equal(result.status, 1)
|
|
14
|
-
const report = JSON.parse(result.stdout)
|
|
15
|
-
assert.equal(report.ok, false)
|
|
16
|
-
assert.match(report.error, /absolute DSH installation path/)
|
|
17
|
-
assert.equal(report.claims, null, 'failed setup must not claim a measured result')
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('host-contract probe exercises a real installation when HOST_ROOT is supplied', {
|
|
21
|
-
skip: process.env.HOST_ROOT ? false : 'Set HOST_ROOT to an absolute installed DSH directory to run real-host integration',
|
|
22
|
-
}, () => {
|
|
23
|
-
const result = run([process.env.HOST_ROOT])
|
|
24
|
-
assert.equal(result.status, 0, result.stdout + result.stderr)
|
|
25
|
-
const report = JSON.parse(result.stdout)
|
|
26
|
-
assert.equal(report.ok, true)
|
|
27
|
-
assert.equal(report.claims, 0)
|
|
28
|
-
assert.ok(report.checks.some(check => check.name === 'real SlotCore registrations'))
|
|
29
|
-
assert.ok(report.checks.some(check => check.name === 'real ReasoningRow / DisclosureRow / TextShimmer'))
|
|
30
|
-
assert.ok(report.checks.some(check => check.name === 'adapter anchors for title layers and fold state'))
|
|
31
|
-
assert.ok(report.limitations.some(limit => limit.includes('trusted-user-event')))
|
|
32
|
-
})
|
package/test/import.test.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { NovelStore } from '../src/store.js'
|
|
4
|
-
import { MemoryStorage } from './helpers/memory-storage.js'
|
|
5
|
-
|
|
6
|
-
function bytesFile(name, bytes) {
|
|
7
|
-
const buffer = Uint8Array.from(bytes).buffer
|
|
8
|
-
return { name, arrayBuffer: async () => buffer, text: async () => { throw new Error('must decode original bytes') } }
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function textFile(name, text) {
|
|
12
|
-
return bytesFile(name, new TextEncoder().encode(text))
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function utf16Bytes(text, bigEndian = false) {
|
|
16
|
-
const bytes = bigEndian ? [0xfe, 0xff] : [0xff, 0xfe]
|
|
17
|
-
for (let index = 0; index < text.length; index += 1) {
|
|
18
|
-
const code = text.charCodeAt(index)
|
|
19
|
-
bytes.push(...(bigEndian ? [code >> 8, code & 0xff] : [code & 0xff, code >> 8]))
|
|
20
|
-
}
|
|
21
|
-
return bytes
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
test('TXT and Markdown file imports produce readable stored passages', async () => {
|
|
25
|
-
const storage = new MemoryStorage()
|
|
26
|
-
const store = new NovelStore(storage)
|
|
27
|
-
await store.ready
|
|
28
|
-
try {
|
|
29
|
-
const txt = await store.importFile(textFile('novel.txt', '第一章 初见\n\nTXT 正文。'))
|
|
30
|
-
const md = await store.importFile(textFile('novel.md', '# Markdown Chapter\n\nMarkdown body.'))
|
|
31
|
-
assert.equal(txt.format, 'txt')
|
|
32
|
-
assert.equal(md.format, 'md')
|
|
33
|
-
assert.equal((await storage.getSegment(txt.id, 0)).text, 'TXT 正文。')
|
|
34
|
-
assert.equal((await storage.getSegment(md.id, 0)).chapterTitle, 'Markdown Chapter')
|
|
35
|
-
assert.equal(store.getSettingsSnapshot().currentBookId, txt.id)
|
|
36
|
-
assert.equal(typeof store.importPastedText, 'undefined', '粘贴能力已删除,而非只隐藏入口')
|
|
37
|
-
} finally {
|
|
38
|
-
store.dispose()
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
for (const extension of ['txt', 'md']) {
|
|
43
|
-
for (const [encoding, encode] of [
|
|
44
|
-
['UTF-8 BOM', text => [0xef, 0xbb, 0xbf, ...new TextEncoder().encode(text)]],
|
|
45
|
-
['UTF-16LE BOM', text => utf16Bytes(text)],
|
|
46
|
-
['UTF-16BE BOM', text => utf16Bytes(text, true)],
|
|
47
|
-
]) {
|
|
48
|
-
test(`${extension} imports decode ${encoding} from the original bytes`, async () => {
|
|
49
|
-
const storage = new MemoryStorage()
|
|
50
|
-
const store = new NovelStore(storage)
|
|
51
|
-
await store.ready
|
|
52
|
-
try {
|
|
53
|
-
const heading = extension === 'md' ? '# 初见' : '第一章 初见'
|
|
54
|
-
const book = await store.importFile(bytesFile(`novel.${extension}`, encode(`${heading}\n\n正文包含中文和emoji😀。`)))
|
|
55
|
-
const segment = await storage.getSegment(book.id, 0)
|
|
56
|
-
assert.equal(segment.text, '正文包含中文和emoji😀。')
|
|
57
|
-
assert.equal(segment.chapterTitle, extension === 'md' ? '初见' : '第一章 初见')
|
|
58
|
-
assert.equal(segment.text.includes('�'), false)
|
|
59
|
-
} finally {
|
|
60
|
-
store.dispose()
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
for (const extension of ['txt', 'md']) {
|
|
67
|
-
test(`unsupported GBK ${extension} bytes fail clearly without importing or replacing selection`, async () => {
|
|
68
|
-
const storage = new MemoryStorage()
|
|
69
|
-
const store = new NovelStore(storage)
|
|
70
|
-
await store.ready
|
|
71
|
-
try {
|
|
72
|
-
const existing = await store.importFile(textFile('existing.txt', '原来的正文。'))
|
|
73
|
-
const beforeSettings = { ...storage.settings }
|
|
74
|
-
// GBK encoding of 中文, deliberately not valid UTF-8.
|
|
75
|
-
await assert.rejects(store.importFile(bytesFile(`novel.${extension}`, [0xd6, 0xd0, 0xce, 0xc4])), /编码|UTF/u)
|
|
76
|
-
assert.equal(storage.books.size, 1)
|
|
77
|
-
assert.equal(store.getSnapshot().books.length, 1)
|
|
78
|
-
assert.equal(store.getSettingsSnapshot().currentBookId, existing.id)
|
|
79
|
-
assert.deepEqual(storage.settings, beforeSettings)
|
|
80
|
-
assert.equal((await store.claimPassage('original')).passage.text, '原来的正文。')
|
|
81
|
-
} finally {
|
|
82
|
-
store.dispose()
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
test('a malformed UTF-16 import does not create an empty or corrupted book', async () => {
|
|
88
|
-
const storage = new MemoryStorage()
|
|
89
|
-
const store = new NovelStore(storage)
|
|
90
|
-
await store.ready
|
|
91
|
-
await assert.rejects(store.importFile(bytesFile('broken.txt', [0xff, 0xfe, 0x2d])), /编码|UTF/u)
|
|
92
|
-
assert.equal(storage.books.size, 0)
|
|
93
|
-
assert.equal(store.getSettingsSnapshot().currentBookId, null)
|
|
94
|
-
store.dispose()
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
test('unsupported file extensions fail clearly', async () => {
|
|
98
|
-
const store = new NovelStore(new MemoryStorage())
|
|
99
|
-
await store.ready
|
|
100
|
-
await assert.rejects(store.importFile(textFile('novel.pdf', 'not supported')), /只支持 EPUB、TXT、MD/u)
|
|
101
|
-
store.dispose()
|
|
102
|
-
})
|
package/test/persistence.test.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb'
|
|
4
|
-
import { IndexedDbStorage } from '../src/persistence.js'
|
|
5
|
-
|
|
6
|
-
test('IndexedDB stores metadata/settings separately from lazily-read passage records', async () => {
|
|
7
|
-
const storage = new IndexedDbStorage(new IDBFactory(), IDBKeyRange)
|
|
8
|
-
const book = { id: 'book-test', title: 'Test', format: 'txt', segmentCount: 2, progress: 0 }
|
|
9
|
-
try {
|
|
10
|
-
assert.deepEqual(await storage.load(), { books: [], settings: undefined })
|
|
11
|
-
await storage.addBook(book, [{ text: 'A', chapterTitle: 'One', chapterIndex: 0 }, { text: 'B', chapterTitle: 'Two', chapterIndex: 1 }])
|
|
12
|
-
await storage.saveSettings({ currentBookId: book.id })
|
|
13
|
-
assert.deepEqual(await storage.load(), { books: [book], settings: { currentBookId: book.id } })
|
|
14
|
-
assert.deepEqual(await storage.getSegment(book.id, 1), { text: 'B', chapterTitle: 'Two', chapterIndex: 1 })
|
|
15
|
-
assert.equal(await storage.getSegment(book.id, 2), null)
|
|
16
|
-
|
|
17
|
-
await storage.updateBook({ ...book, progress: 1 })
|
|
18
|
-
assert.equal((await storage.load()).books[0].progress, 1)
|
|
19
|
-
await storage.deleteBook(book.id)
|
|
20
|
-
assert.equal(await storage.getSegment(book.id, 0), null)
|
|
21
|
-
assert.equal(await storage.getSegment(book.id, 1), null)
|
|
22
|
-
await assert.rejects(storage.updateBook({ ...book, progress: 2 }), /找不到这本书/u)
|
|
23
|
-
assert.deepEqual(await storage.load(), { books: [], settings: { currentBookId: book.id } })
|
|
24
|
-
} finally {
|
|
25
|
-
storage.close()
|
|
26
|
-
}
|
|
27
|
-
})
|