Files
foundry-core/internal/platform/canvas_connections_test.go
codex-bot 07fd678cf3
Foundry Go checks / check (push) Failing after 10m4s
Foundry Go checks / check (pull_request) Successful in 7m53s
Add atomic connections across canvas subgraph boundaries
2026-11-06 19:38:11 +08:00

66 lines
3.1 KiB
Go

package platform
import (
"encoding/json"
"fmt"
"testing"
)
func TestConnectionPortReuseCollisionAndCapacity(t *testing.T) {
data := map[string]any{"ports": []any{map[string]any{"id": "custom-stable", "nodeId": "n", "handle": "left", "direction": "input"}}}
id, created, err := connectionPort(data, "n", "left", "input")
if err != nil || created || id != "custom-stable" {
t.Fatal("existing logical port was not reused")
}
id, created, err = connectionPort(data, "n", "left", "output")
if err != nil || !created || id == "custom-stable" {
t.Fatal("input and output ports were conflated")
}
data["ports"].([]any)[1].(map[string]any)["nodeId"] = "other"
if _, _, err = connectionPort(data, "n", "left", "output"); err != errGraphOwnership {
t.Fatal("derived ID collision retargeted a port")
}
ports := []any{}
for i := 0; i < 4000; i++ {
ports = append(ports, map[string]any{"id": fmt.Sprint(i), "nodeId": "other", "handle": "left", "direction": "input"})
}
data["ports"] = ports
if _, _, err = connectionPort(data, "n", "right", "output"); err != errGraphCapacity || len(data["ports"].([]any)) != 4000 {
t.Fatal("port capacity partially mutated frame")
}
}
func TestConnectionExposureBuildsNestedDirectionChain(t *testing.T) {
frame := func(child string) map[string]any {
return map[string]any{"id": "frame", "data": map[string]any{"kind": "frame", "subgraph": map[string]any{"boardId": child}, "ports": []any{}}}
}
docs := map[string]map[string]any{
"root": {"nodes": []any{frame("middle")}},
"middle": {"nodes": []any{frame("leaf")}},
"leaf": {"nodes": []any{map[string]any{"id": "n", "data": map[string]any{"kind": "plain-image", "src": "https://example.com/image.png"}}}},
}
before, _ := json.Marshal(docs["leaf"])
path := []subgraphRelation{{Parent: "middle", Frame: "frame", Child: "leaf"}, {Parent: "root", Frame: "frame", Child: "middle"}}
endpoint := canvasConnectionEndpoint{Board: "leaf", Node: "n", Handle: "bottom"}
exposed, created, err := exposeConnectionEndpoint(endpoint, "output", path, docs)
if err != nil || exposed.Board != "root" || exposed.Node != "frame" || len(created) != 2 {
t.Fatalf("incomplete chain: %+v %v", exposed, err)
}
inner := connectionNode(docs["middle"], "frame")["data"].(map[string]any)["ports"].([]any)[0].(map[string]any)
outer := connectionNode(docs["root"], "frame")["data"].(map[string]any)["ports"].([]any)[0].(map[string]any)
if inner["nodeId"] != "n" || inner["handle"] != "bottom" || outer["nodeId"] != "frame" || outer["handle"] != "port:"+inner["id"].(string) {
t.Fatal("outer port skipped a child boundary")
}
again, created, err := exposeConnectionEndpoint(endpoint, "output", path, docs)
if err != nil || again != exposed || len(created) != 0 {
t.Fatal("repeated exposure duplicated ports")
}
if _, created, err = exposeConnectionEndpoint(endpoint, "input", path, docs); err != nil || len(created) != 2 {
t.Fatal("reverse direction did not create independent input chain")
}
after, _ := json.Marshal(docs["leaf"])
if string(before) != string(after) {
t.Fatal("exposure modified the actual child node")
}
}