sva-engine 0.8.1

Renders a resolved graph into per-node buffers a query can be asked of
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// Concern: proves what reading another node yields, per the representation it holds | Non-concern: what a node evaluates to | IO: (a composition) -> Read or a refusal

use crate::fixtures::graph_of;
use sva_engine::instantiate::{instantiate, resolve_ref_path};
use sva_engine::schedule_from;
use sva_engine::{Ask, EngineError, Held, RenderConfig, Representation, Tier, Var, render, types};
use sva_engine::{DEFAULT_SAMPLE_RATE, identity};

#[test]
fn dependencies_precede_dependents() {
    let g = graph_of(
        "order",
        &[
            ("kick", "sin(2*pi*50*t)\n"),
            ("lead", "@kick*0.5 + @kick(t - 0.01s)*0.3\n"),
        ],
    );
    let instances =
        instantiate(&g, "lead", DEFAULT_SAMPLE_RATE).expect("a graph that instantiates");
    let order = schedule_from(&instances, &["lead".to_string()])
        .expect("a schedule")
        .groups
        .concat();
    let kick_pos = order.iter().position(|p| p == "kick").expect("kick");
    let lead_pos = order.iter().position(|p| p == "lead").expect("lead");
    assert!(kick_pos < lead_pos);
    assert_eq!(
        order.iter().filter(|p| p.as_str() == "kick").count(),
        1,
        "two refs from one node are one member of the walk"
    );
}

#[test]
fn a_walk_above_the_root_is_a_refusal_the_engine_can_locate() {
    assert_eq!(
        resolve_ref_path("piano/bench", "../../escape").expect_err("above the root"),
        EngineError::RefAboveRoot("piano/bench".to_string(), "../../escape".to_string()),
    );
}

/// A shift is a node of its own, so two reads a fraction of a sample apart are two
/// expressions and neither can be served out of the other's key.
#[test]
fn a_fractional_shift_hashes_as_a_new_expression() {
    let hash = |name: &str, body: &str| {
        let g = graph_of(name, &[("src", "sin(2*pi*220*t)\n"), ("node", body)]);
        let typing = types(&g, "node").expect("a law");
        let id = typing.id("node").expect("the root");
        identity(&typing, id).expect("a law hashes")
    };
    let plain = hash("plain", "@src\n");
    let moved = hash("moved", "@src(t - 0.000001s)\n");
    let barely = hash("barely", "@src(t - 0.0000011s)\n");
    assert_ne!(plain, moved, "a shift is a new expression");
    assert_ne!(
        moved, barely,
        "two shifts under one sample apart are two expressions"
    );
}

/// An `sp` offset on a sampled ref moves the reading by whole samples.
#[test]
fn an_sp_offset_read_is_an_integer_index() {
    let rendered = |name: &str, body: &str| {
        let g = graph_of(
            name,
            &[("grid", "chaigne_askenfelt(261.63)\n"), ("node", body)],
        );
        let held = render(
            &g,
            "node",
            RenderConfig::seconds(44_100, 0.01),
            &Tier::default(),
        )
        .unwrap_or_else(|e| panic!("{name}: {e}"));
        let id = held.id("node").expect("the root");
        held.output(id).expect("a rendered read").plane(0).to_vec()
    };
    let plain = rendered("plain-grid", "@grid*0.5\n");
    let moved = rendered("moved-grid", "@grid(t - 2sp)*0.5\n");
    assert_eq!(moved[0], 0.0, "before the read is founded");
    assert_eq!(moved[1], 0.0);
    for i in 2..plain.len() {
        assert!(
            (moved[i] - plain[i - 2]).abs() < 1e-12,
            "sample {i}: {} against {}",
            moved[i],
            plain[i - 2]
        );
    }
}

