Files
superGeometry e2711937c3 Stress tests aimed elsewhere, and what they found. A fuzz of the rebuild: random steps added the way the app adds them (taps from the last rebuild), then an early size changed and back, each step switched off and deleted - built warm (results kept) and from nothing, the two the same, every time (30 seeds, 850 checks: the cache held). Its refused steps found the bugs. Edit Freeform steps had no test: now edits stacked, switched off, rolled back to, with a subtract and a linked copy between, each the same as the cage made that way straight away.
Fixed:
- A face pushed out after an earlier step tilted it, then that tilt changed: the push became a slide - a sheared move, or "Sliding or turning a face within its plane..." on a body with a round face. A move that was a push when made stays a push, square to the face as it is now; a tilt about a line in the face stays that.
- The earlier tilt turned to 0 or switched off: the push lost its face ("The face it was built on is gone"). When the step that made a face makes none now, the face that is plainly the same one (facing the same way, its middle where it was, much the same size) is taken.
- Move Face on a shape projected onto a face (Project as Edge) did nothing and said nothing: the faces round it lie in its plane. Pushed out it now raises it (a boss), in sinks it (a pocket), following the sketch when it moves or grows; tilting it says why it can't.
- Project as Edge from a sketch on a block's top split the bottom too, out of sight behind the block: a line running wholly inside the body was taken as seen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 12:39:07 +02:00

111 lines
6.4 KiB
TypeScript

