package main import ( "context" "net/http" "net/http/httptest" "testing" "time" ) // The generic HTTP board must actually be built from its URLs. // // It was not: buildDeviceDriver had no case for it, so it fell through to the // WebSwitch driver. The board was configured, saved, listed — and every command // went to a WebSwitch address that did not exist, which also left the device // reported as offline and every relay button on the panel greyed out. Nothing in // the UI said so; the URLs were simply never sent. func TestGenericHTTPBoardSendsItsConfiguredURL(t *testing.T) { hit := make(chan string, 4) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hit <- r.URL.Path })) defer srv.Close() // Host deliberately empty — this board's whole address is in the URLs. d := StationDevice{ Type: "httpgen", Channels: 2, OnURLs: []string{srv.URL + "/relay1/on", srv.URL + "/relay2/on"}, OffURLs: []string{srv.URL + "/relay1/off", srv.URL + "/relay2/off"}, } if err := buildDeviceDriver(d).Set(context.Background(), 2, true); err != nil { t.Fatalf("Set: %v", err) } select { case got := <-hit: if got != "/relay2/on" { t.Errorf("board was asked for %q, want /relay2/on", got) } case <-time.After(3 * time.Second): t.Fatal("the configured URL was never requested — the board is not using its own driver") } } // Editing a URL must rebuild the driver. The cached one is keyed by the device's // configuration, and the URLs used not to be part of that key: correcting a typo // handed back the driver still holding the old address, so the fix looked like it // had done nothing until OpsLog was restarted. func TestGenericHTTPBoardKeyCoversItsURLs(t *testing.T) { a := StationDevice{Type: "httpgen", Channels: 2, OnURLs: []string{"http://box/a"}} b := StationDevice{Type: "httpgen", Channels: 2, OnURLs: []string{"http://box/b"}} if deviceKey(a) == deviceKey(b) { t.Error("two boards with different URLs share a cache key — an edited URL would not take effect") } c := a c.OnPat = "http://box/{relay}" if deviceKey(a) == deviceKey(c) { t.Error("changing the ON pattern left the cache key unchanged") } }