// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved. package main import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) func TestPage(t *testing.T) { h := newHarness(t, Options{}) cb, hp, _ := h.home() h.core.Seen("ff00aa", "Bosch", "Compress 6800i AW", "HeatGenerationSystem", "") h.core.PairingRequest("ff00aa") p := &Page{core: h.core, peers: []string{"172.30.32.2"}, ha: true} get := func(peer, lang string) *httptest.ResponseRecorder { r := httptest.NewRequest("GET", "/", nil) r.RemoteAddr = peer + ":5555" r.Header.Set("Accept-Language", lang) w := httptest.NewRecorder() p.ServeHTTP(w, r) return w } if w := get("192.0.2.9", "en"); w.Code != http.StatusForbidden { t.Fatalf("anyone but the ingress proxy: %d", w.Code) } en := get("172.30.32.2", "en-GB,en;q=0.9").Body.String() for _, want := range []string{`id="` + hp.ID + `"`, `id="` + cb.ID + `"`, "Found on the network", "asks to be paired", "ff00aa", "sealed", "Grid operator's limit", "protected", `src="page.js"`} { if !strings.Contains(en, want) { t.Fatalf("page lacks %q", want) } } // a sealed control box has no unpair button and no controls to set sec := en[strings.Index(en, `id="`+cb.ID+`"`):] sec = sec[:strings.Index(sec, "")] if strings.Contains(sec, `value="unpair"`) { t.Fatal("unpair offered for a sealed control box") } de := get("172.30.32.2", "de-DE,de").Body.String() if !strings.Contains(de, "Gekoppelte Geräte") || !strings.Contains(de, "Im Netz gefunden") { t.Fatal("German") } // pairing from the page, by the HA user named in ingress's headers form := url.Values{"action": {"pair"}, "ski": {"ff00aa"}} r := httptest.NewRequest("POST", "/", strings.NewReader(form.Encode())) r.RemoteAddr = "172.30.32.2:1" r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.Header.Set("X-Remote-User-Display-Name", "Dana") r.Header.Set("X-Ingress-Path", "/api/hassio_ingress/abc") w := httptest.NewRecorder() p.ServeHTTP(w, r) if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/api/hassio_ingress/abc/" { t.Fatalf("redirect %d %q", w.Code, w.Header().Get("Location")) } if d := h.core.devices["ff00aa"]; !d.Paired || d.Profile != "eebus-bosch-compress" || !strings.Contains(d.PairedBy, "Dana") { t.Fatalf("paired from the page: %+v", d) } // page.js for the panel's frame (its own scripts only) r = httptest.NewRequest("GET", "/page.js", nil) r.RemoteAddr = "172.30.32.2:1" w = httptest.NewRecorder() p.ServeHTTP(w, r) if !strings.Contains(w.Body.String(), "location.hash") { t.Fatal("page.js") } } // The page's catalogues: every text in English and German, none empty, the // same placeholders; the catalogue's value names in German too. func TestPageTextsBothLanguages(t *testing.T) { for k, en := range texts["en"] { de, ok := texts["de"][k] if !ok || de == "" || en == "" { t.Errorf("text %q: en %q, de %q", k, en, de) } if strings.Count(en, "{}") != strings.Count(de, "{}") { t.Errorf("text %q: placeholders differ", k) } } for k := range texts["de"] { if _, ok := texts["en"][k]; !ok { t.Errorf("text %q only in German", k) } } for _, s := range Catalogue { if s.Name == "" || s.NameDE == "" { t.Errorf("value %s: name %q, German %q", s.Key, s.Name, s.NameDE) } } } // docs/parameters.md U3: the page says what is declared toward the control // box, or why nothing is, and takes the owner's rating. func TestPageLPCAndRating(t *testing.T) { h := newHarness(t, Options{}) p := &Page{core: h.core, peers: []string{"172.30.32.2"}, ha: true} do := func(method string, form url.Values) string { r := httptest.NewRequest(method, "/", strings.NewReader(form.Encode())) r.RemoteAddr = "172.30.32.2:1" r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.Header.Set("Accept-Language", "en") w := httptest.NewRecorder() p.ServeHTTP(w, r) return w.Body.String() } if b := do("GET", nil); !strings.Contains(b, "LPC not offered: nominal power unknown: no controllable device (heat pump, wallbox) paired.") { t.Fatal("no device: refused, said") } hp := h.pair("hp01", "Vaillant", "aroTHERM plus", "HeatPumpAppliance", "Heat pump") b := do("GET", nil) if !strings.Contains(b, "nominal power unknown: enter the controllable devices' ratings (Heat pump)") || !strings.Contains(b, `name="action" value="rating"`) { t.Fatal("a heat pump without its power: refused, the rating form") } do("POST", url.Values{"action": {"rating"}, "dev": {hp.ID}, "kw": {"7,5"}}) if hp.RatedW != 7500 { t.Fatalf("rated: %v", hp.RatedW) } if b := do("GET", nil); !strings.Contains(b, "Toward the control box: nominal power 7500 W declared (LPC offered).") || !strings.Contains(b, `value="7.5"`) { t.Fatal("declared") } }