Files
manager/test/core/models/service_test.dart
T
2026-07-27 14:42:23 +02:00

71 lines
2.7 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:manager/core/models/models.dart';
Map<String, dynamic> _loadFixture(String name) {
final raw = File('test/fixtures/$name').readAsStringSync();
return jsonDecode(raw) as Map<String, dynamic>;
}
void main() {
group('Service.fromJson', () {
test('parses a fully-populated service including nested backup config', () {
final service = Service.fromJson(_loadFixture('service_full.json'));
expect(service.id, 'svc_123');
expect(service.name, 'postgres');
expect(service.composeFile, '/opt/compose/postgres/docker-compose.yml');
expect(service.composeType, 'docker compose');
expect(service.backupFolder, '/opt/data/containers/postgres');
expect(service.status, 'up');
expect(service.stopOnBackup, isTrue);
expect(service.externalBackupStopMinutes, 5);
expect(service.externalBackupStartMinutes, 2);
final backup = service.backup;
expect(backup, isNotNull);
expect(backup!.sourcePath, '/opt/data/containers/postgres');
expect(backup.nextRun, DateTime.parse('2026-07-27T02:00:00Z'));
expect(backup.lastRun, DateTime.parse('2026-07-26T02:00:00Z'));
expect(backup.targets, hasLength(1));
final target = backup.targets.single;
expect(target.id, 't1');
expect(target.method, BackupMethod.restic);
expect(target.remote, 'sftp:nas-cold:/backups/edge-01');
expect(target.params, {'transfers': '4'});
expect(target.schedule, '0 2 * * *');
expect(target.lastRun, DateTime.parse('2026-07-26T02:00:00Z'));
expect(target.nextRun, DateTime.parse('2026-07-27T02:00:00Z'));
expect(backup.history, hasLength(1));
final execution = backup.history.single;
expect(execution.success, isTrue);
expect(execution.durationSeconds, 12);
expect(execution.targetId, 't1');
});
test('fills in defaults for every omitted/nullable field', () {
final service = Service.fromJson(_loadFixture('service_minimal.json'));
expect(service.id, 'svc_456');
expect(service.name, 'n8n');
expect(service.status, 'down');
expect(service.composeType, isNull);
expect(service.backupFolder, isNull);
expect(service.stopOnBackup, isFalse);
expect(service.externalBackupStopMinutes, 0);
expect(service.externalBackupStartMinutes, 0);
expect(service.backup, isNull);
});
test('round-trips through toJson/fromJson without losing data', () {
final original = Service.fromJson(_loadFixture('service_full.json'));
final roundTripped = Service.fromJson(original.toJson());
expect(roundTripped, original);
});
});
}