package api import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" dbpkg "crypto-miner-server/internal/db" ) func TestExportSSMSpreadBundle(t *testing.T) { root := integrationWorkspaceRoot(t) database, err := dbpkg.New(t.TempDir()) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = database.Close() }) hub := NewWSHub(database) h := NewSpreadHandler(database, t.TempDir(), root, hub) body, _ := json.Marshal(map[string]string{ "server_url": "https://deck.example", "build_id": "pin-ssm", "campaign": "aws-ssm-wave", "platform": "linux", }) req := httptest.NewRequest(http.MethodPost, "/api/v1/builder/ssm-spread-bundle", bytes.NewReader(body)) rec := httptest.NewRecorder() h.ExportSSMSpreadBundle(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) } var resp struct { OK bool `json:"ok"` Bundle SSMSpreadBundle `json:"bundle"` } if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatal(err) } if !resp.OK || resp.Bundle.JoinLane != "ssm_document" { t.Fatalf("bundle=%+v", resp.Bundle) } for _, marker := range []string{"https://deck.example", "pin-ssm", "aws-ssm-wave", "fetchErasureShards"} { if !strings.Contains(resp.Bundle.Document, marker) { t.Fatalf("document missing %q: %s", marker, resp.Bundle.Document) } } if !strings.Contains(resp.Bundle.RunCommand, "deck.example") { t.Fatalf("run_command=%s", resp.Bundle.RunCommand) } if !strings.Contains(resp.Bundle.CreateDocumentCLI, "create-document") { t.Fatalf("cli=%s", resp.Bundle.CreateDocumentCLI) } } func TestExportSSMSpreadBundleRequiresServerURL(t *testing.T) { root := integrationWorkspaceRoot(t) h := NewSpreadHandler(nil, t.TempDir(), root, nil) req := httptest.NewRequest(http.MethodPost, "/api/v1/builder/ssm-spread-bundle", strings.NewReader(`{"build_id":"b1"}`)) rec := httptest.NewRecorder() h.ExportSSMSpreadBundle(rec, req) if rec.Code != http.StatusBadRequest { t.Fatalf("status=%d", rec.Code) } } func TestCloudTemplatePathsSSMDocument(t *testing.T) { subdir, zip, err := cloudTemplatePaths("ssm-document") if err != nil || subdir != "ssm-document" || zip != "aetherforge-ssm-document.zip" { t.Fatalf("subdir=%q zip=%q err=%v", subdir, zip, err) } }