Files
felhom-controller/controller/internal/web/agent_host_metrics_lang_test.go
T

50 lines
2.0 KiB
Go

package web
import (
"crypto/sha256"
"encoding/hex"
"net/http"
"net/http/httptest"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
)
// W26 (rehearsal 2026-10-20 B6 #16), pinned at the handler: GET /api/host-metrics answers the storage
// labels in the REQUEST's language. TestEnrichHostStorageTargets_FollowsTheLanguage pins the lookup;
// this pins the wiring (the reader's language reaching it), through a fake agent behind the real pinned client.
func TestServeHostMetricsAPI_LabelsFollowTheRequestLanguage(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("GET /host/metrics", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"ok":true,"data":{"vmid":1,"host":{"node":"n"},"storage_targets":[
{"name":"local-lvm","type":"lvmthin","state":"attached","reachable":true},
{"name":"felhom-pbs","type":"pbs","state":"attached","reachable":true}]}}`))
})
agent := httptest.NewTLSServer(mux)
defer agent.Close()
sum := sha256.Sum256(agent.Certificate().Raw)
s := testServer(t)
s.cfg = &config.Config{LocalAPI: config.LocalAPIConfig{
Endpoint: strings.TrimPrefix(agent.URL, "https://"), Token: "tok", Fingerprint: hex.EncodeToString(sum[:])}}
get := func(lang string) string {
rec := httptest.NewRecorder()
s.ServeHostMetricsAPI(rec, httptest.NewRequest(http.MethodGet, "/api/host-metrics?lang="+lang, nil))
if rec.Code != http.StatusOK {
t.Fatalf("lang=%s: status %d body %s", lang, rec.Code, rec.Body.String())
}
return rec.Body.String()
}
en, hu := get("en"), get("hu")
if !strings.Contains(en, "Internal SSD") || !strings.Contains(en, "Off-site backup") {
t.Errorf("English answer lacks the English labels: %s", en)
}
if strings.Contains(en, "Belső") || strings.Contains(en, "Távoli") {
t.Errorf("English answer carries Hungarian labels: %s", en)
}
if !strings.Contains(hu, "Belső SSD") || !strings.Contains(hu, "Távoli biztonsági mentés") {
t.Errorf("Hungarian answer lacks the Hungarian labels: %s", hu)
}
}