package platform import ( "bytes" "context" "crypto/rand" "crypto/sha256" "database/sql" "encoding/base64" "encoding/hex" "encoding/json" "net/http" "net/http/httptest" "os" "testing" ) func TestCanonicalModelMigrationAndIncrementalLifecycle(t *testing.T) { dsn := os.Getenv("PLATFORM_TEST_DATABASE_URL") if dsn == "" { t.Skip("requires an isolated integration database") } db, err := sql.Open("pgx", dsn) if err != nil { t.Fatal(err) } defer db.Close() ctx := context.Background() var entropy [12]byte _, _ = rand.Read(entropy[:]) project := "canonical-" + hex.EncodeToString(entropy[:]) defer func() { for _, table := range []string{"canvas_model_operations", "canvas_model_identity_map", "canvas_model_requests", "canvas_model_projects", "canvas_asset_bindings", "canvas_subgraphs", "workspaces", "project_accounts"} { _, _ = db.ExecContext(ctx, "DELETE FROM "+table+" WHERE project=$1", project) } }() var account, workspace, root string if err = db.QueryRowContext(ctx, `INSERT INTO project_accounts(project,id) VALUES($1,gen_random_uuid()) RETURNING id::text`, project).Scan(&account); err != nil { t.Fatal(err) } var tokenBytes [32]byte _, _ = rand.Read(tokenBytes[:]) token := base64.RawURLEncoding.EncodeToString(tokenBytes[:]) hash := sha256.Sum256([]byte(token)) if _, err = db.ExecContext(ctx, `INSERT INTO project_sessions(token_hash,project,account_id,expires_at) VALUES($1,$2,$3,now()+interval '1 hour')`, base64.RawURLEncoding.EncodeToString(hash[:]), project, account); err != nil { t.Fatal(err) } if err = db.QueryRowContext(ctx, `INSERT INTO workspaces(project,name,owner_id) VALUES($1,'Canonical',$2) RETURNING id::text`, project, account).Scan(&workspace); err != nil { t.Fatal(err) } if _, err = db.ExecContext(ctx, `INSERT INTO workspace_members(project,workspace_id,account_id,role) VALUES($1,$2,$3,'admin')`, project, workspace, account); err != nil { t.Fatal(err) } legacy := json.RawMessage(`{"schema_version":1,"nodes":[` + modelLegacyFrame + `,{"id":"a","type":"plainText","position":{"x":150,"y":260},"width":240,"height":72,"data":{"kind":"plain-text","title":"a","text":"preserved","frameId":"f"}}],"edges":[],"viewport":{"x":0,"y":0,"zoom":1}}`) if err = db.QueryRowContext(ctx, `INSERT INTO materials(project,workspace_id,kind,title,created_by,canvas_document) VALUES($1,$2,'board','Root',$3,$4) RETURNING id::text`, project, workspace, account, legacy).Scan(&root); err != nil { t.Fatal(err) } mux := http.NewServeMux() Register(mux, db, map[string]string{project: "https://app.example.com/auth/callback"}, "https://api.example.com", nil) base := "/projects/" + project + "/workspaces/" + workspace + "/materials/" + root + "/canvas" call := func(method, path string, input any, key string, status int) response { t.Helper() raw, _ := json.Marshal(input) request := httptest.NewRequest(method, path, bytes.NewReader(raw)) request.Header.Set("Authorization", "Bearer "+token) request.Header.Set("Accept", "application/vnd.foundry.canvas+json;version=2") if key != "" { request.Header.Set("Idempotency-Key", key) } writer := httptest.NewRecorder() mux.ServeHTTP(writer, request) if writer.Code != status { t.Fatalf("%s %s got %d %s want %d", method, path, writer.Code, writer.Body.String(), status) } out := response{} if writer.Body.Len() > 0 { if err := json.Unmarshal(writer.Body.Bytes(), &out); err != nil { t.Fatal(err) } } return out } read := func() canvasModel { t.Helper() tx, err := db.BeginTx(ctx, nil) if err != nil { t.Fatal(err) } defer tx.Rollback() model, err := loadCanvasModel(ctx, tx, project, workspace, root) if err != nil { t.Fatal(err) } return model } if call("GET", base+"/model", nil, "", 409)["error"] != "canonical_migration_required" { t.Fatal("legacy read silently migrated") } var count int if err = db.QueryRowContext(ctx, `SELECT count(*) FROM canvas_model_projects WHERE project=$1`, project).Scan(&count); err != nil || count != 0 { t.Fatal("GET created a canonical project") } migrationKey := modelTestID(1000) migrationInput := response{"protocol_version": 2, "request_id": migrationKey, "expected_versions": map[string]int64{root: 1}} migration := call("POST", base+"/model/migrate", migrationInput, migrationKey, 200) if migration["status"] != "applied" { t.Fatal("migration missing applied receipt") } retry := call("POST", base+"/model/migrate", migrationInput, migrationKey, 200) if !modelEqual(migration["identity_map"], retry["identity_map"]) { t.Fatal("migration retry allocated a second identity map") } identities := migration["identity_map"].([]any) nodeID, frameID := "", "" for _, item := range identities { mapping := item.(map[string]any) switch mapping["sourceId"] { case "a": nodeID = mapping["elementId"].(string) case "f": frameID = mapping["elementId"].(string) } } model := read() if nodeID == "" || frameID == "" || len(model.Canvases) != 2 { t.Fatal("migration lost identities or an implicit partition") } child := modelString(model.Elements[nodeID]["ownerCanvasId"]) if child == root || model.Canvases[child].Parent.EntryElementID != frameID || model.Versions[root] != 2 || model.Versions[child] != 1 { t.Fatal("canonical owning tree or version barrier is incorrect") } if call("PATCH", base, response{"document": legacy}, "", 409)["error"] != "canonical_write_required" { t.Fatal("old snapshot writer can overwrite canonical records") } call("GET", base, nil, "", 409) if _, err = db.ExecContext(ctx, `UPDATE materials SET canvas_document=$4,canvas_version=canvas_version+1 WHERE project=$1 AND workspace_id=$2 AND id=$3`, project, workspace, root, legacy); err == nil { t.Fatal("database identity barrier allowed an old writer") } edit := func(n int, versions map[string]int64, changes ...modelChange) response { t.Helper() key := modelTestID(n) input := modelBatch{ProtocolVersion: 2, RequestID: key, Expected: versions, Operations: []modelOperation{{ID: modelTestID(n + 10000), Changes: changes}}} return call("POST", base+"/model/operations", input, key, 200) } titleChange := modelChange{Kind: "field", Target: modelTarget{ID: nodeID, Incarnation: 1}, Field: "data.title", Before: modelJSON("a"), After: modelJSON("renamed")} first := edit(1001, map[string]int64{child: 1}, titleChange) if first["model"].(map[string]any)["complete"] != false { t.Fatal("operation receipt pretends to be a full project snapshot") } model = read() if model.Versions[root] != 2 || model.Versions[child] != 2 || model.Elements[nodeID]["data"].(map[string]any)["text"] != "preserved" { t.Fatal("child edit changed unrelated canvas/version/content") } requestKey := modelTestID(1001) repeat := modelBatch{ProtocolVersion: 2, RequestID: requestKey, Expected: map[string]int64{child: 1}, Operations: []modelOperation{{ID: modelTestID(11001), Changes: []modelChange{titleChange}}}} call("POST", base+"/model/operations", repeat, requestKey, 200) if read().Versions[child] != 2 { t.Fatal("exact retry executed twice") } if call("GET", base+"/model/operations/"+requestKey, nil, "", 200)["status"] != "applied" { t.Fatal("lost ACK cannot be queried") } if call("GET", base+"/model/operations/"+modelTestID(9999), nil, "", 404)["status"] != "unknown" { t.Fatal("missing receipt claimed zero writes") } // Move is a row owner update, retaining ElementId and incarnation. geometry := model.Elements[nodeID]["geometry"] edit(1002, map[string]int64{root: 2, child: 2}, modelChange{Kind: "field", Target: modelTarget{ID: nodeID, Incarnation: 1}, Field: "ownerCanvasId", Before: modelJSON(child), After: modelJSON(root)}, modelChange{Kind: "field", Target: modelTarget{ID: nodeID, Incarnation: 1}, Field: "geometry", Before: modelJSON(geometry), After: modelJSON(map[string]any{"x": 150, "y": 260, "width": 240, "height": 72, "rotation": 0})}) model = read() if model.Elements[nodeID]["ownerCanvasId"] != root || modelInt(model.Elements[nodeID]["incarnation"]) != 1 { t.Fatal("move recreated the element") } // Future component/envelope bytes survive editing another object and legacy projections. opaqueID := modelTestID(3000) opaque := modelTestNode(opaqueID, root) opaque["componentId"], opaque["dataVersion"], opaque["data"], opaque["futureEnvelope"] = "future.plugin", float64(9), map[string]any{"raw": []any{map[string]any{"keep": true}}}, map[string]any{"extension": "retained"} edit(1003, map[string]int64{root: 3}, modelChange{Kind: "element", ID: opaqueID, Before: modelJSON(nil), After: modelJSON(opaque)}) model = read() if !modelEqual(model.Elements[opaqueID], opaque) { t.Fatal("future component data changed") } edit(1004, map[string]int64{root: 4}, modelChange{Kind: "element", ID: nodeID, Before: modelJSON(model.Elements[nodeID]), After: modelJSON(nil)}) model = read() if model.Tombstones[nodeID] != 1 { t.Fatal("delete did not persist its lifecycle tombstone") } restored := modelTestNode(nodeID, root) restored["componentId"] = "plain-text" restored["incarnation"] = float64(2) restored["data"].(map[string]any)["title"] = "restored" edit(1005, map[string]int64{root: 5}, modelChange{Kind: "element", ID: nodeID, Before: modelJSON(nil), After: modelJSON(restored)}) staleKey := modelTestID(1006) stale := modelBatch{ProtocolVersion: 2, RequestID: staleKey, Expected: map[string]int64{root: 6}, Operations: []modelOperation{{ID: modelTestID(11006), Changes: []modelChange{{Kind: "field", Target: modelTarget{ID: nodeID, Incarnation: 1}, Field: "data.title", Before: modelJSON("restored"), After: modelJSON("stale callback")}}}}} if call("POST", base+"/model/operations", stale, staleKey, 409)["status"] != "not-applied" { t.Fatal("stale callback did not receive a zero-write receipt") } if call("GET", base+"/model/operations/"+staleKey, nil, "", 200)["terminal"] != true { t.Fatal("terminal rejection was not queryable") } model = read() if model.Versions[root] != 6 || !modelEqual(model.Elements[opaqueID], opaque) || modelInt(model.Elements[nodeID]["incarnation"]) != 2 { t.Fatal("stale callback or unrelated update damaged state") } duplicateKey := modelTestID(1007) duplicate := modelBatch{ProtocolVersion: 2, RequestID: duplicateKey, Expected: map[string]int64{root: 6}, Operations: []modelOperation{{ID: modelTestID(11002), Changes: []modelChange{{Kind: "field", Target: modelTarget{ID: nodeID, Incarnation: 2}, Field: "data.title", Before: modelJSON("restored"), After: modelJSON("duplicate effect")}}}}} if call("POST", base+"/model/operations", duplicate, duplicateKey, 409)["error"] != "operation_already_applied" { t.Fatal("logical operation was executed under a new request ID") } var owner string var incarnation int64 if err = db.QueryRowContext(ctx, `SELECT owner_canvas_id::text,incarnation FROM canvas_model_elements WHERE project=$1 AND element_id=$2 AND NOT deleted`, project, nodeID).Scan(&owner, &incarnation); err != nil || owner != root || incarnation != 2 { t.Fatal("canonical row authority disagrees with the API") } if call("GET", base+"/model", nil, "", 200)["model"].(map[string]any)["complete"] != true { t.Fatal("initial read has no explicit coverage") } }