/// A duration on a sampled ref is an index offset wherever it is a whole sample count at
/// the observation's rate, and a bar resolved against a tempo is such a duration.
#[test]
fn a_bar_offset_that_lands_on_the_grid_reads_a_sampled_ref() {
    let mut g = graph_of(
        "bar-offset",
        &[
            ("grid", "sample(sin(2*pi*220*t))\n"),
            ("node", "@grid(t - 1b)*0.5\n"),
        ],
    );
    g.resolve_bar_spans(sva_ast::PerBar {
        seconds: 2.0,
        per: 1.0,
    });
    let held = render(
        &g,
        "node",
        RenderConfig::seconds(8_000, 2.5),
        &Tier::default(),
    )
    .expect("a bar offset that lands on the grid");
    let id = held.id("node").expect("the root");
    let buffer = held.output(id).expect("a rendered read");
    let delay = 16_000;
    for i in 0..buffer.len() {
        let at = i as f64 - f64::from(delay);
        let want = 0.5 * (std::f64::consts::TAU * 220.0 * at / 8_000.0).sin();
        assert!(
            (buffer.at(0, i) - want).abs() < 1e-9,
            "sample {i}: {} against {want}",
            buffer.at(0, i)
        );
    }
}

/// A closed form reading another keeps the read in its own body, the node and its shift, and
/// the node read keeps its own body: a reading of lines composes them, and holds no buffer.
#[test]
fn a_law_ref_stays_a_read_of_a_node_and_allocates_no_buffer() {
    let g = graph_of(
        "read",
        &[
            ("src", "sin(2*pi*220*t)\n"),
            ("node", "@src*0.5 + @src(t - 0.01s)\n"),
        ],
    );
    let config = RenderConfig::seconds(8_192, 0.25).asking(vec![Ask {
        node: "node".to_string(),
        representation: Representation::Lines,
    }]);
    let held = render(&g, "node", config, &Tier::default()).expect("a law");
    assert!(held.buffers.is_empty(), "no buffer stands behind a law");

    let (id, src) = (
        held.id("node").expect("the root"),
        held.id("src").expect("src"),
    );
    let sva_engine::Value::ClosedForm(form) = held.tys.value(id) else {
        panic!("a law");
    };
    assert_eq!(
        sva_engine::nodes_in(&form.body),
        vec![src],
        "the read stays a node"
    );
    let sva_engine::Value::ClosedForm(own) = held.tys.value(src) else {
        panic!("a law");
    };
    assert!(
        sva_engine::nodes_in(&own.body).is_empty(),
        "src is its own body alone"
    );
}

/// `sp` is one step of the rate in use in seconds, so a closed form read `2sp` back is that
/// closed form moved two samples, exact at any rate.
#[test]
fn a_law_read_at_a_grid_offset_renders() {
    let g = graph_of(
        "indexed-law",
        &[
            ("src", "sin(2*pi*220*t)\n"),
            ("node", "@src(t - 2sp)*0.5\n"),
        ],
    );
    let held = render(
        &g,
        "node",
        RenderConfig::seconds(8_000, 0.01),
        &Tier::default(),
    )
    .expect("a law read on the grid");
    let id = held.id("node").expect("the root");
    let buffer = held.output(id).expect("a rendered read");
    assert_eq!(buffer.len(), 80);
    for i in 0..buffer.len() {
        let at = i as f64 / 8_000.0 - 2.0 / 8_000.0;
        let want = 0.5 * (std::f64::consts::TAU * 220.0 * at).sin();
        assert!(
            (buffer.at(0, i) - want).abs() < 1e-9,
            "sample {i}: {} against {want}",
            buffer.at(0, i)
        );
    }
}

