@gongfpp/dsh-novel-fish 1.1.0 → 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 +41 -20
- package/docs/evidence/settings-reliability.png +0 -0
- package/index.json +12 -0
- package/lib/client.js +734 -128
- package/locale/en.json +15 -0
- package/locale/zh.json +15 -0
- package/package.json +5 -7
- 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-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/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
|
@@ -1,518 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { JSDOM } from 'jsdom'
|
|
4
|
-
import React from 'react'
|
|
5
|
-
import TestRenderer, { act } from 'react-test-renderer'
|
|
6
|
-
import { passageSnapshotKey } from '../src/store.js'
|
|
7
|
-
import { registerChatAdapter } from '../src/chat-adapter.js'
|
|
8
|
-
import { PG_NOTICE, PG_LICENSE_DOWNLOAD } from '../src/pg-notice.js'
|
|
9
|
-
import { readFile } from 'node:fs/promises'
|
|
10
|
-
|
|
11
|
-
const fullLicense = await readFile(new URL('../samples/ebooks/LICENSE.md', import.meta.url), 'utf8')
|
|
12
|
-
const licenseHref = `data:text/plain;charset=utf-8,${encodeURIComponent(fullLicense)}`
|
|
13
|
-
const getSampleLicenseHref = () => licenseHref
|
|
14
|
-
|
|
15
|
-
// Mirrors CURRENT 0.2.1-rc.2 DisclosureRow/TextShimmer nesting, not a replacement
|
|
16
|
-
// renderer. Trusted events below are handler-contract probes, NOT browser evidence.
|
|
17
|
-
function row({ leadingOnly = false, running = true } = {}) {
|
|
18
|
-
const toggle = leadingOnly ? '' : 'role="button" tabindex="0" aria-expanded="false"'
|
|
19
|
-
const leading = leadingOnly ? 'button type="button" aria-expanded="false" aria-label="Native title"' : 'span'
|
|
20
|
-
const layer = highlight => `<span class="native-content"><span class="native-title" ${highlight ? 'data-shimmer-text="Native title"' : ''}>${highlight ? '' : 'Native title'}</span><span data-shimmer-decoration aria-hidden="true" class="native-separator"></span><span class="native-summary"><span class="native-text" ${highlight ? 'data-shimmer-text="Native summary"' : ''}>${highlight ? '' : 'Native summary'}</span></span></span>`
|
|
21
|
-
return `<div data-variant="think"><div data-disclosure-header><div data-disclosure-row class="native-row" ${toggle}><${leading} class="native-leading"><span class="native-idle"><svg width="14" data-glyph="think"></svg></span><svg class="native-hover" data-glyph="down"></svg></${leadingOnly ? 'button' : 'span'}><span class="native-shimmer" ${running ? 'data-shimmer' : ''}>${layer(false)}${running ? `<span aria-hidden="true" inert class="native-decoration"><span class="native-sweep">${layer(true)}</span></span>` : ''}</span></div></div></div>`
|
|
22
|
-
}
|
|
23
|
-
function thought(key, content = row()) {
|
|
24
|
-
return `<div data-chat-flow-kind="assistant-step" data-chat-node-key="${key}">${content}</div>`
|
|
25
|
-
}
|
|
26
|
-
const readingKey = (session, node, index = 0) => JSON.stringify([session, node, 'reasoning', index])
|
|
27
|
-
const settle = () => new Promise(resolve => setImmediate(resolve))
|
|
28
|
-
|
|
29
|
-
function installDom(dom) {
|
|
30
|
-
const originals = Object.fromEntries(['document', 'Element', 'MutationObserver'].map(key => [key, globalThis[key]]))
|
|
31
|
-
for (const key of Object.keys(originals)) globalThis[key] = dom.window[key]
|
|
32
|
-
return () => {
|
|
33
|
-
dom.window.close()
|
|
34
|
-
for (const [key, value] of Object.entries(originals)) {
|
|
35
|
-
if (value === undefined) delete globalThis[key]
|
|
36
|
-
else globalThis[key] = value
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
function adapterFixture(content = thought('node-a'), session = 'session-a') {
|
|
41
|
-
const dom = new JSDOM(`<body><div data-conversation-session="${session}">${content}</div></body>`)
|
|
42
|
-
const restoreDom = installDom(dom)
|
|
43
|
-
let settings = { enabled: true, currentBookId: 'book', targetTypes: ['reasoning'], mainTitle: '思考', extraTitle: 'Checking project structure', continueOnReopen: false }
|
|
44
|
-
const claims = [], continues = [], types = [], effects = [], actions = {}
|
|
45
|
-
const statuses = new Map()
|
|
46
|
-
let passagesChanged
|
|
47
|
-
const add = document.addEventListener.bind(document)
|
|
48
|
-
document.addEventListener = (type, handler, ...args) => { actions[type] = handler; return add(type, handler, ...args) }
|
|
49
|
-
let settingsChanged
|
|
50
|
-
const store = {
|
|
51
|
-
cacheGeneration: 0,
|
|
52
|
-
settingsSource: {}, passageSource: {}, getSettingsSnapshot: () => settings,
|
|
53
|
-
subscribeSettings: fn => { settingsChanged = fn; return () => {} },
|
|
54
|
-
subscribePassages: callback => { passagesChanged = callback; return () => {} },
|
|
55
|
-
getPassageStatus: (_book, key) => statuses.get(key) ?? ({ kind: 'empty' }),
|
|
56
|
-
claimPassage: (key, type) => { claims.push(key); types.push(type); return Promise.resolve() },
|
|
57
|
-
continuePassage: (key, type) => { continues.push(key); types.push(type); return Promise.resolve() },
|
|
58
|
-
}
|
|
59
|
-
const ctx = { slots: { inject: (_key, fn) => fn(), register: () => () => {} }, effect: fn => { effects.push(fn()) } }
|
|
60
|
-
return {
|
|
61
|
-
dom, claims, continues, types, actions, start: () => registerChatAdapter(ctx, store, React, 'novel', { getSampleLicenseHref }),
|
|
62
|
-
passage: (key, status) => { statuses.set(key, status); passagesChanged() },
|
|
63
|
-
invalidate: () => { store.cacheGeneration += 1 },
|
|
64
|
-
settings: patch => { settings = { ...settings, ...patch }; settingsChanged() },
|
|
65
|
-
close: () => { effects.reverse().forEach(dispose => dispose?.()); restoreDom() },
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const trustedClick = target => ({ isTrusted: true, type: 'click', defaultPrevented: false, button: 0, target })
|
|
69
|
-
|
|
70
|
-
test('text-only adaptation retains native glyphs, shimmer layers, layout nodes and tool rows', async () => {
|
|
71
|
-
const f = adapterFixture(thought('a') + thought('b') + `<div data-chat-flow-kind="tool-call">${row()}</div>`)
|
|
72
|
-
try {
|
|
73
|
-
const roots = document.querySelectorAll('[data-chat-flow-kind="assistant-step"] [data-variant="think"]')
|
|
74
|
-
const header = roots[0].querySelector('[data-disclosure-row]')
|
|
75
|
-
const leading = header.firstElementChild
|
|
76
|
-
const glyphs = leading.outerHTML
|
|
77
|
-
const title = header.querySelector('.native-title')
|
|
78
|
-
const titleText = title.firstChild
|
|
79
|
-
const nodes = [...header.querySelectorAll('*')]
|
|
80
|
-
const tool = document.querySelector('[data-chat-flow-kind="tool-call"]')
|
|
81
|
-
const toolHtml = tool.innerHTML
|
|
82
|
-
f.start()
|
|
83
|
-
assert.equal(document.querySelectorAll('[data-novel-target]').length, 2)
|
|
84
|
-
assert.equal(header.firstElementChild, leading)
|
|
85
|
-
assert.equal(leading.outerHTML, glyphs)
|
|
86
|
-
assert.equal(title.firstChild, titleText, 'native React text node is retained')
|
|
87
|
-
assert.equal(title.textContent, '思考')
|
|
88
|
-
assert.equal(header.querySelector('.native-summary').textContent, 'Checking project structure')
|
|
89
|
-
assert.deepEqual([...header.querySelectorAll('*')], nodes, 'no replacement title, overlay or arrow nodes')
|
|
90
|
-
assert.deepEqual([...header.querySelectorAll('[data-shimmer-text]')].map(node => node.getAttribute('data-shimmer-text')), ['思考', 'Checking project structure'])
|
|
91
|
-
assert.equal(header.querySelector('[data-shimmer]').hasAttribute('data-shimmer'), true)
|
|
92
|
-
assert.equal(tool.innerHTML, toolHtml)
|
|
93
|
-
assert.equal(document.querySelector('style, [data-novel-label], [data-novel-icon]'), null)
|
|
94
|
-
assert.equal(header.querySelector('[style]'), null, 'native font/color/spacing/hover styles remain authoritative')
|
|
95
|
-
// Simulate a native streaming write; disabling must restore the latest host text.
|
|
96
|
-
titleText.nodeValue = 'Updated native title'
|
|
97
|
-
await settle()
|
|
98
|
-
assert.equal(title.textContent, '思考')
|
|
99
|
-
f.settings({ enabled: false })
|
|
100
|
-
assert.equal(title.textContent, 'Updated native title')
|
|
101
|
-
assert.equal(header.querySelector('.native-summary').textContent, 'Native summary')
|
|
102
|
-
assert.equal(header.querySelector('[data-shimmer-text]').getAttribute('data-shimmer-text'), 'Native title')
|
|
103
|
-
assert.equal(leading.outerHTML, glyphs)
|
|
104
|
-
assert.equal(document.querySelector('[data-novel-target]'), null)
|
|
105
|
-
} finally { f.close() }
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
test('only verified user closed-to-open transitions claim; automatic, canceled and nested actions do not', async () => {
|
|
109
|
-
const f = adapterFixture()
|
|
110
|
-
try {
|
|
111
|
-
f.start()
|
|
112
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
113
|
-
const nested = document.createElement('button')
|
|
114
|
-
nested.textContent = 'Independent action'; header.append(nested)
|
|
115
|
-
const link = document.createElement('a'); link.href = '#other'; header.append(link)
|
|
116
|
-
header.dispatchEvent(new f.dom.window.MouseEvent('click', { bubbles: true }))
|
|
117
|
-
f.actions.click({ ...trustedClick(header), defaultPrevented: true })
|
|
118
|
-
f.actions.click({ ...trustedClick(header), button: 1 })
|
|
119
|
-
f.actions.click(trustedClick(nested))
|
|
120
|
-
f.actions.click(trustedClick(link))
|
|
121
|
-
f.actions.keydown({ isTrusted: true, type: 'keydown', key: ' ', repeat: true, target: header })
|
|
122
|
-
f.actions.keydown({ isTrusted: true, type: 'keydown', key: 'Escape', target: header })
|
|
123
|
-
f.actions.keydown({ isTrusted: true, type: 'keydown', key: 'Enter', target: nested })
|
|
124
|
-
await settle()
|
|
125
|
-
assert.equal(f.claims.length, 0)
|
|
126
|
-
f.actions.click(trustedClick(header))
|
|
127
|
-
assert.equal(f.claims.length, 0, 'capture is intent only')
|
|
128
|
-
await settle()
|
|
129
|
-
assert.equal(f.claims.length, 0, 'no host transition means no read')
|
|
130
|
-
header.setAttribute('aria-expanded', 'true')
|
|
131
|
-
header.append(document.createTextNode('streaming update'))
|
|
132
|
-
await settle()
|
|
133
|
-
assert.equal(f.claims.length, 0, 'history/automatic expansion and stream changes do not read')
|
|
134
|
-
f.actions.click(trustedClick(header))
|
|
135
|
-
header.setAttribute('aria-expanded', 'false')
|
|
136
|
-
await settle()
|
|
137
|
-
assert.equal(f.claims.length, 0, 'closing does not read')
|
|
138
|
-
f.actions.click(trustedClick(header.querySelector('.native-title')))
|
|
139
|
-
header.setAttribute('aria-expanded', 'true')
|
|
140
|
-
assert.equal(f.claims.length, 0)
|
|
141
|
-
await settle()
|
|
142
|
-
assert.deepEqual(f.claims, [readingKey('session-a', 'node-a')])
|
|
143
|
-
} finally { f.close() }
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
test('Enter/Space use the native toggle and thought regions get independent temporary keys', async () => {
|
|
147
|
-
const f = adapterFixture(thought('node-a', row() + row()))
|
|
148
|
-
try {
|
|
149
|
-
f.start()
|
|
150
|
-
const headers = document.querySelectorAll('[data-disclosure-row]')
|
|
151
|
-
for (const [index, target] of [...headers].entries()) {
|
|
152
|
-
f.actions.keydown({ isTrusted: true, type: 'keydown', key: index ? ' ' : 'Enter', repeat: false, target })
|
|
153
|
-
target.setAttribute('aria-expanded', 'true')
|
|
154
|
-
}
|
|
155
|
-
await settle()
|
|
156
|
-
assert.deepEqual(f.claims, [readingKey('session-a', 'node-a', 0), readingKey('session-a', 'node-a', 1)])
|
|
157
|
-
} finally { f.close() }
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
test('leading-button mode ignores title/row clicks and keeps the native button accessible label', async () => {
|
|
161
|
-
const f = adapterFixture(thought('node-a', row({ leadingOnly: true })))
|
|
162
|
-
try {
|
|
163
|
-
f.start()
|
|
164
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
165
|
-
const toggle = header.firstElementChild
|
|
166
|
-
f.actions.click(trustedClick(header))
|
|
167
|
-
f.actions.click(trustedClick(header.querySelector('.native-title')))
|
|
168
|
-
await settle()
|
|
169
|
-
assert.equal(f.claims.length, 0)
|
|
170
|
-
assert.equal(toggle.getAttribute('aria-label'), 'Native title')
|
|
171
|
-
f.actions.click(trustedClick(toggle.querySelector('svg')))
|
|
172
|
-
toggle.setAttribute('aria-expanded', 'true')
|
|
173
|
-
await settle()
|
|
174
|
-
assert.equal(f.claims.length, 1)
|
|
175
|
-
assert.equal(toggle.getAttribute('aria-label'), 'Native title', 'no accessible-control rewrite')
|
|
176
|
-
} finally { f.close() }
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
test('book switch, disposal and removed rows cancel pending reads; rapid intent is coalesced', async () => {
|
|
180
|
-
const f = adapterFixture()
|
|
181
|
-
try {
|
|
182
|
-
f.start()
|
|
183
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
184
|
-
f.actions.click(trustedClick(header))
|
|
185
|
-
header.setAttribute('aria-expanded', 'true')
|
|
186
|
-
f.settings({ currentBookId: 'other-book' })
|
|
187
|
-
await settle()
|
|
188
|
-
assert.equal(f.claims.length, 0)
|
|
189
|
-
header.setAttribute('aria-expanded', 'false')
|
|
190
|
-
f.actions.click(trustedClick(header)); f.actions.click(trustedClick(header))
|
|
191
|
-
header.setAttribute('aria-expanded', 'true')
|
|
192
|
-
await settle()
|
|
193
|
-
assert.equal(f.claims.length, 1)
|
|
194
|
-
header.setAttribute('aria-expanded', 'false')
|
|
195
|
-
f.actions.click(trustedClick(header))
|
|
196
|
-
header.setAttribute('aria-expanded', 'true')
|
|
197
|
-
header.remove()
|
|
198
|
-
await settle()
|
|
199
|
-
assert.equal(f.claims.length, 1)
|
|
200
|
-
} finally { f.close() }
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
test('关闭重开、类型移除重选和切书切回不能复活帧内旧意图', async () => {
|
|
204
|
-
const f = adapterFixture()
|
|
205
|
-
const frames = []
|
|
206
|
-
f.dom.window.requestAnimationFrame = callback => { frames.push(callback); return frames.length }
|
|
207
|
-
f.dom.window.cancelAnimationFrame = () => {}
|
|
208
|
-
try {
|
|
209
|
-
f.start()
|
|
210
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
211
|
-
for (const cancel of [
|
|
212
|
-
() => { f.settings({ enabled: false }); f.settings({ enabled: true }) },
|
|
213
|
-
() => { f.settings({ targetTypes: [] }); f.settings({ targetTypes: ['reasoning'] }) },
|
|
214
|
-
() => { f.settings({ currentBookId: 'other' }); f.settings({ currentBookId: 'book' }) },
|
|
215
|
-
() => f.invalidate(), // 持久化尚未发出设置快照时,意图代次已失效。
|
|
216
|
-
]) {
|
|
217
|
-
header.setAttribute('aria-expanded', 'false')
|
|
218
|
-
f.actions.click(trustedClick(header))
|
|
219
|
-
header.setAttribute('aria-expanded', 'true')
|
|
220
|
-
cancel()
|
|
221
|
-
frames.at(-1)()
|
|
222
|
-
assert.equal(f.claims.length, 0)
|
|
223
|
-
}
|
|
224
|
-
header.setAttribute('aria-expanded', 'false')
|
|
225
|
-
f.actions.click(trustedClick(header)); header.setAttribute('aria-expanded', 'true')
|
|
226
|
-
frames.at(-1)()
|
|
227
|
-
assert.equal(f.claims.length, 1, '重新主动展开的新意图仍可读取')
|
|
228
|
-
} finally { f.close() }
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
test('微任务回退同样不会恢复已取消的用户展开', async () => {
|
|
232
|
-
const f = adapterFixture()
|
|
233
|
-
try {
|
|
234
|
-
f.start()
|
|
235
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
236
|
-
f.actions.click(trustedClick(header)); header.setAttribute('aria-expanded', 'true')
|
|
237
|
-
f.settings({ enabled: false }); f.settings({ enabled: true })
|
|
238
|
-
await settle()
|
|
239
|
-
assert.equal(f.claims.length, 0)
|
|
240
|
-
} finally { f.close() }
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
function bodyFixture() {
|
|
244
|
-
const dom = new JSDOM(`<div data-conversation-session="session-a">${thought('14:assistant-step1:1')}</div>`)
|
|
245
|
-
const marker = dom.window.document.createElement('div')
|
|
246
|
-
dom.window.document.querySelector('[data-variant="think"]').append(marker)
|
|
247
|
-
let Body
|
|
248
|
-
const ctx = { slots: { inject: (_key, fn) => fn(), register: (_options, component) => { Body = component; return () => {} } }, effect: () => {} }
|
|
249
|
-
registerChatAdapter(ctx, {}, React, 'novel', { getSampleLicenseHref })
|
|
250
|
-
return { dom, marker, Body }
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
test('纯文本小说和章节位于原思考后的分隔线下;重挂载和流式更新不读取', async () => {
|
|
254
|
-
const { dom, marker, Body } = bodyFixture()
|
|
255
|
-
const settings = { enabled: true, currentBookId: 'book', targetTypes: ['reasoning'] }
|
|
256
|
-
let status = { kind: 'ready', index: 0, passage: { text: 'A cached passage', chapterTitle: '第一章' } }
|
|
257
|
-
let claims = 0
|
|
258
|
-
const forwarded = []
|
|
259
|
-
const props = {
|
|
260
|
-
text: 'ORIGINAL MODEL THOUGHT', running: true, sessionId: 'session-a',
|
|
261
|
-
useNovelSettings: selector => selector(settings),
|
|
262
|
-
useNovelPassages: selector => selector({ [passageSnapshotKey('book', readingKey('session-a', '14:assistant-step1:1'))]: status }),
|
|
263
|
-
renderFactorySlot: (name, input) => { forwarded.push({ name, ...input }); return React.createElement('p', null, input.text) }, t: key => key,
|
|
264
|
-
onClaimPassage: () => { claims += 1 },
|
|
265
|
-
}
|
|
266
|
-
let view
|
|
267
|
-
try {
|
|
268
|
-
await act(async () => { view = TestRenderer.create(React.createElement(Body, props), { createNodeMock: () => marker }) })
|
|
269
|
-
assert.equal(view.root.findByProps({ 'data-novel-original': true }).findByType('p').children.join(''), props.text)
|
|
270
|
-
assert.equal(view.root.findByProps({ 'data-novel-passage': 1 }).children.join(''), 'A cached passage')
|
|
271
|
-
const passage = view.root.findByProps({ 'data-novel-passage': 1 })
|
|
272
|
-
assert.equal(passage.props.style.maxHeight, 'min(40vh, 20.8em)')
|
|
273
|
-
assert.equal(passage.props.style.overflowY, 'auto')
|
|
274
|
-
assert.equal(passage.props.tabIndex, 0, '溢出的完整正文支持键盘滚动')
|
|
275
|
-
assert.equal(view.root.findByProps({ 'data-novel-original': true }).props.style, undefined, '不约束宿主原始正文')
|
|
276
|
-
assert.equal(view.root.findByProps({ 'data-novel-label': true }).children.join(''), 'chat.novel · 第一章')
|
|
277
|
-
assert.equal(view.root.findAllByType('hr').length, 1)
|
|
278
|
-
assert.equal(view.toJSON().children[0].props['data-novel-original'], true)
|
|
279
|
-
assert.equal(view.toJSON().children[1].props['data-novel-append'], true)
|
|
280
|
-
assert.equal(view.toJSON().children[1].props.style.whiteSpace, 'pre-wrap')
|
|
281
|
-
await act(async () => view.update(React.createElement(Body, { ...props, text: 'stream update', running: false })))
|
|
282
|
-
assert.match(JSON.stringify(view.toJSON()), /stream update.*A cached passage/u)
|
|
283
|
-
assert.ok(forwarded.some(input => input.name === 'conversation.chat.reasoning.content' && input.text === 'stream update' && input.running === false))
|
|
284
|
-
assert.equal(claims, 0)
|
|
285
|
-
await act(async () => view.unmount())
|
|
286
|
-
await act(async () => { view = TestRenderer.create(React.createElement(Body, props), { createNodeMock: () => marker }) })
|
|
287
|
-
assert.match(JSON.stringify(view.toJSON()), /ORIGINAL MODEL THOUGHT.*A cached passage/u)
|
|
288
|
-
const literal = 'normal (prose) $100 and $200\na\n===\n indent\nuser@example.com https://example.invalid  <script> & text'
|
|
289
|
-
status = { kind: 'ready', index: 0, passage: { text: literal } }
|
|
290
|
-
await act(async () => view.update(React.createElement(Body, props)))
|
|
291
|
-
assert.equal(view.root.findByProps({ 'data-novel-passage': 1 }).children.join(''), literal)
|
|
292
|
-
assert.ok(forwarded.every(input => ['ORIGINAL MODEL THOUGHT', 'stream update'].includes(input.text)), '只有真实思考进入宿主 Markdown Factory')
|
|
293
|
-
for (const tag of ['a', 'img', 'script', 'pre', 'h1']) assert.equal(view.root.findAllByType(tag).length, 0)
|
|
294
|
-
for (const next of [undefined, { kind: 'loading' }, { kind: 'error' }, { kind: 'end' }]) {
|
|
295
|
-
status = next
|
|
296
|
-
await act(async () => view.update(React.createElement(Body, { ...props, running: false })))
|
|
297
|
-
assert.match(JSON.stringify(view.toJSON()), /ORIGINAL MODEL THOUGHT/u)
|
|
298
|
-
assert.equal(view.root.findAllByProps({ 'data-novel-passage': 1 }).length, 0)
|
|
299
|
-
const notice = view.root.findByProps({ role: next?.kind === 'error' ? 'alert' : 'status' })
|
|
300
|
-
assert.equal(notice.children.length, 1, '状态消息同样作为纯文本呈现')
|
|
301
|
-
assert.equal(forwarded.at(-1).name, 'conversation.chat.reasoning.content')
|
|
302
|
-
assert.equal(forwarded.at(-1).text, 'ORIGINAL MODEL THOUGHT')
|
|
303
|
-
assert.equal(forwarded.at(-1).running, false)
|
|
304
|
-
}
|
|
305
|
-
assert.equal(claims, 0)
|
|
306
|
-
settings.enabled = false
|
|
307
|
-
await act(async () => view.update(React.createElement(Body, props)))
|
|
308
|
-
assert.equal(view.root.findAllByProps({ 'data-novel-append': true }).length, 0)
|
|
309
|
-
} finally { await act(async () => view?.unmount()); dom.window.close() }
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
function toolRow(name, variant, callId) {
|
|
313
|
-
const native = row().replace('data-variant="think"', `data-tool="${name}" data-variant="${variant}"`)
|
|
314
|
-
return `<div data-chat-flow-kind="tool-call" data-chat-node-key="tools"><div data-chat-call-id="${callId}">${native.slice(0, -6)}<p data-native-output>真实工具输入输出</p>${native.slice(-6)}</div></div>`
|
|
315
|
-
}
|
|
316
|
-
function commandRow() {
|
|
317
|
-
return '<div data-chat-flow-kind="tool-call" data-chat-node-key="tools"><div data-chat-call-id="command"><div class="native-command-card"><div data-sample="bash" data-variant="bash" role="button" aria-expanded="false" tabindex="0"><span><svg data-glyph="bash"></svg></span><span><span><span>运行命令</span><span data-shimmer-decoration></span><span>原生摘要</span></span></span></div><pre data-native-output>exit code: 0</pre></div></div></div>'
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
test('五类区域独立定位,工具原正文保持且分隔线下只追加一次,卸载清理', async () => {
|
|
321
|
-
const f = adapterFixture(thought('thought') + toolRow('web_fetch', 'read', 'other') + toolRow('read', 'read', 'read') + toolRow('grep', 'search', 'search') + commandRow())
|
|
322
|
-
try {
|
|
323
|
-
f.start()
|
|
324
|
-
f.settings({ targetTypes: ['reasoning', 'tool-call', 'read-file', 'command', 'file-search'] })
|
|
325
|
-
const outputs = [...document.querySelectorAll('[data-native-output]')]
|
|
326
|
-
const originalOutputs = outputs.map(node => node.outerHTML)
|
|
327
|
-
const headers = [...document.querySelectorAll('[data-disclosure-row], [data-sample="bash"]')]
|
|
328
|
-
for (const header of headers) {
|
|
329
|
-
f.actions.click(trustedClick(header))
|
|
330
|
-
header.setAttribute('aria-expanded', 'true')
|
|
331
|
-
}
|
|
332
|
-
await settle()
|
|
333
|
-
assert.deepEqual(f.types, ['reasoning', 'tool-call', 'read-file', 'file-search', 'command'])
|
|
334
|
-
const literal = '价格 $100 and $200 (正文) user@example.com\na\n===\n'
|
|
335
|
-
for (const [index, key] of f.claims.entries()) f.passage(key, { kind: 'ready', index, passage: { text: literal, chapterTitle: '章节甲' } })
|
|
336
|
-
await settle()
|
|
337
|
-
assert.equal(document.querySelectorAll('[data-novel-append]').length, 4, '思考正文由其原生body slot负责')
|
|
338
|
-
for (const append of document.querySelectorAll('[data-novel-append]')) {
|
|
339
|
-
assert.equal(append.querySelectorAll('hr').length, 1)
|
|
340
|
-
assert.ok(append.previousElementSibling.hasAttribute('data-native-output'), '小说位于原生工具正文之后')
|
|
341
|
-
assert.match(append.querySelector('[data-novel-label]').textContent, /章节甲/u)
|
|
342
|
-
assert.equal(append.lastElementChild.textContent, literal)
|
|
343
|
-
assert.equal(append.lastElementChild.style.maxHeight, 'min(40vh, 20.8em)')
|
|
344
|
-
assert.equal(append.lastElementChild.style.overflowY, 'auto')
|
|
345
|
-
assert.equal(append.lastElementChild.tabIndex, 0)
|
|
346
|
-
assert.equal(append.querySelector('a, img, script, pre, h1'), null)
|
|
347
|
-
}
|
|
348
|
-
assert.deepEqual(outputs.map(node => node.outerHTML), originalOutputs)
|
|
349
|
-
f.settings({ targetTypes: ['reasoning'] })
|
|
350
|
-
assert.equal(document.querySelectorAll('[data-novel-append]').length, 0)
|
|
351
|
-
assert.deepEqual(outputs.map(node => node.outerHTML), originalOutputs)
|
|
352
|
-
} finally { f.close() }
|
|
353
|
-
})
|
|
354
|
-
|
|
355
|
-
test('工具自动展开及内部文件链接不消耗,折叠重挂载仍用稳定callId键', async () => {
|
|
356
|
-
const f = adapterFixture(toolRow('read', 'read', 'call-a'))
|
|
357
|
-
try {
|
|
358
|
-
f.start(); f.settings({ targetTypes: ['read-file'] })
|
|
359
|
-
let header = document.querySelector('[data-disclosure-row]')
|
|
360
|
-
header.setAttribute('aria-expanded', 'true')
|
|
361
|
-
await settle()
|
|
362
|
-
assert.equal(f.claims.length, 0)
|
|
363
|
-
const link = document.createElement('button'); header.append(link)
|
|
364
|
-
f.actions.click(trustedClick(link)); await settle()
|
|
365
|
-
assert.equal(f.claims.length, 0)
|
|
366
|
-
header.setAttribute('aria-expanded', 'false')
|
|
367
|
-
f.actions.click(trustedClick(header)); header.setAttribute('aria-expanded', 'true'); await settle()
|
|
368
|
-
const key = f.claims[0]
|
|
369
|
-
document.querySelector('[data-chat-flow-kind]').outerHTML = toolRow('read', 'read', 'call-a')
|
|
370
|
-
await settle()
|
|
371
|
-
header = document.querySelector('[data-disclosure-row]')
|
|
372
|
-
f.actions.click(trustedClick(header)); header.setAttribute('aria-expanded', 'true'); await settle()
|
|
373
|
-
assert.deepEqual(f.claims, [key, key], '组件重挂载不生成另一个阅读缓存键')
|
|
374
|
-
assert.equal(f.types[0], 'read-file')
|
|
375
|
-
} finally { f.close() }
|
|
376
|
-
})
|
|
377
|
-
|
|
378
|
-
test('native thought remains visible when its DOM identity is unavailable', async () => {
|
|
379
|
-
const { dom, Body } = bodyFixture()
|
|
380
|
-
let view
|
|
381
|
-
try {
|
|
382
|
-
await act(async () => { view = TestRenderer.create(React.createElement(Body, {
|
|
383
|
-
text: 'REAL THOUGHT BEFORE IDENTITY', running: false,
|
|
384
|
-
useNovelSettings: select => select({ enabled: true, targetTypes: ['reasoning'] }),
|
|
385
|
-
useNovelPassages: select => select({}), renderFactorySlot: (_name, input) => input.text, t: key => key,
|
|
386
|
-
}), { createNodeMock: () => null }) })
|
|
387
|
-
assert.match(JSON.stringify(view.toJSON()), /REAL THOUGHT BEFORE IDENTITY/u)
|
|
388
|
-
assert.equal(view.root.findAllByProps({ 'data-novel-append': true }).length, 0)
|
|
389
|
-
} finally { await act(async () => view?.unmount()); dom.window.close() }
|
|
390
|
-
})
|
|
391
|
-
|
|
392
|
-
test('默认关闭续读时同一区域重复主动展开仍走缓存 claim', async () => {
|
|
393
|
-
const f = adapterFixture(thought('node-a'))
|
|
394
|
-
try {
|
|
395
|
-
f.start()
|
|
396
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
397
|
-
for (let index = 0; index < 2; index += 1) {
|
|
398
|
-
header.setAttribute('aria-expanded', 'false')
|
|
399
|
-
f.actions.click(trustedClick(header))
|
|
400
|
-
header.setAttribute('aria-expanded', 'true')
|
|
401
|
-
await settle()
|
|
402
|
-
}
|
|
403
|
-
assert.equal(f.continues.length, 0)
|
|
404
|
-
assert.deepEqual(f.claims, [readingKey('session-a', 'node-a'), readingKey('session-a', 'node-a')])
|
|
405
|
-
} finally { f.close() }
|
|
406
|
-
})
|
|
407
|
-
|
|
408
|
-
test('开启续读后同一区域主动展开改为继续下一段,非可信事件仍不读', async () => {
|
|
409
|
-
const f = adapterFixture(thought('node-a'))
|
|
410
|
-
try {
|
|
411
|
-
f.start()
|
|
412
|
-
f.settings({ continueOnReopen: true })
|
|
413
|
-
const header = document.querySelector('[data-disclosure-row]')
|
|
414
|
-
header.setAttribute('aria-expanded', 'false')
|
|
415
|
-
f.actions.click({ ...trustedClick(header), isTrusted: false })
|
|
416
|
-
header.setAttribute('aria-expanded', 'true')
|
|
417
|
-
await settle()
|
|
418
|
-
assert.equal(f.continues.length, 0, '未验证的用户事件不推进阅读')
|
|
419
|
-
for (let index = 0; index < 2; index += 1) {
|
|
420
|
-
header.setAttribute('aria-expanded', 'false')
|
|
421
|
-
f.actions.click(trustedClick(header))
|
|
422
|
-
header.setAttribute('aria-expanded', 'true')
|
|
423
|
-
await settle()
|
|
424
|
-
}
|
|
425
|
-
assert.deepEqual(f.continues, [readingKey('session-a', 'node-a'), readingKey('session-a', 'node-a')])
|
|
426
|
-
assert.equal(f.claims.length, 0)
|
|
427
|
-
} finally { f.close() }
|
|
428
|
-
})
|
|
429
|
-
|
|
430
|
-
test('流式文本和属性变更在同一帧内只合并成一次重扫', async () => {
|
|
431
|
-
const f = adapterFixture(thought('node-a'))
|
|
432
|
-
const frames = []
|
|
433
|
-
let cancelled = 0
|
|
434
|
-
f.dom.window.requestAnimationFrame = callback => { frames.push(callback); return frames.length }
|
|
435
|
-
f.dom.window.cancelAnimationFrame = () => { cancelled += 1 }
|
|
436
|
-
try {
|
|
437
|
-
f.start()
|
|
438
|
-
const title = document.querySelector('.native-title')
|
|
439
|
-
for (let index = 0; index < 4; index += 1) title.firstChild.nodeValue = `stream ${index}`
|
|
440
|
-
document.querySelector('[data-disclosure-row]').setAttribute('data-state', 'open')
|
|
441
|
-
await settle()
|
|
442
|
-
assert.equal(frames.length, 1, '一帧内的多次变更共享一次刷新,而不是每个 token 全量扫描')
|
|
443
|
-
frames[0]()
|
|
444
|
-
assert.equal(title.textContent, '思考')
|
|
445
|
-
assert.equal(cancelled, 0)
|
|
446
|
-
} finally { f.close() }
|
|
447
|
-
})
|
|
448
|
-
|
|
449
|
-
test('PG reasoning has a full readable §1.E.1 notice and offline license outside the clipped passage', async () => {
|
|
450
|
-
const { dom, marker, Body } = bodyFixture()
|
|
451
|
-
const settings = { enabled: true, currentBookId: 'legacy-arbitrary-id', targetTypes: ['reasoning'] }
|
|
452
|
-
let status = { kind: 'ready', index: 12, projectGutenberg: true, passage: { text: 'PG prose <img src=x> $100' } }
|
|
453
|
-
const props = {
|
|
454
|
-
text: 'REAL HOST THOUGHT', running: true, sessionId: 'session-a',
|
|
455
|
-
useNovelSettings: select => select(settings),
|
|
456
|
-
useNovelPassages: select => select({ [passageSnapshotKey(settings.currentBookId, readingKey('session-a', '14:assistant-step1:1'))]: status }),
|
|
457
|
-
renderFactorySlot: (_name, input) => React.createElement('p', null, input.text), t: key => key,
|
|
458
|
-
}
|
|
459
|
-
let view
|
|
460
|
-
try {
|
|
461
|
-
await act(async () => { view = TestRenderer.create(React.createElement(Body, props), { createNodeMock: () => marker }) })
|
|
462
|
-
const notice = view.root.findByProps({ 'data-novel-pg-notice': true })
|
|
463
|
-
const passage = view.root.findByProps({ 'data-novel-passage': 13 })
|
|
464
|
-
assert.equal(notice.parent, passage.parent, 'notice is a sibling, never inside the scroll viewport')
|
|
465
|
-
assert.equal(notice.findByType('p').children.join(''), PG_NOTICE)
|
|
466
|
-
assert.equal(notice.props.style.maxHeight, undefined)
|
|
467
|
-
assert.equal(notice.props.style.overflowY, undefined)
|
|
468
|
-
assert.equal(notice.props.style.whiteSpace, 'normal')
|
|
469
|
-
assert.equal(view.root.findAllByType('details').length, 0)
|
|
470
|
-
const license = notice.findByType('a')
|
|
471
|
-
assert.equal(license.props.href, licenseHref)
|
|
472
|
-
assert.equal(license.props.download, PG_LICENSE_DOWNLOAD)
|
|
473
|
-
assert.equal(decodeURIComponent(license.props.href.split(',').slice(1).join(',')), fullLicense)
|
|
474
|
-
assert.equal(passage.children.join(''), status.passage.text, 'novel stays literal text')
|
|
475
|
-
assert.equal(view.root.findAllByType('img').length, 0)
|
|
476
|
-
assert.equal(view.root.findByProps({ 'data-novel-original': true }).findByType('p').children.join(''), props.text)
|
|
477
|
-
for (const next of [{ ...status, projectGutenberg: false }, { kind: 'loading' }, { kind: 'end' }, { kind: 'error' }]) {
|
|
478
|
-
status = next
|
|
479
|
-
await act(async () => view.update(React.createElement(Body, props)))
|
|
480
|
-
assert.equal(view.root.findAllByProps({ 'data-novel-pg-notice': true }).length, 0)
|
|
481
|
-
assert.equal(view.root.findAllByType('a').length, 0, 'AQ/unrelated books and status messages have no PG notice')
|
|
482
|
-
}
|
|
483
|
-
} finally { await act(async () => view?.unmount()); dom.window.close() }
|
|
484
|
-
})
|
|
485
|
-
|
|
486
|
-
test('PG tool append exposes identical full notice/offline license without touching host output', async () => {
|
|
487
|
-
const f = adapterFixture(toolRow('read', 'read', 'pg-call') + commandRow())
|
|
488
|
-
try {
|
|
489
|
-
f.start(); f.settings({ targetTypes: ['read-file', 'command'] })
|
|
490
|
-
const outputs = [...document.querySelectorAll('[data-native-output]')]
|
|
491
|
-
const originalOutputs = outputs.map(node => node.outerHTML)
|
|
492
|
-
for (const header of document.querySelectorAll('[data-disclosure-row], [data-sample="bash"]')) {
|
|
493
|
-
f.actions.click(trustedClick(header)); header.setAttribute('aria-expanded', 'true')
|
|
494
|
-
}
|
|
495
|
-
await settle()
|
|
496
|
-
for (const key of f.claims) f.passage(key, { kind: 'ready', index: 3, projectGutenberg: true, passage: { text: 'Literal PG <script> & prose' } })
|
|
497
|
-
await settle()
|
|
498
|
-
assert.equal(document.querySelectorAll('[data-novel-pg-notice]').length, 2)
|
|
499
|
-
for (const append of document.querySelectorAll('[data-novel-append]')) {
|
|
500
|
-
const notice = append.querySelector('[data-novel-pg-notice]')
|
|
501
|
-
const passage = append.querySelector('[data-novel-passage]')
|
|
502
|
-
assert.equal(notice.parentElement, passage.parentElement)
|
|
503
|
-
assert.equal(notice.querySelector('p').textContent, PG_NOTICE)
|
|
504
|
-
assert.equal(notice.style.maxHeight, '')
|
|
505
|
-
assert.equal(notice.style.overflowY, '')
|
|
506
|
-
assert.equal(append.querySelector('details, script'), null)
|
|
507
|
-
assert.equal(notice.querySelector('a').getAttribute('href'), licenseHref)
|
|
508
|
-
assert.equal(notice.querySelector('a').download, PG_LICENSE_DOWNLOAD)
|
|
509
|
-
assert.equal(passage.textContent, 'Literal PG <script> & prose')
|
|
510
|
-
}
|
|
511
|
-
assert.deepEqual(outputs.map(node => node.outerHTML), originalOutputs)
|
|
512
|
-
f.settings({ mainTitle: 'New title' })
|
|
513
|
-
assert.equal(document.querySelectorAll('[data-novel-pg-notice]').length, 2, 'refresh never duplicates notices')
|
|
514
|
-
for (const key of f.claims) f.passage(key, { kind: 'ready', index: 4, projectGutenberg: false, passage: { text: 'AQ or unrelated prose' } })
|
|
515
|
-
assert.equal(document.querySelector('[data-novel-pg-notice], [data-novel-append] a'), null)
|
|
516
|
-
assert.deepEqual(outputs.map(node => node.outerHTML), originalOutputs)
|
|
517
|
-
} finally { f.close() }
|
|
518
|
-
})
|
package/test/client.test.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { readFile } from 'node:fs/promises'
|
|
4
|
-
import vm from 'node:vm'
|
|
5
|
-
import React from 'react'
|
|
6
|
-
import TestRenderer, { act } from 'react-test-renderer'
|
|
7
|
-
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb'
|
|
8
|
-
import { IndexedDbStorage } from '../src/persistence.js'
|
|
9
|
-
import { DEFAULT_SETTINGS } from '../src/store.js'
|
|
10
|
-
|
|
11
|
-
import { HostSwitch, hostPrimitives } from './helpers/host-primitives.js'
|
|
12
|
-
|
|
13
|
-
test('构建产物只在本包详情注册设置,复用宿主 Switch 与原有书库', async () => {
|
|
14
|
-
const indexedDB = new IDBFactory()
|
|
15
|
-
const storage = new IndexedDbStorage(indexedDB, IDBKeyRange)
|
|
16
|
-
const book = { id: 'existing-book', title: '已有书籍', format: 'txt', progress: 1, segmentCount: 2 }
|
|
17
|
-
const settings = { ...DEFAULT_SETTINGS, enabled: false, currentBookId: book.id, mainTitle: '已保存标题' }
|
|
18
|
-
await storage.addBook(book, [{ text: 'A' }, { text: 'B' }])
|
|
19
|
-
await storage.saveSettings(settings)
|
|
20
|
-
const manifest = JSON.parse(await readFile(new URL('../samples/ebooks/sources.json', import.meta.url), 'utf8'))
|
|
21
|
-
const notices = await readFile(new URL('../samples/ebooks/LICENSE.md', import.meta.url), 'utf8')
|
|
22
|
-
const imports = [], effects = [], injections = [], registrations = [], decodedPayloads = []
|
|
23
|
-
let module
|
|
24
|
-
vm.runInNewContext(await readFile(new URL('../lib/client.js', import.meta.url), 'utf8'), {
|
|
25
|
-
window: { __ModuleLoader__: { load: value => { module = value } } },
|
|
26
|
-
indexedDB, IDBKeyRange, TextDecoder, TextEncoder, URL, Blob,
|
|
27
|
-
atob: payload => { decodedPayloads.push(payload); return atob(payload) },
|
|
28
|
-
setTimeout, clearTimeout, setImmediate, clearImmediate,
|
|
29
|
-
console,
|
|
30
|
-
})
|
|
31
|
-
const plugin = module.factory(name => {
|
|
32
|
-
imports.push(name)
|
|
33
|
-
if (name === 'react') return React
|
|
34
|
-
if (name === '@deepseek-ai/dsh-client-ui-primitives') return hostPrimitives
|
|
35
|
-
throw new Error(`Unexpected host import: ${name}`)
|
|
36
|
-
})
|
|
37
|
-
plugin.apply({
|
|
38
|
-
locale: { register: () => () => {}, bind: () => key => key },
|
|
39
|
-
effect: callback => { effects.push(callback) },
|
|
40
|
-
slots: {
|
|
41
|
-
inject: (name, callback) => { injections.push(name); callback() },
|
|
42
|
-
register: (options, Component) => { registrations.push({ options, Component }); return () => {} },
|
|
43
|
-
},
|
|
44
|
-
})
|
|
45
|
-
const own = registrations.find(entry => entry.options.name === 'plugins.bundle.config')
|
|
46
|
-
let view
|
|
47
|
-
try {
|
|
48
|
-
assert.equal(module.id, '@gongfpp/dsh-novel-fish')
|
|
49
|
-
assert.ok(imports.includes('@deepseek-ai/dsh-client-ui-primitives'))
|
|
50
|
-
assert.equal(injections.includes('settings.general.item'), false)
|
|
51
|
-
assert.equal(own.options.key, module.id)
|
|
52
|
-
assert.equal(registrations.filter(entry => entry.options.name === 'plugins.bundle.config').length, 1)
|
|
53
|
-
assert.ok(injections.includes('conversation.chat.reasoning.body'))
|
|
54
|
-
const injected = own.options.inject()
|
|
55
|
-
const source = injected.hooks.novelState
|
|
56
|
-
await new Promise((resolve, reject) => {
|
|
57
|
-
// Real default-book parsing competes with parallel EPUB/host tests. This
|
|
58
|
-
// checks completion, not a 2-second performance SLA on the test runner.
|
|
59
|
-
const timer = setTimeout(() => { off(); reject(new Error('Book hydration timed out')) }, 10000)
|
|
60
|
-
const check = () => {
|
|
61
|
-
const state = source.getSnapshot()
|
|
62
|
-
if (state.error || state.hydrated) {
|
|
63
|
-
off(); clearTimeout(timer)
|
|
64
|
-
if (state.error) reject(new Error(`Book hydration failed: ${state.error}`))
|
|
65
|
-
else resolve()
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const off = source.subscribe(check)
|
|
69
|
-
check()
|
|
70
|
-
})
|
|
71
|
-
assert.equal(source.getSnapshot().error, null)
|
|
72
|
-
assert.equal(source.getSnapshot().books.find(entry => entry.id === book.id).progress, 1)
|
|
73
|
-
assert.equal(source.getSnapshot().settings.mainTitle, settings.mainTitle)
|
|
74
|
-
const props = { ...injected, t: key => key, view: 'page',
|
|
75
|
-
useNovelState: select => select(React.useSyncExternalStore(source.subscribe, source.getSnapshot)),
|
|
76
|
-
useNovelUi: select => select(React.useSyncExternalStore(injected.hooks.novelUi.subscribe, injected.hooks.novelUi.getSnapshot)),
|
|
77
|
-
}
|
|
78
|
-
await act(async () => { view = TestRenderer.create(React.createElement(own.Component, props)) })
|
|
79
|
-
const enabledSwitch = () => view.root.findAllByType(HostSwitch).find(node => node.props.label === 'settings.enabled')
|
|
80
|
-
assert.equal(enabledSwitch().props.checked, false)
|
|
81
|
-
assert.equal(source.getSnapshot().books.find(entry => entry.id === book.id).progress, 1)
|
|
82
|
-
await act(async () => { await injected.onSetOption('enabled', true) })
|
|
83
|
-
assert.equal(enabledSwitch().props.checked, true)
|
|
84
|
-
const saved = await storage.load()
|
|
85
|
-
assert.equal(saved.settings.enabled, true)
|
|
86
|
-
assert.equal(saved.settings.currentBookId, book.id)
|
|
87
|
-
assert.equal(saved.settings.mainTitle, settings.mainTitle)
|
|
88
|
-
assert.equal(saved.books.find(entry => entry.id === book.id).progress, 1)
|
|
89
|
-
assert.deepEqual(await storage.getSegment(book.id, 1), { text: 'B' })
|
|
90
|
-
const expectedSamples = manifest.books.map(({ id, title, author, format, bytes, source, license }) => ({ id, title, author, format, bytes, source, license }))
|
|
91
|
-
assert.deepEqual(JSON.parse(JSON.stringify(injected.sampleBooks)), expectedSamples)
|
|
92
|
-
for (const sample of manifest.books) {
|
|
93
|
-
assert.ok(sample.license?.trim(), `Missing concise license for ${sample.id}`)
|
|
94
|
-
assert.equal(sample.bytes, (await readFile(new URL(`../samples/ebooks/${sample.file}`, import.meta.url))).byteLength)
|
|
95
|
-
}
|
|
96
|
-
assert.equal(decodedPayloads.length, manifest.books.filter(book => book.format !== 'md').length, 'Initial hydration provisions all bundled books once')
|
|
97
|
-
assert.equal(injected.onImportSample, undefined, 'No separate bundled import action')
|
|
98
|
-
const licenseHref = injected.getSampleLicenseHref()
|
|
99
|
-
assert.ok(licenseHref.startsWith('data:text/plain;charset=utf-8,'))
|
|
100
|
-
assert.equal(decodeURIComponent(licenseHref.slice(licenseHref.indexOf(',') + 1)), notices)
|
|
101
|
-
assert.equal(injected.getSampleLicenseHref(), licenseHref)
|
|
102
|
-
assert.equal(decodedPayloads.length, manifest.books.filter(book => book.format !== 'md').length, 'Offline notices must not decode another ebook')
|
|
103
|
-
for (const sample of injected.sampleBooks) {
|
|
104
|
-
const seeded = source.getSnapshot().books.find(entry => entry.bundledId === sample.id)
|
|
105
|
-
assert.equal(seeded.id, `bundled:${sample.id}`)
|
|
106
|
-
assert.equal(seeded.title, sample.title)
|
|
107
|
-
assert.equal(seeded.format, sample.format)
|
|
108
|
-
assert.ok(seeded.segmentCount > 1)
|
|
109
|
-
}
|
|
110
|
-
assert.equal((await storage.load()).books.length, 1 + manifest.books.length)
|
|
111
|
-
const seededId = `bundled:${injected.sampleBooks[0].id}`
|
|
112
|
-
await act(async () => { await injected.onSelectBook(seededId) })
|
|
113
|
-
assert.equal(source.getSnapshot().settings.currentBookId, seededId)
|
|
114
|
-
await act(async () => { await injected.onDeleteBook(seededId) })
|
|
115
|
-
assert.equal(source.getSnapshot().books.some(entry => entry.id === seededId), false)
|
|
116
|
-
assert.equal((await storage.load()).bundledBooksInitialized, true)
|
|
117
|
-
assert.equal((await storage.load()).books.length, manifest.books.length)
|
|
118
|
-
assert.equal(source.getSnapshot().books.find(entry => entry.id === book.id).progress, 1)
|
|
119
|
-
} finally {
|
|
120
|
-
if (view) await act(async () => view.unmount())
|
|
121
|
-
effects[1]()() // Dispose only the store effect; do not install chat DOM observers.
|
|
122
|
-
storage.close()
|
|
123
|
-
}
|
|
124
|
-
})
|