|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | + |
| 3 | +import 'package:webtrit_phone/environment_config.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('EnvRegistry', () { |
| 7 | + late EnvRegistry registry; |
| 8 | + |
| 9 | + setUp(() => registry = EnvRegistry()); |
| 10 | + |
| 11 | + test('string: override wins; empty and missing fall back to compile-time', () { |
| 12 | + registry.apply({'A': 'override'}); |
| 13 | + expect(registry.string('A', 'default'), 'override'); |
| 14 | + |
| 15 | + registry.apply({'A': ''}); |
| 16 | + expect(registry.string('A', 'default'), 'default'); |
| 17 | + |
| 18 | + registry.apply({}); |
| 19 | + expect(registry.string('A', 'default'), 'default'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('stringOrNull: empty and missing fall back (may be null)', () { |
| 23 | + registry.apply({'A': ''}); |
| 24 | + expect(registry.stringOrNull('A', null), isNull); |
| 25 | + expect(registry.stringOrNull('A', 'd'), 'd'); |
| 26 | + |
| 27 | + registry.apply({'A': 'x'}); |
| 28 | + expect(registry.stringOrNull('A', 'd'), 'x'); |
| 29 | + }); |
| 30 | + |
| 31 | + test('boolean: true/false honoured; malformed and empty fall back', () { |
| 32 | + registry.apply({'B': 'TRUE'}); |
| 33 | + expect(registry.boolean('B', false), isTrue); |
| 34 | + |
| 35 | + registry.apply({'B': 'false'}); |
| 36 | + expect(registry.boolean('B', true), isFalse); |
| 37 | + |
| 38 | + registry.apply({'B': 'yes'}); // malformed -> compile-time |
| 39 | + expect(registry.boolean('B', true), isTrue); |
| 40 | + |
| 41 | + registry.apply({'B': ''}); // empty -> compile-time |
| 42 | + expect(registry.boolean('B', true), isTrue); |
| 43 | + |
| 44 | + registry.apply({}); |
| 45 | + expect(registry.boolean('B', false), isFalse); |
| 46 | + }); |
| 47 | + |
| 48 | + test('integer: numeric honoured; malformed and empty fall back', () { |
| 49 | + registry.apply({'N': '42'}); |
| 50 | + expect(registry.integer('N', 1), 42); |
| 51 | + |
| 52 | + registry.apply({'N': 'abc'}); |
| 53 | + expect(registry.integer('N', 7), 7); |
| 54 | + |
| 55 | + registry.apply({'N': ''}); |
| 56 | + expect(registry.integer('N', 7), 7); |
| 57 | + }); |
| 58 | + |
| 59 | + test('has / clear', () { |
| 60 | + registry.apply({'A': 'x'}); |
| 61 | + expect(registry.has('A'), isTrue); |
| 62 | + |
| 63 | + registry.clear(); |
| 64 | + expect(registry.has('A'), isFalse); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + group('EnvironmentConfig overrides', () { |
| 69 | + tearDown(EnvironmentConfig.clearOverrides); |
| 70 | + |
| 71 | + test('APP_NAME reflects an override and falls back when empty/cleared', () { |
| 72 | + EnvironmentConfig.applyOverrides({EnvironmentConfig.APP_NAME__NAME: 'Custom'}); |
| 73 | + expect(EnvironmentConfig.APP_NAME, 'Custom'); |
| 74 | + |
| 75 | + EnvironmentConfig.applyOverrides({EnvironmentConfig.APP_NAME__NAME: ''}); |
| 76 | + expect(EnvironmentConfig.APP_NAME, 'WebTrit'); |
| 77 | + |
| 78 | + EnvironmentConfig.clearOverrides(); |
| 79 | + expect(EnvironmentConfig.APP_NAME, 'WebTrit'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('a non-positive polling-interval override falls back to the default', () { |
| 83 | + const name = EnvironmentConfig.USER_REPOSITORY_POLLING_INTERVAL_SECONDS__NAME; |
| 84 | + |
| 85 | + EnvironmentConfig.applyOverrides({name: '0'}); |
| 86 | + expect(EnvironmentConfig.USER_REPOSITORY_POLLING_INTERVAL_SECONDS, 10); |
| 87 | + |
| 88 | + EnvironmentConfig.applyOverrides({name: '-5'}); |
| 89 | + expect(EnvironmentConfig.USER_REPOSITORY_POLLING_INTERVAL_SECONDS, 10); |
| 90 | + |
| 91 | + EnvironmentConfig.applyOverrides({name: '30'}); |
| 92 | + expect(EnvironmentConfig.USER_REPOSITORY_POLLING_INTERVAL_SECONDS, 30); |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
0 commit comments