import Testing import Foundation @testable import Music @MainActor struct PlaylistViewModelTests { // Verifies createPlaylistAndAddTrack does the full job in one call: // 1. Seed a track into an in-memory DB and build a PlaylistViewModel over it. // 2. Call createPlaylistAndAddTrack with a name and the seeded track. // 3. The returned playlist has the given name and a real (non-nil) id. // 4. The DB shows that playlist now contains exactly the seeded track. // 5. The new playlist is recorded as the last-used playlist. @Test func createPlaylistAndAddTrackCreatesPlaylistAndAddsTrack() throws { // 1. Seed a track and build the view model. let db = try DatabaseService(inMemory: true) var track = Track.fixture(fileURL: "/song.mp3", title: "Song A") try db.insert(&track) let vm = PlaylistViewModel(db: db) // 2. Create a new playlist and add the track in one step. let created = try vm.createPlaylistAndAddTrack(name: "Road Trip", track: track) // 3. The returned playlist is well-formed (a real id was assigned, name matches). let createdId = try #require(created.id) #expect(created.name == "Road Trip") // 4. The playlist contains exactly the seeded track. let tracks = try db.fetchPlaylistTracks(playlistId: createdId) #expect(tracks.count == 1) #expect(tracks[0].id == track.id) // 5. The new playlist became the last-used playlist. #expect(vm.lastUsedPlaylistId == createdId) } }