mirror of
https://github.com/linagora/tmail-flutter.git
synced 2026-12-06 08:06:47 +00:00
1 parent
6fe712b259
commit
49bb0e80e7
13 files changed
+584
-1
No files matched your search
@@ -7,11 +7,20 @@ abstract final class WorkplaceIntentDataBuilder {
|
||||
static WorkplaceAction actionOf(WorkplaceIntentConfig config) =>
|
||||
switch (config) {
|
||||
WorkplacePickIntentConfig() => WorkplaceAction.pick,
|
||||
WorkplaceUploadIntentConfig() => WorkplaceAction.upload,
|
||||
};
|
||||
|
||||
static Map<String, dynamic> dataOf(WorkplaceIntentConfig config) =>
|
||||
switch (config) {
|
||||
WorkplacePickIntentConfig() => _pickData(config),
|
||||
WorkplaceUploadIntentConfig() => _uploadData(config),
|
||||
};
|
||||
|
||||
static Map<String, dynamic> _uploadData(WorkplaceUploadIntentConfig config) => {
|
||||
'url': config.request.url.toString(),
|
||||
'folderId': WorkplaceMagicFolder.mail.value,
|
||||
'name': config.request.name,
|
||||
'reference': config.request.reference.toJson(),
|
||||
};
|
||||
|
||||
static Map<String, dynamic> _pickData(WorkplacePickIntentConfig config) =>
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// The `io.cozy.mails.attachments` reference Drive keeps on a saved file, so
|
||||
/// the same blob is recognised as saved on any device.
|
||||
class DriveAttachmentReference with EquatableMixin {
|
||||
static const type = 'io.cozy.mails.attachments';
|
||||
static const _maxIdLength = 256;
|
||||
|
||||
final String accountId;
|
||||
final String emailId;
|
||||
final String blobId;
|
||||
|
||||
const DriveAttachmentReference({
|
||||
required this.accountId,
|
||||
required this.emailId,
|
||||
required this.blobId,
|
||||
});
|
||||
|
||||
String get id => '$accountId:$emailId:$blobId';
|
||||
|
||||
/// Parts are joined by `:` and the id rides in a URL path: a part with
|
||||
/// either character, or an empty one, would make the reference ambiguous.
|
||||
bool get isValid =>
|
||||
[accountId, emailId, blobId].every(_isValidPart) &&
|
||||
id.length <= _maxIdLength;
|
||||
|
||||
Map<String, dynamic> toJson() => {'type': type, 'id': id};
|
||||
|
||||
static bool _isValidPart(String part) =>
|
||||
part.isNotEmpty && !part.contains(':') && !part.contains('/');
|
||||
|
||||
@override
|
||||
List<Object?> get props => [accountId, emailId, blobId];
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// A file as Drive reports it after an UPLOAD or a MOVE.
|
||||
class DriveStoredFile with EquatableMixin {
|
||||
final String fileId;
|
||||
final String? name;
|
||||
final String? dirId;
|
||||
final String? path;
|
||||
final int? size;
|
||||
|
||||
const DriveStoredFile({
|
||||
required this.fileId,
|
||||
this.name,
|
||||
this.dirId,
|
||||
this.path,
|
||||
this.size,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [fileId, name, dirId, path, size];
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'drive_stored_file.dart';
|
||||
|
||||
/// Why the save is known to have failed.
|
||||
enum DriveNotSavedReason {
|
||||
uploadError,
|
||||
cancelled,
|
||||
quotaExceeded,
|
||||
forbidden,
|
||||
unsupported,
|
||||
unreachable,
|
||||
stopped,
|
||||
sizeMismatch,
|
||||
}
|
||||
|
||||
/// The save may or may not have happened; a later LOOKUP can tell.
|
||||
enum DriveUnconfirmedReason {
|
||||
unverifiable,
|
||||
timeout,
|
||||
rendererGone,
|
||||
malformed,
|
||||
stopped,
|
||||
}
|
||||
|
||||
sealed class DriveUploadOutcome {
|
||||
const DriveUploadOutcome();
|
||||
}
|
||||
|
||||
/// Drive confirmed a file with the attachment reference.
|
||||
final class DriveUploadSaved extends DriveUploadOutcome {
|
||||
final DriveStoredFile file;
|
||||
|
||||
const DriveUploadSaved(this.file);
|
||||
}
|
||||
|
||||
/// The size confirms the file even though Drive returned no matching reference.
|
||||
final class DriveUploadSavedUnreferenced extends DriveUploadOutcome {
|
||||
final DriveStoredFile file;
|
||||
|
||||
const DriveUploadSavedUnreferenced(this.file);
|
||||
}
|
||||
|
||||
final class DriveUploadUnconfirmed extends DriveUploadOutcome {
|
||||
final DriveUnconfirmedReason reason;
|
||||
final DriveStoredFile? file;
|
||||
|
||||
/// When posting began, for timing the recovery LOOKUP.
|
||||
final DateTime? dispatchedAt;
|
||||
|
||||
const DriveUploadUnconfirmed(this.reason, {this.file, this.dispatchedAt});
|
||||
}
|
||||
|
||||
final class DriveUploadNotSaved extends DriveUploadOutcome {
|
||||
final DriveNotSavedReason reason;
|
||||
final Object? error;
|
||||
|
||||
const DriveUploadNotSaved(this.reason, {this.error});
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'drive_attachment_reference.dart';
|
||||
|
||||
/// One attachment Drive is asked to fetch from [url] and store.
|
||||
class DriveUploadRequest {
|
||||
/// Short-lived, secret download URL: never logged.
|
||||
final Uri url;
|
||||
final String name;
|
||||
final DriveAttachmentReference reference;
|
||||
|
||||
/// `Attachment.size`; lets a result with no reference still be checked.
|
||||
final int? expectedSize;
|
||||
|
||||
const DriveUploadRequest({
|
||||
required this.url,
|
||||
required this.name,
|
||||
required this.reference,
|
||||
this.expectedSize,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'DriveUploadRequest(name: $name, reference: ${reference.id})';
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
import 'drive_upload_request.dart';
|
||||
import 'workplace_action_config.dart';
|
||||
import 'workplace_theme.dart';
|
||||
|
||||
@@ -26,3 +27,13 @@ final class WorkplacePickIntentConfig extends WorkplaceIntentConfig {
|
||||
@override
|
||||
List<Object?> get props => [addAsLink, addAsAttachment, theme];
|
||||
}
|
||||
|
||||
/// UPLOAD: Drive fetches [request] and stores it in the Mail folder.
|
||||
final class WorkplaceUploadIntentConfig extends WorkplaceIntentConfig {
|
||||
final DriveUploadRequest request;
|
||||
|
||||
const WorkplaceUploadIntentConfig(this.request);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [request];
|
||||
}
|
||||
@@ -2,7 +2,17 @@ class WorkplaceCreateIntentException implements Exception {}
|
||||
|
||||
class WorkplaceExchangeTokenException implements Exception {}
|
||||
|
||||
class DriveIntentErrorException implements Exception {}
|
||||
/// Drive answered an intent with `error`; [status] is its HTTP-like code
|
||||
/// (413 = quota) when it sent one.
|
||||
class DriveIntentErrorException implements Exception {
|
||||
final String? message;
|
||||
final int? status;
|
||||
|
||||
DriveIntentErrorException({this.message, this.status});
|
||||
|
||||
@override
|
||||
String toString() => 'DriveIntentErrorException: status=$status';
|
||||
}
|
||||
|
||||
/// `POST /intents` named no Drive service: this stack cannot serve the intent.
|
||||
class WorkplaceIntentUnsupportedException implements Exception {}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import '../../data/model/workplace_intent_data_builder.dart';
|
||||
import '../../data/transport/workplace_access_session.dart';
|
||||
import '../../domain/entity/drive_upload_outcome.dart';
|
||||
import '../../domain/entity/drive_upload_request.dart';
|
||||
import '../../domain/entity/workplace_intent.dart';
|
||||
import '../../domain/entity/workplace_intent_config.dart';
|
||||
import '../../domain/exceptions/workplace_exceptions.dart';
|
||||
import 'drive_intent_cancel_signal.dart';
|
||||
import 'drive_intent_creator.dart';
|
||||
import 'drive_intent_host.dart';
|
||||
import 'drive_intent_protocol.dart';
|
||||
import 'drive_intent_result.dart';
|
||||
import 'drive_upload_classifier.dart';
|
||||
|
||||
typedef DriveIntentHostFactory = DriveIntentHost Function();
|
||||
|
||||
/// Runs UPLOAD against Drive in a hidden page, one new host per
|
||||
/// intent so a dead renderer never poisons the next call.
|
||||
class DriveIntentClient {
|
||||
DriveIntentClient({
|
||||
required this._creator,
|
||||
required this._protocol,
|
||||
required this._hostFactory,
|
||||
});
|
||||
|
||||
static const uploadResultTimeout = Duration(seconds: 60);
|
||||
|
||||
final DriveIntentCreator _creator;
|
||||
final DriveIntentProtocol _protocol;
|
||||
final DriveIntentHostFactory _hostFactory;
|
||||
|
||||
Future<DriveUploadOutcome> upload(
|
||||
WorkplaceAccessSession session,
|
||||
Uri platformUrl,
|
||||
DriveUploadRequest request, {
|
||||
DriveIntentCancelSignal? cancel,
|
||||
}) async {
|
||||
if (cancel?.isCancelled ?? false) {
|
||||
return const DriveUploadNotSaved(DriveNotSavedReason.stopped);
|
||||
}
|
||||
final config = WorkplaceUploadIntentConfig(request);
|
||||
final WorkplaceIntent intent;
|
||||
try {
|
||||
intent = await _creator.create(session, platformUrl, config);
|
||||
} catch (error) {
|
||||
return DriveUploadNotSaved(_creationReason(error), error: error);
|
||||
}
|
||||
final result = await _protocol.run(DriveIntentRunRequest(
|
||||
intent: intent,
|
||||
data: WorkplaceIntentDataBuilder.dataOf(config),
|
||||
host: _hostFactory(),
|
||||
resultTimeout: uploadResultTimeout,
|
||||
cancel: cancel ?? DriveIntentCancelSignal(),
|
||||
));
|
||||
return _uploadOutcome(result, request);
|
||||
}
|
||||
|
||||
DriveNotSavedReason _creationReason(Object error) => switch (error) {
|
||||
WorkplaceIntentUnsupportedException() => DriveNotSavedReason.unsupported,
|
||||
WorkplaceIntentForbiddenException() => DriveNotSavedReason.forbidden,
|
||||
_ => DriveNotSavedReason.unreachable,
|
||||
};
|
||||
|
||||
DriveUploadOutcome _uploadOutcome(
|
||||
DriveIntentResult result,
|
||||
DriveUploadRequest request,
|
||||
) => switch (result) {
|
||||
DriveIntentDone(:final document, :final sentAt) => classifyUploadDone(document, request, sentAt),
|
||||
DriveIntentErrored(:final message, :final status) => DriveUploadNotSaved(
|
||||
status == 413 ? DriveNotSavedReason.quotaExceeded : DriveNotSavedReason.uploadError,
|
||||
error: DriveIntentErrorException(message: message, status: status),
|
||||
),
|
||||
DriveIntentCancelledByDrive() => const DriveUploadNotSaved(DriveNotSavedReason.cancelled),
|
||||
DriveIntentPageFailed(:final error) => DriveUploadNotSaved(DriveNotSavedReason.unreachable, error: error),
|
||||
DriveIntentUnsent(cut: DriveIntentCut.stopped) => const DriveUploadNotSaved(DriveNotSavedReason.stopped),
|
||||
DriveIntentUnsent() => const DriveUploadNotSaved(DriveNotSavedReason.unreachable),
|
||||
DriveIntentUnanswered(:final cut, :final sentAt) =>
|
||||
DriveUploadUnconfirmed(_unconfirmedReason(cut), dispatchedAt: sentAt),
|
||||
};
|
||||
|
||||
DriveUnconfirmedReason _unconfirmedReason(DriveIntentCut cut) => switch (cut) {
|
||||
DriveIntentCut.timeout => DriveUnconfirmedReason.timeout,
|
||||
DriveIntentCut.rendererGone => DriveUnconfirmedReason.rendererGone,
|
||||
DriveIntentCut.stopped => DriveUnconfirmedReason.stopped,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import '../../domain/entity/drive_stored_file.dart';
|
||||
|
||||
/// Reads a Drive file document, flat or JSON:API (`attributes`); null when it
|
||||
/// carries no usable id.
|
||||
DriveStoredFile? parseStoredFile(Object? document) {
|
||||
if (document is! Map) return null;
|
||||
final attributes = document['attributes'];
|
||||
final fields = attributes is Map ? attributes : document;
|
||||
final id = _text(document['_id']) ?? _text(document['id']);
|
||||
if (id == null) return null;
|
||||
return DriveStoredFile(
|
||||
fileId: id,
|
||||
name: _text(fields['name']),
|
||||
dirId: _text(fields['dir_id']),
|
||||
path: _text(fields['path']),
|
||||
size: _size(fields['size']),
|
||||
);
|
||||
}
|
||||
|
||||
String? _text(Object? value) => value is String && value.isNotEmpty ? value : null;
|
||||
|
||||
int? _size(Object? value) => switch (value) {
|
||||
int() => value,
|
||||
String() => int.tryParse(value),
|
||||
_ => null,
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
import '../../domain/entity/drive_attachment_reference.dart';
|
||||
import '../../domain/entity/drive_upload_outcome.dart';
|
||||
import '../../domain/entity/drive_upload_request.dart';
|
||||
import 'drive_stored_file_parser.dart';
|
||||
|
||||
/// Turns the raw `done` document of an UPLOAD into what Mail can claim:
|
||||
/// saved only when the reference or the size proves it is the right file.
|
||||
DriveUploadOutcome classifyUploadDone(
|
||||
Object? document,
|
||||
DriveUploadRequest request,
|
||||
DateTime? dispatchedAt,
|
||||
) {
|
||||
final file = parseStoredFile(document);
|
||||
if (file == null) return _malformed(dispatchedAt);
|
||||
final expected = request.expectedSize;
|
||||
if (expected != null && expected != file.size) {
|
||||
return const DriveUploadNotSaved(DriveNotSavedReason.sizeMismatch);
|
||||
}
|
||||
if (_isReferenced(document, request.reference.id)) {
|
||||
return DriveUploadSaved(file);
|
||||
}
|
||||
if (expected != null) return DriveUploadSavedUnreferenced(file);
|
||||
return DriveUploadUnconfirmed(
|
||||
DriveUnconfirmedReason.unverifiable,
|
||||
file: file,
|
||||
dispatchedAt: dispatchedAt,
|
||||
);
|
||||
}
|
||||
|
||||
DriveUploadUnconfirmed _malformed(DateTime? dispatchedAt) => DriveUploadUnconfirmed(
|
||||
DriveUnconfirmedReason.malformed,
|
||||
dispatchedAt: dispatchedAt,
|
||||
);
|
||||
|
||||
bool _isReferenced(Object? document, String referenceId) {
|
||||
final relationships = document is Map ? document['relationships'] : null;
|
||||
final referencedBy = relationships is Map ? relationships['referenced_by'] : null;
|
||||
final data = referencedBy is Map ? referencedBy['data'] : null;
|
||||
return data is List &&
|
||||
data.any(
|
||||
(entry) =>
|
||||
entry is Map &&
|
||||
entry['type'] == DriveAttachmentReference.type &&
|
||||
entry['id'] == referenceId,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:workplace/domain/entity/drive_upload_outcome.dart';
|
||||
import 'package:workplace/domain/exceptions/workplace_exceptions.dart';
|
||||
import 'package:workplace/presentation/intent_client/drive_intent_cancel_signal.dart';
|
||||
import 'package:workplace/presentation/intent_client/drive_intent_client.dart';
|
||||
import 'package:workplace/presentation/intent_client/drive_intent_creator.dart';
|
||||
import 'package:workplace/presentation/intent_client/drive_intent_protocol.dart';
|
||||
import 'package:workplace/presentation/intent_client/drive_intent_result.dart';
|
||||
import 'package:workplace/presentation/router/drive_intent_message_router.dart';
|
||||
|
||||
import 'intent_client_fakes.dart';
|
||||
|
||||
typedef _Answer = String Function(String id, Map<String, dynamic> data);
|
||||
|
||||
void main() {
|
||||
late DriveIntentMessageRouter router;
|
||||
late FakeRepository repository;
|
||||
late List<FakeHost> hosts;
|
||||
late _Answer answer;
|
||||
late DriveIntentClient client;
|
||||
final sentAt = DateTime.utc(2026);
|
||||
final url = Uri.parse(driveOrigin);
|
||||
|
||||
setUp(() {
|
||||
router = fakeRouter();
|
||||
repository = FakeRepository();
|
||||
hosts = [];
|
||||
answer = (id, _) => message('done', id: id, extra: {'document': {'_id': 'f', 'size': 10}});
|
||||
client = DriveIntentClient(
|
||||
creator: DriveIntentCreator(repository),
|
||||
protocol: DriveIntentProtocol(router: router, now: () => sentAt),
|
||||
hostFactory: () {
|
||||
final id = 'i${repository.counter}';
|
||||
final host = FakeHost()
|
||||
..onOpen = (() => scheduleMicrotask(() => send(router, message('ready', id: id))))
|
||||
..onPost = (data) => send(router, answer(id, data));
|
||||
hosts.add(host);
|
||||
return host;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('upload', () {
|
||||
test('posts the UPLOAD data and returns the classified file', () async {
|
||||
final outcome = await client.upload(fakeSession(), url, fakeUploadRequest());
|
||||
|
||||
expect((outcome as DriveUploadSavedUnreferenced).file.fileId, 'f');
|
||||
expect(hosts.single.posted.single, containsPair('name', 'a.pdf'));
|
||||
expect(hosts.single.posted.single['reference'], {
|
||||
'type': 'io.cozy.mails.attachments',
|
||||
'id': fakeReference().id,
|
||||
});
|
||||
});
|
||||
|
||||
test('uses the post timestamp when Drive returns a malformed document', () async {
|
||||
answer = (id, _) => message('done', id: id);
|
||||
|
||||
final outcome = await client.upload(fakeSession(), url, fakeUploadRequest()) as DriveUploadUnconfirmed;
|
||||
|
||||
expect(outcome.reason, DriveUnconfirmedReason.malformed);
|
||||
expect(outcome.dispatchedAt, sentAt);
|
||||
});
|
||||
|
||||
test('maps creation failures to not saved', () async {
|
||||
final cases = {
|
||||
WorkplaceIntentUnsupportedException(): DriveNotSavedReason.unsupported,
|
||||
WorkplaceIntentForbiddenException(): DriveNotSavedReason.forbidden,
|
||||
StateError('x'): DriveNotSavedReason.unreachable,
|
||||
};
|
||||
for (final entry in cases.entries) {
|
||||
repository.createErrors.add(entry.key);
|
||||
final outcome = await client.upload(fakeSession(), url, fakeUploadRequest());
|
||||
expect((outcome as DriveUploadNotSaved).reason, entry.value);
|
||||
}
|
||||
expect(hosts, isEmpty);
|
||||
});
|
||||
|
||||
test('an already cancelled signal creates no intent', () async {
|
||||
final cancel = DriveIntentCancelSignal()..cancel();
|
||||
|
||||
final outcome = await client.upload(fakeSession(), url, fakeUploadRequest(), cancel: cancel);
|
||||
|
||||
expect((outcome as DriveUploadNotSaved).reason, DriveNotSavedReason.stopped);
|
||||
expect(repository.configs, isEmpty);
|
||||
});
|
||||
|
||||
test('maps Drive errors: 413 is quota, others upload errors', () async {
|
||||
for (final (status, reason) in [
|
||||
(413, DriveNotSavedReason.quotaExceeded),
|
||||
(500, DriveNotSavedReason.uploadError),
|
||||
]) {
|
||||
answer = (id, _) => message('error', id: id, extra: {'error': {'status': status}});
|
||||
final outcome = await client.upload(fakeSession(), url, fakeUploadRequest());
|
||||
expect((outcome as DriveUploadNotSaved).reason, reason);
|
||||
}
|
||||
});
|
||||
|
||||
test('a cancel from Drive is cancelled', () async {
|
||||
answer = (id, _) => message('cancel', id: id);
|
||||
|
||||
final outcome = await client.upload(fakeSession(), url, fakeUploadRequest());
|
||||
|
||||
expect((outcome as DriveUploadNotSaved).reason, DriveNotSavedReason.cancelled);
|
||||
});
|
||||
|
||||
final pageError = StateError('page failed');
|
||||
final unreachable = isA<DriveUploadNotSaved>()
|
||||
.having((value) => value.reason, 'reason', DriveNotSavedReason.unreachable);
|
||||
final stopped = isA<DriveUploadNotSaved>()
|
||||
.having((value) => value.reason, 'reason', DriveNotSavedReason.stopped);
|
||||
final unconfirmed = isA<DriveUploadUnconfirmed>()
|
||||
.having((value) => value.dispatchedAt, 'dispatchedAt', sentAt);
|
||||
for (final (description, result, expected) in [
|
||||
('timeout before posting is unreachable', const DriveIntentUnsent(DriveIntentCut.timeout), unreachable),
|
||||
('renderer loss before posting is unreachable', const DriveIntentUnsent(DriveIntentCut.rendererGone), unreachable),
|
||||
(
|
||||
'page failure is unreachable and preserves the error',
|
||||
DriveIntentPageFailed(pageError),
|
||||
unreachable.having((value) => value.error, 'error', same(pageError)),
|
||||
),
|
||||
('stop before posting is stopped', const DriveIntentUnsent(DriveIntentCut.stopped), stopped),
|
||||
(
|
||||
'timeout after posting is unconfirmed',
|
||||
DriveIntentUnanswered(DriveIntentCut.timeout, sentAt),
|
||||
unconfirmed.having((value) => value.reason, 'reason', DriveUnconfirmedReason.timeout),
|
||||
),
|
||||
(
|
||||
'stop after posting is unconfirmed',
|
||||
DriveIntentUnanswered(DriveIntentCut.stopped, sentAt),
|
||||
unconfirmed.having((value) => value.reason, 'reason', DriveUnconfirmedReason.stopped),
|
||||
),
|
||||
]) {
|
||||
test(description, () async {
|
||||
final client = DriveIntentClient(
|
||||
creator: DriveIntentCreator(repository),
|
||||
protocol: _FixedResultProtocol(router: router, result: result),
|
||||
hostFactory: FakeHost.new,
|
||||
);
|
||||
expect(await client.upload(fakeSession(), url, fakeUploadRequest()), expected);
|
||||
});
|
||||
}
|
||||
|
||||
test('a lost renderer after dispatch is unconfirmed with the dispatch time, on a new host next time', () async {
|
||||
answer = (id, _) {
|
||||
Timer.run(hosts.last.onRendererGone!);
|
||||
return '{}';
|
||||
};
|
||||
final lost = await client.upload(fakeSession(), url, fakeUploadRequest());
|
||||
answer = (id, _) => message('done', id: id, extra: {'document': {'_id': 'f', 'size': 10}});
|
||||
final retry = await client.upload(fakeSession(), url, fakeUploadRequest());
|
||||
|
||||
final unconfirmed = lost as DriveUploadUnconfirmed;
|
||||
expect(unconfirmed.reason, DriveUnconfirmedReason.rendererGone);
|
||||
expect(unconfirmed.dispatchedAt, sentAt);
|
||||
expect(retry, isA<DriveUploadSavedUnreferenced>());
|
||||
expect(hosts, hasLength(2));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class _FixedResultProtocol extends DriveIntentProtocol {
|
||||
final DriveIntentResult result;
|
||||
|
||||
_FixedResultProtocol({required super.router, required this.result});
|
||||
|
||||
@override
|
||||
Future<DriveIntentResult> run(DriveIntentRunRequest request) async => result;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:workplace/domain/entity/drive_upload_outcome.dart';
|
||||
import 'package:workplace/presentation/intent_client/drive_upload_classifier.dart';
|
||||
|
||||
import 'intent_client_fakes.dart';
|
||||
|
||||
void main() {
|
||||
final at = DateTime(2026);
|
||||
final refId = fakeReference().id;
|
||||
Map<String, dynamic> referencedBy(String id) => {
|
||||
'referenced_by': {
|
||||
'data': [
|
||||
{'type': 'io.cozy.mails.attachments', 'id': id},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
DriveUploadOutcome classify(Object? document, {int? expectedSize = 10}) =>
|
||||
classifyUploadDone(document, fakeUploadRequest(expectedSize: expectedSize), at);
|
||||
|
||||
test('a non-map or id-less document is malformed', () {
|
||||
for (final document in [null, 'x', <String, dynamic>{}, {'_id': ''}]) {
|
||||
final outcome = classify(document) as DriveUploadUnconfirmed;
|
||||
expect(outcome.reason, DriveUnconfirmedReason.malformed);
|
||||
expect(outcome.dispatchedAt, at);
|
||||
}
|
||||
});
|
||||
|
||||
test('reads the flat shape and the attributes shape', () {
|
||||
final flat = classify({'_id': 'f', 'name': 'a.pdf', 'size': 10, 'dir_id': 'd', 'path': '/p'});
|
||||
final nested = classify({
|
||||
'id': 'f',
|
||||
'attributes': {'name': 'a.pdf', 'size': '10'},
|
||||
'relationships': referencedBy(refId),
|
||||
});
|
||||
|
||||
expect((flat as DriveUploadSavedUnreferenced).file.dirId, 'd');
|
||||
expect((nested as DriveUploadSaved).file.size, 10);
|
||||
});
|
||||
|
||||
test('a size different from the expected one, or none, is not saved', () {
|
||||
for (final size in [11, null]) {
|
||||
final outcome = classify({'_id': 'f', 'size': size, 'relationships': referencedBy(refId)})
|
||||
as DriveUploadNotSaved;
|
||||
expect(outcome.reason, DriveNotSavedReason.sizeMismatch);
|
||||
}
|
||||
});
|
||||
|
||||
test('another reference does not count as referenced', () {
|
||||
final outcome = classify({'_id': 'f', 'size': 10, 'relationships': referencedBy('other')});
|
||||
|
||||
expect(outcome, isA<DriveUploadSavedUnreferenced>());
|
||||
});
|
||||
|
||||
test('a reference entry of another type does not count', () {
|
||||
final outcome = classify({
|
||||
'_id': 'f',
|
||||
'size': 10,
|
||||
'relationships': {
|
||||
'referenced_by': {
|
||||
'data': [
|
||||
{'type': 'io.cozy.other', 'id': refId},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(outcome, isA<DriveUploadSavedUnreferenced>());
|
||||
});
|
||||
|
||||
test('without an expected size only the reference proves the save', () {
|
||||
final referenced = classify({'_id': 'f', 'relationships': referencedBy(refId)}, expectedSize: null);
|
||||
final bare = classify({'_id': 'f', 'size': 10}, expectedSize: null);
|
||||
|
||||
expect(referenced, isA<DriveUploadSaved>());
|
||||
expect((bare as DriveUploadUnconfirmed).reason, DriveUnconfirmedReason.unverifiable);
|
||||
});
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:workplace/data/transport/workplace_access_session.dart';
|
||||
import 'package:workplace/domain/entity/drive_attachment_reference.dart';
|
||||
import 'package:workplace/domain/entity/drive_upload_request.dart';
|
||||
import 'package:workplace/domain/entity/workplace_access_mode.dart';
|
||||
import 'package:workplace/domain/entity/workplace_intent.dart';
|
||||
import 'package:workplace/domain/entity/workplace_intent_config.dart';
|
||||
@@ -20,6 +22,17 @@ WorkplaceIntent fakeIntent([String id = 'i1']) => WorkplaceIntent(
|
||||
client: 'https://mail.example.com',
|
||||
);
|
||||
|
||||
DriveAttachmentReference fakeReference([String blob = 'b1']) =>
|
||||
DriveAttachmentReference(accountId: 'a', emailId: 'e', blobId: blob);
|
||||
|
||||
DriveUploadRequest fakeUploadRequest({int? expectedSize = 10, String blob = 'b1'}) =>
|
||||
DriveUploadRequest(
|
||||
url: Uri.parse('https://jmap.example.com/dl?token=secret'),
|
||||
name: 'a.pdf',
|
||||
reference: fakeReference(blob),
|
||||
expectedSize: expectedSize,
|
||||
);
|
||||
|
||||
/// Router with no window listener: tests feed it with [send].
|
||||
DriveIntentMessageRouter fakeRouter() =>
|
||||
DriveIntentMessageRouter(windowSource: _NoWindowSource());
|
||||
|
||||
Reference in new issue
Block a user