96 lines
3.2 KiB
Dart
96 lines
3.2 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:sakura_mobile/src/core/legado_models.dart';
|
|
import 'package:sakura_mobile/src/core/legado_rule_parser.dart';
|
|
import 'package:sakura_mobile/src/core/online_cache.dart';
|
|
|
|
void main() {
|
|
group('在线书籍章节导航', () {
|
|
test('导入并序列化 BookInfo、TOC 与 Content 规则', () {
|
|
final source = LegadoSource.fromJson({
|
|
'bookSourceName': '测试源',
|
|
'bookSourceUrl': 'https://example.com',
|
|
'bookSourceType': 0,
|
|
'ruleBookInfo': {
|
|
'name': 'h1',
|
|
'author': '.author',
|
|
'coverUrl': '.cover@src',
|
|
'intro': '.intro',
|
|
'tocUrl': '.catalog@href',
|
|
},
|
|
'ruleToc': {
|
|
'chapterList': '.chapters li',
|
|
'chapterName': 'a',
|
|
'chapterUrl': 'a@href',
|
|
'nextTocUrl': '.next@href',
|
|
},
|
|
'ruleContent': {
|
|
'content': '#content',
|
|
'nextChapterUrl': '.next@href',
|
|
'prevChapterUrl': '.prev@href',
|
|
},
|
|
});
|
|
|
|
expect(source.bookInfoTocRule, '.catalog@href');
|
|
expect(source.tocListRule, '.chapters li');
|
|
expect(source.tocNextPageRule, '.next@href');
|
|
expect(source.contentRule, '#content');
|
|
|
|
final restored = LegadoSource.fromJson(source.toJson());
|
|
expect(restored.bookInfoNameRule, 'h1');
|
|
expect(restored.tocUrlRule, 'a@href');
|
|
expect(restored.contentNextRule, '.next@href');
|
|
});
|
|
|
|
test('CSS 规则能提取章节名称与相对链接', () {
|
|
const html = '''
|
|
<ul class="chapters">
|
|
<li><a href="/book/1.html">第一章 初遇</a></li>
|
|
<li><a href="/book/2.html">第二章 再会</a></li>
|
|
</ul>
|
|
''';
|
|
final parser = RuleParser(html, baseUrl: 'https://example.com/book/');
|
|
final rows = parser.selectList('.chapters li');
|
|
expect(rows, hasLength(2));
|
|
expect(parser.select(rows.first, 'a'), '第一章 初遇');
|
|
expect(parser.select(rows.first, 'a@href'), '/book/1.html');
|
|
});
|
|
|
|
test('会话缓存按书源和 URL 隔离', () {
|
|
final cache = OnlineContentCache();
|
|
const info = OnlineBookInfo(
|
|
title: '示例书',
|
|
tocUrl: 'https://example.com/toc',
|
|
sourceName: '测试源',
|
|
);
|
|
cache.putBookInfo('source-a', 'https://example.com/detail', info);
|
|
|
|
expect(
|
|
cache.getBookInfo('source-a', 'https://example.com/detail')?.title,
|
|
'示例书',
|
|
);
|
|
expect(
|
|
cache.getBookInfo('source-b', 'https://example.com/detail'),
|
|
isNull,
|
|
);
|
|
});
|
|
|
|
test('阅读记录 JSON 往返保持章节位置', () {
|
|
final record = OnlineReadingRecord(
|
|
sourceId: 'source-a',
|
|
sourceName: '测试源',
|
|
bookTitle: '示例书',
|
|
detailUrl: 'https://example.com/detail',
|
|
chapterUrl: 'https://example.com/chapter/3',
|
|
chapterTitle: '第三章',
|
|
chapterIndex: 2,
|
|
totalChapters: 20,
|
|
lastReadTime: DateTime(2026, 6, 21, 9),
|
|
);
|
|
final restored = OnlineReadingRecord.fromJson(record.toJson());
|
|
expect(restored.chapterIndex, 2);
|
|
expect(restored.chapterTitle, '第三章');
|
|
expect(restored.lastReadTime, DateTime(2026, 6, 21, 9));
|
|
});
|
|
});
|
|
}
|