/// A ref read at a time no offset names is the callee's closed form at that time, the modulo
/// exact: the read tiles, and the reading node is a point-sampled closed form rather than a
/// pair.
#[test]
fn a_law_ref_tiled_by_modulo_point_samples() {
    let g = graph_of(
        "tiled",
        &[("src", "sin(2*pi*100*t)\n"), ("node", "@src(t % 0.0037)\n")],
    );
    let typing = types(&g, "node").expect("a tiled read types");
    let held = typing.id("node").expect("the root");
    let warped = typing.ty(held);
    assert_eq!(
        (warped.held, warped.dual),
        (Held::Form(Var::T), false),
        "a warped time leaves A"
    );

    let rendered = render(
        &g,
        "node",
        RenderConfig::seconds(8_000, 0.05),
        &Tier::default(),
    )
    .expect("a tiled read");
    let root = rendered.id("node").expect("the root");
    let buffer = rendered.output(root).expect("a point-sampled law");
    for i in 0..buffer.len() {
        let tiled = (i as i64 * 10_000) % (37 * 8_000);
        let want = (std::f64::consts::TAU * 100.0 * (tiled as f64 / 80_000_000.0)).sin();
        assert!(
            (buffer.at(0, i) - want).abs() < 1e-9,
            "sample {i}: {} against {want}",
            buffer.at(0, i)
        );
    }
}

/// A ref naming one number is that number, so a template may write its pitches as offsets
/// from a key node rather than as literals.
#[test]
fn a_key_times_a_semitone_offset_folds() {
    let g = graph_of(
        "key-offset",
        &[
            ("variables/key", "D3\n"),
            ("voice", "sin(2*pi*f0*t)\n"),
            ("node", "@voice(t, f0=@variables/key*3st)\n"),
        ],
    );
    let config = RenderConfig::seconds(8_192, 0.25).asking(vec![Ask {
        node: "node".to_string(),
        representation: Representation::Lines,
    }]);
    let held = render(&g, "node", config, &Tier::default()).expect("a folded key");
    let id = held.id("node").expect("the root");
    let found = sva_engine::answer(&held, id, Representation::Lines).expect("lines");
    let sva_engine::Output::Lines(lines) = found.value else {
        panic!("expected a line list");
    };
    let hz: Vec<f64> = lines
        .iter()
        .map(|l| l.hz.abs())
        .filter(|h| *h > 0.0)
        .collect();
    let want =
        sva_formula::note::frequency(sva_ast::note_midi("D3").expect("D3")) * 2f64.powf(3.0 / 12.0);
    assert!(
        hz.iter().all(|h| (h - want).abs() < 1e-9),
        "a minor third above D3 is {want} Hz, got {hz:?}"
    );
}

/// Folding a ref is for the ones naming one number. A series and a delta each hold a closed form,
/// and reading either under arithmetic composes it rather than flattening it.
#[test]
fn a_ref_that_is_not_one_number_composes_as_a_law() {
    let g = graph_of(
        "not-a-number",
        &[
            ("saw", "sum(k, 1, 10, sin(2*pi*k*220*t)/k)\n"),
            ("imp", "delta(t)\n"),
            ("voiced", "@saw*2\n"),
            ("struck", "@imp*2\n"),
        ],
    );

    let held = render(
        &g,
        "voiced",
        RenderConfig::seconds(44_100, 0.05),
        &Tier::default(),
    )
    .expect("a series read under a product");
    let buffer = held
        .output(held.id("voiced").expect("the root"))
        .expect("a rendered series");
    let peak = (0..buffer.len()).fold(0.0f64, |m, i| m.max(buffer.at(0, i).abs()));
    assert!(peak > 1.0, "a series ref keeps its partials, peak {peak}");

    let config = RenderConfig::seconds(44_100, 0.05).asking(vec![Ask {
        node: "struck".to_string(),
        representation: Representation::Atoms,
    }]);
    let held =
        render(&g, "struck", config, &Tier::default()).expect("a delta read under a product");
    let id = held.id("struck").expect("the root");
    let sva_engine::Output::Atoms(atoms) = sva_engine::answer(&held, id, Representation::Atoms)
        .expect("atoms")
        .value
    else {
        panic!("expected an atom list");
    };
    assert!(
        atoms.iter().any(|a| a.contains("delta")),
        "a delta ref stays a delta: {atoms:?}"
    );
}

