Implement Canvas V2 subgraphs, assets and asynchronous AI execution
This commit is contained in:
@@ -10,6 +10,9 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"gitea.entroia.com/Zayden-Jung/foundry-core/internal/auth"
|
||||
"image"
|
||||
"image/png"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -55,6 +58,10 @@ func TestOnboardingAndSharing(t *testing.T) {
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
defer func() {
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM canvas_asset_bindings WHERE project=$1", project)
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM canvas_assets WHERE project=$1", project)
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM canvas_transactions WHERE project=$1", project)
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM canvas_subgraphs WHERE project=$1", project)
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM workspaces WHERE project=$1", project)
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM project_accounts WHERE project=$1", project)
|
||||
_, _ = db.ExecContext(ctx, "DELETE FROM email_outbox WHERE project=$1", project)
|
||||
@@ -82,6 +89,12 @@ func TestOnboardingAndSharing(t *testing.T) {
|
||||
if len(versions) > 0 {
|
||||
r.Header.Set("If-Match", versions[0])
|
||||
}
|
||||
if len(versions) > 1 {
|
||||
r.Header.Set("Accept", versions[1])
|
||||
}
|
||||
if len(versions) > 2 {
|
||||
r.Header.Set("Idempotency-Key", versions[2])
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
mux.ServeHTTP(w, r)
|
||||
if w.Code != status {
|
||||
@@ -286,6 +299,347 @@ func TestOnboardingAndSharing(t *testing.T) {
|
||||
t.Fatal("kind/title filter returned incorrect materials")
|
||||
}
|
||||
|
||||
// Subgraph creation is atomic and idempotent, and cannot be undone by
|
||||
// a stale or legacy whole-document save.
|
||||
subBoard := call("POST", workspacePath+"/materials", tokens[0], response{"kind": "board", "title": "Subgraphs", "content": response{}}, 201)
|
||||
subPath := workspacePath + "/materials/" + subBoard["id"].(string) + "/canvas"
|
||||
accept := "application/vnd.foundry.canvas+json;version=2"
|
||||
key := "11111111-1111-4111-8111-111111111111"
|
||||
command := response{"frame": response{"id": "frame-new", "title": "Child", "position": response{"x": 100, "y": 100}, "width": 640, "height": 400, "labelMode": "fixed"}}
|
||||
subgraphCreated := call("POST", subPath+"/subgraphs", tokens[0], command, 201, `"1"`, accept, key)
|
||||
retried := call("POST", subPath+"/subgraphs", tokens[0], command, 201, `"1"`, accept, key)
|
||||
if subgraphCreated["transaction_id"] != retried["transaction_id"] {
|
||||
t.Fatal("retry created another transaction")
|
||||
}
|
||||
call("POST", subPath+"/subgraphs", tokens[0], response{"frame": response{"id": "other", "title": "Child", "position": response{"x": 100, "y": 100}, "width": 640, "height": 400, "labelMode": "fixed"}}, 409, `"1"`, accept, key)
|
||||
call("GET", subPath, tokens[0], nil, 409)
|
||||
savedSub := call("GET", subPath, tokens[0], nil, 200, "", accept)
|
||||
if savedSub["version"] != float64(2) {
|
||||
t.Fatal("idempotent retry changed version")
|
||||
}
|
||||
call("PATCH", subPath, tokens[0], response{"document": response{"schema_version": 2, "nodes": []any{}, "edges": []any{}, "viewport": response{"x": 0, "y": 0, "zoom": 1}}}, 409, `"2"`, accept)
|
||||
childID := subgraphCreated["child_board"].(map[string]any)["id"].(string)
|
||||
childPath := workspacePath + "/materials/" + childID + "/canvas"
|
||||
child := call("GET", childPath, tokens[0], nil, 200, "", accept)
|
||||
if child["version"] != float64(1) {
|
||||
t.Fatal("child initial version")
|
||||
}
|
||||
call("GET", childPath, tokens[2], nil, 404, "", accept)
|
||||
var children int
|
||||
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM canvas_subgraphs WHERE project=$1 AND parent_board_id=$2", project, subBoard["id"]).Scan(&children); err != nil || children != 1 {
|
||||
t.Fatalf("child relationship count: %d %v", children, err)
|
||||
}
|
||||
|
||||
defaultMaterials := call("GET", workspacePath+"/materials?limit=100", tokens[0], nil, 200)
|
||||
for _, v := range defaultMaterials["items"].([]any) {
|
||||
if v.(map[string]any)["id"] == childID {
|
||||
t.Fatal("child leaked into dashboard")
|
||||
}
|
||||
}
|
||||
explicitMaterials := call("GET", workspacePath+"/materials?limit=100&include_subgraphs=true", tokens[0], nil, 200)
|
||||
foundChild := false
|
||||
for _, v := range explicitMaterials["items"].([]any) {
|
||||
m := v.(map[string]any)
|
||||
if m["id"] == childID {
|
||||
foundChild = true
|
||||
if m["parent_board_id"] != subBoard["id"] || m["frame_id"] != "frame-new" {
|
||||
t.Fatal("missing child ownership metadata")
|
||||
}
|
||||
}
|
||||
}
|
||||
if !foundChild {
|
||||
t.Fatal("include_subgraphs omitted authorized child")
|
||||
}
|
||||
dissolvePath := subPath + "/subgraphs/frame-new/dissolve"
|
||||
call("POST", dissolvePath, tokens[0], response{"expected_versions": map[string]int64{subBoard["id"].(string): 1, childID: 1}}, 412, "", accept, "66666666-6666-4666-8666-666666666666")
|
||||
call("GET", childPath, tokens[0], nil, 200, "", accept)
|
||||
dissolveCommand := response{"expected_versions": map[string]int64{subBoard["id"].(string): 2, childID: 1}}
|
||||
dissolved := call("POST", dissolvePath, tokens[0], dissolveCommand, 200, "", accept, "77777777-7777-4777-8777-777777777777")
|
||||
dissolveRetry := call("POST", dissolvePath, tokens[0], dissolveCommand, 200, "", accept, "77777777-7777-4777-8777-777777777777")
|
||||
if dissolved["transaction_id"] != dissolveRetry["transaction_id"] {
|
||||
t.Fatal("dissolve retry duplicated")
|
||||
}
|
||||
call("GET", childPath, tokens[0], nil, 404, "", accept)
|
||||
dissolveUndoPath := subPath + "/transactions/" + dissolved["transaction_id"].(string) + "/undo"
|
||||
call("POST", dissolveUndoPath, tokens[0], response{"expected_versions": map[string]int64{subBoard["id"].(string): 3, childID: 2}}, 200, "", accept, "88888888-8888-4888-8888-888888888888")
|
||||
call("GET", childPath, tokens[0], nil, 200, "", accept)
|
||||
|
||||
migrationBoard := call("POST", workspacePath+"/materials", tokens[0], response{"kind": "board", "title": "Legacy migration", "content": response{}}, 201)
|
||||
migrationPath := workspacePath + "/materials/" + migrationBoard["id"].(string) + "/canvas"
|
||||
call("PATCH", migrationPath, tokens[0], response{"document": json.RawMessage(legacyMigrationFixture)}, 200, `"1"`)
|
||||
plan := call("POST", migrationPath+"/subgraphs/migrate", tokens[0], response{"dry_run": true}, 200, "", accept)
|
||||
if plan["child_count"] != float64(2) {
|
||||
t.Fatal("migration preview child count")
|
||||
}
|
||||
var countBefore int
|
||||
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM materials WHERE project=$1", project).Scan(&countBefore); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
call("POST", migrationPath+"/subgraphs/migrate", tokens[0], response{"dry_run": false, "plan_id": "stale"}, 412, `"2"`, accept, "22222222-2222-4222-8222-222222222222")
|
||||
var countAfter int
|
||||
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM materials WHERE project=$1", project).Scan(&countAfter); err != nil || countBefore != countAfter {
|
||||
t.Fatal("preflight/stale plan created orphan child")
|
||||
}
|
||||
migrationCommand := response{"dry_run": false, "plan_id": plan["plan_id"]}
|
||||
migrated := call("POST", migrationPath+"/subgraphs/migrate", tokens[0], migrationCommand, 200, `"2"`, accept, "33333333-3333-4333-8333-333333333333")
|
||||
migrationRetry := call("POST", migrationPath+"/subgraphs/migrate", tokens[0], migrationCommand, 200, `"2"`, accept, "33333333-3333-4333-8333-333333333333")
|
||||
if migrated["transaction_id"] != migrationRetry["transaction_id"] {
|
||||
t.Fatal("migration retry duplicated transaction")
|
||||
}
|
||||
expectedUndo := map[string]int64{migrationBoard["id"].(string): 3}
|
||||
for _, v := range migrated["children"].([]any) {
|
||||
c := v.(map[string]any)
|
||||
expectedUndo[c["board_id"].(string)] = 1
|
||||
}
|
||||
undoPath := migrationPath + "/transactions/" + migrated["transaction_id"].(string) + "/undo"
|
||||
undoCommand := response{"expected_versions": expectedUndo}
|
||||
undone := call("POST", undoPath, tokens[0], undoCommand, 200, "", accept, "44444444-4444-4444-8444-444444444444")
|
||||
undoRetry := call("POST", undoPath, tokens[0], undoCommand, 200, "", accept, "44444444-4444-4444-8444-444444444444")
|
||||
if undone["transaction_id"] != undoRetry["transaction_id"] {
|
||||
t.Fatal("undo retry duplicated")
|
||||
}
|
||||
restoredLegacy := call("GET", migrationPath, tokens[0], nil, 200)
|
||||
if restoredLegacy["version"] != float64(4) || restoredLegacy["document"].(map[string]any)["schema_version"] != float64(1) {
|
||||
t.Fatal("undo did not restore legacy document")
|
||||
}
|
||||
for child := range expectedUndo {
|
||||
if child != migrationBoard["id"].(string) {
|
||||
call("GET", workspacePath+"/materials/"+child+"/canvas", tokens[0], nil, 404, "", accept)
|
||||
}
|
||||
}
|
||||
redoPlan := call("POST", migrationPath+"/subgraphs/migrate", tokens[0], response{"dry_run": true}, 200, "", accept)
|
||||
redone := call("POST", migrationPath+"/subgraphs/migrate", tokens[0], response{"dry_run": false, "plan_id": redoPlan["plan_id"]}, 200, `"4"`, accept, "55555555-5555-4555-8555-555555555555")
|
||||
var membersSource, membersTarget string
|
||||
for _, v := range redone["child_boards"].([]any) {
|
||||
child := v.(map[string]any)
|
||||
if child["frame_id"] == "f" {
|
||||
membersSource = child["id"].(string)
|
||||
} else if child["frame_id"] == "empty" {
|
||||
membersTarget = child["id"].(string)
|
||||
}
|
||||
}
|
||||
parentID := migrationBoard["id"].(string)
|
||||
memberPath := migrationPath + "/subgraphs/f/members"
|
||||
moveCommand := response{"source_board_id": membersSource, "target_board_id": membersTarget, "node_ids": []string{"a"}, "expected_versions": map[string]int64{parentID: 5, membersSource: 1, membersTarget: 1}}
|
||||
movedMembers := call("POST", memberPath, tokens[0], moveCommand, 200, "", accept, "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb")
|
||||
memberRetry := call("POST", memberPath, tokens[0], moveCommand, 200, "", accept, "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb")
|
||||
if movedMembers["transaction_id"] != memberRetry["transaction_id"] {
|
||||
t.Fatal("member retry duplicated transaction")
|
||||
}
|
||||
sourceCanvasPath := workspacePath + "/materials/" + membersSource + "/canvas"
|
||||
targetCanvasPath := workspacePath + "/materials/" + membersTarget + "/canvas"
|
||||
sourceRead := call("GET", sourceCanvasPath, tokens[0], nil, 200, "", accept)
|
||||
targetRead := call("GET", targetCanvasPath, tokens[0], nil, 200, "", accept)
|
||||
sourceJSON, _ := json.Marshal(sourceRead["document"])
|
||||
targetJSON, _ := json.Marshal(targetRead["document"])
|
||||
if canvasNodeData(sourceJSON)["a"] != nil || canvasNodeData(targetJSON)["a"] == nil {
|
||||
t.Fatal("member move duplicated or lost ownership")
|
||||
}
|
||||
call("POST", memberPath, tokens[0], response{"source_board_id": membersSource, "target_board_id": membersTarget, "node_ids": []string{"b"}, "expected_versions": map[string]int64{parentID: 5, membersSource: 2, membersTarget: 2}}, 412, "", accept, "cccccccc-cccc-4ccc-8ccc-cccccccccccc")
|
||||
call("POST", migrationPath+"/transactions/"+movedMembers["transaction_id"].(string)+"/undo", tokens[0], response{"expected_versions": map[string]int64{parentID: 6, membersSource: 2, membersTarget: 2}}, 200, "", accept, "dddddddd-dddd-4ddd-8ddd-dddddddddddd")
|
||||
parentMove := call("POST", memberPath, tokens[0], response{"source_board_id": parentID, "target_board_id": membersSource, "node_ids": []string{"outside"}, "expected_versions": map[string]int64{parentID: 7, membersSource: 3}}, 200, "", accept, "eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee")
|
||||
call("POST", migrationPath+"/transactions/"+parentMove["transaction_id"].(string)+"/undo", tokens[0], response{"expected_versions": map[string]int64{parentID: 8, membersSource: 4}}, 200, "", accept, "ffffffff-ffff-4fff-8fff-ffffffffffff")
|
||||
innerCreated := call("POST", sourceCanvasPath+"/subgraphs", tokens[0], response{"frame": response{"id": "inner-frame", "title": "Inner", "position": response{"x": 20, "y": 40}, "width": 400, "height": 300, "labelMode": "fixed"}}, 201, `"5"`, accept, "10000000-0000-4000-8000-000000000001")
|
||||
innerID := innerCreated["child_board"].(map[string]any)["id"].(string)
|
||||
nestedMove := call("POST", memberPath, tokens[0], response{"source_board_id": membersSource, "target_board_id": membersTarget, "node_ids": []string{"inner-frame"}, "expected_versions": map[string]int64{parentID: 9, membersSource: 6, membersTarget: 3, innerID: 1}}, 200, "", accept, "10000000-0000-4000-8000-000000000002")
|
||||
var nestedParent string
|
||||
if err := db.QueryRowContext(ctx, "SELECT parent_board_id::text FROM canvas_subgraphs WHERE project=$1 AND child_board_id=$2", project, innerID).Scan(&nestedParent); err != nil || nestedParent != membersTarget {
|
||||
t.Fatalf("nested child was copied or not reparented: %s %v", nestedParent, err)
|
||||
}
|
||||
call("POST", migrationPath+"/transactions/"+nestedMove["transaction_id"].(string)+"/undo", tokens[0], response{"expected_versions": map[string]int64{parentID: 10, membersSource: 7, membersTarget: 4, innerID: 2}}, 200, "", accept, "10000000-0000-4000-8000-000000000003")
|
||||
if err := db.QueryRowContext(ctx, "SELECT parent_board_id::text FROM canvas_subgraphs WHERE project=$1 AND child_board_id=$2", project, innerID).Scan(&nestedParent); err != nil || nestedParent != membersSource {
|
||||
t.Fatalf("undo did not restore nested ownership: %s %v", nestedParent, err)
|
||||
}
|
||||
|
||||
// Moving an externally exposed node inside a nested frame must repair the
|
||||
// ancestor boundary atomically and preserve its port/edge identities.
|
||||
innerMemberPath := sourceCanvasPath + "/subgraphs/inner-frame/members"
|
||||
nestedMemberCommand := response{"source_board_id": membersSource, "target_board_id": innerID, "node_ids": []string{"b"}, "expected_versions": map[string]int64{parentID: 11, membersSource: 8, innerID: 3}}
|
||||
nestedMemberMove := call("POST", innerMemberPath, tokens[0], nestedMemberCommand, 200, "", accept, "30000000-0000-4000-8000-000000000001")
|
||||
ancestorRead := call("GET", migrationPath, tokens[0], nil, 200, "", accept)
|
||||
ancestorJSON, _ := json.Marshal(ancestorRead["document"])
|
||||
frameData := canvasNodeData(ancestorJSON)["f"]
|
||||
ancestorPorts := frameData["ports"].([]any)
|
||||
if len(ancestorPorts) != 1 || ancestorPorts[0].(map[string]any)["nodeId"] != "inner-frame" || !strings.HasPrefix(ancestorPorts[0].(map[string]any)["handle"].(string), "port:") {
|
||||
t.Fatal("ancestor port not redirected through nested frame")
|
||||
}
|
||||
outerPortID := ancestorPorts[0].(map[string]any)["id"]
|
||||
call("POST", sourceCanvasPath+"/subgraphs/inner-frame/dissolve", tokens[0], response{"expected_versions": map[string]int64{membersSource: 9, innerID: 4}}, 428, "", accept, "30000000-0000-4000-8000-000000000002")
|
||||
dissolvedNested := call("POST", sourceCanvasPath+"/subgraphs/inner-frame/dissolve", tokens[0], response{"expected_versions": map[string]int64{parentID: 12, membersSource: 9, innerID: 4}}, 200, "", accept, "30000000-0000-4000-8000-000000000002")
|
||||
dissolvedAncestor := call("GET", migrationPath, tokens[0], nil, 200, "", accept)
|
||||
dissolvedAncestorJSON, _ := json.Marshal(dissolvedAncestor["document"])
|
||||
port := canvasNodeData(dissolvedAncestorJSON)["f"]["ports"].([]any)[0].(map[string]any)
|
||||
if port["id"] != outerPortID || port["nodeId"] != "b" || port["handle"] != "right" {
|
||||
t.Fatal("dissolve lost ancestor logical endpoint")
|
||||
}
|
||||
call("POST", sourceCanvasPath+"/transactions/"+dissolvedNested["transaction_id"].(string)+"/undo", tokens[0], response{"expected_versions": map[string]int64{parentID: 13, membersSource: 10, innerID: 5}}, 200, "", accept, "30000000-0000-4000-8000-000000000003")
|
||||
// The documents now match the previous move's after-state even though all
|
||||
// versions are newer. Undo must work as a stack without resetting versions.
|
||||
call("POST", sourceCanvasPath+"/transactions/"+nestedMemberMove["transaction_id"].(string)+"/undo", tokens[0], response{"expected_versions": map[string]int64{parentID: 14, membersSource: 11, innerID: 6}}, 200, "", accept, "30000000-0000-4000-8000-000000000004")
|
||||
restoredAncestor := call("GET", migrationPath, tokens[0], nil, 200, "", accept)
|
||||
restoredAncestorJSON, _ := json.Marshal(restoredAncestor["document"])
|
||||
port = canvasNodeData(restoredAncestorJSON)["f"]["ports"].([]any)[0].(map[string]any)
|
||||
if port["id"] != outerPortID || port["nodeId"] != "b" || port["handle"] != "right" {
|
||||
t.Fatal("undo stack did not restore ancestor port")
|
||||
}
|
||||
|
||||
var assetPNG bytes.Buffer
|
||||
if err := png.Encode(&assetPNG, image.NewRGBA(image.Rect(0, 0, 3, 2))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
upload := func(encoded []byte, key string, status int) response {
|
||||
t.Helper()
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
part, err := writer.CreateFormFile("image", "misleading.jpg")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = part.Write(encoded)
|
||||
_ = writer.Close()
|
||||
request := httptest.NewRequest("POST", workspacePath+"/assets", &body)
|
||||
request.Header.Set("Authorization", "Bearer "+tokens[0])
|
||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
request.Header.Set("Idempotency-Key", key)
|
||||
result := httptest.NewRecorder()
|
||||
mux.ServeHTTP(result, request)
|
||||
if result.Code != status {
|
||||
t.Fatalf("asset upload got %d: %s", result.Code, result.Body.String())
|
||||
}
|
||||
out := response{}
|
||||
if err := json.Unmarshal(result.Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
uploaded := upload(assetPNG.Bytes(), "99999999-9999-4999-8999-999999999999", 201)
|
||||
retriedAsset := upload(assetPNG.Bytes(), "99999999-9999-4999-8999-999999999999", 201)
|
||||
if uploaded["id"] != retriedAsset["id"] || uploaded["mime_type"] != "image/png" {
|
||||
t.Fatal("asset idempotency or actual MIME detection")
|
||||
}
|
||||
download := func(token string, status int) {
|
||||
t.Helper()
|
||||
request := httptest.NewRequest("GET", workspacePath+"/assets/"+uploaded["id"].(string), nil)
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
result := httptest.NewRecorder()
|
||||
mux.ServeHTTP(result, request)
|
||||
if result.Code != status {
|
||||
t.Fatalf("asset download got %d want %d: %s", result.Code, status, result.Body.String())
|
||||
}
|
||||
if status == 200 && (!bytes.Equal(result.Body.Bytes(), assetPNG.Bytes()) || result.Header().Get("Content-Type") != "image/png") {
|
||||
t.Fatal("asset bytes changed")
|
||||
}
|
||||
}
|
||||
download(tokens[0], 200)
|
||||
download(tokens[1], 404)
|
||||
download(tokens[2], 404)
|
||||
assetBoard := call("POST", workspacePath+"/materials", tokens[0], response{"kind": "board", "title": "Assets", "content": response{}}, 201)
|
||||
assetDoc := response{"schema_version": 2, "nodes": []any{response{"id": "asset-node", "type": "plainImage", "position": response{"x": 0, "y": 0}, "data": response{"kind": "plain-image", "title": "Image", "text": "", "src": uploaded["url"], "assetId": uploaded["id"]}}}, "edges": []any{}, "viewport": response{"x": 0, "y": 0, "zoom": 1}}
|
||||
call("PATCH", workspacePath+"/materials/"+assetBoard["id"].(string)+"/canvas", tokens[0], response{"document": assetDoc}, 200, `"1"`, accept)
|
||||
download(tokens[1], 200)
|
||||
download(tokens[2], 404)
|
||||
// assetId is optional: moving a stable managed URL must still add a
|
||||
// durable binding for its new owning canvas.
|
||||
assetCanvasPath := workspacePath + "/materials/" + assetBoard["id"].(string) + "/canvas"
|
||||
delete(assetDoc["nodes"].([]any)[0].(response)["data"].(response), "assetId")
|
||||
call("PATCH", assetCanvasPath, tokens[0], response{"document": assetDoc}, 200, `"2"`, accept)
|
||||
assetFrame := call("POST", assetCanvasPath+"/subgraphs", tokens[0], response{"frame": response{"id": "asset-frame", "title": "Images", "position": response{"x": 0, "y": 0}, "width": 640, "height": 400, "labelMode": "fixed"}}, 201, `"3"`, accept, "40000000-0000-4000-8000-000000000001")
|
||||
assetChild := assetFrame["child_board"].(map[string]any)["id"].(string)
|
||||
call("POST", assetCanvasPath+"/subgraphs/asset-frame/members", tokens[0], response{"source_board_id": assetBoard["id"], "target_board_id": assetChild, "node_ids": []string{"asset-node"}, "expected_versions": map[string]int64{assetBoard["id"].(string): 4, assetChild: 1}}, 200, "", accept, "40000000-0000-4000-8000-000000000002")
|
||||
var managedBindings int
|
||||
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM canvas_asset_bindings WHERE project=$1 AND asset_id=$2 AND board_id=$3", project, uploaded["id"], assetChild).Scan(&managedBindings); err != nil || managedBindings != 1 {
|
||||
t.Fatalf("stable URL lost child asset binding: %v count=%d", err, managedBindings)
|
||||
}
|
||||
call("POST", assetCanvasPath+"/subgraphs/asset-frame/dissolve", tokens[0], response{"expected_versions": map[string]int64{assetBoard["id"].(string): 5, assetChild: 2}}, 200, "", accept, "40000000-0000-4000-8000-000000000003")
|
||||
download(tokens[1], 200)
|
||||
upload([]byte("not an image"), "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", 400)
|
||||
|
||||
limitBoard := call("POST", workspacePath+"/materials", tokens[0], response{"kind": "board", "title": "Limits", "content": response{}}, 201)
|
||||
limitPath := workspacePath + "/materials/" + limitBoard["id"].(string) + "/canvas"
|
||||
oversizedNodes := []any{}
|
||||
for i := 0; i < 501; i++ {
|
||||
oversizedNodes = append(oversizedNodes, response{"id": "limit-" + strconv.Itoa(i), "type": "material", "position": response{"x": 0, "y": 0}, "data": response{"kind": "text", "title": "", "text": ""}})
|
||||
}
|
||||
call("PATCH", limitPath, tokens[0], response{"document": response{"schema_version": 1, "nodes": oversizedNodes, "edges": []any{}, "viewport": response{"x": 0, "y": 0, "zoom": 1}}}, 413, `"1"`)
|
||||
if call("GET", limitPath, tokens[0], nil, 200)["version"] != float64(1) {
|
||||
t.Fatal("graph limit partially saved")
|
||||
}
|
||||
caseFrame := response{"frame": response{"id": "case-frame", "title": "Case", "position": response{"x": 0, "y": 0}, "width": 640, "height": 400, "labelMode": "fixed"}}
|
||||
upperKey := call("POST", limitPath+"/subgraphs", tokens[0], caseFrame, 201, `"1"`, accept, "ABCDEFAB-CDEF-4ABC-8DEF-ABCDEFABCDEF")
|
||||
lowerKey := call("POST", limitPath+"/subgraphs", tokens[0], caseFrame, 201, `"1"`, accept, "abcdefab-cdef-4abc-8def-abcdefabcdef")
|
||||
if upperKey["transaction_id"] != lowerKey["transaction_id"] {
|
||||
t.Fatal("UUID key case changed operation identity")
|
||||
}
|
||||
concurrentBoard := call("POST", workspacePath+"/materials", tokens[0], response{"kind": "board", "title": "Concurrent create", "content": response{}}, 201)
|
||||
concurrentPath := workspacePath + "/materials/" + concurrentBoard["id"].(string) + "/canvas/subgraphs"
|
||||
type concurrentResult struct {
|
||||
code int
|
||||
body []byte
|
||||
}
|
||||
results := make(chan concurrentResult, 4)
|
||||
startConcurrent := make(chan struct{})
|
||||
encodedFrame, _ := json.Marshal(caseFrame)
|
||||
for i := 0; i < 4; i++ {
|
||||
go func() {
|
||||
<-startConcurrent
|
||||
r := httptest.NewRequest("POST", concurrentPath, bytes.NewReader(encodedFrame))
|
||||
r.Header.Set("Authorization", "Bearer "+tokens[0])
|
||||
r.Header.Set("Accept", accept)
|
||||
r.Header.Set("If-Match", `"1"`)
|
||||
r.Header.Set("Idempotency-Key", "60000000-0000-4000-8000-000000000001")
|
||||
w := httptest.NewRecorder()
|
||||
mux.ServeHTTP(w, r)
|
||||
results <- concurrentResult{w.Code, append([]byte(nil), w.Body.Bytes()...)}
|
||||
}()
|
||||
}
|
||||
close(startConcurrent)
|
||||
var concurrentTransaction string
|
||||
for i := 0; i < 4; i++ {
|
||||
result := <-results
|
||||
var decoded response
|
||||
if result.code != 201 || json.Unmarshal(result.body, &decoded) != nil {
|
||||
t.Fatalf("concurrent create: %d %s", result.code, result.body)
|
||||
}
|
||||
id := decoded["transaction_id"].(string)
|
||||
if concurrentTransaction != "" && concurrentTransaction != id {
|
||||
t.Fatal("concurrent retry duplicated transaction")
|
||||
}
|
||||
concurrentTransaction = id
|
||||
}
|
||||
var concurrentChildren int
|
||||
if err := db.QueryRowContext(ctx, `SELECT count(*) FROM canvas_subgraphs WHERE project=$1 AND parent_board_id=$2`, project, concurrentBoard["id"]).Scan(&concurrentChildren); err != nil || concurrentChildren != 1 {
|
||||
t.Fatalf("concurrent retry duplicated children: %d %v", concurrentChildren, err)
|
||||
}
|
||||
rollbackBoard := call("POST", workspacePath+"/materials", tokens[0], response{"kind": "board", "title": "Rollback", "content": response{}}, 201)
|
||||
rollbackPath := workspacePath + "/materials/" + rollbackBoard["id"].(string) + "/canvas"
|
||||
nearLimit := []any{}
|
||||
for i := 0; i < 53; i++ {
|
||||
text := ""
|
||||
if i < 52 {
|
||||
text = strings.Repeat("x", 100000)
|
||||
}
|
||||
nearLimit = append(nearLimit, response{"id": "rollback-" + strconv.Itoa(i), "type": "material", "position": response{"x": 0, "y": 0}, "data": response{"kind": "text", "title": "", "text": text}})
|
||||
}
|
||||
rollbackDoc := response{"schema_version": 1, "nodes": nearLimit, "edges": []any{}, "viewport": response{"x": 0, "y": 0, "zoom": 1}}
|
||||
encodedRollback, _ := json.Marshal(rollbackDoc)
|
||||
remaining := canvasLimit - 300 - len(encodedRollback)
|
||||
if remaining <= 0 || remaining > 100000 {
|
||||
t.Fatal("rollback fixture budget")
|
||||
}
|
||||
nearLimit[52].(response)["data"].(response)["text"] = strings.Repeat("x", remaining)
|
||||
call("PATCH", rollbackPath, tokens[0], response{"document": rollbackDoc}, 200, `"1"`)
|
||||
var materialBeforeFailure, materialAfterFailure int
|
||||
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM materials WHERE project=$1", project).Scan(&materialBeforeFailure); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
call("POST", rollbackPath+"/subgraphs", tokens[0], caseFrame, 413, `"2"`, accept, "50000000-0000-4000-8000-000000000001")
|
||||
if err := db.QueryRowContext(ctx, "SELECT count(*) FROM materials WHERE project=$1", project).Scan(&materialAfterFailure); err != nil || materialAfterFailure != materialBeforeFailure {
|
||||
t.Fatal("failed parent validation left an orphan child")
|
||||
}
|
||||
if call("GET", rollbackPath, tokens[0], nil, 200)["version"] != float64(2) {
|
||||
t.Fatal("failed create changed parent version")
|
||||
}
|
||||
|
||||
email := "fixture@example.invalid"
|
||||
password := "a-long-fixture-password"
|
||||
call("POST", base+"/auth/register", "", response{"email": email, "password": password}, 202)
|
||||
|
||||
Reference in New Issue
Block a user