// Run: npx tsx tests/freeformedit.test.ts - Edit Freeform steps: a freeform's
// cage changed later in History, as a step of its own. Whatever is around it,
// the shape must be the one made with the edited cage straight away: edits
// stacked (the later one wins), with steps between (a subtract, a linked
// copy), switched off, rolled back to, deleted, the freeform itself switched
// off or deleted (the edit then says so). Each rebuild warm (results kept from
// the last) the same as built from nothing.
import { check, done, near, sketch, rect, XY, profile } from "./kit";
import { debugBodies, regenerate, resetKernel } from "../src/kernel/engine";
import { measureVolume, type Shape3D } from "replicad";
import { boxCage, planeCage, type Cage } from "../src/freeform/cage";
import type { Feature, Vec3 } from "../src/doc/types";
import type { RegenResult } from "../src/kernel/protocol";
const S = 30;
const cage0 = boxCage([S, S, S], 2, [0, 0, S / 2]);
/** The cage with its points at the top middle (and, with `side`, the +x middle) pulled out. */
function pulled(c: Cage, up: number, side = 0): Cage {
const verts = c.verts.map((p): Vec3 => {
if (near(p[2], S * 1.5) && near(p[0], 0) && near(p[1], 0)) return [p[0], p[1], p[2] + up];
if (side && near(p[0], S / 2) && near(p[1], 0) && near(p[2], S)) return [p[0] + side, p[1], p[2]];
return p;
});
return { verts, faces: c.faces };
}
const c1 = pulled(cage0, 10);
const c2 = pulled(cage0, 10, 8);
check("the cages differ where they should", JSON.stringify(c1) !== JSON.stringify(cage0) && JSON.stringify(c2) !== JSON.stringify(c1));
const ff = (cage: Cage): Feature => ({ id: "ff", name: "Freeform 01", type: "freeform", cage } as Feature);
const edit = (id: string, cage: Cage, extra: Record<string, unknown> = {}): Feature => ({ id, name: id, type: "freeformEdit", target: "ff", patch: { cage }, ...extra } as Feature);
const es = [rect("r", 5, -20, 30, 20)];
const cutter: Feature[] = [sketch("kb", XY(10), es), { id: "blk", name: "Block", type: "extrude", source: profile("kb", es), distance: 40, mode: "new" } as Feature];
const sub: Feature = { id: "sub", name: "Subtract", type: "boolean", op: "subtract", target: "ff", tools: ["blk"] } as Feature;
const copy: Feature = { id: "cp", name: "Copy", type: "transform", bodies: ["ff"], copy: true, link: true, op: { kind: "move", translate: [60, 0, 0] } } as Feature;
/** Each body's volume (and the failing steps), in order. */
function print(r: RegenResult): string {
const v = [...debugBodies()].sort((a, b) => (a[0] < b[0] ? -1 : 1)).map(([id, s]) => `${id}:${Math.abs(measureVolume(s as Shape3D)).toPrecision(9)}`);
return v.join(" ") + " | " + r.errors.map((e) => e.featureId).sort().join(",");
}
const copyOf = (fs: Feature[]) => JSON.parse(JSON.stringify(fs)) as Feature[];
const warm = async (fs: Feature[]) => {
const r = await regenerate(copyOf(fs));
return { r, p: print(r) };
};
const cold = async (fs: Feature[]) => {
resetKernel();
return warm(fs);
};
/** Built warm after `prev`, and from nothing: the same, and `want` when given. */
async function same(label: string, prev: Feature[], fs: Feature[], want?: Feature[]) {
await warm(prev);
const w = await warm(fs);
const c = await cold(fs);
check(`${label}: warm = from nothing`, w.p === c.p, { warm: w.p, cold: c.p });
if (want) {
const x = await cold(want);
check(`${label}: as made with that cage straight away`, w.p.split(" | ")[0] === x.p.split(" | ")[0], { got: w.p, want: x.p });
}
return w;
}
const plain = await cold([ff(cage0)]);
const pulledUp = await cold([ff(c1)]);
const vol = (p: string) => Number(p.split(" | ")[0].split(":")[1]);
check("pulled up, the freeform is bigger", vol(pulledUp.p) > vol(plain.p) + 100, [plain.p, pulledUp.p]);
// One edit, then a second on top (the later cage wins).
await same("one edit", [ff(cage0)], [ff(cage0), edit("e1", c1)], [ff(c1)]);
await same("two edits", [ff(cage0), edit("e1", c1)], [ff(cage0), edit("e1", c1), edit("e2", c2)], [ff(c2)]);
// With a subtract and a linked copy between the freeform and its edits.
const mid = [ff(cage0), ...cutter, sub, copy];
await same("subtract and copy, then an edit", mid, [...mid, edit("e1", c1)], [ff(c1), ...cutter, sub, copy]);
const both = [...mid, edit("e1", c1), edit("e2", c2)];
await same("... and a second edit", [...mid, edit("e1", c1)], both, [ff(c2), ...cutter, sub, copy]);
// The second edit switched off, rolled back to, deleted: the first edit's shape.
const want1 = [ff(c1), ...cutter, sub, copy];
await same("second edit off", both, both.map((f) => (f.id === "e2" ? { ...f, suppressed: true } : f)), want1);
await same("rolled back to before the second edit", both, both.map((f) => (f.id === "e2" ? { ...f, rolledBack: true } : f)), want1);
await same("second edit deleted", both, both.filter((f) => f.id !== "e2"), want1);
// The first one off: the second's cage (it holds the first's change too).
await same("first edit off", both, both.map((f) => (f.id === "e1" ? { ...f, suppressed: true } : f)), [ff(c2), ...cutter, sub, copy]);
// Both off, then on again.
const off = both.map((f) => (f.type === "freeformEdit" ? { ...f, suppressed: true } : f));
await same("both edits off", both, off, mid);
await same("both on again", off, both, [ff(c2), ...cutter, sub, copy]);
// The freeform itself switched off or deleted: its edits say so (warm too).
for (const [what, list] of [
["the freeform off", both.map((f) => (f.id === "ff" ? { ...f, suppressed: true } : f))],
["the freeform deleted", both.filter((f) => f.id !== "ff")],
] as const) {
await warm(both);
const w = await warm(list as Feature[]);
const c = await cold(list as Feature[]);
const said = new Set(w.r.errors.map((e) => e.featureId));
check(`${what}: both edits say the freeform is gone`, said.has("e1") && said.has("e2"), w.r.errors.map((e) => `${e.featureId}: ${e.message}`));
check(`${what}: warm = from nothing`, w.p === c.p, { warm: w.p, cold: c.p });
}
// A sheet given a thickness by an edit (not by its own step).
const sheet: Feature = { id: "ff", name: "Sheet", type: "freeform", cage: planeCage([40, 40], 4, [0, 0, 0]), surface: true } as Feature;
const thick = { ...sheet, thickness: 2 } as Feature;
await same("a sheet made 2 thick by an edit", [sheet], [sheet, { id: "e1", name: "e1", type: "freeformEdit", target: "ff", patch: { thickness: 2 } } as Feature], [thick]);
const th = await cold([thick]);
check("... 2 thick: 40 x 40 x 2", near(vol(th.p), 3200, 1), th.p);
done();