/// A time written one per component is a per-lane substitution: each lane of the callee is
/// read at the time its own lane names.
#[test]
fn a_per_channel_shift_on_a_law_substitutes_per_lane() {
    let g = graph_of(
        "per-lane",
        &[
            ("wide", "join(sin(2*pi*220*t), sin(2*pi*330*t))\n"),
            ("taps", "@wide(join(t - 0.01s, t - 0.02s))\n"),
        ],
    );
    let held = render(
        &g,
        "taps",
        RenderConfig::seconds(44_100, 0.05),
        &Tier::default(),
    )
    .expect("a law read once per component");
    let buffer = held
        .output(held.id("taps").expect("the root"))
        .expect("two lanes");
    assert_eq!(buffer.width(), 2);
    for i in [0usize, 441, 1000] {
        let t = i as f64 / 44_100.0;
        let left = (std::f64::consts::TAU * 220.0 * (t - 0.01)).sin();
        let right = (std::f64::consts::TAU * 330.0 * (t - 0.02)).sin();
        assert!(
            (buffer.at(0, i) - left).abs() < 1e-12,
            "left at {i}: {} against {left}",
            buffer.at(0, i)
        );
        assert!(
            (buffer.at(1, i) - right).abs() < 1e-12,
            "right at {i}: {} against {right}",
            buffer.at(1, i)
        );
    }
}

/// A buffer read carries one offset, so two times at once is two reads, and the refusal
/// writes both of them out.
#[test]
fn a_per_channel_shift_on_samples_names_each_read() {
    let g = graph_of(
        "per-lane-samples",
        &[
            ("wide", "sample(join(sin(2*pi*220*t), sin(2*pi*330*t)))\n"),
            ("taps", "@wide(join(t - 0.01s, t - 0.02s))\n"),
        ],
    );
    let e = types(&g, "taps").expect_err("samples read at two times refuse");
    let EngineError::Refused(d) = &e else {
        panic!("expected a written refusal, got {e:?}");
    };
    assert_eq!(d.code, "engine.per_lane_read_on_samples");
    assert!(d.message.contains("t - 0.01"), "{}", d.message);
    assert!(d.message.contains("t - 0.02"), "{}", d.message);
    assert!(d.help.contains("ch(@wide("), "{}", d.help);
}

/// A mono buffer has no second component to read, so the repair names one read per time.
#[test]
fn a_per_channel_shift_on_a_mono_buffer_names_one_read_per_time() {
    let g = graph_of(
        "per-lane-mono",
        &[
            ("mono", "sample(sin(2*pi*220*t))\n"),
            ("taps", "@mono(join(t - 0.01s, t - 0.02s))\n"),
        ],
    );
    let e = types(&g, "taps").expect_err("one buffer, two times");
    let EngineError::Refused(d) = &e else {
        panic!("expected a written refusal, got {e:?}");
    };
    assert_eq!(d.code, "engine.per_lane_read_on_samples");
    assert!(
        d.help.contains("join(@mono(t - 0.01), @mono(t - 0.02))"),
        "{}",
        d.help
    );
    assert!(
        !d.help.contains("ch("),
        "a mono buffer has no component 1: {}",
        d.help
    );
}

