feat: Auto sync label from web socket
This commit is contained in:
@@ -7,6 +7,7 @@ import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/batch_set_email_processing_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/handle_error_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/mail_api_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/session_mixin.dart';
|
||||
|
||||
import '../../../fixtures/account_fixtures.dart';
|
||||
import '../../../fixtures/session_fixtures.dart';
|
||||
@@ -55,7 +56,10 @@ class ManualMockHttpClient implements HttpClient {
|
||||
}
|
||||
|
||||
class TestBatchSetEmailProcessing
|
||||
with HandleSetErrorMixin, MailAPIMixin, BatchSetEmailProcessingMixin {}
|
||||
with HandleSetErrorMixin,
|
||||
SessionMixin,
|
||||
MailAPIMixin,
|
||||
BatchSetEmailProcessingMixin {}
|
||||
|
||||
void main() {
|
||||
group('BatchSetEmailProcessingMixin test', () {
|
||||
|
||||
@@ -12,12 +12,13 @@ import 'package:jmap_dart_client/jmap/mail/email/email_filter_condition.dart';
|
||||
import 'package:model/error_type_handler/set_method_error_handler_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/handle_error_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/mail_api_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/base/mixin/session_mixin.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
||||
|
||||
import '../../../fixtures/account_fixtures.dart';
|
||||
import '../../../fixtures/session_fixtures.dart';
|
||||
|
||||
class TestMailApiMixin with HandleSetErrorMixin, MailAPIMixin {
|
||||
class TestMailApiMixin with HandleSetErrorMixin, SessionMixin, MailAPIMixin {
|
||||
@override
|
||||
void handleSetErrors({
|
||||
SetMethodErrors? notDestroyedError,
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:labels/model/label.dart';
|
||||
import 'package:tmail_ui_user/features/labels/presentation/utils/label_utils.dart';
|
||||
|
||||
void main() {
|
||||
group('LabelUtils.applyLabelChanges', () {
|
||||
test('should add new labels when created list is not empty', () {
|
||||
final currentLabels = <Label>[];
|
||||
final newLabel = Label(id: Id('1'), displayName: 'Work');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [newLabel],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.id?.value, '1');
|
||||
});
|
||||
|
||||
test(
|
||||
'should update existing label when an updated label with the same ID is provided',
|
||||
() {
|
||||
final oldLabel = Label(id: Id('1'), displayName: 'Old Name');
|
||||
final currentLabels = <Label>[oldLabel];
|
||||
|
||||
final updatedLabel = Label(id: Id('1'), displayName: 'New Name');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabel],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'New Name');
|
||||
});
|
||||
|
||||
test('should remove labels when their ID is present in destroyedIds', () {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'To Delete'),
|
||||
Label(id: Id('2'), displayName: 'Keep Me'),
|
||||
];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [Id('1')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.any((l) => l.id?.value == '1'), isFalse);
|
||||
expect(currentLabels.any((l) => l.id?.value == '2'), isTrue);
|
||||
});
|
||||
|
||||
test('should handle create, update, and destroy operations simultaneously',
|
||||
() {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Old'),
|
||||
Label(id: Id('2'), displayName: 'Remove'),
|
||||
];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [Label(id: Id('3'), displayName: 'New')],
|
||||
updated: [Label(id: Id('1'), displayName: 'Updated')],
|
||||
destroyedIds: [Id('2')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 2);
|
||||
expect(currentLabels.any((l) => l.displayName == 'Updated'), isTrue);
|
||||
expect(currentLabels.any((l) => l.displayName == 'New'), isTrue);
|
||||
expect(currentLabels.any((l) => l.id?.value == '2'), isFalse);
|
||||
});
|
||||
|
||||
test('should do nothing when all change lists are empty', () {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Existing'),
|
||||
];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Existing');
|
||||
});
|
||||
|
||||
test(
|
||||
'should handle destroying IDs that do not exist in currentLabels gracefully',
|
||||
() {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Keep Me'),
|
||||
];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [Id('999')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.id?.value, '1');
|
||||
});
|
||||
|
||||
test(
|
||||
'should treat updated labels as new additions if they do not exist in currentLabels (Upsert behavior)',
|
||||
() {
|
||||
final currentLabels = <Label>[];
|
||||
final labelToUpdate = Label(id: Id('1'), displayName: 'Ghost Label');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [labelToUpdate],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Ghost Label');
|
||||
});
|
||||
|
||||
test('should move updated labels to the end of the list', () {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'A'),
|
||||
Label(id: Id('2'), displayName: 'B'),
|
||||
Label(id: Id('3'), displayName: 'C'),
|
||||
];
|
||||
|
||||
final updatedLabelA = Label(id: Id('1'), displayName: 'A Updated');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabelA],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 3);
|
||||
expect(currentLabels[0].id?.value, '2');
|
||||
expect(currentLabels[1].id?.value, '3');
|
||||
expect(currentLabels[2].id?.value, '1');
|
||||
expect(currentLabels[2].displayName, 'A Updated');
|
||||
});
|
||||
|
||||
test('should prioritize update over destroy if an ID appears in both lists',
|
||||
() {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Original'),
|
||||
];
|
||||
|
||||
final updatedLabel = Label(id: Id('1'), displayName: 'Revived');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabel],
|
||||
destroyedIds: [Id('1')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Revived');
|
||||
});
|
||||
});
|
||||
}
|
||||
+92
-6
@@ -11,6 +11,7 @@ import 'package:mockito/mockito.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/action/push_notification_state_change_action.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/push_base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/listener/email_change_listener.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/listener/label_change_listener.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/listener/mailbox_change_listener.dart';
|
||||
|
||||
import 'push_base_controller_test.mocks.dart';
|
||||
@@ -26,6 +27,7 @@ class TestPushController extends PushBaseController {
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<EmailChangeListener>(),
|
||||
MockSpec<MailboxChangeListener>(),
|
||||
MockSpec<LabelChangeListener>(),
|
||||
])
|
||||
void main() {
|
||||
final accountId = AccountId(Id('accountId'));
|
||||
@@ -35,6 +37,7 @@ void main() {
|
||||
group('mappingTypeStateToAction:', () {
|
||||
final emailChangeListener = MockEmailChangeListener();
|
||||
final mailboxChangeListener = MockMailboxChangeListener();
|
||||
final labelChangeListener = MockLabelChangeListener();
|
||||
|
||||
test(
|
||||
'should call emailChangeListener.dispatchActions with SynchronizeEmailOnForegroundAction '
|
||||
@@ -53,7 +56,8 @@ void main() {
|
||||
userName,
|
||||
isForeground: true,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
@@ -68,6 +72,7 @@ void main() {
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(mailboxChangeListener.dispatchActions(any));
|
||||
verifyNever(labelChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
@@ -87,7 +92,8 @@ void main() {
|
||||
userName,
|
||||
isForeground: false,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
@@ -103,6 +109,7 @@ void main() {
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(mailboxChangeListener.dispatchActions(any));
|
||||
verifyNever(labelChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
@@ -122,12 +129,14 @@ void main() {
|
||||
userName,
|
||||
isForeground: true,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
verifyNever(emailChangeListener.dispatchActions(any));
|
||||
verifyNever(mailboxChangeListener.dispatchActions(any));
|
||||
verifyNever(labelChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
@@ -147,7 +156,8 @@ void main() {
|
||||
userName,
|
||||
isForeground: false,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
@@ -163,6 +173,7 @@ void main() {
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(mailboxChangeListener.dispatchActions(any));
|
||||
verifyNever(labelChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
@@ -182,7 +193,8 @@ void main() {
|
||||
userName,
|
||||
isForeground: true,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
@@ -196,6 +208,7 @@ void main() {
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(emailChangeListener.dispatchActions(any));
|
||||
verifyNever(labelChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
@@ -215,7 +228,8 @@ void main() {
|
||||
userName,
|
||||
isForeground: false,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
@@ -230,6 +244,78 @@ void main() {
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(emailChangeListener.dispatchActions(any));
|
||||
verifyNever(labelChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
'should call labelChangeListener.dispatchActions with SynchronizeLabelOnForegroundAction '
|
||||
'when mapTypeState contains labelType '
|
||||
'and isForeground is true',
|
||||
() {
|
||||
// arrange
|
||||
final state = State('some-state');
|
||||
final mapTypeState = {TypeName.labelType.value: state.value};
|
||||
|
||||
// act
|
||||
final pushBaseController = TestPushController();
|
||||
pushBaseController.mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
accountId,
|
||||
userName,
|
||||
isForeground: true,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
verify(
|
||||
labelChangeListener.dispatchActions([
|
||||
SynchronizeLabelOnForegroundAction(
|
||||
TypeName.labelType,
|
||||
state,
|
||||
accountId,
|
||||
),
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(emailChangeListener.dispatchActions(any));
|
||||
verifyNever(mailboxChangeListener.dispatchActions(any));
|
||||
});
|
||||
|
||||
test(
|
||||
'should call labelChangeListener.dispatchActions with StoreLabelStateToRefreshAction '
|
||||
'when mapTypeState contains labelType '
|
||||
'and isForeground is false',
|
||||
() {
|
||||
// arrange
|
||||
final state = State('some-state');
|
||||
final mapTypeState = {TypeName.labelType.value: state.value};
|
||||
|
||||
// act
|
||||
final pushBaseController = TestPushController();
|
||||
pushBaseController.mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
accountId,
|
||||
userName,
|
||||
isForeground: false,
|
||||
emailChangeListener: emailChangeListener,
|
||||
mailboxChangeListener: mailboxChangeListener,
|
||||
labelChangeListener: labelChangeListener,
|
||||
);
|
||||
|
||||
// assert
|
||||
verify(
|
||||
labelChangeListener.dispatchActions([
|
||||
StoreLabelStateToRefreshAction(
|
||||
TypeName.labelType,
|
||||
state,
|
||||
accountId,
|
||||
userName,
|
||||
),
|
||||
]),
|
||||
).called(1);
|
||||
verifyNever(emailChangeListener.dispatchActions(any));
|
||||
verifyNever(mailboxChangeListener.dispatchActions(any));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user