@gongfpp/dsh-novel-fish 1.0.1 → 1.2.0
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 +21 -0
- package/README.md +65 -22
- package/docs/evidence/reading-demo-110.png +0 -0
- package/docs/evidence/settings-reliability.png +0 -0
- package/index.json +24 -0
- package/lib/client.js +1225 -191
- package/locale/en.json +21 -0
- package/locale/zh.json +21 -0
- package/package.json +6 -7
- package/samples/ebooks/LICENSE.md +355 -0
- package/samples/ebooks/NOTICE.md +33 -0
- package/samples/ebooks/aq-zheng-zhuan-wikisource.md +998 -0
- package/samples/ebooks/million-pound-bank-note-pg61522.txt +6970 -0
- package/samples/ebooks/sources.json +58 -0
- package/screenshots.json +6 -0
- package/docs/acceptance.md +0 -42
- package/docs/community-markets.md +0 -71
- package/docs/compatibility-017.md +0 -70
- package/docs/compatibility-101.md +0 -41
- package/docs/continuous-reading-020.md +0 -46
- 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/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-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 -139
- package/docs/settings-update.md +0 -49
- package/docs/type-append-update.md +0 -50
- package/pnpm-workspace.yaml +0 -5
- package/scripts/build.mjs +0 -69
- package/scripts/check-host-contracts.mjs +0 -246
- package/scripts/licenses/path-webpack.txt +0 -28
- package/src/chat-adapter.js +0 -253
- package/src/client.js +0 -55
- package/src/epub.js +0 -166
- package/src/novel-content.js +0 -20
- package/src/persistence.js +0 -130
- package/src/segments.js +0 -135
- package/src/settings.js +0 -219
- package/src/store.js +0 -371
- package/src/text-decoding.js +0 -28
- package/test/chat-adapter.test.js +0 -433
- package/test/client.test.js +0 -82
- 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/release.test.js +0 -85
- package/test/segments.test.js +0 -48
- package/test/settings.test.js +0 -271
- package/test/store.test.js +0 -617
- package/test/text-decoding.test.js +0 -49
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
|
-
})
|
package/test/release.test.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import semver from 'semver'
|
|
4
|
-
import { readFile, access } from 'node:fs/promises'
|
|
5
|
-
|
|
6
|
-
const root = new URL('../', import.meta.url)
|
|
7
|
-
const read = path => readFile(new URL(path, root), 'utf8')
|
|
8
|
-
|
|
9
|
-
test('public package identity matches the Host patch and browser module', async () => {
|
|
10
|
-
const manifest = JSON.parse(await read('package.json'))
|
|
11
|
-
assert.equal(manifest.name, '@gongfpp/dsh-novel-fish')
|
|
12
|
-
assert.equal(manifest.repository.url, 'https://github.com/gongfpp/dsh-novel-fish.git')
|
|
13
|
-
assert.equal(manifest.engines.dsh, '>=0.2.1-rc.2 <0.3.0-0')
|
|
14
|
-
assert.ok((await read('cordis.patch.yml')).includes(`name: '${manifest.name}'`))
|
|
15
|
-
assert.ok((await read('src/client.js')).includes(`const MODULE_ID = '${manifest.name}'`))
|
|
16
|
-
assert.ok((await read('lib/client.js')).includes(manifest.name))
|
|
17
|
-
for (const path of ['./index.js', './lib/client.js', './assets/icon.png', './locale/en.json', './locale/zh.json']) {
|
|
18
|
-
await access(new URL(path, root))
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('host compatibility covers the whole 0.2.x line but not earlier work or 0.3', async () => {
|
|
23
|
-
const { engines } = JSON.parse(await read('package.json'))
|
|
24
|
-
// DSH preflight evaluates prereleases explicitly, unlike npm's default semver rules.
|
|
25
|
-
// A ceiling of the form `<=0.2.2-alpha.2` sorts below `0.2.2-rc.1`, which is how
|
|
26
|
-
// 1.0.0 locked out the rc hosts it was meant to serve; `<0.3.0-0` keeps every
|
|
27
|
-
// 0.2.x prerelease admissible and still rejects 0.3.0's own prereleases.
|
|
28
|
-
for (const version of [
|
|
29
|
-
'0.2.1-rc.2',
|
|
30
|
-
'0.2.1-rc.3',
|
|
31
|
-
'0.2.1-rc.4',
|
|
32
|
-
'0.2.1-rc.5',
|
|
33
|
-
'0.2.2-alpha.2',
|
|
34
|
-
'0.2.2-alpha.3',
|
|
35
|
-
'0.2.2-rc.1',
|
|
36
|
-
'0.2.2',
|
|
37
|
-
]) {
|
|
38
|
-
assert.equal(semver.satisfies(version, engines.dsh, { includePrerelease: true }), true, version)
|
|
39
|
-
}
|
|
40
|
-
for (const version of ['0.2.0', '0.2.1-alpha.5', '0.2.1-rc.1', '0.3.0-rc.1', '0.3.0', '1.0.0']) {
|
|
41
|
-
assert.equal(semver.satisfies(version, engines.dsh, { includePrerelease: true }), false, version)
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test('notices include JSZip prebundled libraries and the zlib copyright', async () => {
|
|
46
|
-
const notices = await read('THIRD-PARTY-NOTICES.txt')
|
|
47
|
-
for (const name of ['pako@1.0.11', 'lie@3.3.0', 'immediate@3.0.6', 'setimmediate@1.0.5', 'crc-32@1.2.2', 'Jean-loup Gailly and Mark Adler']) {
|
|
48
|
-
assert.ok(notices.includes(name), `Missing third-party notice: ${name}`)
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('市场索引保留固定提交来源、版本唯一和宿主兼容范围', async () => {
|
|
53
|
-
const manifest = JSON.parse(await read('package.json'))
|
|
54
|
-
const index = JSON.parse(await read('index.json'))
|
|
55
|
-
assert.equal(index.format, 1)
|
|
56
|
-
const entry = index.plugins.find(plugin => plugin.name === manifest.name)
|
|
57
|
-
assert.ok(entry)
|
|
58
|
-
assert.equal(entry.author, manifest.author)
|
|
59
|
-
assert.equal(entry.repository.url + '.git', manifest.repository.url)
|
|
60
|
-
await access(new URL(entry.icon, root))
|
|
61
|
-
assert.ok(entry.versions.length)
|
|
62
|
-
assert.equal(new Set(entry.versions.map(version => version.version)).size, entry.versions.length)
|
|
63
|
-
for (const version of entry.versions) {
|
|
64
|
-
assert.match(version.version, /^\d+\.\d+\.\d+$/u)
|
|
65
|
-
assert.ok(Number.isSafeInteger(version.publishTimestamp) && version.publishTimestamp > 0)
|
|
66
|
-
if (version.version === manifest.version) assert.equal(version.engines.dsh, manifest.engines.dsh)
|
|
67
|
-
else assert.match(version.engines.dsh, /^(?:\^|~|>=)?\d+\.\d+\.\d+(?:-[\w.-]+)?/u)
|
|
68
|
-
assert.equal(version.source.type, 'git')
|
|
69
|
-
assert.equal(version.source.url, entry.repository.url)
|
|
70
|
-
assert.match(version.source.commit, /^[a-f0-9]{40}$/u)
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
test('README local links resolve and installation contains no personal absolute path', async () => {
|
|
75
|
-
const readme = await read('README.md')
|
|
76
|
-
assert.doesNotMatch(readme, /\/Users\/|C:\\Users\\/u)
|
|
77
|
-
const index = JSON.parse(await read('index.json'))
|
|
78
|
-
const published = index.plugins.find(plugin => plugin.name === '@gongfpp/dsh-novel-fish').versions[0]
|
|
79
|
-
assert.ok(readme.includes(`github:gongfpp/dsh-novel-fish#v${published.version}`), 'installation uses an actually published version')
|
|
80
|
-
for (const match of readme.matchAll(/\]\(([^)]+)\)/gu)) {
|
|
81
|
-
const path = match[1].replace(/^<|>$/gu, '').split('#')[0]
|
|
82
|
-
if (!path || /^[a-z]+:/iu.test(path)) continue
|
|
83
|
-
await access(new URL(path, root))
|
|
84
|
-
}
|
|
85
|
-
})
|
package/test/segments.test.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { DEFAULT_SEGMENT_LENGTH, parseTextBook, segmentChapters } from '../src/segments.js'
|
|
4
|
-
|
|
5
|
-
test('Markdown headings become chapter labels and paragraph breaks survive import', () => {
|
|
6
|
-
const result = parseTextBook('# 序章\n\n第一段。\n\n第二段。\n\n## 章节二\n\n正文。', 'md', 600)
|
|
7
|
-
assert.deepEqual(result.chapters.map(chapter => chapter.title), ['序章', '章节二'])
|
|
8
|
-
assert.equal(result.chapters[0].text, '第一段。\n\n第二段。')
|
|
9
|
-
assert.equal(result.segments[0].chapterTitle, '序章')
|
|
10
|
-
assert.equal(result.segments[0].text, '第一段。\n\n第二段。')
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
test('TXT chapter headings are kept as chapter labels', () => {
|
|
14
|
-
const result = parseTextBook('第一章 初见\n\n内容甲。\n\n第二章 重逢\n\n内容乙。', 'txt', 600)
|
|
15
|
-
assert.deepEqual(result.chapters.map(chapter => chapter.title), ['第一章 初见', '第二章 重逢'])
|
|
16
|
-
assert.deepEqual(result.segments.map(segment => segment.text), ['内容甲。', '内容乙。'])
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test('TXT narrative starting with an ordinal stays in the readable body', () => {
|
|
20
|
-
const prose = '第二天,他回到家里,看见一封信。'
|
|
21
|
-
const result = parseTextBook(`第一章 初见\n\n${prose}\n\n他读完信后,缓缓坐下。`, 'txt', 600)
|
|
22
|
-
assert.deepEqual(result.chapters.map(chapter => chapter.title), ['第一章 初见'])
|
|
23
|
-
assert.equal(result.segments[0].text, `${prose}\n\n他读完信后,缓缓坐下。`)
|
|
24
|
-
assert.equal(parseTextBook(prose, 'txt').segments[0].text, prose)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
test('TXT headings require a chapter number immediately followed by a chapter unit', () => {
|
|
28
|
-
const result = parseTextBook('第十二回 故人重来\n\n内容甲。\n\nChapter 13 The Letter\n\n内容乙。', 'txt')
|
|
29
|
-
assert.deepEqual(result.chapters.map(chapter => chapter.title), ['第十二回 故人重来', 'Chapter 13 The Letter'])
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test('long text is split around target length without breaking Unicode code points', () => {
|
|
33
|
-
const source = '猫🐈'.repeat(401)
|
|
34
|
-
const segments = segmentChapters([{ title: '正文', text: source }], 600)
|
|
35
|
-
assert.ok(segments.every(segment => [...segment.text].length <= 600))
|
|
36
|
-
assert.equal(segments.map(segment => segment.text).join(''), source)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
test('paragraph boundaries take priority and the default target is about 600 characters', () => {
|
|
40
|
-
assert.equal(DEFAULT_SEGMENT_LENGTH, 600)
|
|
41
|
-
const segments = segmentChapters([{ title: '正文', text: `${'甲'.repeat(300)}。\n\n${'乙'.repeat(300)}。` }])
|
|
42
|
-
assert.equal(segments.length, 2)
|
|
43
|
-
assert.ok(segments.every(segment => [...segment.text].length < 600))
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
test('empty plain text is rejected instead of importing an unreadable book', () => {
|
|
47
|
-
assert.throws(() => parseTextBook(' \n\n ', 'txt'), /没有可阅读的正文文本/u)
|
|
48
|
-
})
|