Skip to content

Instantly share code, notes, and snippets.

@vedantroy
Created June 18, 2021 20:52
Show Gist options
  • Save vedantroy/06e7ce355fc90194f56fc60afe583e23 to your computer and use it in GitHub Desktop.
Save vedantroy/06e7ce355fc90194f56fc60afe583e23 to your computer and use it in GitHub Desktop.
test old rust / new JS compatability
const execSync = require("child_process").execSync;
const fs = require("fs")
const crypto = require("crypto")
const assert = require("assert")
function assertNewJsEqOldRust(doc) {
const bytes = Buffer.from(Automerge.save(doc)).toString("base64")
const tempFileName = `changes-${crypto.randomBytes(4).readUInt32LE(0)}.json`
const outFile = `/tmp/${tempFileName}`
//console.log(`Writing to: ${outFile}`)
execSync(`../automerge-rs/target/debug/round-trip ${outFile} ${bytes}`)
const newChanges = JSON.parse(fs.readFileSync(outFile)).map(c => new Uint8Array(c));
assert(Array.isArray(newChanges))
fs.unlinkSync(outFile)
const [finalNewDoc, _] = Automerge.applyChanges(Automerge.init(), newChanges);
assert.deepStrictEqual(finalNewDoc, doc)
}
let Automerge = require("./src/automerge");
let doc = Automerge.from({
hello: "world",
list: [ 1,2,3,4,5,6],
obj1: {
key: "val",
obj2: {
key: "val2",
obj3: {
key: "val3",
list: [ 1,2,3,4,5,6]
}
}
}
});
console.log("----");
assertNewJsEqOldRust(doc)
console.log(Buffer.from(Automerge.save(doc)).toString("base64"))
doc = Automerge.change(doc, (d) => {
d.list[3] = "a"
})
console.log("----");
assertNewJsEqOldRust(doc)
console.log(Buffer.from(Automerge.save(doc)).toString("base64"))
doc = Automerge.change(doc, (d) => {
d.list.push(4)
d.list.push(5)
d.list.push(6)
})
console.log("----");
assertNewJsEqOldRust(doc)
console.log(Buffer.from(Automerge.save(doc)).toString("base64"))
doc = Automerge.change(doc, (d) => {
d.list.unshift(0)
d.list.unshift(-1)
})
console.log("----");
assertNewJsEqOldRust(doc)
console.log(Buffer.from(Automerge.save(doc)).toString("base64"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment