8eb64f883f
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
106 lines
4.2 KiB
Swift
106 lines
4.2 KiB
Swift
// RenderOptionsAccessoryTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#if os(macOS)
|
|
import Testing
|
|
import AppKit
|
|
import LyrebirdSynth
|
|
@testable import LyrebirdLiveCompileUI
|
|
|
|
// Serialized: these tests mutate a shared `UserDefaults` key (the persisted
|
|
// last-format), so they must not interleave.
|
|
@Suite("RenderOptionsAccessory", .serialized)
|
|
@MainActor
|
|
struct RenderOptionsAccessoryTests {
|
|
|
|
// Popup order (see RenderOptionsAccessory.options).
|
|
private let wavFloat32Index: Int = 0
|
|
private let wavInt24Index: Int = 2
|
|
private let rf64Float32Index: Int = 5
|
|
|
|
private func makeAccessory() -> (RenderOptionsAccessory, NSSavePanel) {
|
|
let panel: NSSavePanel = NSSavePanel()
|
|
let accessory: RenderOptionsAccessory = RenderOptionsAccessory(
|
|
baseName: "render", initialDuration: 5.0, savePanel: panel
|
|
)
|
|
return (accessory, panel)
|
|
}
|
|
|
|
// MARK: - Duration parsing
|
|
|
|
@Test("parseDuration accepts positive finite numbers and rejects the rest")
|
|
func durationParsing() {
|
|
#expect(RenderOptionsAccessory.parseDuration("5.0") == 5.0)
|
|
#expect(RenderOptionsAccessory.parseDuration(" 2.5 ") == 2.5)
|
|
#expect(RenderOptionsAccessory.parseDuration("0") == nil)
|
|
#expect(RenderOptionsAccessory.parseDuration("-1") == nil)
|
|
#expect(RenderOptionsAccessory.parseDuration("") == nil)
|
|
#expect(RenderOptionsAccessory.parseDuration("abc") == nil)
|
|
#expect(RenderOptionsAccessory.parseDuration("nan") == nil)
|
|
#expect(RenderOptionsAccessory.parseDuration("inf") == nil)
|
|
}
|
|
|
|
// MARK: - Format read-out
|
|
|
|
@Test("Selecting a format updates selectedFormat")
|
|
func selectedFormatReflectsPopup() {
|
|
RenderOptionsAccessory.clearPersistedFormatForTesting()
|
|
defer { RenderOptionsAccessory.clearPersistedFormatForTesting() }
|
|
let (accessory, _): (RenderOptionsAccessory, NSSavePanel) = makeAccessory()
|
|
|
|
accessory.selectFormatForTesting(at: wavFloat32Index)
|
|
#expect(accessory.selectedFormat == .wavFloat32)
|
|
accessory.selectFormatForTesting(at: wavInt24Index)
|
|
#expect(accessory.selectedFormat == .wavInt24)
|
|
accessory.selectFormatForTesting(at: rf64Float32Index)
|
|
#expect(accessory.selectedFormat == .rf64Float32)
|
|
}
|
|
|
|
// MARK: - Panel filename sync
|
|
|
|
@Test("Changing format preserves a user-typed filename stem")
|
|
func formatChangePreservesUserStem() {
|
|
RenderOptionsAccessory.clearPersistedFormatForTesting()
|
|
defer { RenderOptionsAccessory.clearPersistedFormatForTesting() }
|
|
let (accessory, panel): (RenderOptionsAccessory, NSSavePanel) = makeAccessory()
|
|
|
|
// User renames the target, then switches format.
|
|
panel.nameFieldStringValue = "mySound.caf"
|
|
accessory.selectFormatForTesting(at: wavFloat32Index)
|
|
#expect(panel.nameFieldStringValue == "mySound.wav")
|
|
|
|
// Switching again keeps the stem, swaps only the extension.
|
|
accessory.selectFormatForTesting(at: rf64Float32Index)
|
|
#expect(panel.nameFieldStringValue == "mySound.wav") // RF64 keeps .wav
|
|
}
|
|
|
|
@Test("Empty filename falls back to the base name")
|
|
func emptyStemFallsBackToBaseName() {
|
|
RenderOptionsAccessory.clearPersistedFormatForTesting()
|
|
defer { RenderOptionsAccessory.clearPersistedFormatForTesting() }
|
|
let (accessory, panel): (RenderOptionsAccessory, NSSavePanel) = makeAccessory()
|
|
panel.nameFieldStringValue = ""
|
|
accessory.selectFormatForTesting(at: wavInt24Index)
|
|
#expect(panel.nameFieldStringValue == "render.wav")
|
|
}
|
|
|
|
// MARK: - Persistence
|
|
|
|
@Test("Last-selected format persists to a fresh accessory")
|
|
func formatSelectionPersists() {
|
|
RenderOptionsAccessory.clearPersistedFormatForTesting()
|
|
defer { RenderOptionsAccessory.clearPersistedFormatForTesting() }
|
|
|
|
let (first, _): (RenderOptionsAccessory, NSSavePanel) = makeAccessory()
|
|
first.selectFormatForTesting(at: rf64Float32Index) // persists "rf64F32"
|
|
|
|
// A brand-new accessory should open on the remembered format.
|
|
let (second, _): (RenderOptionsAccessory, NSSavePanel) = makeAccessory()
|
|
#expect(second.selectedFormat == .rf64Float32)
|
|
}
|
|
}
|
|
#endif
|