From 63f82c7e26c01437514454c8398c7135aae64faf Mon Sep 17 00:00:00 2001 From: Ethan Lee <125412902+ethan-tbd@users.noreply.github.com> Date: Fri, 14 Jun 2024 14:29:42 -0700 Subject: [PATCH] chore: add test data (#204) --- pubspec.lock | 2 +- pubspec.yaml | 1 + test/features/account/account_page_test.dart | 7 +- test/features/app/app_tabs_test.dart | 22 ++- test/features/app/app_test.dart | 19 ++- .../countries/countries_notifier_test.dart | 29 ++-- .../countries/countries_page_test.dart | 37 ++--- .../feature_flags_notifier_test.dart | 30 ++-- .../lucid/lucid_offerings_page_test.dart | 24 ++- test/features/home/home_page_test.dart | 30 ++-- test/features/payin/payin_test.dart | 34 +--- .../payment/payment_amount_page_test.dart | 24 ++- .../payment/payment_details_page_test.dart | 59 ++----- .../payment/payment_methods_page_test.dart | 89 ++++------- .../payment/payment_review_page_test.dart | 32 ++-- .../payment/payment_types_page_test.dart | 16 +- test/features/payout/payout_test.dart | 35 +---- test/features/pfis/pfis_notifier_test.dart | 19 ++- .../features/send/send_details_page_test.dart | 5 +- test/features/send/send_page_test.dart | 14 +- test/helpers/test_data.dart | 147 ++++++++++++++++++ 21 files changed, 324 insertions(+), 351 deletions(-) create mode 100644 test/helpers/test_data.dart diff --git a/pubspec.lock b/pubspec.lock index 96523179..a8e8c8cd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -812,7 +812,7 @@ packages: source: hosted version: "1.3.2" typeid: - dependency: transitive + dependency: "direct dev" description: name: typeid sha256: "1e4beac12e6cdf65299bdc458a44e3a2a4b2b0bd88ca93bee81011ab4e1133cd" diff --git a/pubspec.yaml b/pubspec.yaml index 79da2cb1..311bf0f7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,7 @@ dev_dependencies: mocktail: ^1.0.2 flutter_launcher_icons: ^0.13.1 json_schema: ^5.1.7 + typeid: ^1.0.1 flutter: uses-material-design: true diff --git a/test/features/account/account_page_test.dart b/test/features/account/account_page_test.dart index b6df5c36..4a908c6a 100644 --- a/test/features/account/account_page_test.dart +++ b/test/features/account/account_page_test.dart @@ -7,11 +7,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; -void main() async { - const dap = 'username@didpay.me'; - +void main() { late MockPfisNotifier mockPfisNotifier; late MockVcsNotifier mockVcsNotifier; late MockFeatureFlagsNotifier mockFeatureFlagsNotifier; @@ -36,7 +35,7 @@ void main() async { testWidgets('should show DAP', (tester) async { await tester.pumpWidget(accountPageTestWidget()); - expect(find.text(dap), findsOneWidget); + expect(find.text(TestData.dap), findsOneWidget); }); testWidgets('should show linked pfis', (tester) async { diff --git a/test/features/app/app_tabs_test.dart b/test/features/app/app_tabs_test.dart index 056a554a..a8c4c509 100644 --- a/test/features/app/app_tabs_test.dart +++ b/test/features/app/app_tabs_test.dart @@ -1,11 +1,9 @@ -import 'package:didpay/features/account/account_balance.dart'; import 'package:didpay/features/account/account_balance_notifier.dart'; import 'package:didpay/features/account/account_page.dart'; import 'package:didpay/features/app/app_tabs.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/feature_flags/feature_flags_notifier.dart'; import 'package:didpay/features/home/home_page.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/send/send_page.dart'; import 'package:didpay/features/tbdex/tbdex_service.dart'; @@ -14,16 +12,17 @@ import 'package:didpay/features/vcs/vcs_notifier.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:web5/web5.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - final did = await DidDht.create(); - final accountBalance = - AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {}); - const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); + await TestData.initializeDids(); + + final did = TestData.aliceDid; + final pfis = TestData.getPfis(); + final accountBalance = TestData.getAccountBalance(); late MockTbdexService mockTbdexService; late MockPfisNotifier mockPfisNotifier; @@ -32,19 +31,18 @@ void main() async { setUp(() { mockTbdexService = MockTbdexService(); - mockPfisNotifier = MockPfisNotifier([pfi]); + mockPfisNotifier = MockPfisNotifier(pfis); mockVcsNotifier = MockVcsNotifier([]); mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]); when( - () => mockTbdexService.getExchanges(did, [pfi]), + () => mockTbdexService.getExchanges(did, pfis), ).thenAnswer((_) async => {}); when( - () => mockTbdexService.getAccountBalance([pfi]), + () => mockTbdexService.getAccountBalance(pfis), ).thenAnswer( - (_) async => - AccountBalance(total: '0', currencyCode: 'USD', balancesMap: {}), + (_) async => accountBalance, ); }); diff --git a/test/features/app/app_test.dart b/test/features/app/app_test.dart index 43802b60..34491743 100644 --- a/test/features/app/app_test.dart +++ b/test/features/app/app_test.dart @@ -1,22 +1,22 @@ -import 'package:didpay/features/account/account_balance.dart'; import 'package:didpay/features/app/app_tabs.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/feature_flags/feature_flags_notifier.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/tbdex/tbdex_service.dart'; import 'package:didpay/features/transaction/transaction_notifier.dart'; import 'package:didpay/features/vcs/vcs_notifier.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:web5/web5.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - final did = await DidDht.create(); - const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); + await TestData.initializeDids(); + + final did = TestData.aliceDid; + final pfis = TestData.getPfis(); late MockTbdexService mockTbdexService; late MockPfisNotifier mockPfisNotifier; @@ -25,19 +25,18 @@ void main() async { setUp(() { mockTbdexService = MockTbdexService(); - mockPfisNotifier = MockPfisNotifier([pfi]); + mockPfisNotifier = MockPfisNotifier(pfis); mockVcsNotifier = MockVcsNotifier([]); mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]); when( - () => mockTbdexService.getExchanges(did, [pfi]), + () => mockTbdexService.getExchanges(did, pfis), ).thenAnswer((_) async => {}); when( - () => mockTbdexService.getAccountBalance([pfi]), + () => mockTbdexService.getAccountBalance(pfis), ).thenAnswer( - (_) async => - AccountBalance(total: '0', currencyCode: 'USD', balancesMap: {}), + (_) async => TestData.getAccountBalance(), ); }); diff --git a/test/features/countries/countries_notifier_test.dart b/test/features/countries/countries_notifier_test.dart index e0651082..0ec03897 100644 --- a/test/features/countries/countries_notifier_test.dart +++ b/test/features/countries/countries_notifier_test.dart @@ -1,11 +1,15 @@ -import 'package:didpay/features/countries/countries.dart'; import 'package:didpay/features/countries/countries_notifier.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; void main() { + final australia = TestData.getCountry('Australia', 'AU'); + final us = TestData.getCountry('United States', 'US'); + final initialCountries = [australia]; + late MockBox mockBox; setUp(() { @@ -14,10 +18,9 @@ void main() { group('CountriesNotifier', () { test('should create and load initial list', () async { - final initialCountries = [const Country(name: 'Mexico', code: 'MXN')]; - when(() => mockBox.get(CountriesNotifier.storageKey)).thenReturn( - initialCountries.map((country) => country.toJson()).toList(),); + initialCountries.map((country) => country.toJson()).toList(), + ); final notifier = await CountriesNotifier.create(mockBox); expect(notifier.state, initialCountries); @@ -25,11 +28,9 @@ void main() { }); test('should add a new Country', () async { - final initialCountries = [const Country(name: 'Mexico', code: 'MXN')]; - const newCountry = Country(name: 'United States', code: 'USD'); - when(() => mockBox.get(CountriesNotifier.storageKey)).thenReturn( - initialCountries.map((country) => country.toJson()).toList(),); + initialCountries.map((country) => country.toJson()).toList(), + ); when( () => mockBox.put( CountriesNotifier.storageKey, @@ -39,28 +40,28 @@ void main() { final notifier = await CountriesNotifier.create(mockBox); - final addedCountry = await notifier.add(newCountry); + final addedCountry = await notifier.add(us); - expect(notifier.state, [...initialCountries, newCountry]); - expect(addedCountry, newCountry); + expect(notifier.state, [...initialCountries, us]); + expect(addedCountry, us); verify( () => mockBox.put( CountriesNotifier.storageKey, [ ...initialCountries.map((country) => country.toJson()), - newCountry.toJson(), + us.toJson(), ], ), ).called(1); }); test('should remove a Country', () async { - final initialCountries = [const Country(name: 'Mexico', code: 'MXN')]; final countryToRemove = initialCountries.first; when(() => mockBox.get(CountriesNotifier.storageKey)).thenReturn( - initialCountries.map((country) => country.toJson()).toList(),); + initialCountries.map((country) => country.toJson()).toList(), + ); when(() => mockBox.put(CountriesNotifier.storageKey, any())) .thenAnswer((_) async {}); diff --git a/test/features/countries/countries_page_test.dart b/test/features/countries/countries_page_test.dart index efa9ea95..a2e12459 100644 --- a/test/features/countries/countries_page_test.dart +++ b/test/features/countries/countries_page_test.dart @@ -1,38 +1,35 @@ -import 'package:didpay/features/countries/countries.dart'; import 'package:didpay/features/countries/countries_notifier.dart'; import 'package:didpay/features/countries/countries_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() { - Widget testableWidget({List initialCountries = const []}) { - final countriesNotifier = MockCountriesNotifier(initialCountries); - - return WidgetHelpers.testableWidget( - child: const CountriesPage(), - overrides: [ - countriesProvider.overrideWith((ref) => countriesNotifier), - ], - ); - } + final australia = TestData.getCountry('Australia', 'AU'); + final us = TestData.getCountry('United States', 'US'); + final initialCountries = [australia, us]; group('CountriesPage', () { - testWidgets('should show countries', (tester) async { - await tester.pumpWidget( - testableWidget( - initialCountries: const [ - Country(name: 'Mexico', code: 'MXN'), - Country(name: 'United States', code: 'USD'), - ], - ), + Widget countriesPageTestWidget() { + final countriesNotifier = MockCountriesNotifier(initialCountries); + + return WidgetHelpers.testableWidget( + child: const CountriesPage(), + overrides: [ + countriesProvider.overrideWith((ref) => countriesNotifier), + ], ); + } + + testWidgets('should show countries', (tester) async { + await tester.pumpWidget(countriesPageTestWidget()); await tester.pumpAndSettle(); - expect(find.text('Mexico'), findsOneWidget); + expect(find.text('Australia'), findsOneWidget); expect(find.text('United States'), findsOneWidget); }); }); diff --git a/test/features/feature_flags/feature_flags_notifier_test.dart b/test/features/feature_flags/feature_flags_notifier_test.dart index 57121f4e..665b2e0b 100644 --- a/test/features/feature_flags/feature_flags_notifier_test.dart +++ b/test/features/feature_flags/feature_flags_notifier_test.dart @@ -1,11 +1,15 @@ -import 'package:didpay/features/feature_flags/feature_flag.dart'; import 'package:didpay/features/feature_flags/feature_flags_notifier.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; void main() { + final testFlag = TestData.getFeatureFlag('test', 'test description'); + final newFlag = TestData.getFeatureFlag('new test', 'new test description'); + final initialFeatureFlags = [testFlag]; + late MockBox mockBox; setUp(() { @@ -14,10 +18,6 @@ void main() { group('FeatureFlagsNotifier', () { test('should create and load initial list', () async { - final initialFeatureFlags = [ - const FeatureFlag(name: 'test', description: 'test flag'), - ]; - when(() => mockBox.get(FeatureFlagsNotifier.storageKey)).thenReturn( initialFeatureFlags.map((flag) => flag.toJson()).toList(), ); @@ -28,14 +28,9 @@ void main() { }); test('should add a new FeatureFlag', () async { - final initialFeatureFlags = [ - const FeatureFlag(name: 'test', description: 'test flag'), - ]; - const newFeatureFlag = - FeatureFlag(name: 'new test', description: 'new test flag'); - when(() => mockBox.get(FeatureFlagsNotifier.storageKey)).thenReturn( - initialFeatureFlags.map((flag) => flag.toJson()).toList(),); + initialFeatureFlags.map((flag) => flag.toJson()).toList(), + ); when( () => mockBox.put( FeatureFlagsNotifier.storageKey, @@ -45,26 +40,23 @@ void main() { final notifier = await FeatureFlagsNotifier.create(mockBox); - final addedFeatureFlag = await notifier.add(newFeatureFlag); + final addedFeatureFlag = await notifier.add(newFlag); - expect(notifier.state, [...initialFeatureFlags, newFeatureFlag]); - expect(addedFeatureFlag, newFeatureFlag); + expect(notifier.state, [...initialFeatureFlags, newFlag]); + expect(addedFeatureFlag, newFlag); verify( () => mockBox.put( FeatureFlagsNotifier.storageKey, [ ...initialFeatureFlags.map((flag) => flag.toJson()), - newFeatureFlag.toJson(), + newFlag.toJson(), ], ), ).called(1); }); test('should remove a FeatureFlag', () async { - final initialFeatureFlags = [ - const FeatureFlag(name: 'test', description: 'test flag'), - ]; final featureFlagToRemove = initialFeatureFlags.first; when(() => mockBox.get(FeatureFlagsNotifier.storageKey)).thenReturn( diff --git a/test/features/feature_flags/lucid/lucid_offerings_page_test.dart b/test/features/feature_flags/lucid/lucid_offerings_page_test.dart index 1c84355a..2636137f 100644 --- a/test/features/feature_flags/lucid/lucid_offerings_page_test.dart +++ b/test/features/feature_flags/lucid/lucid_offerings_page_test.dart @@ -1,38 +1,32 @@ -import 'dart:convert'; - import 'package:didpay/features/feature_flags/feature_flags_notifier.dart'; import 'package:didpay/features/feature_flags/lucid/lucid_offerings_page.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/tbdex/tbdex_service.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:tbdex/tbdex.dart'; import '../../../helpers/mocks.dart'; +import '../../../helpers/test_data.dart'; import '../../../helpers/widget_helpers.dart'; -void main() { - const jsonString = - r'''[{"metadata":{"kind":"offering","from":"did:web:localhost%3A8892:ingress","id":"offering_01hv22zfv1eptadkm92v278gh9","protocol":"1.0","createdAt":"2024-04-12T20:57:11Z","updatedAt":"2024-04-12T20:57:11Z"},"data":{"description":"MXN for USD","payoutUnitsPerPayinUnit":"16.34","payin":{"currencyCode":"USD","methods":[{"kind":"STORED_BALANCE","name":"Account balance"}]},"payout":{"currencyCode":"MXN","methods":[{"kind":"SPEI","estimatedSettlementTime":300,"name":"SPEI","requiredPaymentDetails":{"$schema":"http://json-schema.org/draft-07/schema#","additionalProperties":false,"properties":{"clabe":{"type":"string"},"fullName":{"type":"string"}},"required":["clabe","fullName"]}}]}},"signature":"eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDp3ZWI6bG9jYWxob3N0JTNBODg5MjppbmdyZXNzIzAifQ..le65W3WyI2UKMJojADv_lTQixt0wDmnMMBVaWC_2BaYVQfe8HY3gQyPqbI4dT-iDNRjg_EdlCvTiEzANfp0lDw"}]'''; - const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); +void main() async { + await TestData.initializeDids(); + + final pfis = TestData.getPfis(); + final offerings = TestData.getOfferingsMap(); - final jsonList = jsonDecode(jsonString) as List; - final offerings = { - pfi: [Offering.fromJson(jsonList[0])], - }; late MockTbdexService mockTbdexService; late MockPfisNotifier mockPfisNotifier; late MockFeatureFlagsNotifier mockFeatureFlagsNotifier; setUp(() { mockTbdexService = MockTbdexService(); - mockPfisNotifier = MockPfisNotifier([pfi]); + mockPfisNotifier = MockPfisNotifier(pfis); mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]); when( - () => mockTbdexService.getOfferings([pfi]), + () => mockTbdexService.getOfferings(pfis), ).thenAnswer((_) async => offerings); }); @@ -69,7 +63,7 @@ void main() { await tester.pumpAndSettle(); expect( - find.widgetWithText(ListTile, 'USD → MXN'), + find.widgetWithText(ListTile, 'AUD → USD'), findsOneWidget, ); }); diff --git a/test/features/home/home_page_test.dart b/test/features/home/home_page_test.dart index 8772e92a..db977bc7 100644 --- a/test/features/home/home_page_test.dart +++ b/test/features/home/home_page_test.dart @@ -1,37 +1,27 @@ -import 'dart:convert'; - import 'package:didpay/features/account/account_balance.dart'; import 'package:didpay/features/account/account_balance_card.dart'; import 'package:didpay/features/account/account_balance_notifier.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/home/home_page.dart'; import 'package:didpay/features/payment/payment_amount_page.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/tbdex/tbdex_service.dart'; import 'package:didpay/features/transaction/transaction_notifier.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:tbdex/tbdex.dart'; -import 'package:web5/web5.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - final did = await DidDht.create(); - - const jsonString = - r'''[{"metadata":{"kind":"offering","from":"did:web:localhost%3A8892:ingress","id":"offering_01hv22zfv1eptadkm92v278gh9","protocol":"1.0","createdAt":"2024-04-12T20:57:11Z","updatedAt":"2024-04-12T20:57:11Z"},"data":{"description":"MXN for USD","payoutUnitsPerPayinUnit":"16.34","payin":{"currencyCode":"USD","methods":[{"kind":"STORED_BALANCE","name":"Account balance"}]},"payout":{"currencyCode":"MXN","methods":[{"kind":"SPEI","estimatedSettlementTime":300,"name":"SPEI","requiredPaymentDetails":{"$schema":"http://json-schema.org/draft-07/schema#","additionalProperties":false,"properties":{"clabe":{"type":"string"},"fullName":{"type":"string"}},"required":["clabe","fullName"]}}]}},"signature":"eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDp3ZWI6bG9jYWxob3N0JTNBODg5MjppbmdyZXNzIzAifQ..le65W3WyI2UKMJojADv_lTQixt0wDmnMMBVaWC_2BaYVQfe8HY3gQyPqbI4dT-iDNRjg_EdlCvTiEzANfp0lDw"}]'''; - const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); + await TestData.initializeDids(); - final jsonList = jsonDecode(jsonString) as List; - final offerings = { - pfi: [Offering.fromJson(jsonList[0])], - }; - final accountBalance = - AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {}); + final did = TestData.aliceDid; + final pfis = TestData.getPfis(); + final offerings = TestData.getOfferingsMap(); + final accountBalance = TestData.getAccountBalance(); late MockTbdexService mockTbdexService; late MockPfisNotifier mockPfisNotifier; @@ -51,18 +41,18 @@ void main() async { setUp(() { mockTbdexService = MockTbdexService(); - mockPfisNotifier = MockPfisNotifier([pfi]); + mockPfisNotifier = MockPfisNotifier(pfis); when( - () => mockTbdexService.getOfferings([pfi]), + () => mockTbdexService.getOfferings(pfis), ).thenAnswer((_) async => offerings); when( - () => mockTbdexService.getExchanges(did, [pfi]), + () => mockTbdexService.getExchanges(did, pfis), ).thenAnswer((_) async => {}); when( - () => mockTbdexService.getAccountBalance([pfi]), + () => mockTbdexService.getAccountBalance(pfis), ).thenAnswer( (_) async => AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {}), diff --git a/test/features/payin/payin_test.dart b/test/features/payin/payin_test.dart index 5071d2ba..e95b0d58 100644 --- a/test/features/payin/payin_test.dart +++ b/test/features/payin/payin_test.dart @@ -8,40 +8,16 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tbdex/tbdex.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; -void main() { +void main() async { + await TestData.initializeDids(); + group('Payin', () { final amount = ValueNotifier('70'); final pfi = ValueNotifier(null); - final offering = ValueNotifier( - Offering.create( - 'pfiDid', - OfferingData( - description: '', - payoutUnitsPerPayinUnit: '1', - payin: PayinDetails( - currencyCode: 'AUD', - min: '0.01', - max: '100.00', - methods: [ - PayinMethod( - kind: 'DEBIT_CARD', - ), - ], - ), - payout: PayoutDetails( - currencyCode: 'USDC', - methods: [ - PayoutMethod( - estimatedSettlementTime: 0, - kind: 'DEBIT_CARD', - ), - ], - ), - ), - ), - ); + final offering = ValueNotifier(TestData.getOffering()); final keyPress = ValueNotifier(NumberKeyPress(0, '')); final paymentState = PaymentState( diff --git a/test/features/payment/payment_amount_page_test.dart b/test/features/payment/payment_amount_page_test.dart index b56561b0..d9e81404 100644 --- a/test/features/payment/payment_amount_page_test.dart +++ b/test/features/payment/payment_amount_page_test.dart @@ -1,40 +1,34 @@ -import 'dart:convert'; - import 'package:didpay/features/payin/payin.dart'; import 'package:didpay/features/payment/payment_amount_page.dart'; import 'package:didpay/features/payment/payment_fee_details.dart'; import 'package:didpay/features/payment/payment_state.dart'; import 'package:didpay/features/payout/payout.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/tbdex/tbdex_service.dart'; import 'package:didpay/features/transaction/transaction.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:tbdex/tbdex.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; -void main() { - const jsonString = - r'''[{"metadata":{"kind":"offering","from":"did:web:localhost%3A8892:ingress","id":"offering_01hv22zfv1eptadkm92v278gh9","protocol":"1.0","createdAt":"2024-04-12T20:57:11Z","updatedAt":"2024-04-12T20:57:11Z"},"data":{"description":"MXN for USD","payoutUnitsPerPayinUnit":"16.34","payin":{"currencyCode":"USD","methods":[{"kind":"STORED_BALANCE","name":"Account balance"}]},"payout":{"currencyCode":"MXN","methods":[{"kind":"SPEI","estimatedSettlementTime":300,"name":"SPEI","requiredPaymentDetails":{"$schema":"http://json-schema.org/draft-07/schema#","additionalProperties":false,"properties":{"clabe":{"type":"string"},"fullName":{"type":"string"}},"required":["clabe","fullName"]}}]}},"signature":"eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDp3ZWI6bG9jYWxob3N0JTNBODg5MjppbmdyZXNzIzAifQ..le65W3WyI2UKMJojADv_lTQixt0wDmnMMBVaWC_2BaYVQfe8HY3gQyPqbI4dT-iDNRjg_EdlCvTiEzANfp0lDw"}]'''; - const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); +void main() async { + await TestData.initializeDids(); + + final offerings = TestData.getOfferingsMap(); + final pfis = TestData.getPfis(); - final jsonList = jsonDecode(jsonString) as List; - final offerings = { - pfi: [Offering.fromJson(jsonList[0])], - }; late MockTbdexService mockTbdexService; late MockPfisNotifier mockPfisNotifier; setUp(() { mockTbdexService = MockTbdexService(); - mockPfisNotifier = MockPfisNotifier([pfi]); + mockPfisNotifier = MockPfisNotifier(pfis); when( - () => mockTbdexService.getOfferings([pfi]), + () => mockTbdexService.getOfferings(pfis), ).thenAnswer((_) async => offerings); }); @@ -58,7 +52,7 @@ void main() { expect(find.byType(Payout), findsOneWidget); }); - testWidgets('should show Fee Details', (tester) async { + testWidgets('should show fee details', (tester) async { await tester.pumpWidget(paymentAmountPageTestWidget()); await tester.pumpAndSettle(); diff --git a/test/features/payment/payment_details_page_test.dart b/test/features/payment/payment_details_page_test.dart index 371e8539..3a0187c2 100644 --- a/test/features/payment/payment_details_page_test.dart +++ b/test/features/payment/payment_details_page_test.dart @@ -1,25 +1,24 @@ -import 'dart:convert'; - -import 'package:decimal/decimal.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/payment/payment_details_page.dart'; import 'package:didpay/features/payment/payment_methods_page.dart'; import 'package:didpay/features/payment/payment_state.dart'; import 'package:didpay/features/payment/payment_types_page.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/transaction/transaction.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:json_schema/json_schema.dart'; import 'package:tbdex/tbdex.dart'; -import 'package:web5/web5.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - final did = await DidDht.create(); + await TestData.initializeDids(); + + final did = TestData.aliceDid; + final schema = TestData.paymentDetailsSchema(); + late MockPfisNotifier mockPfisNotifier; group('PaymentDetailsPage', () { @@ -27,41 +26,6 @@ void main() async { mockPfisNotifier = MockPfisNotifier([]); }); - final schema = JsonSchema.create( - jsonDecode(r''' - { - "$schema": "http://json-schema.org/draft-07/schema#", - "type": "object", - "properties": { - "cardNumber": { - "type": "string", - "title": "Card number", - "description": "The 16-digit debit card number", - "minLength": 16, - "maxLength": 16 - }, - "expiryDate": { - "type": "string", - "description": "The expiry date of the card in MM/YY format", - "pattern": "^(0[1-9]|1[0-2])\\/([0-9]{2})$" - }, - "cardHolderName": { - "type": "string", - "description": "Name of the cardholder as it appears on the card" - }, - "cvv": { - "type": "string", - "description": "The 3-digit CVV code", - "minLength": 3, - "maxLength": 3 - } - }, - "required": ["cardNumber", "expiryDate", "cardHolderName", "cvv"], - "additionalProperties": false - } - '''), - ); - Widget paymentDetailsPageTestWidget({ List payinMethods = const [], List payoutMethods = const [], @@ -69,11 +33,6 @@ void main() async { WidgetHelpers.testableWidget( child: PaymentDetailsPage( paymentState: PaymentState( - selectedPfi: const Pfi(did: ''), - payoutAmount: Decimal.parse('17.00'), - payinCurrency: 'USD', - payoutCurrency: 'MXN', - exchangeRate: Decimal.parse('17.00'), transactionType: TransactionType.deposit, payinMethods: payinMethods, payoutMethods: payoutMethods, @@ -301,8 +260,8 @@ void main() async { child: paymentDetailsPageTestWidget( payinMethods: [ PayinMethod( - kind: 'MOMO_MPESA', - name: 'M-Pesa', + kind: 'test', + name: 'test', requiredPaymentDetails: schema, ), ], @@ -311,7 +270,7 @@ void main() async { ); expect(find.byType(TextFormField), findsExactly(4)); - expect(find.text('Card number'), findsOneWidget); + expect(find.text('cardNumber'), findsOneWidget); expect(find.text('expiryDate'), findsOneWidget); expect(find.text('cardHolderName'), findsOneWidget); expect(find.text('cvv'), findsOneWidget); diff --git a/test/features/payment/payment_methods_page_test.dart b/test/features/payment/payment_methods_page_test.dart index c6dca6f5..de6ee2f9 100644 --- a/test/features/payment/payment_methods_page_test.dart +++ b/test/features/payment/payment_methods_page_test.dart @@ -1,73 +1,36 @@ -import 'dart:convert'; - import 'package:didpay/features/payment/payment_methods_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:json_schema/json_schema.dart'; import 'package:tbdex/tbdex.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; -final schema = JsonSchema.create( - jsonDecode(r''' - { - "$schema": "http://json-schema.org/draft-07/schema#", - "type": "object", - "properties": { - "cardNumber": { - "type": "string", - "title": "Card number", - "description": "The 16-digit debit card number", - "minLength": 16, - "maxLength": 16 - }, - "expiryDate": { - "type": "string", - "description": "The expiry date of the card in MM/YY format", - "pattern": "^(0[1-9]|1[0-2])\\/([0-9]{2})$" - }, - "cardHolderName": { - "type": "string", - "description": "Name of the cardholder as it appears on the card" - }, - "cvv": { - "type": "string", - "description": "The 3-digit CVV code", - "minLength": 3, - "maxLength": 3 - } - }, - "required": ["cardNumber", "expiryDate", "cardHolderName", "cvv"], - "additionalProperties": false - } - '''), -); - -final _paymentMethods = [ - PayinMethod( - kind: 'BANK_ACCESS BANK', - name: 'Access Bank', - requiredPaymentDetails: schema, - fee: '9.0', - ), - PayinMethod( - kind: 'MOMO_MTN', - name: 'MTN', - requiredPaymentDetails: schema, - ), -]; - -Widget paymentMethodsPageTestWidget() => WidgetHelpers.testableWidget( - child: PaymentMethodsPage( - paymentCurrency: '', - selectedPaymentMethod: - ValueNotifier(_paymentMethods.first), - paymentMethods: _paymentMethods, - ), - ); - void main() { + final schema = TestData.paymentDetailsSchema(); + final paymentMethods = [ + PayinMethod( + kind: 'BANK_ACCESS BANK', + name: 'Access Bank', + requiredPaymentDetails: schema, + fee: '9.0', + ), + PayinMethod( + kind: 'MOMO_MTN', + name: 'MTN', + requiredPaymentDetails: schema, + ), + ]; + group('PaymentMethodsPage', () { + Widget paymentMethodsPageTestWidget() => WidgetHelpers.testableWidget( + child: PaymentMethodsPage( + paymentCurrency: '', + selectedPaymentMethod: + ValueNotifier(paymentMethods.first), + paymentMethods: paymentMethods, + ), + ); testWidgets('should show search field', (tester) async { await tester.pumpWidget( WidgetHelpers.testableWidget(child: paymentMethodsPageTestWidget()), @@ -84,8 +47,8 @@ void main() { child: PaymentMethodsPage( paymentCurrency: '', selectedPaymentMethod: - ValueNotifier(_paymentMethods.first), - paymentMethods: _paymentMethods, + ValueNotifier(paymentMethods.first), + paymentMethods: paymentMethods, ), ), ); diff --git a/test/features/payment/payment_review_page_test.dart b/test/features/payment/payment_review_page_test.dart index 96620387..4caa30f9 100644 --- a/test/features/payment/payment_review_page_test.dart +++ b/test/features/payment/payment_review_page_test.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - import 'package:auto_size_text/auto_size_text.dart'; import 'package:decimal/decimal.dart'; import 'package:didpay/features/did/did_provider.dart'; @@ -12,25 +10,17 @@ import 'package:didpay/features/transaction/transaction.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -import 'package:tbdex/tbdex.dart'; -import 'package:web5/web5.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - const quoteString = - '{"data":{"payin":{"fee":"0.1","amount":"10","currencyCode":"USD"},"payout":{"fee":"0.5","amount":"500","currencyCode":"MXN"},"expiresAt":"2024-05-03T22:26:39Z"},"metadata":{"id":"quote_01hwxpncmpfkmt16azhe9vhxvr","to":"did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6Ik5qUUNMeE9tVEN4NFlYQ1MyR2t0T2FQbkZHLXBUZFRqZ0F0U3AtX002SEEifQ","from":"did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6ImswMEpaTXFVdGRtUFZORkVhOHIxek1FWXZ3WXFCVnVVcWtLS3BsdkxhSEkifQ","kind":"quote","protocol":"1.0","createdAt":"2024-05-02T22:26:39Z","exchangeId":"rfq_01hwxpmsrje6cts27kdzy3n66y"},"signature":"eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDpqd2s6ZXlKcmRIa2lPaUpQUzFBaUxDSmpjbllpT2lKRlpESTFOVEU1SWl3aWVDSTZJbXN3TUVwYVRYRlZkR1J0VUZaT1JrVmhPSEl4ZWsxRldYWjNXWEZDVm5WVmNXdExTM0JzZGt4aFNFa2lmUSMwIn0..KRs_rAGNTZ_w9H-8lg3RR4jShi4iPTz4sW9o7eCmaMKPH4ETYbK4n0xRskKzyS-Wkx3oqGY0vFQs5kYsysi_Aw"}'; - const orderString = - '''{"metadata":{"kind":"order","to":"did:jwk:eyJrdHkiOiJPS1AiLCJhbGciOiJFZERTQSIsImtpZCI6ImpCU21VNF94OGZYSkZ4QkNyUWN3QlJ1VTZ4WG9sSG1STjF0bkFLSWFzWDgiLCJjcnYiOiJFZDI1NTE5IiwieCI6ImJBdHk5dWwyU1ZvUlRQUm51aVlVdHVtN2x0WHE4c2VCWFJBQ3o4SjRoZGMifQ","from":"did:jwk:eyJrdHkiOiJPS1AiLCJhbGciOiJFZERTQSIsImtpZCI6InFCY1o3blNCZnIxdjRObnd1bjJGbEFuQm9RdjdQeDJPODE4NTNwMU9EdG8iLCJjcnYiOiJFZDI1NTE5IiwieCI6ImtGWnkzUE9hRmhCRjFTbERubUlYSjFCYUxtVGpOeXJQWDB0bXRXMVppNUEifQ","id":"order_01hy4b39mcewxvzckyf6x96ykp","exchangeId":"rfq_01hy4b39m8f21s1q8y1kwa7ec6","createdAt":"2024-05-17T22:34:54.988029Z","protocol":"1.0"},"data":{},"signature":"eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDpqd2s6ZXlKcmRIa2lPaUpQUzFBaUxDSmhiR2NpT2lKRlpFUlRRU0lzSW10cFpDSTZJbkZDWTFvM2JsTkNabkl4ZGpST2JuZDFiakpHYkVGdVFtOVJkamRRZURKUE9ERTROVE53TVU5RWRHOGlMQ0pqY25ZaU9pSkZaREkxTlRFNUlpd2llQ0k2SW10R1dua3pVRTloUm1oQ1JqRlRiRVJ1YlVsWVNqRkNZVXh0VkdwT2VYSlFXREIwYlhSWE1WcHBOVUVpZlEjMCJ9..nbp76ytzYAvvG9bizY5ez2TGv7SazA6vZFV_9vPGq1M-_vi2Bs7FP4DumWJOtgJBZ_vMJGxZWwW8oXVYN31ECA"}'''; - - final quoteJson = jsonDecode(quoteString); - final orderJson = jsonDecode(orderString); - - final quote = Quote.fromJson(quoteJson); - final order = Order.fromJson(orderJson); + await TestData.initializeDids(); - final did = await DidDht.create(); + final did = TestData.aliceDid; + final quote = TestData.getQuote(); + final order = TestData.getOrder(); late MockTbdexService mockTbdexService; @@ -78,10 +68,10 @@ void main() async { ); await tester.pumpAndSettle(); - expect(find.widgetWithText(AutoSizeText, '10'), findsOneWidget); - expect(find.text('USD'), findsOneWidget); - expect(find.widgetWithText(AutoSizeText, '500'), findsOneWidget); - expect(find.text('MXN'), findsOneWidget); + expect(find.widgetWithText(AutoSizeText, '100'), findsOneWidget); + expect(find.text('AUD'), findsOneWidget); + expect(find.widgetWithText(AutoSizeText, '0.12'), findsOneWidget); + expect(find.text('BTC'), findsOneWidget); }); testWidgets('should show fee details', (tester) async { @@ -91,7 +81,7 @@ void main() async { await tester.pumpAndSettle(); expect(find.byType(PaymentFeeDetails), findsOneWidget); - expect(find.text('0.5 MXN'), findsOneWidget); + expect(find.text('0.01 AUD'), findsOneWidget); }); testWidgets('should show bank name', (tester) async { @@ -110,7 +100,7 @@ void main() async { ); await tester.pumpAndSettle(); - await tester.tap(find.text('Pay 10 USD')); + await tester.tap(find.text('Pay 100 AUD')); await tester.pumpAndSettle(); expect(find.text('Order confirmed!'), findsOneWidget); diff --git a/test/features/payment/payment_types_page_test.dart b/test/features/payment/payment_types_page_test.dart index af6663f8..80f24b85 100644 --- a/test/features/payment/payment_types_page_test.dart +++ b/test/features/payment/payment_types_page_test.dart @@ -4,18 +4,18 @@ import 'package:flutter_test/flutter_test.dart'; import '../../helpers/widget_helpers.dart'; -final _paymentTypes = { - 'Bank', - 'Mobile money', - 'Wallet', -}; - void main() { + final paymentTypes = { + 'Bank', + 'Mobile money', + 'Wallet', + }; + group('PaymentTypesPage', () { Widget paymentTypesPageTestWidget() => WidgetHelpers.testableWidget( child: PaymentTypesPage( - selectedPaymentType: ValueNotifier(_paymentTypes.first), - paymentTypes: _paymentTypes, + selectedPaymentType: ValueNotifier(paymentTypes.first), + paymentTypes: paymentTypes, payinCurrency: '', ), ); diff --git a/test/features/payout/payout_test.dart b/test/features/payout/payout_test.dart index d5a94637..15a10816 100644 --- a/test/features/payout/payout_test.dart +++ b/test/features/payout/payout_test.dart @@ -7,40 +7,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tbdex/tbdex.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; -void main() { +void main() async { + await TestData.initializeDids(); group('Payout', () { final amount = ValueNotifier(Decimal.one); final pfi = ValueNotifier(null); - final offering = ValueNotifier( - Offering.create( - 'pfiDid', - OfferingData( - description: '', - payoutUnitsPerPayinUnit: '1', - payin: PayinDetails( - currencyCode: 'AUD', - min: '0.01', - max: '100.00', - methods: [ - PayinMethod( - kind: 'DEBIT_CARD', - ), - ], - ), - payout: PayoutDetails( - currencyCode: 'USDC', - methods: [ - PayoutMethod( - estimatedSettlementTime: 0, - kind: 'DEBIT_CARD', - ), - ], - ), - ), - ), - ); + final offering = ValueNotifier(TestData.getOffering()); final paymentState = PaymentState( transactionType: TransactionType.deposit, @@ -71,7 +46,7 @@ void main() { WidgetHelpers.testableWidget(child: payoutTestWidget()), ); - expect(find.textContaining('USDC'), findsOneWidget); + expect(find.textContaining('USD'), findsOneWidget); }); testWidgets('should show the `You get` label', (tester) async { diff --git a/test/features/pfis/pfis_notifier_test.dart b/test/features/pfis/pfis_notifier_test.dart index b034520d..6a405fa8 100644 --- a/test/features/pfis/pfis_notifier_test.dart +++ b/test/features/pfis/pfis_notifier_test.dart @@ -1,11 +1,17 @@ -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; + +void main() async { + await TestData.initializeDids(); + + final initialPfi = TestData.getPfi('did:dht:pfiDid'); + final newPfi = TestData.getPfi('did:dht:newDid'); + final initialPfis = [initialPfi]; -void main() { late MockBox mockBox; late MockPfisService mockPfisService; @@ -16,8 +22,6 @@ void main() { group('PfisNotifier', () { test('should create and load initial list', () async { - final initialPfis = [const Pfi(did: 'did:web:x%3A8892:ingress')]; - when(() => mockBox.get(PfisNotifier.storageKey)) .thenReturn(initialPfis.map((pfi) => pfi.toJson()).toList()); final notifier = await PfisNotifier.create(mockBox, mockPfisService); @@ -28,9 +32,6 @@ void main() { }); test('should add a new Pfi', () async { - final initialPfis = [const Pfi(did: 'did:web:x%3A8892:ingress')]; - const newPfi = Pfi(did: 'did:web:x%3A8892:egress'); - when(() => mockBox.get(PfisNotifier.storageKey)) .thenReturn(initialPfis.map((pfi) => pfi.toJson()).toList()); when( @@ -55,12 +56,10 @@ void main() { [...initialPfis.map((pfi) => pfi.toJson()), newPfi.toJson()], ), ).called(1); - verify(() => mockPfisService.createPfi('did:web:x%3A8892:egress')) - .called(1); + verify(() => mockPfisService.createPfi(newPfi.did)).called(1); }); test('should remove a Pfi', () async { - final initialPfis = [const Pfi(did: 'did:web:x%3A8892:ingress')]; final pfiToRemove = initialPfis.first; when(() => mockBox.get(PfisNotifier.storageKey)) diff --git a/test/features/send/send_details_page_test.dart b/test/features/send/send_details_page_test.dart index 1a3518dd..018f876a 100644 --- a/test/features/send/send_details_page_test.dart +++ b/test/features/send/send_details_page_test.dart @@ -2,13 +2,14 @@ import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/send/send_details_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:web5/web5.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - final did = await DidDht.create(); + await TestData.initializeDids(); + final did = TestData.aliceDid; group('SendDetailsPage', () { Widget sendDetailsPageTestWidget() => WidgetHelpers.testableWidget( child: const SendDetailsPage(sendAmount: '25'), diff --git a/test/features/send/send_page_test.dart b/test/features/send/send_page_test.dart index 426fc5e1..13121e8d 100644 --- a/test/features/send/send_page_test.dart +++ b/test/features/send/send_page_test.dart @@ -1,9 +1,7 @@ import 'package:auto_size_text/auto_size_text.dart'; -import 'package:didpay/features/account/account_balance.dart'; import 'package:didpay/features/account/account_balance_notifier.dart'; import 'package:didpay/features/did/did_provider.dart'; import 'package:didpay/features/feature_flags/feature_flags_notifier.dart'; -import 'package:didpay/features/pfis/pfi.dart'; import 'package:didpay/features/pfis/pfis_notifier.dart'; import 'package:didpay/features/send/send_details_page.dart'; import 'package:didpay/features/send/send_page.dart'; @@ -11,23 +9,23 @@ import 'package:didpay/shared/next_button.dart'; import 'package:didpay/shared/number/number_pad.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:web5/web5.dart'; import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; import '../../helpers/widget_helpers.dart'; void main() async { - final did = await DidDht.create(); - const pfi = Pfi(did: 'did:web:x%3A8892:ingress'); + await TestData.initializeDids(); - final accountBalance = - AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {}); + final did = TestData.aliceDid; + final pfis = TestData.getPfis(); + final accountBalance = TestData.getAccountBalance(); late MockPfisNotifier mockPfisNotifier; late MockFeatureFlagsNotifier mockFeatureFlagsNotifier; setUp(() { - mockPfisNotifier = MockPfisNotifier([pfi]); + mockPfisNotifier = MockPfisNotifier(pfis); mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]); }); diff --git a/test/helpers/test_data.dart b/test/helpers/test_data.dart new file mode 100644 index 00000000..dd1d316f --- /dev/null +++ b/test/helpers/test_data.dart @@ -0,0 +1,147 @@ +import 'dart:convert'; + +import 'package:didpay/features/account/account_balance.dart'; +import 'package:didpay/features/countries/countries.dart'; +import 'package:didpay/features/feature_flags/feature_flag.dart'; +import 'package:didpay/features/pfis/pfi.dart'; +import 'package:json_schema/json_schema.dart'; +import 'package:tbdex/src/protocol/models/message.dart'; +import 'package:tbdex/src/protocol/models/message_data.dart'; +import 'package:tbdex/src/protocol/models/offering.dart'; +import 'package:tbdex/src/protocol/models/order.dart'; +import 'package:tbdex/src/protocol/models/quote.dart'; +import 'package:tbdex/src/protocol/models/resource_data.dart'; +import 'package:typeid/typeid.dart'; +import 'package:web5/web5.dart'; + +class TestData { + static const String dap = 'username@didpay.me'; + + static final _aliceKeyManager = InMemoryKeyManager(); + static final _pfiKeyManager = InMemoryKeyManager(); + + static late final BearerDid aliceDid; + static late final BearerDid pfiDid; + + static Future initializeDids() async { + aliceDid = await DidDht.create(keyManager: _aliceKeyManager); + pfiDid = await DidDht.create(keyManager: _pfiKeyManager); + } + + static Pfi getPfi(String did) => Pfi(did: did); + + static Country getCountry(String name, String code) => + Country(name: name, code: code); + + static FeatureFlag getFeatureFlag(String name, String description) => + FeatureFlag(name: name, description: description); + + static Map> getOfferingsMap() => { + Pfi(did: pfiDid.uri): [getOffering()], + }; + + static List getPfis() => [Pfi(did: pfiDid.uri)]; + + static AccountBalance getAccountBalance() => + AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {}); + + static Offering getOffering({ + PresentationDefinition? requiredClaims, + }) => + Offering.create( + pfiDid.uri, + OfferingData( + description: 'A sample offering', + payoutUnitsPerPayinUnit: '10', + payin: PayinDetails( + currencyCode: 'AUD', + min: '0.01', + max: '100.00', + methods: [ + PayinMethod( + kind: 'DEBIT_CARD', + requiredPaymentDetails: paymentDetailsSchema(), + ), + ], + ), + payout: PayoutDetails( + currencyCode: 'USD', + methods: [ + PayoutMethod( + estimatedSettlementTime: 0, + kind: 'DEBIT_CARD', + requiredPaymentDetails: paymentDetailsSchema(), + ), + ], + ), + requiredClaims: requiredClaims, + ), + ); + + static Quote getQuote() => Quote.create( + aliceDid.uri, + pfiDid.uri, + TypeId.generate(MessageKind.rfq.name), + QuoteData( + expiresAt: '2022-01-01T00:00:00Z', + payin: QuoteDetails( + currencyCode: 'AUD', + amount: '100', + fee: '0.01', + paymentInstruction: PaymentInstruction( + link: 'https://block.xyz', + instruction: 'payin instruction', + ), + ), + payout: QuoteDetails( + currencyCode: 'BTC', + amount: '0.12', + fee: '0.02', + paymentInstruction: PaymentInstruction( + link: 'https://block.xyz', + instruction: 'payout instruction', + ), + ), + ), + ); + + static Order getOrder({String? to}) => Order.create( + to ?? pfiDid.uri, + aliceDid.uri, + TypeId.generate(MessageKind.rfq.name), + ); + + static JsonSchema paymentDetailsSchema() => JsonSchema.create( + jsonDecode(r''' + { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "cardNumber": { + "type": "string", + "description": "The 16-digit debit card number", + "minLength": 16, + "maxLength": 16 + }, + "expiryDate": { + "type": "string", + "description": "The expiry date of the card in MM/YY format", + "pattern": "^(0[1-9]|1[0-2])\\/([0-9]{2})$" + }, + "cardHolderName": { + "type": "string", + "description": "Name of the cardholder as it appears on the card" + }, + "cvv": { + "type": "string", + "description": "The 3-digit CVV code", + "minLength": 3, + "maxLength": 3 + } + }, + "required": ["cardNumber", "expiryDate", "cardHolderName", "cvv"], + "additionalProperties": false + } + '''), + ); +}