- internal/exporter: pure BuildScene + 21-char base62 IDs, port ellipses, device rect+text pairs, IO diamonds, arrow bindings, legend texts. Bundles intentionally omitted per design §4.1. - internal/db: PersistExcalidrawIDs idempotent updater per project. - internal/server: POST /api/projects/:pid/sync/export — loads snapshot, mints/reuses excalidraw_ids, PUTs scene to mxdrw with bearer auth. Returns viewer URL + element_count + mxdrw response. Roundtrip hand-tested against mxdrw.msbls.de: scene saved, IDs stable across re-exports.
166 lines
4.7 KiB
Go
166 lines
4.7 KiB
Go
package exporter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"mgit.msbls.de/m/mcables/internal/db"
|
|
)
|
|
|
|
// deterministic id generator for tests
|
|
func newSeq() func() string {
|
|
i := 0
|
|
return func() string {
|
|
i++
|
|
return "id" + strings.Repeat("0", 19-len(itoa(i))) + itoa(i)
|
|
}
|
|
}
|
|
|
|
func itoa(i int) string {
|
|
if i == 0 {
|
|
return "0"
|
|
}
|
|
buf := [20]byte{}
|
|
pos := len(buf)
|
|
for i > 0 {
|
|
pos--
|
|
buf[pos] = byte('0' + i%10)
|
|
i /= 10
|
|
}
|
|
return string(buf[pos:])
|
|
}
|
|
|
|
func sampleSnapshot() *db.Snapshot {
|
|
pid := int64(1)
|
|
devID := int64(10)
|
|
devID2 := int64(11)
|
|
portID := int64(100)
|
|
portID2 := int64(101)
|
|
ioID := int64(200)
|
|
|
|
return &db.Snapshot{
|
|
Project: db.Project{ID: pid, Name: "LOFT", DrawingName: "LOFT.excalidraw"},
|
|
Frames: []db.Frame{
|
|
{ID: 1, ProjectID: pid, Name: "desk", X: 100, Y: 100, Width: 800, Height: 500},
|
|
},
|
|
Devices: []db.Device{
|
|
{ID: devID, ProjectID: pid, Name: "NAS", Color: "#1e1e1e", X: 200, Y: 200, Width: 100, Height: 35, FrameID: ptr(int64(1))},
|
|
{ID: devID2, ProjectID: pid, Name: "Switch", Color: "#1e1e1e", X: 400, Y: 200, Width: 100, Height: 35},
|
|
},
|
|
Ports: []db.Port{
|
|
{ID: portID, ProjectID: pid, DeviceID: devID, TypeID: 5, XOffset: 50, YOffset: 35},
|
|
{ID: portID2, ProjectID: pid, DeviceID: devID2, TypeID: 5, XOffset: 50, YOffset: 35},
|
|
},
|
|
IOMarkers: []db.IOMarker{
|
|
{ID: ioID, ProjectID: pid, Label: "Wall A", X: 50, Y: 50},
|
|
},
|
|
Cables: []db.Cable{
|
|
{ID: 1000, ProjectID: pid, TypeID: 5,
|
|
FromPortID: &portID, ToPortID: &portID2, Auto: false},
|
|
},
|
|
CableTypes: []db.CableType{
|
|
{ID: 1, Name: "Power", Color: "#e03131"},
|
|
{ID: 2, Name: "USB", Color: "#2f9e44"},
|
|
{ID: 3, Name: "HDMI", Color: "#1971c2"},
|
|
{ID: 4, Name: "DP", Color: "#9c36b5"},
|
|
{ID: 5, Name: "RJ45", Color: "#ffd500"},
|
|
},
|
|
}
|
|
}
|
|
|
|
func ptr[T any](v T) *T { return &v }
|
|
|
|
func TestBuildScene_BasicShape(t *testing.T) {
|
|
snap := sampleSnapshot()
|
|
scene, ids := BuildScene(snap, 1700000000000, newSeq())
|
|
|
|
if scene.Type != "excalidraw" || scene.Version != 2 {
|
|
t.Errorf("bad header: %+v", scene)
|
|
}
|
|
// frame(1) + device-rect+text(2 each) + ports(2) + io+text(2) +
|
|
// cable(1) + legend(5) = 1 + 4 + 2 + 2 + 1 + 5 = 15.
|
|
if len(scene.Elements) < 15 {
|
|
t.Errorf("element count = %d, want ≥15", len(scene.Elements))
|
|
}
|
|
if len(ids.Frames) != 1 || len(ids.Devices) != 2 || len(ids.Ports) != 2 ||
|
|
len(ids.IOMarkers) != 1 || len(ids.Cables) != 1 {
|
|
t.Errorf("id assignment shape wrong: %+v", ids)
|
|
}
|
|
}
|
|
|
|
func TestBuildScene_ReusesExistingExcalidrawIDs(t *testing.T) {
|
|
snap := sampleSnapshot()
|
|
// Pre-assign an excalidraw_id on the first device.
|
|
preset := "preset0000000000000NAS"[:21]
|
|
snap.Devices[0].ExcalidrawID = &preset
|
|
_, ids := BuildScene(snap, 1700000000000, newSeq())
|
|
if ids.Devices[snap.Devices[0].ID] != preset {
|
|
t.Errorf("preset id not reused: got %q, want %q", ids.Devices[snap.Devices[0].ID], preset)
|
|
}
|
|
}
|
|
|
|
func TestBuildScene_ArrowsBindToPorts(t *testing.T) {
|
|
snap := sampleSnapshot()
|
|
scene, ids := BuildScene(snap, 1700000000000, newSeq())
|
|
// The arrow's startBinding should reference the from-port's element id.
|
|
fromPortElID := ids.Ports[100]
|
|
toPortElID := ids.Ports[101]
|
|
var found *Element
|
|
for i := range scene.Elements {
|
|
if scene.Elements[i].Type == "arrow" {
|
|
found = &scene.Elements[i]
|
|
break
|
|
}
|
|
}
|
|
if found == nil {
|
|
t.Fatal("no arrow in scene")
|
|
}
|
|
if found.StartBinding == nil || found.StartBinding.ElementID != fromPortElID {
|
|
t.Errorf("start binding wrong: %+v", found.StartBinding)
|
|
}
|
|
if found.EndBinding == nil || found.EndBinding.ElementID != toPortElID {
|
|
t.Errorf("end binding wrong: %+v", found.EndBinding)
|
|
}
|
|
}
|
|
|
|
func TestBuildScene_BundlesIgnored(t *testing.T) {
|
|
snap := sampleSnapshot()
|
|
// Snapshot.Bundles is unused in the exporter for v0 per design §4.1.
|
|
// Add some and confirm no bundle elements appear in the scene.
|
|
snap.Bundles = []db.Bundle{{ID: 1, Name: "trunk", CableIDs: []int64{1000}}}
|
|
scene, _ := BuildScene(snap, 1700000000000, newSeq())
|
|
for _, e := range scene.Elements {
|
|
if strings.Contains(e.Type, "bundle") {
|
|
t.Errorf("bundle element leaked into scene: %+v", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMarshalScene_IsJSON(t *testing.T) {
|
|
snap := sampleSnapshot()
|
|
scene, _ := BuildScene(snap, 1700000000000, newSeq())
|
|
b, err := MarshalScene(scene)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
var roundtrip map[string]any
|
|
if err := json.Unmarshal(b, &roundtrip); err != nil {
|
|
t.Fatalf("roundtrip: %v", err)
|
|
}
|
|
if roundtrip["type"] != "excalidraw" {
|
|
t.Errorf("type field = %v, want excalidraw", roundtrip["type"])
|
|
}
|
|
}
|
|
|
|
func TestGenerate21(t *testing.T) {
|
|
a := Generate21()
|
|
b := Generate21()
|
|
if len(a) != 21 || len(b) != 21 {
|
|
t.Errorf("len wrong: %d / %d", len(a), len(b))
|
|
}
|
|
if a == b {
|
|
t.Errorf("ids collide: %q == %q", a, b)
|
|
}
|
|
}
|