import 'dart:io'; import 'dart:typed_data'; import 'package:dio/dio.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:sakura_mobile/src/core/legado_engine.dart'; import 'package:sakura_mobile/src/core/legado_js_runner.dart'; import 'package:sakura_mobile/src/core/legado_models.dart'; import 'package:sakura_mobile/src/core/legado_rule_executor.dart'; import 'package:sakura_mobile/src/core/manga_image.dart'; void main() { group('漫画图片规则', () { test('拆分 Legado 图片 URL 与独立 headers', () { final parsed = splitLegadoImageSpec( 'https://img.example.com/1.jpg,{"headers":{"Referer":"https://comic.example.com","Cookie":"a=1"}}', ); expect(parsed.url, 'https://img.example.com/1.jpg'); expect(parsed.headers['Referer'], 'https://comic.example.com'); expect(parsed.headers['Cookie'], 'a=1'); }); test('过滤空值与占位图并清洗转义 URL', () { expect(validateImageCandidate('', 'https://example.com').reason, 'empty'); expect( validateImageCandidate('/images/loading.gif', 'https://example.com') .reason, 'placeholder', ); final candidate = validateImageCandidate( r'https:\/\/img.example.com\/comic\u002F1.jpg', 'https://example.com/chapter', ); expect(candidate.isValid, isTrue); expect(candidate.url, 'https://img.example.com/comic/1.jpg'); }); test('PageImage 合并源 headers、章节 Referer 与图片专属 headers', () { final page = buildPageImage( raw: 'https://img.example.com/1.jpg,{"headers":{"Referer":"https://cdn.example.com","X-Image":"1"}}', chapterUrl: 'https://comic.example.com/chapter/1', index: 0, sourceHeaders: const {'Cookie': 'session=1'}, userAgent: 'Sakura-Test', ); expect(page, isNotNull); expect(page!.headers['Cookie'], 'session=1'); expect(page.headers['User-Agent'], 'Sakura-Test'); expect(page.headers['Referer'], 'https://cdn.example.com'); expect(page.headers['X-Image'], '1'); }); test('识别图片魔数', () { expect(isImageMagic([0xFF, 0xD8, 0xFF, 0x00]), isTrue); expect( isImageMagic([ 0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50, ]), isTrue, ); expect(isImageMagic([0x3C, 0x68, 0x74, 0x6D]), isFalse); }); test('章节号支持小数番外与多种标题格式', () { expect(extractMangaChapterNumber('第660.5回 番外'), 660.5); expect(extractMangaChapterNumber('Ch.660'), 660); expect(extractMangaChapterNumber('12话'), 12); expect(extractMangaChapterNumber('序章'), isNull); }); }); group('manga online source compatibility', () { test('recognizes a mislabeled manga source using strong manga fields', () { final source = LegadoSource.fromJson({ 'bookSourceName': 'Example Manga', 'bookSourceUrl': 'https://manga.example.com', 'bookSourceType': 0, 'ruleSearch': { 'comicList': r'$.data[*]', 'comicName': r'$.title', 'comicUrl': r'$.url', }, 'ruleContent': { 'content': r'$.images[*]', 'imageStyle': 'FULL', }, }); expect(source.isManga, isTrue); }); test('does not reclassify an ordinary novel source', () { final source = LegadoSource.fromJson({ 'bookSourceName': 'Example Novel', 'bookSourceUrl': 'https://novel.example.com', 'bookSourceType': 0, 'ruleSearch': {'bookList': '.book', 'name': '.title@text'}, 'ruleContent': {'content': '#content@text'}, }); expect(source.isNovel, isTrue); }); test('splits inline Legado POST options from a manga search URL', () { final spec = parseLegadoRequestSpec( '/search,{"method":"POST","body":"q={{key}}","headers":{"X-App":"manga"}}', ); expect(spec.url, '/search'); expect(spec.method, 'POST'); expect(spec.body, 'q={{key}}'); expect(spec.headers['X-App'], 'manga'); }); test('accepts common loose Legado request options', () { final spec = parseLegadoRequestSpec( "/search,{'method':'POST';'body':'q={{key}}','headers':'{os: pc}'}", ); expect(spec.url, '/search'); expect(spec.method, 'POST'); expect(spec.body, 'q={{key}}'); expect(spec.headers['os'], 'pc'); }); test('provides common safe Legado variables and base64 helper to JS', () { final output = LegadoJsRunner().run( script: "source.getKey() + '|' + key + '|' + page + '|' + java.base64Decode('5ryr55S7')", result: '', rawHtml: '', baseUrl: 'https://manga.example.com', context: { 'sourceUrl': 'https://manga.example.com', 'key': 'sakura', 'page': '3', }, ); expect(output, 'https://manga.example.com|sakura|3|漫画'); final objectOutput = LegadoJsRunner().run( script: 'result.title + "|" + result.comic_id', result: '{"title":"Sakura","comic_id":42}', rawHtml: '', baseUrl: 'https://manga.example.com', context: {}, ); expect(objectOutput, 'Sakura|42'); }, skip: Platform.isWindows ? 'QuickJS desktop DLL is not bundled' : false); test('manga search uses manga-style JSON fallback matching', () async { final dio = Dio() ..httpClientAdapter = _FakeAdapter({ 'https://manga.example.com/search?q=%E9%AD%94%E6%B3%95': '{"data":[{"comic_name":"魔法少女","author":"Sakura","comic_url":"/comic/1","cover_url":"/cover.jpg"}]}', }); final engine = LegadoEngine(http: dio); const source = LegadoSource( id: 'manga-json', name: 'Manga JSON', url: 'https://manga.example.com', type: 2, enabled: true, searchUrl: '/search?q={{key}}', searchListRule: r'$.missing[*]', ); final results = await engine.search(source, '魔法'); expect(results, hasLength(1)); expect(results.single.title, '魔法少女'); expect(results.single.author, 'Sakura'); expect(results.single.detailUrl, 'https://manga.example.com/comic/1'); expect(results.single.coverUrl, 'https://manga.example.com/cover.jpg'); }); test('matches punctuated titles and builds a detail URL from comic ID', () async { final dio = Dio() ..httpClientAdapter = _FakeAdapter({ 'https://manga.example.com/search?q=%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85': '{"data":[{"comic_name":"火影忍者:疾风传(漫画)","comic_id":"77"}]}' }); final engine = LegadoEngine(http: dio); const source = LegadoSource( id: 'manga-id', name: 'Manga ID', url: 'https://manga.example.com', type: 2, enabled: true, searchUrl: '/search?q={{key}}', searchListRule: r'$.missing[*]', ); final results = await engine.search(source, '火影忍者'); expect(results, hasLength(1)); expect(results.single.detailUrl, 'https://manga.example.com/comic/77'); }); test('manga search uses manga-style HTML card fallback matching', () async { final dio = Dio() ..httpClientAdapter = _FakeAdapter({ 'https://manga.example.com/search?q=%E6%A8%B1%E8%8A%B1': '''
樱花漫画 花见
''', }); final engine = LegadoEngine(http: dio); const source = LegadoSource( id: 'manga-html', name: 'Manga HTML', url: 'https://manga.example.com', type: 2, enabled: true, searchUrl: '/search?q={{key}}', searchListRule: '.missing', ); final results = await engine.search(source, '樱花'); expect(results, hasLength(1)); expect(results.single.title, '樱花漫画'); expect(results.single.author, '花见'); expect( results.single.detailUrl, 'https://manga.example.com/manga/sakura'); expect(results.single.coverUrl, 'https://manga.example.com/sakura.webp'); }); }); group('novel Legado matching compatibility', () { test('uses inline POST settings for a novel source', () async { final adapter = _FakeAdapter({ 'https://novel.example.com/search': '{"data":[{"name":"龙族","author":"江南","url":"/book/1"}]}' }); final engine = LegadoEngine(http: Dio()..httpClientAdapter = adapter); const source = LegadoSource( id: 'novel-post', name: 'Novel POST', url: 'https://novel.example.com', type: 0, enabled: true, searchUrl: '/search,{"method":"POST","body":"q={{key}}"}', searchListRule: r'$.data[*]', searchNameRule: r'$.name', searchAuthorRule: r'$.author', searchDetailRule: r'$.url', ruleKind: 'json', ); final results = await engine.search(source, '龙族'); expect(results.single.title, '龙族'); expect(results.single.detailUrl, 'https://novel.example.com/book/1'); expect(adapter.methods, contains('POST')); }); test('supports root JSON list rules used by Legado novel sources', () { final rules = LegadoRuleExecutor(); const body = '[{"name":"十宗罪","url":"/book/1"},{"name":"别的书","url":"/book/2"}]'; final fragments = rules.selectList( body, body, r'$[*]', baseUrl: 'https://novel.example.com', kind: 'json', ); expect(fragments, hasLength(2)); expect( rules.select( body, fragments.first, r'$.name', baseUrl: 'https://novel.example.com', kind: 'json', ), '十宗罪', ); }); test('supports text.xxx selector used by Legado toc rules', () { final rules = LegadoRuleExecutor(); const html = ''; expect( rules.select( html, html, 'text.查看全部章节@href', baseUrl: 'https://novel.example.com', ), '/all-chapters', ); }); }); } class _FakeAdapter implements HttpClientAdapter { _FakeAdapter(this.responses); final Map responses; final List methods = []; @override Future fetch( RequestOptions options, Stream? requestStream, Future? cancelFuture, ) async { methods.add(options.method.toUpperCase()); final body = responses[options.uri.toString()]; return ResponseBody.fromString( body ?? 'not found', body == null ? 404 : 200, headers: { Headers.contentTypeHeader: ['text/html; charset=utf-8'], }, ); } @override void close({bool force = false}) {} }