diff --git a/test/ut/providers/status_provider_test.dart b/test/ut/providers/status_provider_test.dart new file mode 100644 index 00000000..5f5eee43 --- /dev/null +++ b/test/ut/providers/status_provider_test.dart @@ -0,0 +1,632 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pi_hole_client/constants/enums.dart'; +import 'package:pi_hole_client/models/overtime_data.dart'; +import 'package:pi_hole_client/models/realtime_status.dart'; +import 'package:pi_hole_client/providers/status_provider.dart'; + +void main() { + group('StatusProvider Tests', () { + late StatusProvider statusProvider; + + setUp(() { + statusProvider = StatusProvider(); + }); + + test('Initial values are correct', () { + expect(statusProvider.isServerConnected, false); + expect(statusProvider.getRealtimeStatus, null); + expect(statusProvider.getStatusLoading, LoadStatus.loading); + expect(statusProvider.getOvertimeData, null); + expect(statusProvider.getOvertimeDataJson, null); + expect(statusProvider.getOvertimeDataLoadStatus, 0); + expect(statusProvider.getRefreshServerStatus, false); + expect(statusProvider.startAutoRefresh, false); + }); + + test('setIsServerConnected updates value and notifies listeners', () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + statusProvider.setIsServerConnected(true); + expect(statusProvider.isServerConnected, true); + expect(listenerCalled, true); + }); + + test('setStartAutoRefresh updates value without notifying listeners', () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + statusProvider.setStartAutoRefresh(true); + expect(statusProvider.startAutoRefresh, true); + expect(listenerCalled, false); + }); + + test( + 'setRefreshServerStatus updates value and notifies listeners if true', + () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + statusProvider.setRefreshServerStatus(true); + expect(statusProvider.getRefreshServerStatus, true); + expect(listenerCalled, true); + }, + ); + + test('setStatusLoading updates value and notifies listeners', () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + statusProvider.setStatusLoading(LoadStatus.loaded); + expect(statusProvider.getStatusLoading, LoadStatus.loaded); + expect(listenerCalled, true); + }); + + test('setRealtimeStatus updates value and notifies listeners', () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + final data = { + 'domains_being_blocked': 121860, + 'dns_queries_today': 16, + 'ads_blocked_today': 1, + 'ads_percentage_today': 6.25, + 'unique_domains': 11, + 'queries_forwarded': 9, + 'queries_cached': 6, + 'clients_ever_seen': 2, + 'unique_clients': 2, + 'dns_queries_all_types': 16, + 'reply_UNKNOWN': 0, + 'reply_NODATA': 0, + 'reply_NXDOMAIN': 3, + 'reply_CNAME': 0, + 'reply_IP': 10, + 'reply_DOMAIN': 3, + 'reply_RRNAME': 0, + 'reply_SERVFAIL': 0, + 'reply_REFUSED': 0, + 'reply_NOTIMP': 0, + 'reply_OTHER': 0, + 'reply_DNSSEC': 0, + 'reply_NONE': 0, + 'reply_BLOB': 0, + 'dns_queries_all_replies': 16, + 'privacy_level': 0, + 'status': 'enabled', + 'gravity_last_updated': { + 'file_exists': true, + 'absolute': 1732972589, + 'relative': {'days': 5, 'hours': 18, 'minutes': 14}, + }, + 'top_queries': { + '1.0.26.172.in-addr.arpa': 3, + '8.8.8.8.in-addr.arpa': 3, + 'github.com': 2, + 'gitlab.com': 1, + 'sample.com': 1, + 'test.com': 1, + 'google.com': 1, + 'google.co.jp': 1, + 'yahoo.co.jp': 1, + 'fix.test.com': 1, + }, + 'top_ads': {'test.com': 1}, + 'top_sources': {'172.26.0.1': 10, 'localhost|127.0.0.1': 6}, + 'top_sources_blocked': {'172.26.0.1': 1}, + 'forward_destinations': { + 'blocked|blocked': 6.25, + 'cached|cached': 37.5, + 'other|other': 0, + 'dns.google#53|8.8.8.8#53': 56.25, + }, + 'querytypes': { + 'A (IPv4)': 62.5, + 'AAAA (IPv6)': 0, + 'ANY': 0, + 'SRV': 0, + 'SOA': 0, + 'PTR': 37.5, + 'TXT': 0, + 'NAPTR': 0, + 'MX': 0, + 'DS': 0, + 'RRSIG': 0, + 'DNSKEY': 0, + 'NS': 0, + 'OTHER': 0, + 'SVCB': 0, + 'HTTPS': 0, + }, + }; + + final realtimeStatus = RealtimeStatus.fromJson(data); + statusProvider.setRealtimeStatus(realtimeStatus); + expect(statusProvider.getRealtimeStatus, realtimeStatus); + expect(statusProvider.getStatusLoading, LoadStatus.loaded); + expect(listenerCalled, true); + }); + + test( + 'setOvertimeDataLoadingStatus updates value and notifies listeners', + () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + statusProvider.setOvertimeDataLoadingStatus(1); + expect(statusProvider.getOvertimeDataLoadStatus, 1); + expect(listenerCalled, true); + }, + ); + + test('setOvertimeData updates value and notifies listeners', () { + bool listenerCalled = false; + statusProvider.addListener(() { + listenerCalled = true; + }); + + final data = { + 'domains_over_time': { + '1733391300': 0, + '1733391900': 0, + '1733392500': 0, + '1733393100': 0, + '1733393700': 0, + '1733394300': 0, + '1733394900': 0, + '1733395500': 0, + '1733396100': 0, + '1733396700': 3, + '1733397300': 0, + '1733397900': 0, + '1733398500': 0, + '1733399100': 0, + '1733399700': 0, + '1733400300': 2, + '1733400900': 7, + '1733401500': 0, + '1733402100': 0, + '1733402700': 0, + '1733403300': 0, + '1733403900': 2, + '1733404500': 0, + '1733405100': 0, + '1733405700': 0, + '1733406300': 0, + '1733406900': 0, + '1733407500': 2, + '1733408100': 0, + '1733408700': 0, + '1733409300': 0, + '1733409900': 0, + '1733410500': 0, + '1733411100': 0, + '1733411700': 0, + '1733412300': 0, + '1733412900': 0, + '1733413500': 0, + '1733414100': 0, + '1733414700': 0, + '1733415300': 0, + '1733415900': 0, + '1733416500': 0, + '1733417100': 0, + '1733417700': 0, + '1733418300': 0, + '1733418900': 0, + '1733419500': 0, + '1733420100': 0, + '1733420700': 0, + '1733421300': 0, + '1733421900': 0, + '1733422500': 0, + '1733423100': 0, + '1733423700': 0, + '1733424300': 0, + '1733424900': 0, + '1733425500': 0, + '1733426100': 0, + '1733426700': 0, + '1733427300': 0, + '1733427900': 0, + '1733428500': 0, + '1733429100': 0, + '1733429700': 0, + '1733430300': 0, + '1733430900': 0, + '1733431500': 0, + '1733432100': 0, + '1733432700': 0, + '1733433300': 0, + '1733433900': 0, + '1733434500': 0, + '1733435100': 0, + '1733435700': 0, + '1733436300': 0, + '1733436900': 0, + '1733437500': 0, + '1733438100': 0, + '1733438700': 0, + '1733439300': 0, + '1733439900': 0, + '1733440500': 0, + '1733441100': 0, + '1733441700': 0, + '1733442300': 0, + '1733442900': 0, + '1733443500': 0, + '1733444100': 0, + '1733444700': 0, + '1733445300': 0, + '1733445900': 0, + '1733446500': 0, + '1733447100': 0, + '1733447700': 0, + '1733448300': 0, + '1733448900': 0, + '1733449500': 0, + '1733450100': 0, + '1733450700': 0, + '1733451300': 0, + '1733451900': 0, + '1733452500': 0, + '1733453100': 0, + '1733453700': 0, + '1733454300': 0, + '1733454900': 0, + '1733455500': 0, + '1733456100': 0, + '1733456700': 0, + '1733457300': 0, + '1733457900': 0, + '1733458500': 0, + '1733459100': 0, + '1733459700': 0, + '1733460300': 0, + '1733460900': 0, + '1733461500': 0, + '1733462100': 0, + '1733462700': 0, + '1733463300': 0, + '1733463900': 0, + '1733464500': 0, + '1733465100': 0, + '1733465700': 0, + '1733466300': 0, + '1733466900': 0, + '1733467500': 0, + '1733468100': 0, + '1733468700': 0, + '1733469300': 0, + '1733469900': 0, + '1733470500': 0, + '1733471100': 0, + '1733471700': 0, + '1733472300': 0, + '1733472900': 0, + '1733473500': 0, + '1733474100': 0, + '1733474700': 0, + '1733475300': 0, + '1733475900': 0, + '1733476500': 0, + '1733477100': 0, + }, + 'ads_over_time': { + '1733391300': 0, + '1733391900': 0, + '1733392500': 0, + '1733393100': 0, + '1733393700': 0, + '1733394300': 0, + '1733394900': 0, + '1733395500': 0, + '1733396100': 0, + '1733396700': 0, + '1733397300': 0, + '1733397900': 0, + '1733398500': 0, + '1733399100': 0, + '1733399700': 0, + '1733400300': 0, + '1733400900': 1, + '1733401500': 0, + '1733402100': 0, + '1733402700': 0, + '1733403300': 0, + '1733403900': 0, + '1733404500': 0, + '1733405100': 0, + '1733405700': 0, + '1733406300': 0, + '1733406900': 0, + '1733407500': 0, + '1733408100': 0, + '1733408700': 0, + '1733409300': 0, + '1733409900': 0, + '1733410500': 0, + '1733411100': 0, + '1733411700': 0, + '1733412300': 0, + '1733412900': 0, + '1733413500': 0, + '1733414100': 0, + '1733414700': 0, + '1733415300': 0, + '1733415900': 0, + '1733416500': 0, + '1733417100': 0, + '1733417700': 0, + '1733418300': 0, + '1733418900': 0, + '1733419500': 0, + '1733420100': 0, + '1733420700': 0, + '1733421300': 0, + '1733421900': 0, + '1733422500': 0, + '1733423100': 0, + '1733423700': 0, + '1733424300': 0, + '1733424900': 0, + '1733425500': 0, + '1733426100': 0, + '1733426700': 0, + '1733427300': 0, + '1733427900': 0, + '1733428500': 0, + '1733429100': 0, + '1733429700': 0, + '1733430300': 0, + '1733430900': 0, + '1733431500': 0, + '1733432100': 0, + '1733432700': 0, + '1733433300': 0, + '1733433900': 0, + '1733434500': 0, + '1733435100': 0, + '1733435700': 0, + '1733436300': 0, + '1733436900': 0, + '1733437500': 0, + '1733438100': 0, + '1733438700': 0, + '1733439300': 0, + '1733439900': 0, + '1733440500': 0, + '1733441100': 0, + '1733441700': 0, + '1733442300': 0, + '1733442900': 0, + '1733443500': 0, + '1733444100': 0, + '1733444700': 0, + '1733445300': 0, + '1733445900': 0, + '1733446500': 0, + '1733447100': 0, + '1733447700': 0, + '1733448300': 0, + '1733448900': 0, + '1733449500': 0, + '1733450100': 0, + '1733450700': 0, + '1733451300': 0, + '1733451900': 0, + '1733452500': 0, + '1733453100': 0, + '1733453700': 0, + '1733454300': 0, + '1733454900': 0, + '1733455500': 0, + '1733456100': 0, + '1733456700': 0, + '1733457300': 0, + '1733457900': 0, + '1733458500': 0, + '1733459100': 0, + '1733459700': 0, + '1733460300': 0, + '1733460900': 0, + '1733461500': 0, + '1733462100': 0, + '1733462700': 0, + '1733463300': 0, + '1733463900': 0, + '1733464500': 0, + '1733465100': 0, + '1733465700': 0, + '1733466300': 0, + '1733466900': 0, + '1733467500': 0, + '1733468100': 0, + '1733468700': 0, + '1733469300': 0, + '1733469900': 0, + '1733470500': 0, + '1733471100': 0, + '1733471700': 0, + '1733472300': 0, + '1733472900': 0, + '1733473500': 0, + '1733474100': 0, + '1733474700': 0, + '1733475300': 0, + '1733475900': 0, + '1733476500': 0, + '1733477100': 0, + }, + 'clients': [ + {'name': '', 'ip': '172.26.0.1'}, + {'name': 'localhost', 'ip': '127.0.0.1'}, + ], + 'over_time': { + '1733391300': [0, 0], + '1733391900': [0, 0], + '1733392500': [0, 0], + '1733393100': [0, 0], + '1733393700': [0, 0], + '1733394300': [0, 0], + '1733394900': [0, 0], + '1733395500': [0, 0], + '1733396100': [0, 0], + '1733396700': [3, 0], + '1733397300': [0, 0], + '1733397900': [0, 0], + '1733398500': [0, 0], + '1733399100': [0, 0], + '1733399700': [0, 0], + '1733400300': [0, 2], + '1733400900': [7, 0], + '1733401500': [0, 0], + '1733402100': [0, 0], + '1733402700': [0, 0], + '1733403300': [0, 0], + '1733403900': [0, 2], + '1733404500': [0, 0], + '1733405100': [0, 0], + '1733405700': [0, 0], + '1733406300': [0, 0], + '1733406900': [0, 0], + '1733407500': [0, 2], + '1733408100': [0, 0], + '1733408700': [0, 0], + '1733409300': [0, 0], + '1733409900': [0, 0], + '1733410500': [0, 0], + '1733411100': [0, 0], + '1733411700': [0, 0], + '1733412300': [0, 0], + '1733412900': [0, 0], + '1733413500': [0, 0], + '1733414100': [0, 0], + '1733414700': [0, 0], + '1733415300': [0, 0], + '1733415900': [0, 0], + '1733416500': [0, 0], + '1733417100': [0, 0], + '1733417700': [0, 0], + '1733418300': [0, 0], + '1733418900': [0, 0], + '1733419500': [0, 0], + '1733420100': [0, 0], + '1733420700': [0, 0], + '1733421300': [0, 0], + '1733421900': [0, 0], + '1733422500': [0, 0], + '1733423100': [0, 0], + '1733423700': [0, 0], + '1733424300': [0, 0], + '1733424900': [0, 0], + '1733425500': [0, 0], + '1733426100': [0, 0], + '1733426700': [0, 0], + '1733427300': [0, 0], + '1733427900': [0, 0], + '1733428500': [0, 0], + '1733429100': [0, 0], + '1733429700': [0, 0], + '1733430300': [0, 0], + '1733430900': [0, 0], + '1733431500': [0, 0], + '1733432100': [0, 0], + '1733432700': [0, 0], + '1733433300': [0, 0], + '1733433900': [0, 0], + '1733434500': [0, 0], + '1733435100': [0, 0], + '1733435700': [0, 0], + '1733436300': [0, 0], + '1733436900': [0, 0], + '1733437500': [0, 0], + '1733438100': [0, 0], + '1733438700': [0, 0], + '1733439300': [0, 0], + '1733439900': [0, 0], + '1733440500': [0, 0], + '1733441100': [0, 0], + '1733441700': [0, 0], + '1733442300': [0, 0], + '1733442900': [0, 0], + '1733443500': [0, 0], + '1733444100': [0, 0], + '1733444700': [0, 0], + '1733445300': [0, 0], + '1733445900': [0, 0], + '1733446500': [0, 0], + '1733447100': [0, 0], + '1733447700': [0, 0], + '1733448300': [0, 0], + '1733448900': [0, 0], + '1733449500': [0, 0], + '1733450100': [0, 0], + '1733450700': [0, 0], + '1733451300': [0, 0], + '1733451900': [0, 0], + '1733452500': [0, 0], + '1733453100': [0, 0], + '1733453700': [0, 0], + '1733454300': [0, 0], + '1733454900': [0, 0], + '1733455500': [0, 0], + '1733456100': [0, 0], + '1733456700': [0, 0], + '1733457300': [0, 0], + '1733457900': [0, 0], + '1733458500': [0, 0], + '1733459100': [0, 0], + '1733459700': [0, 0], + '1733460300': [0, 0], + '1733460900': [0, 0], + '1733461500': [0, 0], + '1733462100': [0, 0], + '1733462700': [0, 0], + '1733463300': [0, 0], + '1733463900': [0, 0], + '1733464500': [0, 0], + '1733465100': [0, 0], + '1733465700': [0, 0], + '1733466300': [0, 0], + '1733466900': [0, 0], + '1733467500': [0, 0], + '1733468100': [0, 0], + '1733468700': [0, 0], + '1733469300': [0, 0], + '1733469900': [0, 0], + '1733470500': [0, 0], + '1733471100': [0, 0], + '1733471700': [0, 0], + '1733472300': [0, 0], + '1733472900': [0, 0], + '1733473500': [0, 0], + '1733474100': [0, 0], + '1733474700': [0, 0], + '1733475300': [0, 0], + '1733475900': [0, 0], + '1733476500': [0, 0], + '1733477100': [0, 0], + }, + }; + + final overtimeData = OverTimeData.fromJson(data); + statusProvider.setOvertimeData(overtimeData); + expect(statusProvider.getOvertimeData, overtimeData); + expect(statusProvider.getOvertimeDataJson, overtimeData.toJson()); + expect(listenerCalled, true); + }); + }); +}