harden: precondition + multi-track test for EditableTrackFields.shared

feat/music-streaming
Laurent 1 month ago
parent 31ee9a15df
commit baed9e782a
  1. 2
      Music/Models/EditableTrackFields.swift
  2. 16
      MusicTests/EditableTrackFieldsTests.swift

@ -49,7 +49,9 @@ nonisolated struct EditableTrackFields: Equatable, Sendable {
// Returns prefill values (from the first track) plus the set of fields whose // Returns prefill values (from the first track) plus the set of fields whose
// values are NOT identical across all tracks (shown as "Mixed" in the UI). // values are NOT identical across all tracks (shown as "Mixed" in the UI).
// Precondition: caller must pass at least one track; an empty array will trap.
static func shared(across tracks: [Track]) -> (values: EditableTrackFields, mixed: Set<EditableTrackField>) { static func shared(across tracks: [Track]) -> (values: EditableTrackFields, mixed: Set<EditableTrackField>) {
precondition(!tracks.isEmpty, "shared(across:) requires at least one track")
let base = EditableTrackFields(from: tracks[0]) let base = EditableTrackFields(from: tracks[0])
var mixed: Set<EditableTrackField> = [] var mixed: Set<EditableTrackField> = []
for t in tracks.dropFirst() { for t in tracks.dropFirst() {

@ -48,4 +48,20 @@ struct EditableTrackFieldsTests {
let f = EditableTrackFields(from: t) let f = EditableTrackFields(from: t)
#expect(f.apply(editing: [], to: t) == t) #expect(f.apply(editing: [], to: t) == t)
} }
@Test func sharedAcrossThreeTracksAccumulatesMixed() {
// Step 1: three tracks all share the same album, but title differs on the
// third track and genre differs on the second so both title and
// genre must end up "mixed", while album stays shared.
let t1 = Track.fixture(title: "Same", album: "One Album", genre: "Rock")
let t2 = Track.fixture(title: "Same", album: "One Album", genre: "Pop")
let t3 = Track.fixture(title: "Different", album: "One Album", genre: "Rock")
// Step 2: shared() over all three.
let (values, mixed) = EditableTrackFields.shared(across: [t1, t2, t3])
// Step 3: album is shared (not mixed); title + genre are mixed.
#expect(values.album == "One Album")
#expect(!mixed.contains(.album))
#expect(mixed.contains(.title))
#expect(mixed.contains(.genre))
}
} }

Loading…
Cancel
Save