import Foundation import MusicAssistantCore import XCTest final class InstrumentSelectionPersistenceTests: XCTestCase { func testSelectedInstrumentsRoundTripThroughSongProjectSerialization() throws { let section = SongSection(id: "intro", type: .intro, title: "Intro") let project = SongProject( id: "serialized-instrument-selection", title: "Serialized Instrument Selection", idea: "Preserve selected instruments", sections: [section], instruments: [ InstrumentTrack( instrumentId: "oud", selected: true, variant: "Arabic Oud", playingStyle: "tremolo", role: "lead motif", autoArrangementEnabled: false, placements: [InstrumentPlacement(sectionId: section.id, direction: "Opening")] ), InstrumentTrack( instrumentId: "violin", selected: false, variant: "Violin" ) ] ) let encodedProject = try JSONEncoder().encode(project) let decodedProject = try JSONDecoder().decode(SongProject.self, from: encodedProject) XCTAssertEqual(decodedProject.instruments, project.instruments) XCTAssertEqual(decodedProject.selectedInstrumentIDs, ["oud"]) XCTAssertEqual(decodedProject.instrumentTrack(for: "oud")?.placements.first?.sectionId, section.id) } func testSelectedInstrumentsPersistWhenProjectIsSavedAndReopened() async throws { let directoryURL = FileManager.default.temporaryDirectory .appendingPathComponent("music-assistant-instrument-selection-tests") .appendingPathComponent(UUID().uuidString) defer { try? FileManager.default.removeItem(at: directoryURL) } let store = LocalSongProjectStore(directoryURL: directoryURL) var project = SongProject( id: "persisted-instrument-selection", title: "Persisted Instrument Selection", idea: "Keep catalog selections" ) project.setInstrumentSelected(id: "oud", isSelected: true, variant: "Oud") project.setInstrumentSelected(id: "violin", isSelected: true, variant: "Violin") project.setInstrumentSelected(id: "oud", isSelected: false) try await store.create(project) let reopenedProject = try await store.open(id: project.id) XCTAssertFalse(reopenedProject.isInstrumentSelected(id: "oud")) XCTAssertTrue(reopenedProject.isInstrumentSelected(id: "violin")) XCTAssertEqual(reopenedProject.selectedInstrumentIDs, ["violin"]) XCTAssertEqual(reopenedProject.instrumentTrack(for: "oud")?.variant, "Oud") } }