// LinuxScratchModelTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT // Pure-logic tests for the Linux scratch model's document / file surface // (dirty-tracking, new/load/save, title). Deliberately avoids the paths that // open a native GtkFileDialog (`openDocument()`, `saveAs()`) — those need a // running GTK application and a display, so they are exercised by hand on the // VM, not here. The paths covered are headless-safe: `CodeEditorController`'s // `setText` is a no-op until a widget is attached, and `save()` with a // `currentPath` set writes the file directly without a dialog. #if os(Linux) import Testing import Foundation import LyrebirdSynth @testable import LyrebirdLiveCompile @testable import LyrebirdLiveCompileUI @Suite("LinuxScratchModel document + file I/O") struct LinuxScratchModelTests { private func makeModel(initialSource: String = lyrebirdLinuxScratchSeed) -> LinuxScratchModel { let engine: SynthesisEngine = SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: lyrebirdDefaultEventQueueCapacity, driver: OfflineDriver() ) return LinuxScratchModel(session: LiveSession(engine: engine), initialSource: initialSource) } private func tempPath(_ ext: String = "swift") -> String { FileManager.default.temporaryDirectory .appendingPathComponent("lyrebird-scratch-\(UUID().uuidString).\(ext)") .path } // MARK: - Dirty tracking @Test func freshModelIsCleanWithSeed() { let model: LinuxScratchModel = makeModel() #expect(model.source == lyrebirdLinuxScratchSeed) #expect(model.isDirty == false) #expect(model.currentPath == nil) } @Test func setSourceMarksDirtyOnRealChange() { let model: LinuxScratchModel = makeModel() model.setSource(model.source + "\n// edit") #expect(model.isDirty == true) } @Test func setSourceIgnoresUnchangedEcho() { let model: LinuxScratchModel = makeModel() // A widget echo of the identical value must not flip the dirty flag. model.setSource(model.source) #expect(model.isDirty == false) } // MARK: - New @Test func newDocumentResetsToSeedAndClean() { let model: LinuxScratchModel = makeModel() model.setSource("context.play { OscSin.Audio(freq: 440) }") model.currentPath = "/tmp/whatever.swift" #expect(model.isDirty == true) model.newDocument() #expect(model.source == lyrebirdLinuxScratchSeed) #expect(model.currentPath == nil) #expect(model.isDirty == false) } // MARK: - Load @Test func loadFileReplacesBufferAndAdoptsPathClean() throws { let path: String = tempPath() let body: String = "context.play { OscSin.Audio(freq: 220, mul: 0.2) }\n" try body.write(toFile: path, atomically: true, encoding: .utf8) defer { try? FileManager.default.removeItem(atPath: path) } let model: LinuxScratchModel = makeModel() model.loadFile(path) #expect(model.source == body) #expect(model.currentPath == path) #expect(model.isDirty == false) } @Test func loadFileMissingPathKeepsBuffer() { let model: LinuxScratchModel = makeModel() let before: String = model.source model.loadFile("/nonexistent/does-not-exist.swift") // Failure is reported to the console; the buffer is untouched. #expect(model.source == before) #expect(model.currentPath == nil) } // MARK: - Save @Test func saveWritesToCurrentPathAndClears() throws { let path: String = tempPath() defer { try? FileManager.default.removeItem(atPath: path) } let model: LinuxScratchModel = makeModel() let code: String = "context.play { WhiteNoise.Audio(mul: 0.1) }" model.setSource(code) model.currentPath = path #expect(model.isDirty == true) model.save() // currentPath is set -> writes directly, no dialog #expect(model.isDirty == false) let onDisk: String = try String(contentsOfFile: path, encoding: .utf8) #expect(onDisk == code) } @Test func saveThenLoadRoundTrip() throws { let path: String = tempPath() defer { try? FileManager.default.removeItem(atPath: path) } let writer: LinuxScratchModel = makeModel() let code: String = "context.play { OscSaw.Audio(freq: 110, mul: 0.15) }" writer.setSource(code) writer.currentPath = path writer.save() let reader: LinuxScratchModel = makeModel() reader.loadFile(path) #expect(reader.source == code) #expect(reader.currentPath == path) #expect(reader.isDirty == false) } // MARK: - Title @Test func displayTitleUntitledWhenNoPath() { let model: LinuxScratchModel = makeModel() #expect(model.displayTitle == "untitled.swift") } @Test func displayTitleShowsFilenameAndDirtyMarker() { let model: LinuxScratchModel = makeModel() model.currentPath = "/home/user/tune.swift" #expect(model.displayTitle == "tune.swift") model.setSource(model.source + " ") #expect(model.displayTitle == "● tune.swift") } } #endif