update wasm test for set_object

This commit is contained in:
Orion Henry 2022-03-07 11:46:25 -05:00
parent 95f27f362c
commit beae33402a
3 changed files with 38 additions and 55 deletions
automerge-wasm

View file

@ -6,7 +6,8 @@ export type SyncMessage = Uint8Array;
export type Prop = string | number;
export type Hash = string;
export type Heads = Hash[];
export type Value = string | number | boolean | null | Date | Uint8Array | Array | Object;
export type Value = string | number | boolean | null | Date | Uint8Array
export type ObjType = string | Array | Object
export type FullValue =
["str", string] |
["int", number] |
@ -82,10 +83,12 @@ export function decodeSyncState(data: Uint8Array): SyncState;
export class Automerge {
// change state
set(obj: ObjID, prop: Prop, value: Value, datatype?: Datatype): ObjID | undefined;
make(obj: ObjID, prop: Prop, value: Value, datatype?: Datatype): ObjID;
insert(obj: ObjID, index: number, value: Value, datatype?: Datatype): ObjID | undefined;
push(obj: ObjID, value: Value, datatype?: Datatype): ObjID | undefined;
set(obj: ObjID, prop: Prop, value: Value, datatype?: Datatype): undefined;
set_object(obj: ObjID, prop: Prop, value: ObjType): ObjID;
insert(obj: ObjID, index: number, value: Value, datatype?: Datatype): undefined;
insert_object(obj: ObjID, index: number, value: ObjType): ObjID;
push(obj: ObjID, value: Value, datatype?: Datatype): undefined;
push_object(obj: ObjID, value: ObjType): ObjID;
splice(obj: ObjID, start: number, delete_count: number, text?: string | Array<Value>): ObjID[] | undefined;
inc(obj: ObjID, prop: Prop, value: number): void;
del(obj: ObjID, prop: Prop): void;

View file

@ -160,15 +160,10 @@ impl Automerge {
Ok(())
}
pub fn push_object(
&mut self,
obj: JsValue,
value: JsValue,
datatype: JsValue,
) -> Result<Option<String>, JsValue> {
pub fn push_object(&mut self, obj: JsValue, value: JsValue) -> Result<Option<String>, JsValue> {
let obj = self.import(obj)?;
let (value, subvals) = to_objtype(&value, &datatype.as_string())
.ok_or_else(|| to_js_err("expected object"))?;
let (value, subvals) =
to_objtype(&value, &None).ok_or_else(|| to_js_err("expected object"))?;
let index = self.0.length(&obj);
let opid = self.0.insert_object(&obj, index, value)?;
self.subset(&opid, subvals)?;
@ -196,12 +191,11 @@ impl Automerge {
obj: JsValue,
index: f64,
value: JsValue,
datatype: JsValue,
) -> Result<Option<String>, JsValue> {
let obj = self.import(obj)?;
let index = index as f64;
let (value, subvals) = to_objtype(&value, &datatype.as_string())
.ok_or_else(|| to_js_err("expected object"))?;
let (value, subvals) =
to_objtype(&value, &None).ok_or_else(|| to_js_err("expected object"))?;
let opid = self.0.insert_object(&obj, index as usize, value)?;
self.subset(&opid, subvals)?;
Ok(opid.to_string().into())
@ -228,12 +222,11 @@ impl Automerge {
obj: JsValue,
prop: JsValue,
value: JsValue,
datatype: JsValue,
) -> Result<JsValue, JsValue> {
let obj = self.import(obj)?;
let prop = self.import_prop(prop)?;
let (value, subvals) = to_objtype(&value, &datatype.as_string())
.ok_or_else(|| to_js_err("expected object"))?;
let (value, subvals) =
to_objtype(&value, &None).ok_or_else(|| to_js_err("expected object"))?;
let opid = self.0.set_object(&obj, prop, value)?;
self.subset(&opid, subvals)?;
Ok(opid.to_string().into())

View file

@ -64,7 +64,7 @@ describe('Automerge', () => {
doc.set(root, "bool", true)
doc.set(root, "time1", 1000, "timestamp")
doc.set(root, "time2", new Date(1001))
doc.set(root, "list", []);
doc.set_object(root, "list", []);
doc.set(root, "null", null)
result = doc.value(root,"hello")
@ -124,8 +124,7 @@ describe('Automerge', () => {
let root = "_root"
let result
let submap = doc.set(root, "submap", {})
if (!submap) throw new Error('should be not null')
let submap = doc.set_object(root, "submap", {})
doc.set(submap, "number", 6, "uint")
assert.strictEqual(doc.pendingOps(),2)
@ -141,8 +140,7 @@ describe('Automerge', () => {
let doc = create()
let root = "_root"
let submap = doc.set(root, "numbers", [])
if (!submap) throw new Error('should be not null')
let submap = doc.set_object(root, "numbers", [])
doc.insert(submap, 0, "a");
doc.insert(submap, 1, "b");
doc.insert(submap, 2, "c");
@ -165,8 +163,7 @@ describe('Automerge', () => {
let doc = create()
let root = "_root"
let submap = doc.set(root, "letters", [])
if (!submap) throw new Error('should be not null')
let submap = doc.set_object(root, "letters", [])
doc.insert(submap, 0, "a");
doc.insert(submap, 0, "b");
assert.deepEqual(doc.toJS(), { letters: ["b", "a" ] })
@ -230,7 +227,7 @@ describe('Automerge', () => {
let doc = create()
let root = "_root";
let text = doc.set(root, "text", "", "text");
let text = doc.set_object(root, "text", "");
if (!text) throw new Error('should not be undefined')
doc.splice(text, 0, 0, "hello ")
doc.splice(text, 6, 0, ["w","o","r","l","d"])
@ -282,8 +279,7 @@ describe('Automerge', () => {
it('should be able to splice text', () => {
let doc = create()
let text = doc.set("_root", "text", "", "text");
if (!text) throw new Error('should not be undefined')
let text = doc.set_object("_root", "text", "");
doc.splice(text, 0, 0, "hello world");
let heads1 = doc.commit();
doc.splice(text, 6, 0, "big bad ");
@ -331,8 +327,7 @@ describe('Automerge', () => {
it('local inc increments all visible counters in a sequence', () => {
let doc1 = create("aaaa")
let seq = doc1.set("_root", "seq", [])
if (!seq) throw new Error('Should not be undefined')
let seq = doc1.set_object("_root", "seq", [])
doc1.insert(seq, 0, "hello")
let doc2 = loadDoc(doc1.save(), "bbbb");
let doc3 = loadDoc(doc1.save(), "cccc");
@ -365,11 +360,11 @@ describe('Automerge', () => {
it('recursive sets are possible', () => {
let doc = create("aaaa")
let l1 = doc.make("_root","list",[{ foo: "bar"}, [1,2,3]])
let l2 = doc.insert(l1, 0, { zip: ["a", "b"] })
let l3 = doc.make("_root","info1","hello world") // 'text'
let l1 = doc.set_object("_root","list",[{ foo: "bar"}, [1,2,3]])
let l2 = doc.insert_object(l1, 0, { zip: ["a", "b"] })
let l3 = doc.set_object("_root","info1","hello world") // 'text'
let l4 = doc.set("_root","info2","hello world") // 'str'
let l5 = doc.set("_root","info3","hello world", "text")
let l5 = doc.set_object("_root","info3","hello world")
assert.deepEqual(doc.toJS(), {
"list": [ { zip: ["a", "b"] }, { foo: "bar"}, [ 1,2,3]],
"info1": "hello world".split(""),
@ -382,15 +377,14 @@ describe('Automerge', () => {
it('only returns an object id when objects are created', () => {
let doc = create("aaaa")
let r1 = doc.set("_root","foo","bar")
let r2 = doc.set("_root","list",[])
let r2 = doc.set_object("_root","list",[])
let r3 = doc.set("_root","counter",10, "counter")
let r4 = doc.inc("_root","counter",1)
let r5 = doc.del("_root","counter")
if (!r2) throw new Error('should not be undefined')
let r6 = doc.insert(r2,0,10);
let r7 = doc.insert(r2,0,{});
let r7 = doc.insert_object(r2,0,{});
let r8 = doc.splice(r2,1,0,["a","b","c"]);
let r9 = doc.splice(r2,1,0,["a",[],{},"d"]);
//let r9 = doc.splice(r2,1,0,["a",[],{},"d"]);
assert.deepEqual(r1,null);
assert.deepEqual(r2,"2@aaaa");
assert.deepEqual(r3,null);
@ -399,18 +393,15 @@ describe('Automerge', () => {
assert.deepEqual(r6,null);
assert.deepEqual(r7,"7@aaaa");
assert.deepEqual(r8,null);
assert.deepEqual(r9,["12@aaaa","13@aaaa"]);
//assert.deepEqual(r9,["12@aaaa","13@aaaa"]);
doc.free()
})
it('objects without properties are preserved', () => {
let doc1 = create("aaaa")
let a = doc1.set("_root","a",{});
if (!a) throw new Error('should not be undefined')
let b = doc1.set("_root","b",{});
if (!b) throw new Error('should not be undefined')
let c = doc1.set("_root","c",{});
if (!c) throw new Error('should not be undefined')
let a = doc1.set_object("_root","a",{});
let b = doc1.set_object("_root","b",{});
let c = doc1.set_object("_root","c",{});
let d = doc1.set(c,"d","dd");
let saved = doc1.save();
let doc2 = loadDoc(saved);
@ -427,7 +418,7 @@ describe('Automerge', () => {
it('should handle merging text conflicts then saving & loading', () => {
let A = create("aabbcc")
let At = A.make('_root', 'text', "", "text")
let At = A.set_object('_root', 'text', "")
A.splice(At, 0, 0, 'hello')
let B = A.fork()
@ -478,8 +469,7 @@ describe('Automerge', () => {
let s1 = initSyncState(), s2 = initSyncState()
// make two nodes with the same changes
let list = n1.set("_root","n", [])
if (!list) throw new Error('undefined')
let list = n1.set_object("_root","n", [])
n1.commit("",0)
for (let i = 0; i < 10; i++) {
n1.insert(list,i,i)
@ -502,8 +492,7 @@ describe('Automerge', () => {
let n1 = create(), n2 = create()
// make changes for n1 that n2 should request
let list = n1.set("_root","n",[])
if (!list) throw new Error('undefined')
let list = n1.set_object("_root","n",[])
n1.commit("",0)
for (let i = 0; i < 10; i++) {
n1.insert(list, i, i)
@ -519,8 +508,7 @@ describe('Automerge', () => {
let n1 = create(), n2 = create()
// make changes for n1 that n2 should request
let list = n1.set("_root","n",[])
if (!list) throw new Error('undefined')
let list = n1.set_object("_root","n",[])
n1.commit("",0)
for (let i = 0; i < 10; i++) {
n1.insert(list,i,i)
@ -676,8 +664,7 @@ describe('Automerge', () => {
let n1 = create('01234567'), n2 = create('89abcdef')
let s1 = initSyncState(), s2 = initSyncState(), message = null
let items = n1.set("_root", "items", [])
if (!items) throw new Error('undefined')
let items = n1.set_object("_root", "items", [])
n1.commit("",0)
sync(n1, n2, s1, s2)