Files
sharedinbox/test/unit/email_model_test.dart
T
Thomas GüttlerandClaude Sonnet 4.6 03d35387f7 Linting, tests, README, CI, and code quality improvements
Linting:
- analysis_options.yaml: flutter_lints base + 20 additional rules
  (prefer_single_quotes, avoid_print, cancel_subscriptions,
  unawaited_futures, empty_catches, always_declare_return_types, …)
- unused_import / dead_code treated as errors

pubspec.yaml:
- Remove 6 unused packages: freezed, freezed_annotation, json_annotation,
  json_serializable, riverpod_generator, collection
- Add test: ^1.25.0 for pure-Dart unit tests

Testable utilities (extracted from screen code):
- lib/core/utils/html_utils.dart — htmlToPlain()
- lib/core/utils/format_utils.dart — fmtSize()
- lib/core/utils/logger.dart — thin debugPrint wrapper

Unit tests (test/unit/, no device required):
- html_utils_test.dart — 13 cases covering tags, entities, edge cases
- format_utils_test.dart — B / KB / MB / GB formatting
- email_model_test.dart — EmailAddress JSON roundtrip, toString, EmailDraft
- account_sync_backoff_test.dart — exponential backoff + clamping

Taskfile:
- task test → dart test test/unit/ (fast, no device)
- task test-flutter → flutter test (widget tests)
- task check → analyze + unit tests in parallel

README rewrite:
- Covers user flow (download, add account)
- Developer flow: nix develop, direnv allow, task codegen, task check
- Platform status table (Linux done, others pending)
- Project layout diagram
- Schema-change workflow

CI (.github/workflows/ci.yml):
- analyze-and-test job: flutter analyze + dart test on every push/PR
- integration job: Nix + Stalwart on push to main
- build-linux job: flutter build linux --release

Logging:
- Replace all bare catch (_) {} with log() calls
- Sync backoff errors now print account email + retry delay
- STATUS failures on \Noselect mailboxes logged at debug level
- STARTTLS failures logged before continuing without TLS

.gitignore:
- Add .direnv/, .flutter-plugins-dependencies
- Add all platform generated-plugin wiring files
- Add .env / .env.local
- Add linux/build/

.env.example: documents optional STALWART_PORT overrides

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 08:11:29 +02:00

94 lines
3.0 KiB
Dart

import 'dart:convert';
import 'package:test/test.dart';
import 'package:sharedinbox/core/models/email.dart';
// Mirrors the encoding logic in EmailRepositoryImpl so we can test it
// independently without spinning up a database.
String encodeAddresses(List<EmailAddress> addresses) => jsonEncode(
addresses
.map((a) => {'name': a.name, 'email': a.email})
.toList(),
);
List<EmailAddress> decodeAddresses(String json) {
final list = jsonDecode(json) as List;
return list
.map(
(e) => EmailAddress(
name: e['name'] as String?,
email: e['email'] as String,
),
)
.toList();
}
void main() {
group('EmailAddress JSON roundtrip', () {
test('encodes and decodes a single address with name', () {
const addr = EmailAddress(name: 'Alice', email: 'alice@example.com');
final decoded = decodeAddresses(encodeAddresses([addr]));
expect(decoded, hasLength(1));
expect(decoded.first.name, 'Alice');
expect(decoded.first.email, 'alice@example.com');
});
test('encodes and decodes an address without a display name', () {
const addr = EmailAddress(email: 'bob@example.com');
final decoded = decodeAddresses(encodeAddresses([addr]));
expect(decoded.first.name, isNull);
expect(decoded.first.email, 'bob@example.com');
});
test('encodes and decodes multiple addresses', () {
final addresses = [
const EmailAddress(name: 'Alice', email: 'alice@example.com'),
const EmailAddress(email: 'bob@example.com'),
];
final decoded = decodeAddresses(encodeAddresses(addresses));
expect(decoded, hasLength(2));
expect(decoded[0].email, 'alice@example.com');
expect(decoded[1].email, 'bob@example.com');
});
test('encodes empty list', () {
final decoded = decodeAddresses(encodeAddresses([]));
expect(decoded, isEmpty);
});
test('handles special characters in display name', () {
const addr = EmailAddress(name: 'Müller, Hans', email: 'hans@example.de');
final decoded = decodeAddresses(encodeAddresses([addr]));
expect(decoded.first.name, 'Müller, Hans');
});
});
group('EmailAddress.toString', () {
test('includes name when present', () {
const addr = EmailAddress(name: 'Alice', email: 'alice@example.com');
expect(addr.toString(), 'Alice <alice@example.com>');
});
test('returns just email when name is null', () {
const addr = EmailAddress(email: 'alice@example.com');
expect(addr.toString(), 'alice@example.com');
});
});
group('EmailDraft', () {
test('constructs with required fields', () {
final draft = EmailDraft(
from: const EmailAddress(name: 'Me', email: 'me@example.com'),
to: [const EmailAddress(email: 'you@example.com')],
cc: [],
subject: 'Hello',
body: 'World',
);
expect(draft.subject, 'Hello');
expect(draft.to, hasLength(1));
expect(draft.cc, isEmpty);
});
});
}