Repository Public
1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/CompilerTests.swift3// Description: Compiler space: the lazy JSON tree (rows, expansion, search, spans) and the layout4// of the mixing-graph diagram on a graph produced by the real compiler.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SonataBridge10import Testing11@testable import SonataUI1213@Test func jsonTreeRowsExpansionAndSearch() throws {14 let json = #"{"title":"Aurora","tracks":[{"name":"piano","notes":[{"pitch":62,"span":{"file":0,"start":120,"end":150}},{"pitch":65,"span":{"file":0,"start":120,"end":150}}]},{"name":"strings","notes":[]}],"seed":42,"ok":true,"key":null}"#15 var tree = try #require(JSONTree(json: json))16 // Only the root is open: its five children are visible, scalars before containers.17 var rows = tree.rows()18 #expect(rows.map(\.key) == ["root", "key", "ok", "seed", "title", "tracks"])19 #expect(rows[0].value == "{5} title: Aurora")20 #expect(rows[1].kind == .null && rows[2].value == "true" && rows[2].kind == .bool && rows[3].kind == .number && rows[4].value == "\"Aurora\"")21 #expect(rows[5].value == "[2]" && !rows[5].isExpanded)22 tree.toggle("tracks")23 rows = tree.rows()24 #expect(rows.count == 8 && rows[6].id == "tracks/0" && rows[6].depth == 2 && rows[6].value == "{2} name: piano")25 tree.toggle("tracks")26 #expect(tree.rows().count == 6)2728 let hits = tree.search("PITCH")29 #expect(hits == ["tracks/0/notes/0/pitch", "tracks/0/notes/1/pitch"])30 #expect(tree.search("strings") == ["tracks/1/name"])31 #expect(tree.search("65") == ["tracks/0/notes/1/pitch"])32 #expect(tree.search("").isEmpty && tree.search("zzz").isEmpty)33 tree.expand(to: hits[1])34 #expect(tree.rows().contains { $0.id == hits[1] })3536 // A scalar inherits the span of the node it belongs to; nodes without one have none.37 #expect(tree.span(at: "tracks/0/notes/1/pitch") == JSONSpan(file: 0, start: 120, end: 150))38 #expect(tree.span(at: "tracks/1/name") == nil)39 #expect(tree.totalNodes() == 24)40 tree.collapseAll()41 #expect(tree.rows().count == 6)42 #expect(JSONTree(json: "{not json") == nil)43}4445@Test func jsonTreeCapsHugeContainers() throws {46 let json = "[" + (0..<2000).map(String.init).joined(separator: ",") + "]"47 let tree = try #require(JSONTree(json: json))48 let rows = tree.rows(limitPerContainer: 100)49 #expect(rows.count == 102)50 #expect(rows.last?.value.contains("1900 more") == true)51}5253@Test func graphLayoutOnACompiledGraph() async throws {54 let source = """55 import std.instruments56 import std.effects5758 composition "Graph" {59 tempo 120bpm60 bus hall {61 effects { reverb hall(mix: 1.0) }62 }63 instrument piano: GrandPiano {64 send hall -10dB65 play C4 duration 1/466 effects { compressor }67 }68 instrument lead: ConcertStrings {69 play E4 duration 1/470 effects { distortion }71 }72 }7374 """75 let session = SonataCompilerSession()76 let result = try await session.compile(name: "main", source: source)77 #expect(result.errors.isEmpty, "\(result.errors.map(\.message))")78 let graph = try GraphDocument.decode(json: await session.exportJSON("graph"))79 #expect(graph.nodes.count >= 8)80 #expect(graph.nodes.contains { $0.kind.op == "effect" && $0.kind.effectKind == "reverb" && $0.kind.effectPreset == "hall" })81 #expect(graph.nodes.contains { $0.kind.op == "send" && $0.kind.toBus == 0 })82 #expect(graph.nodes.filter { $0.kind.op == "output" }.count == 1)8384 let layout = GraphLayout.make(graph)85 #expect(layout.frames.count == graph.nodes.count)86 // Every edge goes from a column to a later one.87 for e in graph.edges {88 let a = try #require(layout.columns[e.from]), b = try #require(layout.columns[e.to])89 #expect(a < b, "edge \(e.from) -> \(e.to)")90 }91 // No two nodes overlap and all are inside the canvas.92 let frames = Array(layout.frames.values)93 for i in frames.indices {94 #expect(frames[i].minX >= 0 && frames[i].minY >= 0 && frames[i].maxX <= layout.size.width && frames[i].maxY <= layout.size.height)95 for j in frames.indices where j > i { #expect(!frames[i].intersects(frames[j])) }96 }97 // The distortion is oversampled: its latency must be compensated on the other path.98 if graph.totalLatency > 0 { #expect(graph.nodes.contains { $0.kind.op == "latency_delay" && ($0.kind.samples ?? 0) > 0 }) }99 #expect(graph.nodes.first { $0.kind.op == "instrument" }?.group == "track 0")100}101