77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package platform
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestMigrateDissolveRestoresContentAndEdges(t *testing.T) {
|
|
raw := json.RawMessage(legacyMigrationFixture)
|
|
parent, children, err := migrateLegacyDocument(raw, map[string]string{"f": "11111111-1111-4111-8111-111111111111", "empty": "22222222-2222-4222-8222-222222222222"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
restored, empty, err := dissolveDocuments(parent, children["f"], "f")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
originalNodes := canvasNodesByID(raw)
|
|
restoredNodes := canvasNodesByID(restored)
|
|
for _, id := range []string{"a", "b", "outside"} {
|
|
original := originalNodes[id]
|
|
delete(original["data"].(map[string]any), "frameId")
|
|
if !reflect.DeepEqual(original, restoredNodes[id]) {
|
|
t.Fatalf("node %s not preserved", id)
|
|
}
|
|
}
|
|
if restoredNodes["f"] != nil {
|
|
t.Fatal("dissolved frame remains")
|
|
}
|
|
var originalDoc, restoredDoc, emptyDoc map[string]any
|
|
_ = json.Unmarshal(raw, &originalDoc)
|
|
_ = json.Unmarshal(restored, &restoredDoc)
|
|
_ = json.Unmarshal(empty, &emptyDoc)
|
|
byID := func(doc map[string]any) map[string]any {
|
|
m := map[string]any{}
|
|
for _, v := range doc["edges"].([]any) {
|
|
e := v.(map[string]any)
|
|
m[e["id"].(string)] = e
|
|
}
|
|
return m
|
|
}
|
|
if !reflect.DeepEqual(byID(originalDoc), byID(restoredDoc)) {
|
|
t.Fatal("boundary/internal edges did not round trip")
|
|
}
|
|
if len(emptyDoc["nodes"].([]any)) != 0 || len(emptyDoc["edges"].([]any)) != 0 {
|
|
t.Fatal("archived child duplicates live content")
|
|
}
|
|
}
|
|
func canvasNodesByID(raw json.RawMessage) map[string]map[string]any {
|
|
var doc map[string]any
|
|
_ = json.Unmarshal(raw, &doc)
|
|
out := map[string]map[string]any{}
|
|
for _, v := range doc["nodes"].([]any) {
|
|
n := v.(map[string]any)
|
|
out[n["id"].(string)] = n
|
|
}
|
|
return out
|
|
}
|
|
func TestDissolveRejectsNodeCollisionWithoutMutatingInputs(t *testing.T) {
|
|
parent, children, err := migrateLegacyDocument(json.RawMessage(legacyMigrationFixture), map[string]string{"f": "11111111-1111-4111-8111-111111111111", "empty": "22222222-2222-4222-8222-222222222222"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var doc map[string]any
|
|
_ = json.Unmarshal(children["f"], &doc)
|
|
doc["nodes"].([]any)[0].(map[string]any)["id"] = "outside"
|
|
collision, _ := json.Marshal(doc)
|
|
saved := string(parent)
|
|
if _, _, err := dissolveDocuments(parent, collision, "f"); err != errGraphOwnership {
|
|
t.Fatalf("collision accepted: %v", err)
|
|
}
|
|
if string(parent) != saved {
|
|
t.Fatal("failed operation mutated source")
|
|
}
|
|
}
|