Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Tests/ORIXScriptTests/PresetTests.swift3// Description: Every bundled preset must compile without warnings that matter and run cleanly in the simulator;4// the generated reference must only document things the compiler accepts.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import ORIXDSP10@testable import ORIXScript11import Testing1213@Suite struct PresetTests {14 @Test func everyPresetCompilesAndSimulates() {15 #expect(PresetLibrary.all.count >= 20)16 #expect(Set(PresetLibrary.all.map(\.name)).count == PresetLibrary.all.count)17 for category in PresetCategory.allCases { #expect(PresetLibrary.all.contains { $0.category == category }, "no preset in \(category.rawValue)") }18 for preset in PresetLibrary.all {19 let result = ScriptCompiler.compile(preset.source)20 #expect(result.errors.isEmpty, "\(preset.name): \(result.errors)")21 #expect(result.warnings.isEmpty, "\(preset.name): \(result.warnings)")22 guard let program = result.program else { continue }23 #expect(program.automations.first?.name == preset.name, "\(preset.name): the automation must carry the preset's name")24 var config = SimulationConfig()25 config.decks = [SimulatedDeckConfig(playing: true), SimulatedDeckConfig(), SimulatedDeckConfig(), SimulatedDeckConfig()]26 config.maxBeats = 16027 config.triggers = [(24, 1)]28 let sim = Simulator.run(program, config: config)!29 #expect(sim.error == nil, "\(preset.name): \(String(describing: sim.error))")30 #expect(sim.warnings.isEmpty, "\(preset.name): \(sim.warnings.map(\.text))")31 #expect(sim.stats.clampedValues == 0, "\(preset.name): values out of range")32 if !program.automations[0].requirements.usesEvents { #expect(sim.state == .finished || sim.state == .stopped, "\(preset.name) did not finish: \(sim.state)") }33 #expect(ScriptFormatter.format(preset.source) == preset.source + "\n" || ScriptFormatter.format(preset.source) == preset.source, "\(preset.name) is not formatted canonically")34 }35 }3637 @Test func eightBarTransitionDoesWhatItSays() {38 let program = ScriptCompiler.compile(PresetLibrary.preset(named: "Eight Bar Transition")!.source).program!39 var config = SimulationConfig(); config.maxBeats = 6440 let sim = Simulator.run(program, config: config)!41 #expect(sim.actionBeats("deck.B.play") == [0])42 #expect(sim.actionBeats("deck.A.pause") == [32])43 #expect(sim.value("deck.A.volume", at: 31.9)! < -55)44 #expect(sim.value("deck.A.volume", at: 32) == 0)45 #expect(sim.playSpans[1].first.map { $0.lowerBound < 0.1 && $0.upperBound == 32 } == true)46 }4748 @Test func referenceOnlyDocumentsWhatCompiles() {49 // Every path in the generated tables is accepted in a read or a write.50 let text = ReferenceGenerator.completeReference51 var checked = 052 for line in text.components(separatedBy: "\n") where line.hasPrefix("| `") {53 guard let end = line.dropFirst(3).firstIndex(of: "`") else { continue }54 var path = String(line.dropFirst(3)[..<end]).replacingOccurrences(of: ".X.", with: ".A.").replacingOccurrences(of: "<effect>", with: "delay")55 if path.hasPrefix("on ") { let r = ScriptCompiler.compile("automation \"T\" { \(path.replacingOccurrences(of: "trigger(n)", with: "trigger(1)").replacingOccurrences(of: "…", with: "")) }"); #expect(r.errors.isEmpty, "\(path): \(r.errors)"); checked += 1; continue }56 if path.contains("(") {57 if ScriptSchema.builtins.contains(where: { path.hasPrefix($0.name + "(") }) { continue }58 path = String(path.prefix { $0 != "(" })59 #expect(Autocomplete.documentation(for: path) != nil, "no documentation for \(path)")60 checked += 1; continue61 }62 let r = ScriptCompiler.compile("automation \"T\" { print(\(path)) }")63 #expect(r.errors.isEmpty, "\(path): \(r.errors)")64 checked += 165 }66 #expect(checked > 150)67 #expect(ReferenceGenerator.languageBrief.count > 3000)68 }69}70