/// FORMAT 15.2: a ref inlines symbolically, and a join of pairs is a pair per lane. An
/// exact reading composes through every construct the closed form is written with, not only the
/// sum and the product.
#[test]
fn lines_compose_through_a_join_of_crops() {
    let g = graph_of(
        "compose-through",
        &[
            ("tone", "sin(2*pi*440*t)\n"),
            ("other", "sin(2*pi*550*t)\n"),
            ("wide", "join(@tone(t), @other(t))\n"),
            (
                "windows",
                "join(crop(@tone(t), 0s, 1s), crop(@other(t), 0s, 1s))\n",
            ),
            ("shouldered", "crop(@tone(t), 0s, 1s, rise=0.1, fall=0.1)\n"),
            ("one", "ch(@wide(t), 1)\n"),
            ("squared", "pow(@tone(t), 2)\n"),
            ("stacked", "sum(k, 1, 3, @tone(t)/k)\n"),
        ],
    );
    let read = |node: &str, representation| {
        let config = RenderConfig::seconds(8_192, 1.0).asking(vec![Ask {
            node: node.to_string(),
            representation,
        }]);
        let held =
            render(&g, node, config, &Tier::default()).unwrap_or_else(|e| panic!("{node}: {e}"));
        let id = held.id(node).unwrap_or_else(|| panic!("{node} typed"));
        sva_engine::answer(&held, id, representation).unwrap_or_else(|e| panic!("{node}: {e}"))
    };
    let hz = |node: &str| {
        let sva_engine::Output::Lines(lines) = read(node, Representation::Lines).value else {
            panic!("{node}: expected lines");
        };
        let mut out: Vec<i64> = lines.iter().map(|l| l.hz.round() as i64).collect();
        out.sort_unstable();
        out
    };
    assert_eq!(hz("wide"), vec![-550, -440, 440, 550], "a join of two refs");
    assert_eq!(hz("one"), vec![-550, 550], "one component of a joined pair");

    let count = |node: &str| {
        let sva_engine::Output::Atoms(atoms) = read(node, Representation::Atoms).value else {
            panic!("{node}: expected atoms");
        };
        atoms
    };
    let windows = count("windows");
    assert_eq!(windows.len(), 4, "two cropped lines per lane: {windows:?}");
    assert!(
        windows.iter().all(|a| a.contains("indicator")),
        "each carries its window: {windows:?}"
    );
    let shouldered = count("shouldered");
    assert_eq!(
        shouldered.len(),
        14,
        "seven window pieces times two line atoms: {shouldered:?}"
    );
    assert!(
        shouldered
            .iter()
            .all(|a| a.contains("exponential") && a.contains("indicator")),
        "each atom is the tone under its own piece of the window: {shouldered:?}"
    );
    assert_eq!(
        hz("squared"),
        vec![-880, 0, 880],
        "a ref under pow is the law it inlines to, the constant term its line at zero"
    );
    assert_eq!(
        hz("stacked"),
        vec![-440, -440, -440, 440, 440, 440],
        "a ref under a finite sum is one term per index"
    );
}

/// FORMAT 6.5 and 12: a modal bank is a finite sum of damped sinusoids, so it multiplies,
/// shifts, crops and reads like any other pair rather than only rendering bare.
#[test]
fn a_modal_bank_times_an_envelope_lists_its_modes() {
    let g = graph_of(
        "modal-composes",
        &[
            ("pluck", "string(D3, modes=6)\n"),
            ("voiced", "@pluck(t)*exp(0 - 3*t)\n"),
            ("echoed", "@pluck(t) + 0.8*@pluck(t - 15ms)\n"),
            ("windowed", "crop(@pluck(t), 0s, 1s)\n"),
        ],
    );
    let atoms = |node: &str| {
        let representation = Representation::Atoms;
        let config = RenderConfig::seconds(8_192, 1.0).asking(vec![Ask {
            node: node.to_string(),
            representation,
        }]);
        let held =
            render(&g, node, config, &Tier::default()).unwrap_or_else(|e| panic!("{node}: {e}"));
        let id = held.id(node).unwrap_or_else(|| panic!("{node} typed"));
        let sva_engine::Output::Atoms(atoms) = sva_engine::answer(&held, id, representation)
            .unwrap_or_else(|e| panic!("{node}: {e}"))
            .value
        else {
            panic!("{node}: expected atoms");
        };
        atoms
    };
    let voiced = atoms("voiced");
    assert_eq!(
        voiced.len(),
        12,
        "six modes, one pole pair each: {voiced:?}"
    );
    assert!(
        voiced
            .iter()
            .all(|a| a.contains("exponential") && a.contains("indicator")),
        "each mode rings from the strike under its own decay: {voiced:?}"
    );
    assert_eq!(atoms("echoed").len(), 24, "the bank and its shifted copy");
    assert_eq!(
        atoms("windowed").len(),
        12,
        "a crop of a bank is a bank cropped"
    );
    assert_eq!(
        atoms("pluck").len(),
        12,
        "the bare bank lists its own modes"
    );
}

