Add library tools to local toolboxes
This commit is contained in:
parent
daa5eba8bd
commit
ec7a4a4c1d
15 changed files with 650 additions and 9 deletions
135
tests/library-copy.test.mjs
Normal file
135
tests/library-copy.test.mjs
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
// Rôle : vérifie la copie transactionnelle et indépendante des exemples de librairie.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { useToolboxActions } from "../website/src/features/toolboxes/useToolboxActions.js";
|
||||
|
||||
function toolbox(id = "destination") {
|
||||
return {
|
||||
id,
|
||||
name: "Destination",
|
||||
modules: [],
|
||||
spaces: [
|
||||
{ id: "main", name: "Principal", moduleOrder: { one: [], two: [[], []] } },
|
||||
{ id: "build", name: "Build", moduleOrder: { one: [], two: [[], []] } }
|
||||
],
|
||||
defaultSpaceId: "main",
|
||||
activeSpaceId: "main",
|
||||
updatedAt: "2026-10-22T08:00:00.000Z"
|
||||
};
|
||||
}
|
||||
|
||||
function createHarness(initial, { fail = false } = {}) {
|
||||
let state = structuredClone(initial);
|
||||
let writes = 0;
|
||||
const actions = useToolboxActions({
|
||||
store: {
|
||||
ready: true,
|
||||
persistToolboxSnapshot: async (updater) => {
|
||||
writes += 1;
|
||||
const next = updater(state);
|
||||
if (fail) return false;
|
||||
state = structuredClone(next);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
notify() {}
|
||||
});
|
||||
return { actions, getState: () => state, getWrites: () => writes };
|
||||
}
|
||||
|
||||
test("a library example copies fresh ids into the selected space and remains independent", async () => {
|
||||
const harness = createHarness({ toolboxes: [toolbox()], links: {}, moduleData: {} });
|
||||
const sourceData = { counters: [{ id: "library-counter", label: "Essais", value: 4 }] };
|
||||
const result = await harness.actions.addLibraryToolToToolbox({
|
||||
sourceModule: { id: "library-module", type: "counters", data: sourceData },
|
||||
sourceData,
|
||||
toolboxId: "destination",
|
||||
spaceId: "build",
|
||||
copyData: true
|
||||
});
|
||||
|
||||
assert.ok(result);
|
||||
const storedToolbox = harness.getState().toolboxes[0];
|
||||
const storedModule = storedToolbox.modules.find((module) => module.type === "counters");
|
||||
const storedData = harness.getState().moduleData[`destination:${storedModule.id}`];
|
||||
assert.notEqual(storedModule.id, "library-module");
|
||||
assert.notEqual(storedData.counters[0].id, "library-counter");
|
||||
assert.equal(storedData.counters[0].value, 4);
|
||||
assert.deepEqual(storedToolbox.spaces.find((space) => space.id === "build").moduleOrder.one, [storedModule.id]);
|
||||
assert.equal(storedToolbox.activeSpaceId, "build");
|
||||
|
||||
sourceData.counters[0].value = 99;
|
||||
assert.equal(harness.getState().moduleData[`destination:${storedModule.id}`].counters[0].value, 4);
|
||||
|
||||
const emptyResult = await harness.actions.addLibraryToolToToolbox({
|
||||
sourceModule: { id: "library-empty", type: "counters", data: sourceData },
|
||||
sourceData,
|
||||
toolboxId: "destination",
|
||||
spaceId: "main",
|
||||
copyData: false
|
||||
});
|
||||
const emptyModuleId = emptyResult.moduleIds[0];
|
||||
assert.equal(harness.getState().moduleData[`destination:${emptyModuleId}`], undefined);
|
||||
assert.deepEqual(harness.getState().toolboxes[0].spaces.find((space) => space.id === "main").moduleOrder.one, [emptyModuleId]);
|
||||
});
|
||||
|
||||
test("a linked memo copies its planner and remaps every business reference", async () => {
|
||||
const harness = createHarness({ toolboxes: [toolbox()], links: {}, moduleData: {} });
|
||||
const result = await harness.actions.addLibraryToolToToolbox({
|
||||
sourceModule: {
|
||||
id: "library-memos",
|
||||
type: "memos",
|
||||
data: { plannerLink: "library-planner", items: [{ id: "library-memo", text: "Raid", color: "#f6c453" }] }
|
||||
},
|
||||
sourceData: { plannerLink: "library-planner" },
|
||||
dependencyModules: [{
|
||||
id: "library-planner",
|
||||
type: "taskPlanner",
|
||||
data: {
|
||||
tasks: [{ id: "library-task-a", title: "Parent", type: "unique" }, { id: "library-task-b", title: "Enfant", type: "daily" }],
|
||||
relations: [{ id: "library-relation", fromTaskId: "library-task-b", toTaskId: "library-task-a" }]
|
||||
}
|
||||
}],
|
||||
toolboxId: "destination",
|
||||
spaceId: "main",
|
||||
copyData: true
|
||||
});
|
||||
|
||||
assert.equal(result.moduleIds.length, 2);
|
||||
const stored = harness.getState();
|
||||
const memosModule = stored.toolboxes[0].modules.find((module) => module.type === "memos");
|
||||
const plannerModule = stored.toolboxes[0].modules.find((module) => module.type === "taskPlanner");
|
||||
const memosData = stored.moduleData[`destination:${memosModule.id}`];
|
||||
const plannerData = stored.moduleData[`destination:${plannerModule.id}`];
|
||||
assert.equal(memosData.plannerLink, plannerModule.id);
|
||||
assert.notEqual(memosData.items[0].id, "library-memo");
|
||||
assert.notEqual(plannerData.tasks[0].id, "library-task-a");
|
||||
assert.equal(plannerData.relations[0].fromTaskId, plannerData.tasks[1].id);
|
||||
assert.equal(plannerData.relations[0].toTaskId, plannerData.tasks[0].id);
|
||||
});
|
||||
|
||||
test("creating a first toolbox continues the copy, while a failed write confirms nothing", async () => {
|
||||
const initial = { toolboxes: [], links: {}, moduleData: {} };
|
||||
const createdHarness = createHarness(initial);
|
||||
const created = await createdHarness.actions.addLibraryToolToToolbox({
|
||||
sourceModule: { id: "library-counter", type: "counters", data: { counters: [{ id: "counter", label: "Test", value: 1 }] } },
|
||||
sourceData: { counters: [{ id: "counter", label: "Test", value: 1 }] },
|
||||
newToolboxName: "Depuis la librairie",
|
||||
copyData: true
|
||||
});
|
||||
assert.ok(created);
|
||||
assert.equal(createdHarness.getState().toolboxes[0].name, "Depuis la librairie");
|
||||
assert.deepEqual(createdHarness.getState().toolboxes[0].modules.map((module) => module.type), ["memos", "links", "counters"]);
|
||||
|
||||
const failedHarness = createHarness({ toolboxes: [toolbox()], links: {}, moduleData: {} }, { fail: true });
|
||||
const failed = await failedHarness.actions.addLibraryToolToToolbox({
|
||||
sourceModule: { id: "library-counter", type: "counters", data: {} },
|
||||
sourceData: {},
|
||||
toolboxId: "destination",
|
||||
spaceId: "main",
|
||||
copyData: false
|
||||
});
|
||||
assert.equal(failed, null);
|
||||
assert.equal(failedHarness.getWrites(), 1);
|
||||
assert.deepEqual(failedHarness.getState(), { toolboxes: [toolbox()], links: {}, moduleData: {} });
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue