Files
suaveplan/packages/media/audio/control-surface-schema/src/walk/walk.test.ts
2026-05-27 23:47:53 +01:00

78 lines
2.5 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { flatTransform, makeSurface } from "../__fixtures__/fixtures.js";
import type {
Control,
ControlNode,
Group,
} from "../types/control-node.types.js";
import { walkSurface } from "./walk.js";
function group(id: string, children: ReadonlyArray<ControlNode>): Group {
return {
type: "group",
id,
layout: { kind: "absolute" },
transform: flatTransform(),
children,
};
}
function control(id: string): Control {
return { type: "control", id, kind: "knob", transform: flatTransform() };
}
describe("walkSurface", () => {
it("visits a single-root surface once with empty parent stack", {
timeout: 5000,
}, () => {
const root = group("root", []);
const visited: Array<{ id: string; parents: string[] }> = [];
walkSurface(makeSurface(root), (node, parents) =>
visited.push({ id: node.id, parents: parents.map((p) => p.id) })
);
expect(visited).toEqual([{ id: "root", parents: [] }]);
});
it("visits root → A → [B, C → [D]] → E in DFS pre-order", {
timeout: 5000,
}, () => {
const tree = group("root", [
group("A", [control("B"), group("C", [control("D")])]),
control("E"),
]);
const ids: string[] = [];
walkSurface(makeSurface(tree), (n) => ids.push(n.id));
expect(ids).toEqual(["root", "A", "B", "C", "D", "E"]);
});
it("passes the root-first parent stack to every visit", {
timeout: 5000,
}, () => {
const tree = group("root", [group("A", [control("B")])]);
const stacks = new Map<string, string[]>();
walkSurface(makeSurface(tree), (node, parents) => {
stacks.set(
node.id,
parents.map((p) => p.id)
);
});
expect(stacks.get("root")).toEqual([]);
expect(stacks.get("A")).toEqual(["root"]);
expect(stacks.get("B")).toEqual(["root", "A"]);
});
it("visits nine nodes when a group contains two groups with three controls each", {
timeout: 5000,
}, () => {
const tree = group("root", [
group("g1", [control("c1"), control("c2"), control("c3")]),
group("g2", [control("c4"), control("c5"), control("c6")]),
]);
let count = 0;
walkSurface(makeSurface(tree), () => {
count += 1;
});
expect(count).toBe(9);
});
});