/// The first `n` samples of `body` at 100 Hz, where `form` is the ramp `t` as a closed form
/// and `held` the same ramp as samples.
fn first(name: &str, body: &str, n: usize) -> Result<Vec<f64>, EngineError> {
    let root = format!("{body}\n");
    let g = graph_of(
        name,
        &[("form", "t\n"), ("held", "sample(t)\n"), ("root", &root)],
    );
    let held = render(
        &g,
        "root",
        RenderConfig::seconds(100, n as f64 / 100.0),
        &Tier::default(),
    )?;
    let root = held.id("root").expect("the root");
    Ok(held.output(root).expect("samples").plane(0).to_vec())
}

fn near(got: &[f64], want: &[f64]) -> bool {
    got.len() == want.len() && got.iter().zip(want).all(|(g, w)| (g - w).abs() < 1e-12)
}

/// A reflected read keeps its sign on either representation: `0.11s - t` counts down.
#[test]
fn a_reflected_read_counts_down_on_a_form_and_on_samples() {
    for (name, body) in [
        ("form-rev", "@form(0.11s - t)"),
        ("held-rev", "@held(0.11s - t)"),
        ("held-neg", "@held(-1*(t - 0.11s))"),
        ("held-sp", "@held(11sp - t)"),
    ] {
        let got = first(name, body, 3).expect("a reflected read renders");
        assert!(near(&got, &[0.11, 0.10, 0.09]), "{body}: {got:?}");
    }
}

/// Samples of a closed form read at any multiple of `t` land where that multiple says: on
/// samples where it is whole there, the closed form itself between them.
#[test]
fn a_scaled_read_of_samples_lands_on_or_between_samples() {
    let got = first("held-2t", "@held(2*t - 0.02s)", 3).expect("a whole scale renders");
    assert!(near(&got, &[-0.02, 0.0, 0.02]), "{got:?}");
    let got = first("held-half", "@held(0.5*t)", 3).expect("half a sample renders");
    let want = [0.0, 0.005, 0.01];
    assert!(
        got.iter().zip(want).all(|(g, w)| (g - w).abs() < 1e-9),
        "{got:?}"
    );
}

/// Seconds and steps of the rate in use add in one offset.
#[test]
fn an_offset_in_seconds_and_steps_reads_their_sum() {
    let got = first("mixed", "@held(t - 0.02s - 1sp)", 3).expect("a mixed offset renders");
    let step = 1.0 / 100.0;
    assert!(near(&got, &[-0.02 - step, -0.01 - step, -step]), "{got:?}");
}

/// A `sum` binds its index over its own term, so an inner sum naming the same index reads its
/// own: `sum(k, 1, 2, sum(k, 1, 3, k))` is 2*(1 + 2 + 3), never 3*(1 + 2).
#[test]
fn an_inner_sum_reads_its_own_index_where_an_outer_one_shares_its_name() {
    let peak = |body: &str| {
        let g = graph_of("shadowed", &[("root", body)]);
        let held = render(
            &g,
            "root",
            RenderConfig::seconds(8_000, 0.01),
            &Tier::default(),
        )
        .unwrap_or_else(|e| panic!("{body}: {e}"));
        let root = held.id("root").expect("the root");
        let samples = held.output(root).expect("samples").plane(0).to_vec();
        samples.iter().fold(0f64, |m, s| m.max(s.abs()))
    };
    let shadowed = peak("sum(k, 1, 2, sum(k, 1, 3, k))*sin(2*pi*100*t)\n");
    let distinct = peak("sum(j, 1, 2, sum(k, 1, 3, k))*sin(2*pi*100*t)\n");
    assert!((distinct - 12.0).abs() < 1e-3, "{distinct}");
    assert_eq!(shadowed, distinct, "the inner index is the inner sum's own");
}