feat: implement TXT reader MVP per product/tech design

- Vite + Vue 3 + TS scaffold with hash router (bookshelf / reader)
- core/: encoding detection (UTF-8 strict, GBK fallback, gb18030 last resort),
  paragraph chunker with original-text offsets, offset utils, IndexedDB wrapper
- Reader: windowed paragraph rendering with estimated spacers (constant ~240 DOM
  paragraphs), font-size/line-height settings via CSS vars persisted to localStorage
- Progress: character-offset based, debounced save + flush on back/unload, exact
  restore via binary search; shelf shows percent
- 21 vitest unit tests for core modules
This commit is contained in:
2026-09-13 00:28:06 +08:00
parent d009a14ca2
commit 24373e298a
26 changed files with 3668 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest'
import { splitParagraphs } from '../src/core/chunker'
describe('splitParagraphs', () => {
it('按 \\n 切分并去除首尾空白', () => {
const { paragraphs } = splitParagraphs(' 第一段 \n第二段\n\n 第三段 ')
expect(paragraphs).toEqual(['第一段', '第二段', '第三段'])
})
it('处理 \\r\\n 换行', () => {
const { paragraphs } = splitParagraphs('甲\r\n乙\r\n丙')
expect(paragraphs).toEqual(['甲', '乙', '丙'])
})
it('空行不产生段落,连续空行只算段间距', () => {
const { paragraphs } = splitParagraphs('a\n\n\n\nb')
expect(paragraphs).toEqual(['a', 'b'])
})
it('offsets 与原文对齐:slice 反向校验', () => {
const text = '第一段落内容\n\n第二段落:很长一点。\r\n第三段'
const { paragraphs, offsets } = splitParagraphs(text)
for (let i = 0; i < paragraphs.length; i++) {
const end = i + 1 < offsets.length ? offsets[i + 1] : text.length
// 该段文本必然出现在 text.slice(offsets[i], end) 中
expect(text.slice(offsets[i], end)).toContain(paragraphs[i])
}
expect(text.slice(offsets[0], offsets[0] + paragraphs[0].length)).toBe(paragraphs[0])
expect(text.slice(offsets[1], offsets[1] + paragraphs[1].length)).toBe(paragraphs[1])
})
it('totalChars 等于各段长度之和', () => {
const { paragraphs, offsets, totalChars } = splitParagraphs('hello\nworld!')
expect(totalChars).toBe(paragraphs.reduce((n, p) => n + p.length, 0))
expect(totalChars).toBe(11)
expect(offsets).toEqual([0, 6])
})
it('空文本返回空结果', () => {
const r = splitParagraphs('')
expect(r.paragraphs).toEqual([])
expect(r.offsets).toEqual([])
expect(r.totalChars).toBe(0)
})
it('大文本性能与正确性:10 万段', () => {
const text = Array.from({ length: 100000 }, (_, i) => `第${i}段内容`).join('\n')
const r = splitParagraphs(text)
expect(r.paragraphs.length).toBe(100000)
expect(r.offsets.length).toBe(100000)
expect(text.slice(r.offsets[99999], r.offsets[99999] + 10)).toBe(r.paragraphs[99999])
})
})
+50
View File
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest'
import * as iconv from 'iconv-lite'
import { decodeText } from '../src/core/encoding'
function bytes(...groups: Array<number[] | string>): ArrayBuffer {
const parts: number[] = []
for (const g of groups) {
if (typeof g === 'string') for (const ch of g) parts.push(ch.charCodeAt(0))
else parts.push(...g)
}
return new Uint8Array(parts).buffer
}
function encode(text: string, enc: 'gbk' | 'gb18030'): ArrayBuffer {
const buf = iconv.encode(text, enc)
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
}
const utf8Text = '第一行:Hello 世界!\n第二行:编码检测测试。'
describe('decodeText', () => {
it('UTF-8 文本正确解码', () => {
const buf = Buffer.from(utf8Text, 'utf8')
expect(decodeText(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength))).toBe(utf8Text)
})
it('GBK 中文文本回退正确、无乱码', () => {
expect(decodeText(encode('白日依山尽,黄河入海流。', 'gbk'))).toBe('白日依山尽,黄河入海流。')
})
it('GB18030 生僻字文本正确解码', () => {
const text = '魜喡玤嘦(生僻字测试)'
expect(decodeText(encode(text, 'gb18030'))).not.toContain('\uFFFD')
expect(decodeText(encode(text, 'gb18030'))).toContain('生僻字测试')
})
it('不含 U+FFFD 即视为成功:UTF-8 带 BOM', () => {
const buf = Buffer.from('\uFEFF' + utf8Text, 'utf8')
const out = decodeText(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength))
expect(out).toContain('Hello 世界')
})
it('异常字节不崩溃', () => {
expect(() => decodeText(bytes([0xff, 0xfe, 0x00, 0x41]))).not.toThrow()
})
it('空文件返回空字符串', () => {
expect(decodeText(new ArrayBuffer(0))).toBe('')
})
})
+52
View File
@@ -0,0 +1,52 @@
import { describe, expect, it } from 'vitest'
import { paragraphIndexForOffset, progressPercent } from '../src/core/offset'
import { splitParagraphs } from '../src/core/chunker'
describe('paragraphIndexForOffset', () => {
const offsets = [0, 10, 20, 30]
it('命中段落起点返回该段', () => {
expect(paragraphIndexForOffset(offsets, 0)).toBe(0)
expect(paragraphIndexForOffset(offsets, 10)).toBe(1)
expect(paragraphIndexForOffset(offsets, 30)).toBe(3)
})
it('段落中间的 offset 归属该段', () => {
expect(paragraphIndexForOffset(offsets, 15)).toBe(1)
expect(paragraphIndexForOffset(offsets, 29)).toBe(2)
})
it('超出末尾归属最后一段', () => {
expect(paragraphIndexForOffset(offsets, 100)).toBe(3)
})
it('空数组返回 -1', () => {
expect(paragraphIndexForOffset([], 0)).toBe(-1)
})
it('与 chunker 联动:任意 offset 定位到正确段落', () => {
const text = Array.from({ length: 50 }, (_, i) => `段落${i}:${'内容'.repeat(i + 1)}`).join('\n\n')
const { paragraphs, offsets, totalChars } = splitParagraphs(text)
for (const target of [0, 1, totalChars - 1, Math.floor(totalChars / 2)]) {
const idx = paragraphIndexForOffset(offsets, target)
expect(text.slice(offsets[idx], offsets[idx] + paragraphs[idx].length)).toBe(paragraphs[idx])
}
})
})
describe('progressPercent', () => {
it('常规计算并四舍五入', () => {
expect(progressPercent(0, 1000)).toBe(0)
expect(progressPercent(500, 1000)).toBe(50)
expect(progressPercent(1234, 10000)).toBe(12)
})
it('上限 100,下限 0', () => {
expect(progressPercent(1200, 1000)).toBe(100)
expect(progressPercent(-5, 1000)).toBe(0)
})
it('总字数为 0 时不产生 NaN', () => {
expect(progressPercent(0, 0)).toBe(0)
})
})