Files

290 lines
8.5 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:peertube/features/account/account.dart';
import 'package:peertube/features/channel/channel.dart';
import 'package:peertube/features/video/video.dart';
import 'package:peertube/shared/shared.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
ProviderContainer createProviderContainer({
ProviderContainer? parent,
List<Override> overrides = const [],
List<ProviderObserver>? observers,
}) {
// Create a ProviderContainer, and optionally allow specifying parameters.
final container = ProviderContainer(
parent: parent,
overrides: overrides,
observers: observers,
);
// When the test ends, dispose the container.
addTearDown(container.dispose);
return container;
}
class DioLoggingInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
log.debug('--> ${options.method} ${options.path}');
log.debug('Headers: ${options.headers}');
log.debug('Data: ${options.data}');
super.onRequest(options, handler);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
log.debug(
'<-- ${response.statusCode} ${response.requestOptions.method} ${response.requestOptions.path}',
);
log.debug('Headers: ${response.headers}');
log.debug('Data: ${response.data}');
super.onResponse(response, handler);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
log.debug('<-- Error -->');
log.debug(
'Request: ${err.requestOptions.method} ${err.requestOptions.path}',
);
log.debug('Error: ${err.message}');
super.onError(err, handler);
}
}
// Mocked objets
class MockedObjects {
static final DateTime mockDate = DateTime(2024, 1, 1);
static ConnectedAccountSQL generateMockConnectedAccountSQL() {
return ConnectedAccountSQL(
id: 1,
username: 'mock_user',
host: 'mock.host.com',
displayName: 'Mock User',
avatarUrl: 'https://example.com/avatar_mock.jpg',
isFavorite: true,
lastAccess: mockDate,
);
}
static PlatformAPI generateMockInstanceAPI() {
return PlatformAPI(
name: 'Mock Instance',
shortDescription: 'This is a mock instance',
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
terms: 'Mock terms',
codeOfConduct: 'Mock code of conduct',
hardwareInformation: 'Mock hardware information',
creationReason: 'Mock creation reason',
moderationInformation: 'Mock moderation information',
administrator: 'Mock administrator',
maintenanceLifetime: 'Mock maintenance lifetime',
businessModel: 'Mock business model',
languages: ['English', 'French'],
categories: [1, 2, 3],
banners: [
ActorImage(
fileUrl: 'https://example.com/banner1.jpg',
width: 100,
createdAt: mockDate,
updatedAt: mockDate,
),
ActorImage(
fileUrl: 'https://example.com/banner2.jpg',
width: 120,
createdAt: mockDate,
updatedAt: mockDate,
),
],
avatars: [
ActorImage(
fileUrl: 'https://example.com/avatar1.jpg',
width: 50,
createdAt: mockDate,
updatedAt: mockDate,
),
ActorImage(
fileUrl: 'https://example.com/avatar2.jpg',
width: 60,
createdAt: mockDate,
updatedAt: mockDate,
),
],
);
}
static CurrentPlatformModel generateMockCurrentInstance() {
final instanceAPI = MockedObjects.generateMockInstanceAPI();
return CurrentPlatformModel.fromAPI(
host: 'https://instance.mock',
platformAPI: instanceAPI,
);
}
static Account generateMockAccount() {
return Account(
id: 1,
name: 'John Doe',
url: 'example.com',
host: '',
displayName: '',
avatars: [],
);
}
static AccountDetails generateMockAccountDetails() {
return AccountDetails(
id: 1,
url: 'example.com',
name: 'John Doe',
host: 'example.com',
hostRedundancyAllowed: true,
followingCount: 100,
followersCount: 200,
createdAt: DateTime.now(),
displayName: 'John Doe',
updatedAt: DateTime.now(),
avatars: const [],
description: 'User description',
userId: 123,
);
}
static VideoChannel generateMockVideoChannel() {
return VideoChannel(
id: 2,
name: 'Channel Name',
url: 'example.com',
host: '',
displayName: '',
avatars: [],
);
}
static VideoChannelDetails generateMockVideoChannelDetails() {
final ownerAccountDetails = MockedObjects.generateMockAccount();
return VideoChannelDetails(
id: 2,
url: 'example.com',
name: 'Channel Name',
host: 'example.com',
hostRedundancyAllowed: true,
followingCount: 150,
followersCount: 300,
createdAt: DateTime.now().subtract(const Duration(days: 500)),
displayName: 'Channel Display Name',
updatedAt: DateTime.now(),
avatars: const [],
banners: const [],
description: 'Channel description',
support: 'Support details',
isLocal: true,
ownerAccount: ownerAccountDetails,
);
}
static Video generateMockVideo() {
final account = MockedObjects.generateMockAccount();
final videoChannel = MockedObjects.generateMockVideoChannel();
return Video(
id: 1,
uuid: 'test_uuid',
shortUUID: 'short_test_uuid',
name: 'Test VideoDetails',
category: VideoConstant<int>(id: 1, label: 'label'),
licence: VideoConstant<int>(id: 1, label: 'label'),
language: VideoConstant<String>(id: 'en', label: 'label'),
privacy: VideoConstant<VideoPrivacy>(
id: VideoPrivacy.public,
label: 'label',
),
nsfw: false,
truncatedDescription: 'Short description',
description: 'Full description of the video',
isLocal: true,
duration: 100,
aspectRatio: 16 / 9,
views: 1000,
viewers: 10,
likes: 500,
dislikes: 100,
thumbnailPath: 'thumbnail_path',
previewPath: 'preview_path',
embedPath: 'embed_path',
createdAt: DateTime.now().subtract(const Duration(days: 1)),
updatedAt: DateTime.now(),
publishedAt: DateTime.now(),
originallyPublishedAt: DateTime.now().subtract(const Duration(days: 2)),
isLive: false,
account: account,
channel: videoChannel,
url: buildUri('url.com'),
);
}
static VideoDetails generateMockVideoDetails() {
final accountDetails = MockedObjects.generateMockAccountDetails();
final videoChannelDetails = MockedObjects.generateMockVideoChannelDetails();
return VideoDetails(
id: 1,
uuid: 'test_uuid',
shortUUID: 'short_test_uuid',
name: 'Test VideoDetails',
category: VideoConstant<int>(id: 1, label: 'label'),
licence: VideoConstant<int>(id: 1, label: 'label'),
language: VideoConstant<String>(id: 'en', label: 'label'),
privacy: VideoConstant<VideoPrivacy>(
id: VideoPrivacy.public,
label: 'label',
),
nsfw: false,
truncatedDescription: 'Short description',
description: 'Full description of the video',
isLocal: true,
duration: 100,
aspectRatio: 16 / 9,
views: 1000,
viewers: 10,
likes: 500,
dislikes: 100,
thumbnailPath: 'thumbnail_path',
previewPath: 'preview_path',
embedPath: 'embed_path',
createdAt: DateTime.now().subtract(const Duration(days: 1)),
updatedAt: DateTime.now(),
publishedAt: DateTime.now(),
originallyPublishedAt: DateTime.now().subtract(const Duration(days: 2)),
isLive: false,
account: accountDetails,
channel: videoChannelDetails,
blacklisted: false,
blacklistedReason: 'Not applicable',
files: [],
streamingPlaylists: [],
support: 'Support details',
tags: ['tag1', 'tag2'],
commentsEnabled: true,
downloadEnabled: true,
waitTranscoding: false,
inputFileUpdatedAt: DateTime.now().subtract(const Duration(days: 3)),
state: VideoConstant<VideoState>(
id: VideoState.published,
label: 'label',
),
trackerUrls: [Uri.parse('https://tracker.example.com')],
userHistory: VideoUserHistory(currentTime: 50),
url: buildUri('url.com'),
);
}
}