Skip to content

Commit

Permalink
TF-3226 Parsing minInputLength from Autocomplete capability in session
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 authored and hoangdat committed Nov 20, 2024
1 parent e002be6 commit b344062
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 2 deletions.
31 changes: 31 additions & 0 deletions contact/lib/contact/model/autocomplete_capability.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:jmap_dart_client/http/converter/unsigned_int_nullable_converter.dart';
import 'package:jmap_dart_client/jmap/core/capability/capability_properties.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:json_annotation/json_annotation.dart';

part 'autocomplete_capability.g.dart';

@JsonSerializable(
explicitToJson: true,
includeIfNull: false,
converters: [
UnsignedIntNullableConverter()
]
)
class AutocompleteCapability extends CapabilityProperties {
final UnsignedInt? minInputLength;

AutocompleteCapability({this.minInputLength});

factory AutocompleteCapability.fromJson(Map<String, dynamic> json)
=> _$AutocompleteCapabilityFromJson(json);

Map<String, dynamic> toJson() => _$AutocompleteCapabilityToJson(this);

static AutocompleteCapability deserialize(Map<String, dynamic> json) {
return AutocompleteCapability.fromJson(json);
}

@override
List<Object?> get props => [minInputLength];
}
3 changes: 2 additions & 1 deletion contact/lib/contact_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export 'data/datasource_impl/tmail_contact_datasource_impl.dart';
export 'contact/autocomplete/autocomplete_tmail_contact_method.dart';
export 'contact/autocomplete/autocomplete_tmail_contact_response.dart';

export 'contact/model/capability_contact.dart';
export 'contact/model/capability_contact.dart';
export 'contact/model/autocomplete_capability.dart';
9 changes: 9 additions & 0 deletions lib/features/base/base_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'package:tmail_ui_user/features/base/mixin/message_dialog_action_mixin.da
import 'package:tmail_ui_user/features/base/mixin/popup_context_menu_action_mixin.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
import 'package:tmail_ui_user/features/email/presentation/bindings/mdn_interactor_bindings.dart';
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
import 'package:tmail_ui_user/features/login/domain/usecases/delete_authority_oidc_interactor.dart';
import 'package:tmail_ui_user/features/login/domain/usecases/delete_credential_interactor.dart';
Expand Down Expand Up @@ -516,4 +517,12 @@ abstract class BaseController extends GetxController
authorizationInterceptors.clear();
await cachingManager.closeHive();
}

int getMinInputLengthAutocomplete({
required Session session,
required AccountId accountId,
}) {
final minInputLength = session.getMinInputLengthAutocomplete(accountId);
return minInputLength?.value.toInt() ?? AppConfig.defaultMinInputLengthAutocomplete;
}
}
18 changes: 18 additions & 0 deletions lib/features/home/domain/extensions/session_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

import 'dart:convert';

import 'package:contact/contact/model/autocomplete_capability.dart';
import 'package:contact/contact/model/capability_contact.dart';
import 'package:core/presentation/extensions/uri_extension.dart';
import 'package:core/utils/app_logger.dart';
import 'package:get/get.dart';
import 'package:jmap_dart_client/http/converter/state_converter.dart';
import 'package:jmap_dart_client/http/converter/user_name_converter.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/home/data/model/session_hive_obj.dart';
import 'package:tmail_ui_user/features/home/domain/converter/session_account_converter.dart';
Expand Down Expand Up @@ -61,4 +65,18 @@ extension SessionExtensions on Session {
return '';
}
}

UnsignedInt? getMinInputLengthAutocomplete(AccountId accountId) {
try {
final autocompleteCapability = getCapabilityProperties<AutocompleteCapability>(
accountId,
tmailContactCapabilityIdentifier);
final minInputLength = autocompleteCapability?.minInputLength;
log('SessionExtensions::getMinInputLengthAutocomplete:minInputLength = $minInputLength');
return minInputLength;
} catch (e) {
logError('SessionExtensions::getMinInputLengthAutocomplete():[Exception] $e');
return null;
}
}
}
2 changes: 1 addition & 1 deletion lib/main/utils/app_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:tmail_ui_user/features/login/data/network/config/oidc_constant.d
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';

class AppConfig {
static const int limitCharToStartSearch = 3;
static const int defaultMinInputLengthAutocomplete = 3;
static const int warningAttachmentFileSizeInMegabytes = 10;

static const String appDashboardConfigurationPath = "configurations/app_dashboard.json";
Expand Down
127 changes: 127 additions & 0 deletions test/features/base/base_controller_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:contact/contact/model/autocomplete_capability.dart';
import 'package:contact/contact/model/capability_contact.dart';
import 'package:core/data/network/config/dynamic_url_interceptors.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/state/failure.dart';
Expand All @@ -6,6 +8,12 @@ import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:core/utils/application_manager.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:jmap_dart_client/jmap/core/account/account.dart';
import 'package:jmap_dart_client/jmap/core/capability/empty_capability.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:mockito/annotations.dart';
import 'package:tmail_ui_user/features/base/base_controller.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
Expand All @@ -16,9 +24,11 @@ import 'package:tmail_ui_user/features/manage_account/data/local/language_cache_
import 'package:tmail_ui_user/features/manage_account/domain/usecases/log_out_oidc_interactor.dart';
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception.dart';
import 'package:tmail_ui_user/main/utils/app_config.dart';
import 'package:tmail_ui_user/main/utils/toast_manager.dart';
import 'package:uuid/uuid.dart';

import '../../fixtures/account_fixtures.dart';
import 'base_controller_test.mocks.dart';

class MockBaseController extends BaseController {
Expand Down Expand Up @@ -201,4 +211,121 @@ void main() {
expect(mockBaseController.isUrgentExceptionEnable, false);
});
});

