Files
explicata/tests/lanes.test.ts
T
thalladaandClaude Opus 5.5 edc31aac9a Lanes: a few curves take the order that crosses least, of all orders
The unit test that failed about one run in six was the lanes property: moving
one curve at a time could stop at a local minimum (two crossings where an
order has none). Up to six curves in a gutter every order is now tried, a
strictly better one replacing the order there, so the bundle still keeps its
order while nothing crosses less. The property now asks for the least and
the counterexample it found is kept as a case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
2026-11-16 02:02:41 -05:00

57 lines
3.0 KiB
TypeScript

/** Lanes for the panes view's curves (src/ui/lanes.ts): the order crosses least, and stays put. */
import fc from "fast-check";
import { describe, expect, it } from "vitest";
import { crossings, laneOrder, LANES } from "../src/ui/lanes";
const e = (key: string, sy: number, ey: number) => ({ key, sy, ey });
const keys = (o: { key: string }[]) => o.map((x) => x.key);
/** The fewest crossings any order has. */
const least = <T extends { sy: number; ey: number }>(es: T[]): number => {
if (es.length < 2) return 0;
let best = Infinity;
const go = (left: T[], done: T[]) => {
if (!left.length) best = Math.min(best, crossings(done));
left.forEach((x, i) => go([...left.slice(0, i), ...left.slice(i + 1)], [...done, x]));
};
go(es, []);
return best;
};
describe("lanes", () => {
it("fit the gutter: seven at a 4 px pitch", () => {
expect(LANES).toBe(7);
});
it("rows fanning out to panes stacked below them do not cross: the one with least to go nearest the panes", () => {
// Three rows 35 px apart, opening three panes stacked down the next column.
const rows = [e("a", 300, 100), e("b", 335, 400), e("c", 370, 700)];
const o = laneOrder(rows);
expect(crossings(o)).toBe(0);
// The owner's screenshot: two rows to panes above them, and a link above them to a pane far below, whose run passes both sources (two crossings no order avoids).
const shot = [e("complex", 319, 73), e("shared", 354, 307), e("archaea", 140, 592)];
expect(crossings(laneOrder(shot))).toBe(least(shot));
});
it("never does worse than the rule it starts from, and keeps the last order unless a swap removes a crossing", () => {
fc.assert(
fc.property(fc.array(fc.tuple(fc.integer({ min: 0, max: 1000 }), fc.integer({ min: 0, max: 1000 })), { minLength: 1, maxLength: 9 }), (ys) => {
const es = ys.map(([a, b], i) => e(String(i), a, b));
const o = laneOrder(es);
const again = laneOrder(es, keys(o));
return o.length === es.length && new Set(keys(o)).size === es.length && keys(again).join() === keys(o).join() && (es.length > 6 || crossings(o) === least(es));
}),
{ numRuns: 300 },
);
// Found by the property above (it failed about one run in six): moving one curve at a time stopped at 2 crossings where an order has none.
const stuck = [e("0", 3, 1), e("1", 6, 2), e("2", 7, 4), e("3", 2, 0)];
expect(crossings(laneOrder(stuck))).toBe(0);
// A small scroll that changes no crossing keeps the lanes as they were.
const before = laneOrder([e("a", 300, 100), e("b", 335, 400), e("c", 370, 700)]);
const after = laneOrder([e("a", 290, 100), e("b", 325, 400), e("c", 360, 700)], keys(before));
expect(keys(after)).toEqual(keys(before));
// A new curve goes where the rule puts it; the others keep their order.
const more = laneOrder([e("a", 300, 100), e("b", 335, 400), e("c", 370, 700), e("d", 500, 520)], keys(before));
expect(keys(more).filter((k) => k !== "d")).toEqual(keys(before));
});
});