package web import ( "encoding/json" "io" "log" "reflect" "strings" "testing" "gitea.dooplex.hu/admin/felhom-controller/internal/config" "gitea.dooplex.hu/admin/felhom-controller/internal/monitor" "gitea.dooplex.hu/admin/felhom-controller/internal/report" ) // R-553 — the „your data is not on a separate drive" warning is shown quietly, inline under the // storage bars on two pages. Which warning that is used to be decided by looking for „meghajtó" in // the sentence; slice 2 translates it, and from that day the warning appears in the RED BANNER on // every page of the dashboard instead — a household that changed nothing suddenly reads an alarm. // // RED-PROOF (REPORT): put the strings.Contains(w, "meghajtón"/"adattároló"/"meghajtó") test back in // alerts.go and the TRANSLATED row below fails (Inline=false, no PageOnly). func TestR553_DiskWarningPlacementSurvivesWordingChange(t *testing.T) { cases := []struct { name string text, kind string wantInline bool }{ {"as shipped", "Az adattároló (/mnt/hdd) nem külön meghajtón van — az adatok a rendszermeghajtóra íródnak", monitor.WarnKindStorageNotSeparate, true}, {"TRANSLATED", "The data storage (/mnt/hdd) is not on a separate drive — the data is written to the system drive", monitor.WarnKindStorageNotSeparate, true}, {"a CPU warning stays in the banner", "CPU usage high: 91%", "", false}, {"a disconnected drive stays in the banner (its own alert covers it)", "Meghajtó leválasztva: Külső (/mnt/usb)", monitor.WarnKindStorageDisconnected, false}, {"a warning that merely CONTAINS the old words is not moved", "A meghajtó ellenőrzése nem futott le", "", false}, } for _, c := range cases { am := NewAlertManager(log.New(io.Discard, "", 0)) hr := &monitor.HealthReport{Status: "warn", Warnings: []string{c.text}, WarningKinds: []string{c.kind}} cfg := &config.Config{} cfg.Hub.Enabled = false am.Refresh(hr, cfg, nil, false, "") var found *Alert for i, a := range am.GetAlerts("hu") { if a.Message == c.text { found = &am.GetAlerts("hu")[i] } } if found == nil { t.Fatalf("%s: the warning did not reach the alerts at all", c.name) } if found.Inline != c.wantInline { t.Errorf("%s: Inline = %v, want %v (message %q)", c.name, found.Inline, c.wantInline, c.text) } wantPages := 0 if c.wantInline { wantPages = 2 } if len(found.PageOnly) != wantPages { t.Errorf("%s: PageOnly = %v, want %d page(s)", c.name, found.PageOnly, wantPages) } if c.wantInline && (found.ID != "disk-not-separate" || strings.Join(found.PageOnly, ",") != "dashboard,monitoring") { t.Errorf("%s: id/pages changed: %q %v", c.name, found.ID, found.PageOnly) } } } // THE WIRE IS SACRED — the kinds are an internal field and must not reach the hub. The report's // health block is its own struct; this pins that it carries exactly three fields, their JSON names, // and that a health report WITH kinds marshals to the same bytes as one without. // // RED-PROOF (REPORT): add `WarningKinds []string \`json:"warning_kinds"\“ to report.HealthReport and // both halves fail. func TestR553_HubReportWarningsAreUnchangedOnTheWire(t *testing.T) { rt := reflect.TypeOf(report.HealthReport{}) var names []string for i := 0; i < rt.NumField(); i++ { names = append(names, rt.Field(i).Tag.Get("json")) } if got, want := strings.Join(names, ","), "status,issues,warnings"; got != want { t.Errorf("the hub's health block changed shape: %q, want %q", got, want) } // The same warnings, one report built the way builder.go builds it (Status/Issues/Warnings only). src := &monitor.HealthReport{ Status: "warn", Issues: []string{}, Warnings: []string{"CPU usage high: 91%", "Az adattároló (/mnt/hdd) nem külön meghajtón van"}, WarningKinds: []string{"", monitor.WarnKindStorageNotSeparate}, } wire := report.HealthReport{Status: src.Status, Issues: src.Issues, Warnings: src.Warnings} b, err := json.Marshal(wire) if err != nil { t.Fatal(err) } const want = `{"status":"warn","issues":[],"warnings":["CPU usage high: 91%","Az adattároló (/mnt/hdd) nem külön meghajtón van"]}` if string(b) != want { t.Errorf("the hub report's health bytes CHANGED:\n got %s\nwant %s", b, want) } if strings.Contains(string(b), "kind") { t.Error("a warning KIND reached the wire — the hub contract gained a field nobody agreed to") } }