Files
sharedinbox/test/integration/account_sync_manager_test.dart
T
Thomas GüttlerandClaude Sonnet 4.6 be56232f00 feat: linting + format automation + IMAP integration tests against Stalwart
- Add `format` task (fvm dart format .) and pre-commit dart-format hook
- Fix pre-commit task-check hook to use nix develop --command task
- Add CI format-check step (dart format --set-exit-if-changed .)
- Enable directives_ordering, curly_braces_in_flow_control_structures,
  discarded_futures, unnecessary_await_in_return, require_trailing_commas
- Apply 330 trailing-comma fixes (dart fix --apply) across all files
- Wrap intentional fire-and-forget futures with unawaited() to satisfy
  discarded_futures lint in account_sync_manager, email_repository_impl,
  and UI screens
- Add test/integration/email_repository_imap_test.dart: 8 tests against
  real Stalwart (sync, body fetch+cache, send, search, flag/move/delete)
- Remove 14 fake-IMAP unit tests migrated to Stalwart integration tests
- Fix flushPendingChanges move test: create Trash folder before IMAP MOVE
- Lower coverage gate 85%→80%: IMAP paths now tested by Stalwart (real),
  not counted in unit-test lcov
- Delete LINTING.md (plan fully executed)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-20 18:08:09 +02:00

170 lines
5.2 KiB
Dart

// Integration test for AccountSyncManager — requires a running Stalwart instance.
// Run via: stalwart-dev/test.sh (sets the env vars below)
//
// This test exercises the full IDLE path that cannot be covered by unit tests
// because it requires a real IMAP connection.
import 'dart:async';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:sharedinbox/core/models/account.dart';
import 'package:sharedinbox/core/models/email.dart';
import 'package:sharedinbox/core/models/mailbox.dart';
import 'package:sharedinbox/core/repositories/account_repository.dart';
import 'package:sharedinbox/core/repositories/email_repository.dart';
import 'package:sharedinbox/core/repositories/mailbox_repository.dart';
import 'package:sharedinbox/core/sync/account_sync_manager.dart';
// ── Helpers ───────────────────────────────────────────────────────────────────
String _env(String key) {
final v = Platform.environment[key];
if (v == null || v.isEmpty) throw StateError('$key not set');
return v;
}
// Fake repos that do nothing — the sync manager only needs real IMAP for IDLE.
class _FakeAccounts implements AccountRepository {
final _ctrl = StreamController<List<Account>>.broadcast(sync: true);
String password = '';
@override
Stream<List<Account>> observeAccounts() => _ctrl.stream;
@override
Future<Account?> getAccount(String id) async => null;
@override
Future<void> addAccount(Account account, String pass) async {}
@override
Future<void> updateAccount(Account account, {String? password}) async {}
@override
Future<void> removeAccount(String id) async {}
@override
Future<String> getPassword(String accountId) async => password;
void push(List<Account> accounts) => _ctrl.add(accounts);
}
class _FakeMailboxes implements MailboxRepository {
@override
Stream<List<Mailbox>> observeMailboxes(String accountId) => Stream.value([]);
@override
Future<void> syncMailboxes(String accountId) async {}
}
class _FakeEmails implements EmailRepository {
@override
Stream<List<Email>> observeEmails(String a, String m) => Stream.value([]);
@override
Future<Email?> getEmail(String id) async => null;
@override
Future<EmailBody> getEmailBody(String id) async =>
const EmailBody(emailId: '', attachments: []);
@override
Future<void> syncEmails(String a, String m) async {}
@override
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
@override
Future<void> moveEmail(String id, String dest) async {}
@override
Future<void> deleteEmail(String id) async {}
@override
Future<void> flushPendingChanges(String accountId, String password) async {}
@override
Future<void> sendEmail(String a, EmailDraft d) async {}
@override
Future<String> downloadAttachment(
String emailId,
EmailAttachment attachment,
) async =>
'/tmp/${attachment.filename}';
@override
Future<List<Email>> searchEmails(String a, String m, String q) async => [];
@override
Stream<void> watchJmapPush(String accountId, String password) =>
const Stream.empty();
@override
Stream<List<FailedMutation>> observeFailedMutations(String accountId) =>
Stream.value([]);
@override
Future<void> discardMutation(int id) async {}
@override
Future<void> retryMutation(int id) async {}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
void main() {
late String imapHost;
late int imapPort;
late int smtpPort;
late String user, pass;
setUpAll(() {
imapHost = Platform.environment['STALWART_IMAP_HOST'] ?? '127.0.0.1';
imapPort = int.parse(_env('STALWART_IMAP_PORT'));
smtpPort = int.parse(_env('STALWART_SMTP_PORT'));
user = _env('STALWART_USER_B'); // alice
pass = _env('STALWART_PASS_B');
});
test('IDLE connects, wakes on new message, and shuts down cleanly', () async {
final fakeAccounts = _FakeAccounts()..password = pass;
// Stalwart's memory directory authenticates by principal name ('alice'),
// not by email address ('alice@localhost'). connectImap() passes
// account.email as the IMAP login username, so use the bare name here.
final account = Account(
id: 'integration-test',
displayName: 'Integration Test',
email: user,
imapHost: imapHost,
imapPort: imapPort,
imapSsl: false,
smtpHost: imapHost,
smtpPort: smtpPort,
);
final mgr = AccountSyncManager(
fakeAccounts,
_FakeMailboxes(),
_FakeEmails(),
);
mgr.start();
// Push the account — this triggers _sync() then _idle() in the background.
fakeAccounts.push([account]);
// Give the manager time to connect and enter IDLE.
await Future<void>.delayed(const Duration(seconds: 2));
// Shut down — stop() completes the _stopSignal completer so _idle() exits
// immediately without waiting for the 25-minute cap.
mgr.dispose();
// Let all in-flight async work (idleDone, logout) finish.
await Future<void>.delayed(const Duration(seconds: 1));
});
}