test: add unit tests for Control Room data models
Add comprehensive test coverage for: - ProxyHostModel: 37 tests for JSON converters and serialization - ContainerModel: 44 tests for deserialization, entity conversion, state parsing - StackModel: 55 tests for type/status parsing, timestamps, entity properties 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
50f643dd90
commit
67d2f777c8
@@ -0,0 +1,335 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tatlock_ui/features/control_room/npm/data/models/proxy_host_model.dart';
|
||||
|
||||
void main() {
|
||||
group('NullableIntOrBoolConverter', () {
|
||||
const converter = NullableIntOrBoolConverter();
|
||||
|
||||
group('fromJson', () {
|
||||
test('returns null for null input', () {
|
||||
expect(converter.fromJson(null), isNull);
|
||||
});
|
||||
|
||||
test('returns null for false input', () {
|
||||
expect(converter.fromJson(false), isNull);
|
||||
});
|
||||
|
||||
test('returns null for true input (treats as invalid)', () {
|
||||
// true is not a valid ID, so we return null
|
||||
expect(converter.fromJson(true), isNull);
|
||||
});
|
||||
|
||||
test('returns int for int input', () {
|
||||
expect(converter.fromJson(42), equals(42));
|
||||
});
|
||||
|
||||
test('returns int for positive int', () {
|
||||
expect(converter.fromJson(1), equals(1));
|
||||
});
|
||||
|
||||
test('returns int for zero', () {
|
||||
expect(converter.fromJson(0), equals(0));
|
||||
});
|
||||
|
||||
test('converts double to int', () {
|
||||
expect(converter.fromJson(42.0), equals(42));
|
||||
});
|
||||
|
||||
test('converts double with decimals to int (truncates)', () {
|
||||
expect(converter.fromJson(42.9), equals(42));
|
||||
});
|
||||
|
||||
test('returns null for string input', () {
|
||||
expect(converter.fromJson('42'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for empty string', () {
|
||||
expect(converter.fromJson(''), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('toJson', () {
|
||||
test('returns null for null input', () {
|
||||
expect(converter.toJson(null), isNull);
|
||||
});
|
||||
|
||||
test('returns int for int input', () {
|
||||
expect(converter.toJson(42), equals(42));
|
||||
});
|
||||
|
||||
test('returns zero for zero input', () {
|
||||
expect(converter.toJson(0), equals(0));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('IntOrBoolConverter', () {
|
||||
const converter = IntOrBoolConverter();
|
||||
|
||||
group('fromJson', () {
|
||||
test('returns 0 for null input', () {
|
||||
expect(converter.fromJson(null), equals(0));
|
||||
});
|
||||
|
||||
test('returns 0 for false input', () {
|
||||
expect(converter.fromJson(false), equals(0));
|
||||
});
|
||||
|
||||
test('returns 1 for true input', () {
|
||||
expect(converter.fromJson(true), equals(1));
|
||||
});
|
||||
|
||||
test('returns int for int input', () {
|
||||
expect(converter.fromJson(42), equals(42));
|
||||
});
|
||||
|
||||
test('returns 0 for zero input', () {
|
||||
expect(converter.fromJson(0), equals(0));
|
||||
});
|
||||
|
||||
test('returns 1 for one input', () {
|
||||
expect(converter.fromJson(1), equals(1));
|
||||
});
|
||||
|
||||
test('converts double to int', () {
|
||||
expect(converter.fromJson(42.0), equals(42));
|
||||
});
|
||||
|
||||
test('converts double with decimals to int (truncates)', () {
|
||||
expect(converter.fromJson(42.9), equals(42));
|
||||
});
|
||||
|
||||
test('returns 0 for string input', () {
|
||||
expect(converter.fromJson('42'), equals(0));
|
||||
});
|
||||
|
||||
test('returns 0 for empty string', () {
|
||||
expect(converter.fromJson(''), equals(0));
|
||||
});
|
||||
|
||||
test('returns 0 for invalid types', () {
|
||||
expect(converter.fromJson([1, 2, 3]), equals(0));
|
||||
expect(converter.fromJson({'key': 'value'}), equals(0));
|
||||
});
|
||||
});
|
||||
|
||||
group('toJson', () {
|
||||
test('returns int for int input', () {
|
||||
expect(converter.toJson(42), equals(42));
|
||||
});
|
||||
|
||||
test('returns 0 for zero input', () {
|
||||
expect(converter.toJson(0), equals(0));
|
||||
});
|
||||
|
||||
test('returns 1 for one input', () {
|
||||
expect(converter.toJson(1), equals(1));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('ProxyHostModel JSON serialization', () {
|
||||
test('deserializes with boolean certificate_id (false)', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'http',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 8080,
|
||||
'certificate_id': false, // NPM returns false for no certificate
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.certificateId, isNull);
|
||||
});
|
||||
|
||||
test('deserializes with int certificate_id', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'http',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 8080,
|
||||
'certificate_id': 42,
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.certificateId, equals(42));
|
||||
});
|
||||
|
||||
test('deserializes with null certificate_id', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'http',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 8080,
|
||||
'certificate_id': null,
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.certificateId, isNull);
|
||||
});
|
||||
|
||||
test('deserializes with boolean http2_support (true)', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'http',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 8080,
|
||||
'http2_support': true, // NPM can return boolean
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.http2Support, equals(1));
|
||||
});
|
||||
|
||||
test('deserializes with boolean http2_support (false)', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'http',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 8080,
|
||||
'http2_support': false,
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.http2Support, equals(0));
|
||||
});
|
||||
|
||||
test('deserializes with int http2_support', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'http',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 8080,
|
||||
'http2_support': 1,
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.http2Support, equals(1));
|
||||
});
|
||||
|
||||
test('deserializes with all boolean flags', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'https',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 443,
|
||||
'ssl_forced': true,
|
||||
'certificate_id': false,
|
||||
'enabled': true,
|
||||
'http2_support': true,
|
||||
'hsts_enabled': false,
|
||||
'access_list_id': false,
|
||||
'caching_enabled': true,
|
||||
'block_exploits': true,
|
||||
'allow_websocket_upgrade': false,
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.sslForced, isTrue);
|
||||
expect(model.certificateId, isNull);
|
||||
expect(model.enabled, equals(1));
|
||||
expect(model.http2Support, equals(1));
|
||||
expect(model.hstsEnabled, equals(0));
|
||||
expect(model.accessListId, isNull);
|
||||
expect(model.cachingEnabled, equals(1));
|
||||
expect(model.blockExploits, equals(1));
|
||||
expect(model.allowWebsocketUpgrade, equals(0));
|
||||
});
|
||||
|
||||
test('deserializes with all int flags', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'domain_names': ['example.com'],
|
||||
'forward_scheme': 'https',
|
||||
'forward_host': 'localhost',
|
||||
'forward_port': 443,
|
||||
'ssl_forced': false,
|
||||
'certificate_id': 5,
|
||||
'enabled': 1,
|
||||
'http2_support': 1,
|
||||
'hsts_enabled': 0,
|
||||
'access_list_id': 3,
|
||||
'caching_enabled': 1,
|
||||
'block_exploits': 1,
|
||||
'allow_websocket_upgrade': 0,
|
||||
};
|
||||
|
||||
final model = ProxyHostModel.fromJson(json);
|
||||
|
||||
expect(model.sslForced, isFalse);
|
||||
expect(model.certificateId, equals(5));
|
||||
expect(model.enabled, equals(1));
|
||||
expect(model.http2Support, equals(1));
|
||||
expect(model.hstsEnabled, equals(0));
|
||||
expect(model.accessListId, equals(3));
|
||||
expect(model.cachingEnabled, equals(1));
|
||||
expect(model.blockExploits, equals(1));
|
||||
expect(model.allowWebsocketUpgrade, equals(0));
|
||||
});
|
||||
|
||||
test('toEntity converts correctly', () {
|
||||
final model = ProxyHostModel(
|
||||
id: 1,
|
||||
domainNames: ['example.com', 'www.example.com'],
|
||||
forwardScheme: 'https',
|
||||
forwardHost: 'backend',
|
||||
forwardPort: 8080,
|
||||
sslForced: true,
|
||||
certificateId: 5,
|
||||
enabled: 1,
|
||||
http2Support: 1,
|
||||
hstsEnabled: 1,
|
||||
cachingEnabled: 0,
|
||||
blockExploits: 1,
|
||||
allowWebsocketUpgrade: 1,
|
||||
);
|
||||
|
||||
final entity = model.toEntity();
|
||||
|
||||
expect(entity.id, equals(1));
|
||||
expect(entity.domainNames, equals(['example.com', 'www.example.com']));
|
||||
expect(entity.forwardScheme, equals('https'));
|
||||
expect(entity.forwardHost, equals('backend'));
|
||||
expect(entity.forwardPort, equals(8080));
|
||||
expect(entity.forceSSL, isTrue);
|
||||
expect(entity.sslEnabled, isTrue); // certificateId > 0
|
||||
expect(entity.certificateId, equals(5));
|
||||
expect(entity.enabled, isTrue);
|
||||
expect(entity.http2Support, isTrue);
|
||||
expect(entity.hstsEnabled, isTrue);
|
||||
expect(entity.cacheAssets, isFalse);
|
||||
expect(entity.blockExploits, isTrue);
|
||||
expect(entity.websocketSupport, isTrue);
|
||||
});
|
||||
|
||||
test('toEntity handles null certificateId correctly', () {
|
||||
final model = ProxyHostModel(
|
||||
id: 1,
|
||||
domainNames: ['example.com'],
|
||||
forwardScheme: 'http',
|
||||
forwardHost: 'backend',
|
||||
forwardPort: 80,
|
||||
certificateId: null,
|
||||
);
|
||||
|
||||
final entity = model.toEntity();
|
||||
|
||||
expect(entity.sslEnabled, isFalse);
|
||||
expect(entity.certificateId, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user