test: move blob expiry tests to integration

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Thomas Güttler
2026-04-21 08:32:35 +02:00
co-authored by Claude Sonnet 4.6
parent b0362f9c30
commit e6d13948eb
2 changed files with 52 additions and 66 deletions
@@ -9,6 +9,7 @@
import 'dart:convert';
import 'dart:io';
import 'package:drift/drift.dart' show Value;
import 'package:enough_mail/enough_mail.dart';
import 'package:sharedinbox/core/models/account.dart';
import 'package:sharedinbox/core/models/email.dart';
@@ -329,6 +330,54 @@ void main() {
expect(cached.textBody, body.textBody);
});
test('blob expiry: re-fetches body when cachedAt is null (legacy row)',
() async {
await appendToInbox('legacy-body-test', body: 'Fresh from server');
final r = makeRepo();
await r.accounts.addAccount(account, userPass);
await r.emails.syncEmails('test', 'INBOX');
final emails = await r.emails.observeEmails('test', 'INBOX').first;
final emailId = emails.first.id;
// Simulate a legacy row with no cachedAt.
await r.db.into(r.db.emailBodies).insertOnConflictUpdate(
EmailBodiesCompanion.insert(
emailId: emailId,
textBody: const Value('stale text'),
cachedAt: const Value(null),
),
);
final body = await r.emails.getEmailBody(emailId);
expect(body.textBody, contains('Fresh from server'));
});
test('blob expiry: re-fetches body when cachedAt is older than 7 days',
() async {
await appendToInbox('old-body-test', body: 'Current content');
final r = makeRepo();
await r.accounts.addAccount(account, userPass);
await r.emails.syncEmails('test', 'INBOX');
final emails = await r.emails.observeEmails('test', 'INBOX').first;
final emailId = emails.first.id;
// Simulate a row cached 8 days ago.
await r.db.into(r.db.emailBodies).insertOnConflictUpdate(
EmailBodiesCompanion.insert(
emailId: emailId,
textBody: const Value('old text'),
cachedAt: Value(DateTime.now().subtract(const Duration(days: 8))),
),
);
final body = await r.emails.getEmailBody(emailId);
expect(body.textBody, contains('Current content'));
});
test('sendEmail delivers via SMTP and appends copy to Sent folder', () async {
final subject = 'send-${DateTime.now().millisecondsSinceEpoch}';
final r = makeRepo();
+3 -66
View File
@@ -1596,7 +1596,9 @@ void main() {
group('blob expiry', () {
test('returns cached body when cachedAt is recent', () async {
final r = _makeReposWithFakes();
// Uses _makeRepos (IMAP throws if called) — passing without error proves
// no IMAP connection was made.
final r = _makeRepos();
await r.accounts.addAccount(_account, 'pw');
await r.db.into(r.db.emails).insert(
EmailsCompanion.insert(
@@ -1618,71 +1620,6 @@ void main() {
final body = await r.emails.getEmailBody('acc-1:1');
expect(body.textBody, 'cached text');
expect(r.fakeImap.logoutCalled, isFalse);
});
test('re-fetches body when cachedAt is null (legacy row)', () async {
final r = _makeReposWithFakes();
await r.accounts.addAccount(_account, 'pw');
await r.db.into(r.db.emails).insert(
EmailsCompanion.insert(
id: 'acc-1:1',
accountId: 'acc-1',
mailboxPath: 'INBOX',
uid: 1,
receivedAt: DateTime(2024),
),
);
await r.db.into(r.db.emailBodies).insertOnConflictUpdate(
EmailBodiesCompanion.insert(
emailId: 'acc-1:1',
textBody: const Value('stale text'),
// cachedAt omitted → null
),
);
final msg = imap.MimeMessage.parseFromText(
'Subject: Hi\r\nContent-Type: text/plain\r\n\r\nfresh from IMAP',
);
msg.uid = 1;
r.fakeImap.fetchResults = [msg];
final body = await r.emails.getEmailBody('acc-1:1');
expect(body.textBody, contains('fresh from IMAP'));
expect(r.fakeImap.logoutCalled, isTrue);
});
test('re-fetches body when cachedAt is older than 7 days', () async {
final r = _makeReposWithFakes();
await r.accounts.addAccount(_account, 'pw');
await r.db.into(r.db.emails).insert(
EmailsCompanion.insert(
id: 'acc-1:1',
accountId: 'acc-1',
mailboxPath: 'INBOX',
uid: 1,
receivedAt: DateTime(2024),
),
);
await r.db.into(r.db.emailBodies).insertOnConflictUpdate(
EmailBodiesCompanion.insert(
emailId: 'acc-1:1',
textBody: const Value('old text'),
cachedAt: Value(DateTime.now().subtract(const Duration(days: 8))),
),
);
final msg = imap.MimeMessage.parseFromText(
'Subject: Hi\r\nContent-Type: text/plain\r\n\r\nnew body',
);
msg.uid = 1;
r.fakeImap.fetchResults = [msg];
final body = await r.emails.getEmailBody('acc-1:1');
expect(body.textBody, contains('new body'));
expect(r.fakeImap.logoutCalled, isTrue);
});
});