import 'package:flutter_test/flutter_test.dart'; import 'package:mail_hub/core/protocol/imap/imap_parser.dart'; void main() { group('IMAP 响应解析', () { test('FETCH 带字面量(HEADER + TEXT)', () { final header = 'Subject: test\r\nFrom: a@b.com\r\n'; final text = 'Hello World!'; final parts = [ ImapPart('* 1 FETCH (UID 5 BODY[HEADER] {${header.length}}', header.codeUnits), ImapPart(' BODY[TEXT] {${text.length}}', text.codeUnits), ImapPart(' FLAGS (\\Seen))'), ]; final fd = ImapParser.parseFetch(parts)!; expect(fd.uid, 5); expect(fd.seen, true); expect(String.fromCharCodes(fd.bodies['HEADER']!), contains('Subject: test')); expect(String.fromCharCodes(fd.bodies['TEXT']!), 'Hello World!'); }); test('FETCH 部分抓取 <0.100>', () { final parts = [ ImapPart('* 2 FETCH (UID 9 BODY[TEXT]<0.100> {5}', 'hello'.codeUnits), ImapPart(')'), ]; final fd = ImapParser.parseFetch(parts)!; expect(fd.uid, 9); expect(String.fromCharCodes(fd.bodies['TEXT']!), 'hello'); }); test('SEARCH 结果', () { final lines = ['* SEARCH 1 2 3', '* SEARCH 4 5']; expect(ImapParser.parseSearch(lines), [1, 2, 3, 4, 5]); }); test('LIST 文件夹', () { final lines = [ r'* LIST (\HasNoChildren) "/" "INBOX"', r'* LIST (\HasChildren \Noselect) "/" "[Gmail]"', ]; final list = ImapParser.parseList(lines); expect(list.length, 2); expect(list[0].name, 'INBOX'); expect(list[0].delim, '/'); expect(list[1].attrs, contains(r'\Noselect')); }); test('STATUS 解析', () { final map = ImapParser.parseStatus( '* STATUS INBOX (MESSAGES 231 UNSEEN 4 UIDNEXT 44292)'); expect(map!['MESSAGES'], 231); expect(map['UNSEEN'], 4); expect(map['UIDNEXT'], 44292); }); test('状态响应 OK/NO', () { final ok = ImapStatus.parse('a1 OK [READ-WRITE] SELECT completed'); expect(ok.ok, true); expect(ok.responseCode, 'READ-WRITE'); final no = ImapStatus.parse('a2 NO [AUTHENTICATIONFAILED] Invalid credentials'); expect(no.ok, false); expect(no.responseCode, 'AUTHENTICATIONFAILED'); }); test('INTERNALDATE 解析', () { final fd = ImapParser.parseFetch([ ImapPart('* 1 FETCH (UID 5 INTERNALDATE "17-Aug-2026 08:23:45 +0800")'), ])!; expect(fd.internalDate, isNotNull); expect(fd.internalDate!.year, 2026); expect(fd.internalDate!.month, 8); }); }); }