Files
dragology-pd/test/pd.test.ts
T
david fe6f2bfe76 Browser patch editor for Pure Data (closes #1)
Open a .pd file, drag boxes and pull cords, save it back as a file Pd
will load. Dragging is Dragology: `d.vary` moves a box, and a cord drag
is `d.closest(...).whenFar(vary)` over every inlet in the patch, so the
plug snaps to an inlet and cancels on empty space.

The parser is a byte-exact round trip. Two Pd format details that matter:
`#X connect` indices count comments, and `;;` (an empty record) is not the
same as a blank line. Anything unmodelled — arrays, `#X declare`,
subpatches — is preserved verbatim.

Tests run real Pd: patches round trip, an edited patch loads in
`pd -nogui` without new complaints, and the bridge carries a message into
a running Pd and back.

The bridge writes to server/patches/ and runs `pd -nogui` itself, so
saving and hearing the patch is one button.
2026-11-04 19:19:53 -08:00

90 lines
2.8 KiB
TypeScript

import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import test from "node:test";
import { parsePd, splitAtoms, splitRecords } from "../src/pd/parse.ts";
import { nodePosition, serializePd } from "../src/pd/serialize.ts";
const SYNTH_PD = "/home/david/Arduino/sketches/uno-q-audio-synth/pd";
test("records split on unescaped semicolons only", () => {
assert.deepEqual(splitRecords("#X msg 0 0 a \\; b;"), ["#X msg 0 0 a \\; b"]);
});
test("atoms unescape", () => {
assert.deepEqual(splitAtoms("#X msg 0 0 a\\;b \\, \\$0"), [
"#X",
"msg",
"0",
"0",
"a;b",
",",
"$0",
]);
});
const patch = `#N canvas 0 0 400 300 10;
#X text 10 10 a comment;
#X obj 50 50 osc~ 440;
#X obj 50 90 dac~;
#X obj 120 50 \\;does\\,not\\$exist;
#X connect 1 0 2 0;
#X connect 1 0 2 1;
`;
test("comments occupy an index in the connect space", () => {
const parsed = parsePd(patch);
const cords = Object.values(parsed.cords);
assert.equal(cords.length, 2);
for (const cord of cords) {
assert.equal(parsed.nodes[cord.from].text, "osc~ 440");
assert.equal(parsed.nodes[cord.to].text, "dac~");
}
});
test("round trip is byte exact", () => {
const parsed = parsePd(patch);
assert.equal(serializePd(parsed), patch);
});
test("moving a node only changes its coordinates", () => {
const parsed = parsePd(patch);
const node = Object.values(parsed.nodes).find((n) => n.text === "osc~ 440");
assert.ok(node);
node.x = 200;
node.y = 210;
const out = serializePd(parsed);
assert.equal(out, patch.replace("#X obj 50 50 osc~ 440", "#X obj 200 210 osc~ 440"));
});
test("escaped atoms survive a move", () => {
const parsed = parsePd(patch);
const node = Object.values(parsed.nodes).find((n) => n.text === ";does,not$exist");
assert.ok(node, "escaped object text parsed");
node.y = 300;
assert.match(serializePd(parsed), /#X obj 120 300 \\;does\\,not\\\$exist;/);
});
test("every real patch in the synth project round trips", (t) => {
let files: string[];
try {
files = readdirSync(SYNTH_PD).filter((f) => f.endsWith(".pd"));
} catch {
t.skip("synth project not present");
return;
}
assert.ok(files.length > 0);
for (const file of files) {
const text = readFileSync(join(SYNTH_PD, file), "utf8");
const parsed = parsePd(text, file);
assert.equal(serializePd(parsed), text, `${file} round trip`);
// Every cord must resolve to a real object, never a comment.
for (const cord of Object.values(parsed.cords)) {
assert.notEqual(parsed.nodes[cord.from].kind, "text", `${file}: cord from comment`);
assert.notEqual(parsed.nodes[cord.to].kind, "text", `${file}: cord to comment`);
assert.ok(nodePosition(parsed, cord.from) >= 0);
}
}
});