Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Tests/ORIXScriptTests/DocsTests.swift3// Description: Documentation tests. Every ORIX Script example in docs/orix-script must compile and run in the4// simulator; examples marked as errors must be rejected; console transcripts must parse; the generated5// reference chapters must match the language schema.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10@testable import ORIXScript11import Testing1213enum DocsLocation {14 /// docs/orix-script at the root of the repository.15 static let directory: URL = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()16 .deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("docs/orix-script", isDirectory: true)1718 static func markdownFiles() -> [URL] {19 let e = FileManager.default.enumerator(at: directory, includingPropertiesForKeys: nil)20 return (e?.allObjects as? [URL] ?? []).filter { $0.pathExtension == "md" }.sorted { $0.path < $1.path }21 }22}2324struct DocBlock { var file: String; var line: Int; var tag: String; var code: String }2526func fencedBlocks(in text: String, file: String) -> [DocBlock] {27 var out: [DocBlock] = []28 var current: (tag: String, line: Int, lines: [String])?29 for (i, line) in text.components(separatedBy: "\n").enumerated() {30 if line.trimmingCharacters(in: .whitespaces).hasPrefix("```") {31 if let c = current { out.append(DocBlock(file: file, line: c.line, tag: c.tag, code: c.lines.joined(separator: "\n"))); current = nil }32 else { current = (line.trimmingCharacters(in: .whitespaces).dropFirst(3).trimmingCharacters(in: .whitespaces), i + 1, []) }33 } else if current != nil { current!.lines.append(line) }34 }35 return out36}3738@Suite struct DocsTests {39 @Test func manifestListsExistingChapters() throws {40 let data = try Data(contentsOf: DocsLocation.directory.appendingPathComponent("manifest.json"))41 struct Chapter: Decodable { var title: String; var file: String }42 let chapters = try JSONDecoder().decode([Chapter].self, from: data)43 #expect(chapters.count >= 29)44 for c in chapters {45 let url = DocsLocation.directory.appendingPathComponent(c.file)46 let text = (try? String(contentsOf: url, encoding: .utf8)) ?? ""47 #expect(text.count > 400, "\(c.file) is missing or nearly empty")48 }49 #expect(Set(chapters.map(\.file)).count == chapters.count)50 }5152 @Test func everyExampleCompilesAndRuns() throws {53 var checked = 0, errorExamples = 0, consoleLines = 054 for url in DocsLocation.markdownFiles() {55 let text = try String(contentsOf: url, encoding: .utf8)56 for block in fencedBlocks(in: text, file: url.lastPathComponent) {57 let where_ = "\(block.file):\(block.line)"58 switch block.tag {59 case "orixdj":60 // A complete script, or statements that belong inside an automation.61 let isFile = block.code.contains("automation \"")62 let source = isFile ? block.code : "automation \"Example\" {\n\(block.code)\n}"63 let result = ScriptCompiler.compile(source)64 #expect(result.errors.isEmpty, "\(where_): \(result.errors.map(\.description))")65 guard let program = result.program else { continue }66 var config = SimulationConfig(); config.maxBeats = 20067 config.decks = [SimulatedDeckConfig(playing: true), SimulatedDeckConfig(), SimulatedDeckConfig(), SimulatedDeckConfig()]68 config.triggers = [(8, 1), (12, 2)]69 for automation in program.automations {70 let sim = Simulator.run(program, automation: automation.name, config: config)71 #expect(sim?.error == nil, "\(where_) (\(automation.name)): \(String(describing: sim?.error))")72 }73 checked += 174 case "orixdj-error":75 // An example of a mistake: the compiler (or, failing that, the simulator) must reject it.76 let isFile = block.code.contains("automation \"")77 let result = ScriptCompiler.compile(isFile ? block.code : "automation \"Example\" {\n\(block.code)\n}")78 if let program = result.program { #expect(Simulator.run(program)?.error != nil, "\(where_): documented as an error but it compiles and runs") }79 errorExamples += 180 case "console":81 for line in block.code.components(separatedBy: "\n") where line.hasPrefix("> ") {82 if case let .invalid(d) = ConsoleParser.parse(String(line.dropFirst(2))) { Issue.record("\(where_): console line '\(line)' is invalid: \(d.map(\.description))") }83 consoleLines += 184 }85 default: break86 }87 }88 }89 print("=== ORIX-DOCS === \(checked) examples compiled and simulated, \(errorExamples) error examples rejected, \(consoleLines) console lines parsed === END ===")90 #expect(checked >= 60)91 }9293 @Test func generatedChaptersAreUpToDate() throws {94 for (file, expected) in ReferenceGenerator.generatedChapters {95 let text = (try? String(contentsOf: DocsLocation.directory.appendingPathComponent(file), encoding: .utf8)) ?? ""96 #expect(text == expected, "\(file) is out of date: run `swift run orix-dj-bench docs`")97 }98 }99100 @Test func everyPresetAndErrorCodeIsDocumented() throws {101 let all = try DocsLocation.markdownFiles().map { try String(contentsOf: $0, encoding: .utf8) }.joined(separator: "\n")102 for preset in PresetLibrary.all { #expect(all.contains(preset.name), "preset \(preset.name) is not mentioned in the documentation") }103 for code in ["E-SYNTAX", "E-TYPE", "E-RANGE", "E-PATH", "E-DECK", "E-EFFECT", "E-READONLY", "E-RAMP", "E-OVER", "E-UNDECLARED", "E-CONST", "E-REDECLARED", "E-UNBOUNDED", "E-LIMIT",104 "E-RECURSION", "E-PURE", "E-RETURN", "E-ARGS", "E-EVENT", "E-CAPABILITY", "E-LOOP", "E-MATH", "E-BUDGET", "E-RATE", "E-ACTION", "E-UNAVAILABLE", "E-UNIT", "E-CURVE", "E-EMPTY", "E-LEX"] {105 #expect(all.contains(code), "error code \(code) is not explained in the documentation")106 }107 }108}109