41 lines
1.9 KiB
JavaScript
41 lines
1.9 KiB
JavaScript
// Rôle : vérifie la fusion des ajouts d'images pendant une compression asynchrone.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { useToolboxActions } from "../website/src/features/toolboxes/useToolboxActions.js";
|
|
|
|
test("concurrent image batches preserve intervening edits and completed additions", async (t) => {
|
|
const descriptors = Object.fromEntries(["document", "createImageBitmap"].map((key) => [
|
|
key, Object.getOwnPropertyDescriptor(globalThis, key)
|
|
]));
|
|
t.after(() => {
|
|
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
|
|
else delete globalThis[key];
|
|
}
|
|
});
|
|
const pending = new Map();
|
|
globalThis.createImageBitmap = (file) => new Promise((resolve) => pending.set(file.name, resolve));
|
|
globalThis.document = {
|
|
createElement: () => ({
|
|
getContext: () => ({ drawImage() {} }),
|
|
toDataURL: () => "data:image/jpeg;base64,dGVzdA=="
|
|
})
|
|
};
|
|
let current = { images: [{ id: "old", label: "Old" }] };
|
|
const actions = useToolboxActions({
|
|
store: {
|
|
getModuleData: () => current,
|
|
updateModuleData: (_toolboxId, _moduleId, value) => { current = value; return true; }
|
|
}
|
|
});
|
|
const first = actions.addImageFiles("toolbox", "images", [{ name: "First.png", type: "image/png" }]);
|
|
const second = actions.addImageFiles("toolbox", "images", [{ name: "Second.png", type: "image/png" }]);
|
|
current = { images: [{ id: "old", label: "Renamed" }], annotation: "Keep me" };
|
|
pending.get("Second.png")({ width: 10, height: 10 });
|
|
assert.equal(await second, true);
|
|
pending.get("First.png")({ width: 10, height: 10 });
|
|
assert.equal(await first, true);
|
|
assert.deepEqual(current.images.map((image) => image.label), ["First", "Second", "Renamed"]);
|
|
assert.equal(current.annotation, "Keep me");
|
|
assert.equal(await actions.addImageFiles("toolbox", "images", [{ type: "text/plain" }]), false);
|
|
});
|