Herz-Tipp meldet den Favoriten sofort deterministisch an die Cloud
This commit is contained in:
@@ -1,7 +1,32 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:melo/library/database.dart';
|
||||
import 'package:melo/library/playlist_service.dart';
|
||||
import 'package:melo/services/baka_auth.dart';
|
||||
import 'package:melo/services/melo_cloud_service.dart';
|
||||
|
||||
class _MemorySpeicher implements TokenSpeicher {
|
||||
_MemorySpeicher(this.werte);
|
||||
final Map<String, String> werte;
|
||||
@override
|
||||
Future<String?> lesen(String key) async => werte[key];
|
||||
@override
|
||||
Future<void> schreiben(String key, String wert) async => werte[key] = wert;
|
||||
@override
|
||||
Future<void> loeschen(String key) async => werte.remove(key);
|
||||
}
|
||||
|
||||
Future<BakaAuth> _angemeldeteAuth() async {
|
||||
final auth = BakaAuth(
|
||||
speicher: _MemorySpeicher({'baka_token': 'tok', 'baka_user': 'Baka'}),
|
||||
);
|
||||
await auth.laden();
|
||||
return auth;
|
||||
}
|
||||
|
||||
void main() {
|
||||
late MeloDb db;
|
||||
@@ -61,4 +86,110 @@ void main() {
|
||||
expect(songs.map((s) => s.id).toList(), ['song-3', 'song-1', 'song-2']);
|
||||
expect(notified, true);
|
||||
});
|
||||
|
||||
group('Sofort-Push der Favoriten', () {
|
||||
Future<void> legeSongAn(MeloDb db, {String? cloudId}) async {
|
||||
await db.into(db.songs).insert(SongsCompanion.insert(
|
||||
id: 'song-1',
|
||||
path: '/a.mp3',
|
||||
title: 'A',
|
||||
dateAddedMs: 0,
|
||||
updatedAtMs: 0,
|
||||
));
|
||||
if (cloudId != null) await db.setCloudId('song-1', cloudId);
|
||||
}
|
||||
|
||||
test('setzt deterministisch true beim Favorisieren', () async {
|
||||
final db2 = MeloDb(NativeDatabase.memory());
|
||||
addTearDown(db2.close);
|
||||
await legeSongAn(db2, cloudId: 'c1');
|
||||
|
||||
final gesendet = <Map<String, dynamic>>[];
|
||||
final dienst = PlaylistService(
|
||||
db2,
|
||||
cloud: MeloCloudService(
|
||||
auth: await _angemeldeteAuth(),
|
||||
client: MockClient((anfrage) async {
|
||||
gesendet.add(jsonDecode(anfrage.body) as Map<String, dynamic>);
|
||||
return http.Response(jsonEncode({'status': 'ok'}), 200);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await dienst.toggleFavorite('song-1');
|
||||
|
||||
expect(gesendet, [
|
||||
{'song_id': 'c1', 'set': true}
|
||||
]);
|
||||
});
|
||||
|
||||
test('setzt deterministisch false beim Ent-Favorisieren', () async {
|
||||
final db2 = MeloDb(NativeDatabase.memory());
|
||||
addTearDown(db2.close);
|
||||
await legeSongAn(db2, cloudId: 'c1');
|
||||
await db2.setFavorite('song-1', true);
|
||||
|
||||
final gesendet = <Map<String, dynamic>>[];
|
||||
final dienst = PlaylistService(
|
||||
db2,
|
||||
cloud: MeloCloudService(
|
||||
auth: await _angemeldeteAuth(),
|
||||
client: MockClient((anfrage) async {
|
||||
gesendet.add(jsonDecode(anfrage.body) as Map<String, dynamic>);
|
||||
return http.Response(jsonEncode({'status': 'ok'}), 200);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await dienst.toggleFavorite('song-1');
|
||||
|
||||
expect(gesendet, [
|
||||
{'song_id': 'c1', 'set': false}
|
||||
]);
|
||||
});
|
||||
|
||||
test('ohne cloudId wird nichts gemeldet', () async {
|
||||
final db2 = MeloDb(NativeDatabase.memory());
|
||||
addTearDown(db2.close);
|
||||
await legeSongAn(db2);
|
||||
|
||||
var anfragen = 0;
|
||||
final dienst = PlaylistService(
|
||||
db2,
|
||||
cloud: MeloCloudService(
|
||||
auth: await _angemeldeteAuth(),
|
||||
client: MockClient((_) async {
|
||||
anfragen++;
|
||||
return http.Response(jsonEncode({'status': 'ok'}), 200);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
await dienst.toggleFavorite('song-1');
|
||||
|
||||
expect(anfragen, 0);
|
||||
expect(await db2.watchIsFavorite('song-1').first, isTrue);
|
||||
});
|
||||
|
||||
test('ein Fehler des Servers ändert lokal nichts und wirft nicht',
|
||||
() async {
|
||||
final db2 = MeloDb(NativeDatabase.memory());
|
||||
addTearDown(db2.close);
|
||||
await legeSongAn(db2, cloudId: 'c1');
|
||||
|
||||
final dienst = PlaylistService(
|
||||
db2,
|
||||
cloud: MeloCloudService(
|
||||
auth: await _angemeldeteAuth(),
|
||||
client: MockClient(
|
||||
(_) async => http.Response(jsonEncode({'error': 'weg'}), 500)),
|
||||
),
|
||||
);
|
||||
|
||||
// Der nächste Voll-Abgleich holt den Push additiv nach.
|
||||
await dienst.toggleFavorite('song-1');
|
||||
|
||||
expect(await db2.watchIsFavorite('song-1').first, isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user