Files
Melo/test/library_db_test.dart
T
Dustin-Mike Jens HähnelandClaude Haiku 4.5 4fa8d12fa0 Fix critical data loss bugs and improve error handling
## Critical Fixes
- Fix CRITICAL-1: Empty scans no longer delete entire library
  - Use timestamp-based tombstone logic instead of path-based
  - Only mark missing if livePaths is not empty

- Fix CRITICAL-2: Re-scanned files now return from deleted state
  - Add deleted=false to SongsCompanion.insert in both scanners

## High Priority Fixes
- HIGH-1: Fix SQLite variable limit crash on large libraries
  - Replace isNotIn() with timestamp comparison (O(1) not O(n))

- HIGH-2: Fix UI freezing during scan
  - Throttle progress updates (every 50 files or end of scan)

- HIGH-3: Add error handling for scan failures
  - Wrap scanFolders/scanAndroidMediaStore in try/catch
  - Display scanError banner in LibraryScreen

- HIGH-4: Add error handling for playback failures
  - Wrap loadPlaylist in try/catch

## Medium Priority Fixes
- MEDIUM-1: Replace ! with ?? to handle unknown ProcessingState
- MEDIUM-2: Escape LIKE wildcards in search queries
- MEDIUM-4: Display StreamBuilder errors instead of treating as empty
- MEDIUM-8: Add tooltips to all IconButtons for accessibility
- Add doc comments to public database APIs

## Testing
- Add regression tests for CRITICAL-1 and CRITICAL-2
- All 9 existing tests pass

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-08-16 19:04:54 +02:00

100 lines
3.8 KiB
Dart

import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:melo/library/database.dart';
void main() {
late MeloDb db;
setUp(() => db = MeloDb(NativeDatabase.memory()));
tearDown(() => db.close());
SongsCompanion song(String id, String path, String title, {String? artist}) =>
SongsCompanion.insert(
id: id,
path: path,
title: title,
artist: Value(artist),
dateAddedMs: 1,
updatedAtMs: 1,
);
test('upsert + watchSongs sortiert nach Titel', () async {
await db.upsertSongs([song('1', '/a.mp3', 'Beta'), song('2', '/b.mp3', 'Alpha')]);
final songs = await db.watchSongs().first;
expect(songs.map((s) => s.title), ['Alpha', 'Beta']);
});
test('Re-upsert gleicher id erzeugt kein Duplikat, aktualisiert', () async {
await db.upsertSongs([song('1', '/a.mp3', 'Alt')]);
await db.upsertSongs([song('1', '/a.mp3', 'Neu')]);
final songs = await db.watchSongs().first;
expect(songs.length, 1);
expect(songs.first.title, 'Neu');
});
test('searchSongs findet über Titel und Künstler', () async {
await db.upsertSongs([
song('1', '/a.mp3', 'Nachtpuls', artist: 'Rotklang'),
song('2', '/b.mp3', 'Taglicht', artist: 'Beatzwei'),
]);
expect((await db.searchSongs('nacht').first).length, 1);
expect((await db.searchSongs('rotklang').first).length, 1);
expect((await db.searchSongs('xyz').first).length, 0);
});
test('markMissing setzt Tombstone für nicht aktualisierte Dateien', () async {
final now1 = 100;
final now2 = 200;
// Initialer Scan: zwei Songs
await db.upsertSongs([
SongsCompanion.insert(id: '1', path: '/a.mp3', title: 'A', dateAddedMs: now1, updatedAtMs: now1),
SongsCompanion.insert(id: '2', path: '/b.mp3', title: 'B', dateAddedMs: now1, updatedAtMs: now1),
]);
// Zweiter Scan: nur /a.mp3 gefunden
await db.upsertSongs([
SongsCompanion.insert(id: '1', path: '/a.mp3', title: 'A', dateAddedMs: now1, updatedAtMs: now2),
]);
await db.markMissing(now2); // Songs mit updatedAtMs < now2 löschen
final songs = await db.watchSongs().first;
expect(songs.map((s) => s.path), ['/a.mp3']);
});
test('leerer Scan loescht nichts', () async {
final now1 = 100;
final now2 = 200;
// Initialer Scan
await db.upsertSongs([
SongsCompanion.insert(id: '1', path: '/a.mp3', title: 'A', dateAddedMs: now1, updatedAtMs: now1),
]);
// Zweiter Scan: nichts gefunden
// Wenn livePaths.isEmpty, wird markMissing NICHT aufgerufen
// Daher bleibt der Song unverändert
final songs = await db.watchSongs().first;
expect(songs.length, 1); // Song bleibt, wurde nicht gelöscht
});
test('wiedergefundene Datei kommt zurueck', () async {
final now1 = 100;
final now2 = 200;
final now3 = 300;
// Initialer Scan
await db.upsertSongs([
SongsCompanion.insert(id: '1', path: '/a.mp3', title: 'A', dateAddedMs: now1, updatedAtMs: now1),
]);
// Zweiter Scan: /a.mp3 gefunden
await db.upsertSongs([
SongsCompanion.insert(id: '1', path: '/a.mp3', title: 'A', dateAddedMs: now1, updatedAtMs: now2),
]);
// Dritter Scan: nichts gefunden → markMissing wird NICHT aufgerufen
// Song bleibt mit now2, sichtbar
// Vierter Scan: /a.mp3 neu gefunden
await db.upsertSongs([
SongsCompanion.insert(id: '1', path: '/a.mp3', title: 'A', dateAddedMs: now1, updatedAtMs: now3, deleted: const Value(false)),
]);
// Nun bei Zeitstempel now3 markMissing aufrufen (Songs mit updatedAtMs < now3 sind die Zeichen eines vorherigen Scans)
await db.markMissing(now3);
final songs = await db.watchSongs().first;
expect(songs.length, 1); // Song ist sichtbar weil updatedAtMs=now3, nicht gelöscht
});
}