Files
bvandeusen 4eb9935f8e feat(flutter/cache): cacheFirst helper + drift↔model adapters (#357 plan C)
Foundation for the provider migrations. cacheFirst<D, T> wraps the
drift.watch() + REST cold-cache fallback pattern: yields cached rows
when present, kicks off REST fetch + drift populate when empty +
online, yields empty when offline. REST failures swallow to empty so
callers can surface errors via toast.

adapters.dart adds CachedX → XRef extension methods + reverse
XRef.toDrift() companions for Artist/Album/Track/Playlist. Adapters
accept some loss of server-derived fields (coverUrl, streamUrl,
ownerUsername) — UI already handles empty values; cold-cache fallback
briefly shows the real values before drift takes over.

cache_first_test covers all 4 branches (non-empty, empty+online,
empty+offline, REST failure). adapters_test covers basic round-trips.
Both safe to run on CI runner — no drift open required.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 11:18:29 -04:00

71 lines
2.2 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:minstrel/cache/adapters.dart';
import 'package:minstrel/models/album.dart';
import 'package:minstrel/models/artist.dart';
import 'package:minstrel/models/playlist.dart';
import 'package:minstrel/models/track.dart';
void main() {
test('ArtistRef.toDrift preserves id + name + sortName', () {
const ref = ArtistRef(id: 'a1', name: 'Boards of Canada', sortName: 'Boards of Canada');
final companion = ref.toDrift();
expect(companion.id.value, 'a1');
expect(companion.name.value, 'Boards of Canada');
expect(companion.sortName.value, 'Boards of Canada');
});
test('ArtistRef.toDrift falls back to name when sortName is empty', () {
const ref = ArtistRef(id: 'a1', name: 'The Album Leaf');
final companion = ref.toDrift();
expect(companion.sortName.value, 'The Album Leaf');
});
test('AlbumRef.toDrift preserves id + title + artistId', () {
const ref = AlbumRef(id: 'al1', title: 'Geogaddi', artistId: 'ar1');
final companion = ref.toDrift();
expect(companion.id.value, 'al1');
expect(companion.title.value, 'Geogaddi');
expect(companion.artistId.value, 'ar1');
});
test('TrackRef.toDrift converts seconds to ms + preserves track/disc', () {
const ref = TrackRef(
id: 't1',
title: 'Roygbiv',
albumId: 'al1',
artistId: 'ar1',
durationSec: 137,
trackNumber: 4,
discNumber: 1,
);
final companion = ref.toDrift();
expect(companion.id.value, 't1');
expect(companion.durationMs.value, 137 * 1000);
expect(companion.trackNumber.value, 4);
expect(companion.discNumber.value, 1);
});
test('Playlist.toDrift preserves id + userId + name', () {
const p = Playlist(
id: 'p1',
userId: 'u1',
name: 'My Mix',
description: 'a great mix',
isPublic: true,
systemVariant: null,
trackCount: 12,
coverUrl: '',
ownerUsername: 'alice',
createdAt: '',
updatedAt: '',
);
final companion = p.toDrift();
expect(companion.id.value, 'p1');
expect(companion.userId.value, 'u1');
expect(companion.name.value, 'My Mix');
expect(companion.isPublic.value, true);
expect(companion.trackCount.value, 12);
});
}