Support canvas frame nodes and explicit membership snapshots
This commit is contained in:
@@ -39,3 +39,52 @@ func TestSlugValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Frame nodes follow the frontend contract: membership is data, not containment.
|
||||
const frameFixture = `{"schema_version":1,"nodes":[{"id":"text","type":"material","position":{"x":100,"y":100},"data":{"kind":"text","title":"成员","text":"正文","frameId":"frame"}},{"id":"plain","type":"plainText","position":{"x":0,"y":0},"data":{"kind":"plain-text","title":"纯文本","text":"文字","frameId":"frame"}},{"id":"icon","type":"icon","position":{"x":10,"y":10},"data":{"kind":"icon","title":"图标","text":"","icon":"Star","frameId":"frame"}},{"id":"frame","type":"frame","position":{"x":0,"y":0},"width":600,"height":400,"zIndex":-1,"style":{"width":600,"height":400,"pointerEvents":"none"},"data":{"kind":"frame","title":"框选区","text":"","rotation":0,"labelMode":"fixed"}},{"id":"outside","type":"material","position":{"x":100,"y":100},"data":{"kind":"script","title":"重叠但未加入","text":""}}],"edges":[],"viewport":{"x":0,"y":0,"zoom":1}}`
|
||||
|
||||
func TestCanvasFrames(t *testing.T) {
|
||||
for _, mode := range []string{"hidden", "fixed", "scaled"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
out, ok := normalizeCanvas(json.RawMessage(strings.Replace(frameFixture, `"labelMode":"fixed"`, `"labelMode":"`+mode+`"`, 1)))
|
||||
if !ok {
|
||||
t.Fatal("valid frame rejected")
|
||||
}
|
||||
var doc map[string]any
|
||||
json.Unmarshal(out, &doc)
|
||||
nodes := doc["nodes"].([]any)
|
||||
if nodes[0].(map[string]any)["data"].(map[string]any)["frameId"] != "frame" || nodes[3].(map[string]any)["data"].(map[string]any)["labelMode"] != mode {
|
||||
t.Fatal("frame data lost")
|
||||
}
|
||||
if _, exists := nodes[4].(map[string]any)["data"].(map[string]any)["frameId"]; exists {
|
||||
t.Fatal("position inferred membership")
|
||||
}
|
||||
if _, exists := nodes[3].(map[string]any)["zIndex"]; exists {
|
||||
t.Fatal("rendering state persisted")
|
||||
}
|
||||
})
|
||||
}
|
||||
cases := [][2]string{
|
||||
{`"labelMode":"fixed"`, `"labelMode":"invalid"`},
|
||||
{`"labelMode":"fixed"`, `"labelMode":null`},
|
||||
{`"rotation":0`, `"rotation":1`},
|
||||
{`"frameId":"frame"`, `"frameId":"missing"`},
|
||||
{`"frameId":"frame"`, `"frameId":"text"`},
|
||||
{`"frameId":"frame"`, `"frameId":null`},
|
||||
{`"frameId":"frame"`, `"frameId":""`},
|
||||
{`"labelMode":"fixed"`, `"labelMode":"fixed","frameId":"frame"`},
|
||||
{`"type":"frame"`, `"type":"material"`},
|
||||
{`"edges":[]`, `"edges":[{"id":"e","source":"text","target":"frame"}]`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c[1], func(t *testing.T) {
|
||||
if _, ok := normalizeCanvas(json.RawMessage(strings.Replace(frameFixture, c[0], c[1], 1))); ok {
|
||||
t.Fatal("invalid frame accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
withoutMode := strings.Replace(frameFixture, `,"labelMode":"fixed"`, "", 1)
|
||||
if _, ok := normalizeCanvas(json.RawMessage(withoutMode)); !ok {
|
||||
t.Fatal("optional labelMode rejected")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user