package edge import ( "context" "net/http/httptest" "testing" "time" "github.com/carlosframework/platform/internal/domains" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" ) // TestPathFlipEndToEnd walks the whole chain a real flip takes — claim // published, reconcile pass, fingerprint moves, edge reloads, traffic changes // — and then walks it backwards. Each earlier task proved one layer against a // hand-written input; this is the only test that proves they compose. // // The registry write and the edge reload are driven directly rather than // waited on: the 2s tick is a latency bound, not a correctness property, and // polling it would make this test slow and flaky. What must be true is that // the fingerprint MOVES, which is asserted rather than assumed. func TestPathFlipEndToEnd(t *testing.T) { ctx := context.Background() classic, _ := hostEchoBackend(t, "classic") golive, _ := hostEchoBackend(t, "go") reg := openReg(t) if err := reg.Upsert(registry.Route{ Host: "ti.to", Kind: "service", Account: "tito", App: "classic", Addr: classic.Listener.Addr().String(), Status: "ready", }); err != nil { t.Fatal(err) } if err := reg.Upsert(registry.Route{ Host: "go.tito.io", Kind: "service", Account: "tito", App: "go", Addr: golive.Listener.Addr().String(), Status: "ready", }); err != nil { t.Fatal(err) } store := release.NewDirStore(t.TempDir()) if _, _, err := domains.AttachClaim(ctx, store, domains.Claim{ Schema: 1, Host: "ti.to", Account: "tito", App: "classic", Route: "ti.to", CreatedAt: "2026-09-03T00:00:00Z", }); err != nil { t.Fatal(err) } e, err := New(reg, nil) if err != nil { t.Fatal(err) } srv := httptest.NewServer(e) defer srv.Close() // Before the flip: everything is classic. if _, body := getHostPath(t, srv.URL, "ti.to", "/paulca/awesomeconf"); body != "classic" { t.Fatalf("before the flip: body = %q, want classic", body) } fpBefore, err := reg.Fingerprint() if err != nil { t.Fatal(err) } // The flip: Tito publishes its map, the pass folds it, the edge reloads. if _, err := domains.SetClaimPaths(ctx, store, "ti.to", "tito", "classic", map[string]string{"paulca": "go.tito.io"}); err != nil { t.Fatal(err) } if err := domains.ReconcileAliases(ctx, store, reg, time.Now, nil, nil); err != nil { t.Fatal(err) } fpAfter, err := reg.Fingerprint() if err != nil { t.Fatal(err) } if fpBefore == fpAfter { t.Fatal("fingerprint did not move across a flip — the edge's watcher would never reload") } if err := e.Reload(); err != nil { t.Fatal(err) } // After the flip: that account is on Go, its neighbours are not. if _, body := getHostPath(t, srv.URL, "ti.to", "/paulca/awesomeconf"); body != "go" { t.Fatalf("after the flip: body = %q, want go", body) } if _, body := getHostPath(t, srv.URL, "ti.to", "/someoneelse/event"); body != "classic" { t.Fatalf("after the flip: a neighbouring account moved too, body = %q", body) } if _, body := getHostPath(t, srv.URL, "ti.to", "/paulcarson"); body != "classic" { t.Fatalf("after the flip: a longer account name was dragged along, body = %q", body) } // The rollback: republish without that account. if _, err := domains.SetClaimPaths(ctx, store, "ti.to", "tito", "classic", nil); err != nil { t.Fatal(err) } if err := domains.ReconcileAliases(ctx, store, reg, time.Now, nil, nil); err != nil { t.Fatal(err) } if err := e.Reload(); err != nil { t.Fatal(err) } if _, body := getHostPath(t, srv.URL, "ti.to", "/paulca/awesomeconf"); body != "classic" { t.Fatalf("after the rollback: body = %q, want classic", body) } rows, err := reg.ListPaths() if err != nil { t.Fatal(err) } if len(rows) != 0 { t.Fatalf("after the rollback: %d path rows remain, want 0", len(rows)) } }