package web import ( "bytes" "html/template" "strings" "testing" "gitea.dooplex.hu/admin/felhom-controller/internal/system" ) // R-259 — a disk we could not measure must not be drawn as a healthy empty one. // // These render the REAL dashboard template block against a real SystemInfo, because the defect was // entirely in what the markup does with a zero. A test on the Go struct alone cannot see it. // diskMeterBlock EXTRACTS the system-disk block from the SHIPPED dashboard.html, rather than // duplicating it here. A copied block drifts, and a drifted copy is a test that passes while the // page it claims to cover has changed — the fixture-is-not-the-wire mistake, which this project has // now hit twice (R-262, and the OOB fixture in hub v0.99.0). func diskMeterBlock(t *testing.T) string { t.Helper() raw, err := templateFS.ReadFile("templates/dashboard.html") if err != nil { t.Fatalf("read dashboard.html: %v", err) } src := string(raw) const startMark = `{{if not .SystemInfo.DiskKnown}}` i := strings.Index(src, startMark) if i < 0 { t.Fatalf("the DiskKnown guard is not in dashboard.html — R-259's fix is not in the shipped markup") } // the guard's matching {{end}} is the one closing the if/else chain: take through the // "Kritikusan kevés hely" branch and its two closers const endMark = `Kritikusan kevés hely{{end}}` j := strings.Index(src[i:], endMark) if j < 0 { t.Fatal("could not find the end of the disk meter block") } block := src[i : i+j+len(endMark)] // close the outer if/else opened by startMark return block + "\n \n {{end}}" } // renderDiskMeter renders just the system-disk block of dashboard.html with the production funcmap. func renderDiskMeter(t *testing.T, info system.SystemInfo) string { t.Helper() // the block under test, copied verbatim from dashboard.html by the guard below src := diskMeterBlock(t) tpl, err := template.New("m").Funcs((&Server{}).templateFuncMap()).Parse(src) if err != nil { t.Fatalf("parse: %v", err) } var buf bytes.Buffer if err := tpl.Execute(&buf, map[string]any{"SystemInfo": info}); err != nil { t.Fatalf("execute: %v", err) } return buf.String() } func healthyDisk() system.SystemInfo { return system.SystemInfo{ DiskTotalGB: 100, DiskUsedGB: 42, DiskAvailGB: 58, DiskPercent: 42, DiskKnown: true, } } // SCENARIO D — a disk we could not read says so, and draws nothing. func TestDiskMeter_UnknownDrawsNoPictureAndSaysSo(t *testing.T) { // exactly what a failed statfs leaves behind: every figure zero, and now DiskKnown=false out := renderDiskMeter(t, system.SystemInfo{DiskKnown: false}) if strings.Contains(out, "0.0 GB") || strings.Contains(out, "(0%)") { t.Errorf("a failed measurement printed a figure — „0.0 GB / 0.0 GB (0%%)\" is R-259, "+ "and it is the healthy-looking picture of an unread disk.\n%s", out) } if strings.Contains(out, "meter-fill") { t.Errorf("a meter fill was drawn for a disk that was never measured — a 0%%-wide bar over an "+ "unread store is a picture of emptiness, and a picture is a claim.\n%s", out) } if strings.Contains(out, "nominal") { t.Errorf("an unmeasured disk was coloured as healthy.\n%s", out) } if !strings.Contains(out, "nem olvashat") { // ASCII-safe fragment of „nem olvasható ki" t.Errorf("the customer is not told the measurement could not be taken.\n%s", out) } } // SCENARIO E — a disk we CAN read is unchanged, including its colour band. func TestDiskMeter_KnownIsUnchanged(t *testing.T) { out := renderDiskMeter(t, healthyDisk()) // fmtGB: >=100 renders whole, >=10 renders one decimal (funcmap.go:208-215) for _, want := range []string{"42.0 GB", "100 GB", "(42%)", "meter-fill", "nominal"} { if !strings.Contains(out, want) { t.Errorf("a healthy box lost %q from its meter — this change must be invisible on a "+ "machine that is fine.\n%s", want, out) } } if strings.Contains(out, "nem olvashat") { t.Errorf("a healthy box gained the could-not-measure caveat.\n%s", out) } } // The colour bands still work on a known disk — the guard must not have swallowed them. func TestDiskMeter_KnownStillBands(t *testing.T) { crit := healthyDisk() crit.DiskPercent = 92 out := renderDiskMeter(t, crit) if !strings.Contains(out, "crit") || !strings.Contains(out, "Kritikusan") { t.Errorf("a critically full KNOWN disk lost its warning.\n%s", out) } }