use wavegraph_compiler::{NodeSpec, compile};
use wavegraph_core::bytecode::{Op, VERSION};
fn catalog(kind: &str) -> Option<NodeSpec> {
match kind {
"voice" => Some(NodeSpec {
inputs: &[],
outputs: &["signal"],
}),
"stereo" => Some(NodeSpec {
inputs: &["a", "b"],
outputs: &["l", "r"],
}),
_ => None,
}
}
#[test]
fn declarations_bind_native_ids_while_dependencies_determine_execution_order() {
let patch = compile(
"# forward references; deliberately reverse the native call order
output right main.r
connect bass.signal main.b
node main stereo
mix sum bass.signal bass.signal
gain trim sum.out 64
node bass voice ch=16 shape=square
connect trim.out main.a
output left main.l # comment",
catalog,
)
.expect("valid patch fixture");
assert_eq!(patch.nodes[0].alias, "main");
assert_eq!(
patch.nodes[1].options,
[
("ch".into(), "16".into()),
("shape".into(), "square".into())
]
);
assert_eq!(patch.outputs, [3, 4]);
let (&version, mut code) = patch.bytecode.split_first().expect("valid patch fixture");
assert_eq!(version, VERSION);
for expected in [
Op::Call {
object: 1,
inputs: &[],
outputs: 1,
},
Op::Mix { left: 0, right: 0 },
Op::Gain {
input: 1,
level: 64,
},
Op::Call {
object: 0,
inputs: &[2, 0],
outputs: 2,
},
] {
let (actual, tail) = Op::decode(code).expect("valid patch fixture");
assert_eq!(actual, expected);
code = tail;
}
assert!(code.is_empty());
}
#[test]
fn diagnostics_identify_the_source_line_and_offending_binding() {
let base = "node v voice\noutput left v.signal\noutput right v.signal\n";
for (suffix, line, reason) in [
("node v voice", 4, "duplicate object 'v'"),
("node b unknown", 4, "unknown node type 'unknown'"),
("node 99 voice", 4, "invalid object alias '99'"),
(
"node other voice waveform=a waveform=b",
4,
"duplicate 'waveform'",
),
("node other voice missingvalue", 4, "expected key=value"),
("node amp stereo", 4, "unconnected inlet 'amp.a'"),
("gain g unknown.signal 128", 4, "unknown object 'unknown'"),
("gain g v.wrong 128", 4, "unknown signal outlet 'v.wrong'"),
("gain g v.signal 256", 4, "expected gain level"),
(
"gain g v.signal 64\nconnect v.signal g.in",
5,
"already connected",
),
("gain a b.out 64\ngain b a.out 64", 4, "cycle"),
("gain g g.out 64", 4, "cycle"),
("output left v.signal", 4, "duplicate left output"),
("nide u voice", 4, "invalid 'nide'"),
] {
let error = compile(&format!("{base}{suffix}"), catalog).expect_err("invalid patch");
assert_eq!(error.line, line, "{suffix}: {error}");
assert!(error.message.contains(reason), "{suffix}: {error}");
}
let error = compile("node v voice\noutput left v.signal", catalog).expect_err("invalid patch");
assert_eq!(error.line, 2);
assert!(error.message.contains("missing right output"));
}
#[test]
fn native_atoms_preserve_punctuation_across_unicode_whitespace_and_comments() {
let source = concat!(
"# stereo source\r\n",
"node\u{2003}node osc~ steps=36!+39,42,- custom-key=a=b# inline\r\n",
"gain trim node.audio.left +128\r\n",
"output left trim.out\r\n",
"output right node.audio.right# end of file",
);
let patch = compile(source, |kind| {
(kind == "osc~").then_some(NodeSpec {
inputs: &[],
outputs: &["audio.left", "audio.right"],
})
})
.expect("valid native patch");
assert_eq!(patch.outputs, [2, 1]);
assert_eq!(
patch.nodes[0].options,
[
("steps".into(), "36!+39,42,-".into()),
("custom-key".into(), "a=b".into()),
]
);
}
#[test]
fn statement_rules_require_word_boundaries_and_consume_the_whole_line() {
let base = "node v voice\noutput left v.signal\noutput right v.signal\n";
for suffix in [
"nodeother voice",
"gain trim v.signal 128 extra",
"gain trim v.signal\n128",
] {
let error = compile(&format!("{base}{suffix}"), catalog).expect_err("invalid statement");
assert_eq!(error.line, 4, "{suffix}: {error}");
}
}
#[test]
fn syntax_errors_report_columns_in_characters() {
let error = compile("# comment\r\nnode\u{2003}99 voice", catalog).expect_err("invalid alias");
assert_eq!(error.line, 2);
assert!(error.to_string().contains("column 6"), "{error}");
assert!(error.message.contains("99"), "{error}");
}
#[test]
fn last_bus_index_is_valid_and_the_next_bus_is_rejected() {
use core::fmt::Write;
let mut source = String::from("node v voice\noutput left v.signal\noutput right g254.out\n");
for index in 0..255 {
writeln!(source, "gain g{index} v.signal 1").expect("valid patch fixture");
}
let patch = compile(&source, catalog).expect("valid patch fixture");
assert_eq!(patch.outputs, [0, u8::MAX]);
source.push_str("gain excess v.signal 1");
let error = compile(&source, catalog).expect_err("invalid patch");
assert!(error.message.contains("exceeds 256 signal buses"));
}
#[test]
fn full_object_table_and_large_program_decode_with_the_portable_format() {
use core::fmt::Write;
const INPUTS: &[&str] = &[
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
];
let large_catalog = |kind: &str| match kind {
"sink" => Some(NodeSpec {
inputs: INPUTS,
outputs: &[],
}),
_ => catalog(kind),
};
let mut source = String::from("node v voice\noutput left v.signal\noutput right v.signal\n");
for id in 1..=u8::MAX {
writeln!(source, "node s{id} sink").expect("write to string");
for inlet in INPUTS {
writeln!(source, "connect v.signal s{id}.{inlet}").expect("write to string");
}
}
let patch = compile(&source, large_catalog).expect("valid large program");
assert!(patch.bytecode.len() > 4096);
let (&version, mut bytes) = patch.bytecode.split_first().expect("version byte");
assert_eq!(version, VERSION);
for id in 0..=u8::MAX {
let (op, rest) = Op::decode(bytes).expect("valid instruction");
let inputs: &[u8] = if id == 0 { &[] } else { &[0; INPUTS.len()] };
assert_eq!(
op,
Op::Call {
object: id,
inputs,
outputs: u8::from(id == 0)
}
);
bytes = rest;
}
assert!(bytes.is_empty());
source.push_str("node extra voice");
let error = compile(&source, large_catalog).expect_err("unaddressable object");
assert!(error.message.contains("exceeds 256 native nodes"));
}
#[test]
fn ambiguous_or_unaddressable_catalog_ports_point_to_the_node_declaration() {
for (ports, expected) in [
(&["in", "in"][..], "duplicate inlet"),
(&["out with space"][..], "invalid inlet"),
(&["unused"; 256][..], "exceeds 255 signal inlets"),
] {
let err = compile("# custom catalog\nnode n custom", |_| {
Some(NodeSpec {
inputs: ports,
outputs: &["out"],
})
})
.expect_err("invalid catalog");
assert_eq!(err.line, 2);
assert!(err.message.contains(expected), "{err}");
}
}