group('BaseController::getMinInputLengthAutocomplete::test', () {
late MockBaseController mockBaseController;

setUp(() {
mockBaseController = MockBaseController();
});

test('SHOULD return session min input length WHEN AutocompleteCapability available', () {
// Arrange
const expectedMinInputLength = 5;
final session = Session(
{
tmailContactCapabilityIdentifier: AutocompleteCapability(
minInputLength: UnsignedInt(expectedMinInputLength)
)
},
{
AccountFixtures.aliceAccountId: Account(
AccountName('Alice'),
true,
false,
{
tmailContactCapabilityIdentifier: AutocompleteCapability(
minInputLength: UnsignedInt(expectedMinInputLength)
)
},
)
},
{},
UserName(''),
Uri(),
Uri(),
Uri(),
Uri(),
State(''));

// Act
final result = mockBaseController.getMinInputLengthAutocomplete(
session: session,
accountId: AccountFixtures.aliceAccountId,
);

// Assert
expect(result, expectedMinInputLength);
});

test('SHOULD return session min input length WHEN AutocompleteCapability available, but no minInputLength', () {
// Arrange
const expectedMinInputLength = AppConfig.defaultMinInputLengthAutocomplete;
final session = Session(
{
tmailContactCapabilityIdentifier: AutocompleteCapability()
},
{
AccountFixtures.aliceAccountId: Account(
AccountName('Alice'),
true,
false,
{
tmailContactCapabilityIdentifier: AutocompleteCapability()
},
)
},
{},
UserName(''),
Uri(),
Uri(),
Uri(),
Uri(),
State(''),
);

// Act
final result = mockBaseController.getMinInputLengthAutocomplete(
session: session,
accountId: AccountFixtures.aliceAccountId,
);

// Assert
expect(result, expectedMinInputLength);
});

test('SHOULD return default min input length WHEN AutocompleteCapability is not available', () {
// Arrange
final session = Session(
{
tmailContactCapabilityIdentifier: EmptyCapability()
},
{
AccountFixtures.aliceAccountId: Account(
AccountName('Alice'),
true,
false,
{
tmailContactCapabilityIdentifier: EmptyCapability()
},
)
},
{},
UserName(''),
Uri(),
Uri(),
Uri(),
Uri(),
State(''));

// Act
final result = mockBaseController.getMinInputLengthAutocomplete(
session: session,
accountId: AccountFixtures.aliceAccountId,
);

// Assert
expect(result, AppConfig.defaultMinInputLengthAutocomplete);
});
});
}
105 changes: 105 additions & 0 deletions test/features/session/session_extensions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'package:contact/contact_module.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:jmap_dart_client/jmap/core/account/account.dart';
import 'package:jmap_dart_client/jmap/core/capability/empty_capability.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';

import '../../fixtures/account_fixtures.dart';

void main() {
group('getMinInputLengthAutocomplete::test', () {
test('SHOULD return minInputLength WHEN AutocompleteCapability is available', () {
// Arrange
final autocompleteCapability = AutocompleteCapability(minInputLength: UnsignedInt(3));
final session = Session(
{
tmailContactCapabilityIdentifier: autocompleteCapability
},
{
AccountFixtures.aliceAccountId: Account(
AccountName('Alice'),
true,
false,
{
tmailContactCapabilityIdentifier: autocompleteCapability
},
)
},
{},
UserName(''),
Uri(),
Uri(),
Uri(),
Uri(),
State(''));

// Act
final result = session.getMinInputLengthAutocomplete(AccountFixtures.aliceAccountId);

// Assert
expect(result, autocompleteCapability.minInputLength);
});

test('SHOULD return null WHEN AutocompleteCapability is not available', () {
// Arrange
final session = Session(
{
tmailContactCapabilityIdentifier: EmptyCapability()
},
{
AccountFixtures.aliceAccountId: Account(
AccountName('Alice'),
true,
false,
{
tmailContactCapabilityIdentifier: EmptyCapability()
},
)
},
{},
UserName(''),
Uri(),
Uri(),
Uri(),
Uri(),
State(''));

// Act
final result = session.getMinInputLengthAutocomplete(AccountFixtures.aliceAccountId);

// Assert
expect(result, isNull);
});

test('SHOULD return null WHEN autocomplete capability is not supported', () {
// Arrange
final session = Session(
{},
{
AccountFixtures.aliceAccountId: Account(
AccountName('Alice'),
true,
false,
{},
)
},
{},
UserName(''),
Uri(),
Uri(),
Uri(),
Uri(),
State(''));

// Act
final result = session.getMinInputLengthAutocomplete(AccountFixtures.aliceAccountId);

// Assert
expect(result, isNull);
});
});
}

0 comments on commit b344062

Please sign in to comment.