176 lines
4.6 KiB
JavaScript
176 lines
4.6 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { planAssemblyUpdate } from "../src/scene/assembly-update.ts";
|
|
|
|
function component(instanceId, componentCode, version = 1) {
|
|
return {
|
|
instanceId,
|
|
component: { componentCode, componentName: componentCode, version },
|
|
};
|
|
}
|
|
|
|
function match(components, rootInstanceId, connections = []) {
|
|
return {
|
|
components,
|
|
assembly: { rootInstanceId, connections },
|
|
};
|
|
}
|
|
|
|
function connection(parentInstanceId, childInstanceId, parentFrame = "mount") {
|
|
return {
|
|
parentInstanceId,
|
|
childInstanceId,
|
|
parentFrame,
|
|
childFrame: "root",
|
|
};
|
|
}
|
|
|
|
test("retains identical component revisions even when response instance IDs change", () => {
|
|
const current = match([component("old-root", "torso")], "old-root");
|
|
const next = match([component("new-root", "torso")], "new-root");
|
|
|
|
const plan = planAssemblyUpdate(current, next);
|
|
|
|
assert.deepEqual(
|
|
plan.retained.map(({ current: oldItem, next: newItem }) => [
|
|
oldItem.instanceId,
|
|
newItem.instanceId,
|
|
]),
|
|
[["old-root", "new-root"]],
|
|
);
|
|
assert.deepEqual(plan.replaced, []);
|
|
assert.deepEqual(plan.added, []);
|
|
assert.deepEqual(plan.removed, []);
|
|
});
|
|
|
|
test("pairs a component version change in the same assembly slot as a replacement", () => {
|
|
const current = match(
|
|
[component("torso", "torso"), component("old-hand", "hand", 1)],
|
|
"torso",
|
|
[connection("torso", "old-hand")],
|
|
);
|
|
const next = match(
|
|
[component("torso-next", "torso"), component("new-hand", "hand", 2)],
|
|
"torso-next",
|
|
[connection("torso-next", "new-hand")],
|
|
);
|
|
|
|
const plan = planAssemblyUpdate(current, next);
|
|
|
|
assert.equal(plan.retained.length, 1);
|
|
assert.deepEqual(
|
|
plan.replaced.map(({ current: oldItem, next: newItem }) => [
|
|
oldItem.instanceId,
|
|
newItem.instanceId,
|
|
]),
|
|
[["old-hand", "new-hand"]],
|
|
);
|
|
});
|
|
|
|
test("classifies additions and removals without inventing replacement pairs", () => {
|
|
const current = match([component("torso", "torso")], "torso");
|
|
const added = match(
|
|
[component("torso-next", "torso"), component("hand", "hand")],
|
|
"torso-next",
|
|
);
|
|
const removed = match([], null);
|
|
|
|
assert.deepEqual(
|
|
planAssemblyUpdate(current, added).added.map((item) => item.instanceId),
|
|
["hand"],
|
|
);
|
|
assert.deepEqual(
|
|
planAssemblyUpdate(current, removed).removed.map((item) => item.instanceId),
|
|
["torso"],
|
|
);
|
|
});
|
|
|
|
test("matches duplicate component instances by connection slot before order", () => {
|
|
const current = match(
|
|
[
|
|
component("torso", "torso"),
|
|
component("left-old", "hand"),
|
|
component("right-old", "hand"),
|
|
],
|
|
"torso",
|
|
[
|
|
connection("torso", "left-old", "left_mount"),
|
|
connection("torso", "right-old", "right_mount"),
|
|
],
|
|
);
|
|
const next = match(
|
|
[
|
|
component("torso-next", "torso"),
|
|
component("right-new", "hand"),
|
|
component("left-new", "hand"),
|
|
],
|
|
"torso-next",
|
|
[
|
|
connection("torso-next", "right-new", "right_mount"),
|
|
connection("torso-next", "left-new", "left_mount"),
|
|
],
|
|
);
|
|
|
|
const plan = planAssemblyUpdate(current, next);
|
|
|
|
assert.deepEqual(
|
|
plan.retained.map(({ current: oldItem, next: newItem }) => [
|
|
oldItem.instanceId,
|
|
newItem.instanceId,
|
|
]),
|
|
[
|
|
["torso", "torso-next"],
|
|
["right-old", "right-new"],
|
|
["left-old", "left-new"],
|
|
],
|
|
);
|
|
});
|
|
|
|
test("replaces descendants whose parent changed and keeps unaffected siblings", () => {
|
|
const current = match(
|
|
[
|
|
component("root-old", "root"),
|
|
component("leg-old", "leg", 1),
|
|
component("foot-old", "foot"),
|
|
component("hand-old", "hand"),
|
|
],
|
|
"root-old",
|
|
[
|
|
connection("root-old", "leg-old", "leg_mount"),
|
|
connection("leg-old", "foot-old", "foot_mount"),
|
|
connection("root-old", "hand-old", "hand_mount"),
|
|
],
|
|
);
|
|
const next = match(
|
|
[
|
|
component("root-next", "root"),
|
|
component("leg-new", "leg", 2),
|
|
component("foot-next", "foot"),
|
|
component("hand-next", "hand"),
|
|
],
|
|
"root-next",
|
|
[
|
|
connection("root-next", "leg-new", "leg_mount"),
|
|
connection("leg-new", "foot-next", "foot_mount"),
|
|
connection("root-next", "hand-next", "hand_mount"),
|
|
],
|
|
);
|
|
|
|
const plan = planAssemblyUpdate(current, next);
|
|
|
|
assert.deepEqual(
|
|
plan.replaced.map(({ current: oldItem, next: newItem }) => [
|
|
oldItem.instanceId,
|
|
newItem.instanceId,
|
|
]),
|
|
[
|
|
["leg-old", "leg-new"],
|
|
["foot-old", "foot-next"],
|
|
],
|
|
);
|
|
assert.deepEqual(
|
|
plan.retained.map(({ current: oldItem }) => oldItem.instanceId),
|
|
["root-old", "hand-old"],
|
|
);
|
|
});
|