From b66652a16b66bbdb28c1449384054c25813e2d06 Mon Sep 17 00:00:00 2001 From: Sebastien DUMETZ Date: Tue, 23 Jan 2024 16:44:01 +0100 Subject: [PATCH] use dereferenced versions of merge tools --- .../server/routes/scenes/put/document.test.ts | 60 ++++++++++++++++++- source/server/routes/scenes/put/document.ts | 11 ++-- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/source/server/routes/scenes/put/document.test.ts b/source/server/routes/scenes/put/document.test.ts index bf73caa7..bde38812 100644 --- a/source/server/routes/scenes/put/document.test.ts +++ b/source/server/routes/scenes/put/document.test.ts @@ -63,7 +63,7 @@ describe("PUT /scenes/:scene/scene.svx.json", function(){ expect(JSON.parse(data)).to.deep.equal(sampleDoc); }); - it("uses the structured scene merge algorithm when possible", async function(){ + it("uses a merge algorithm when possible", async function(){ //Insert a change that happened between checkout and commit let currentDoc = JSON.parse(sampleDocString); @@ -88,4 +88,62 @@ describe("PUT /scenes/:scene/scene.svx.json", function(){ expect(data.metas[0].collection.titles["FR"]).to.equal("Titre 1"); }); + it("performs structured merge on the document", async function(){ + //This is a slightly less trivial case where we check if proper deduplication is applied + //Insert a change that happened between checkout and commit + + let currentDoc = JSON.parse(sampleDocString); + currentDoc.nodes.push({ + "name": "Model 1", + "model": 1, + }); + currentDoc.models.push({ + "units": "mm", + "derivatives": [ + { + "usage": "Web3D", + "quality": "High", + "assets": [{ + "uri": "models/model1.glb", + "type": "Model", + } + ] + } + ] + }); + await vfs.writeDoc(JSON.stringify(currentDoc), scene_id, user.uid); + + + //Make our user reference the first doc generation + sampleDoc.asset.id = 1; + sampleDoc.nodes.push({ + "name": "Model 2", + "model": 1, + }); + sampleDoc.models.push({ + "units": "mm", + "derivatives": [ + { + "usage": "Web3D", + "quality": "High", + "assets": [{ + "uri": "models/model2.glb", + "type": "Model", + } + ] + } + ] + }); + let r = await request(this.server).put(`/scenes/${titleSlug}/scene.svx.json`) + .auth("bob", "12345678") + .set("Content-Type", "application/si-dpo-3d.document+json") + .send(sampleDoc) + .expect(204); + + let {ctime, mtime, data:docString, id, ...doc} = await vfs.getDoc(scene_id); + const data = JSON.parse(docString); + expect(data.models).to.have.length(3); + expect(data.nodes).to.have.length(6); + }); + }); diff --git a/source/server/routes/scenes/put/document.ts b/source/server/routes/scenes/put/document.ts index fab5648e..f92f3dde 100644 --- a/source/server/routes/scenes/put/document.ts +++ b/source/server/routes/scenes/put/document.ts @@ -1,4 +1,4 @@ - +import {inspect} from "util"; import path from "path"; import { Request, Response } from "express"; @@ -48,14 +48,15 @@ export default async function handlePutDocument(req :Request, res :Response){ const {data: refDocString} = await tr.getDocById(refId); const refDoc = JSON.parse(refDocString); - const docDiff = merge.diff(refDoc, newDoc); + const docDiff = merge.diffDoc(refDoc, newDoc); if(Object.keys(docDiff).length == 0){ - console.log("Nothing to do"); + console.log("PUT document: Nothing to do"); //Nothing to do return res.status(204).send(); } - console.log("Merge changes : ", JSON.stringify(docDiff)); - const mergedDoc = merge.apply(currentDoc, docDiff); + + console.log("PUT document (merge): ", JSON.stringify(docDiff, null, 2)); + const mergedDoc = merge.applyDoc(currentDoc, docDiff); let s = JSON.stringify(mergedDoc); if(s == "{}") throw new BadRequestError(`Invalid json document`); let id = await tr.writeDoc(s, scene, uid);