Files
Vetting/internal/web/templates/host_tile_test.go
T
josh d0bfae14c8
CI / Lint + build + test (push) Has been cancelled
Heartbeat-first dispatch: retire WoL-as-default, add WaitingReboot
Every supported host runs vetting-reporter in-OS and heartbeats every
30s. WoL was never the thing that started vetting — the heartbeat
response's reboot_for_vetting command was. Firing WoL first only
crowded the run log with misleading diagnostics when the real failure
mode is "reporter isn't installed."

- StartRun 409s if the host hasn't heartbeated within 60s, pointing
  the operator at /register/quick.sh.
- Dispatcher re-checks LastSeenAt at dispatch time (run may sit in
  Queued long enough for the host to go offline); stale hosts mark
  the run Failed with failed_stage=dispatch instead of looping.
- New StateWaitingReboot + TriggerRebootCommanded capture the actual
  semantics. StateWaitingWoL kept as the hook point for a future
  manual-override button.
- Tile disables the Start button with a quick.sh tooltip when the
  host is offline, matching the server-side 409.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-18 01:10:34 -04:00

115 lines
3.8 KiB
Go

package templates
import (
"context"
"strings"
"testing"
"time"
"vetting/internal/model"
)
func TestHumanAgoFrom(t *testing.T) {
now := time.Date(2026, 4, 17, 12, 0, 0, 0, time.UTC)
cases := []struct {
name string
ago time.Duration
want string
}{
{"just now", 5 * time.Second, "online"},
{"edge-just-under-minute", 59 * time.Second, "online"},
{"one minute", 60 * time.Second, "1m ago"},
{"five minutes", 5 * time.Minute, "5m ago"},
{"fifty-nine minutes", 59 * time.Minute, "59m ago"},
{"one hour", 1 * time.Hour, "1h ago"},
{"eight hours", 8 * time.Hour, "8h ago"},
{"one day", 24 * time.Hour, "1d ago"},
{"three days", 72 * time.Hour, "3d ago"},
// Clock skew: "future" heartbeat clamps to "online" rather than
// printing "-3m ago" or panicking.
{"future clamps to online", -5 * time.Second, "online"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := humanAgoFrom(now, now.Add(-tc.ago))
if got != tc.want {
t.Fatalf("humanAgoFrom(%v) = %q, want %q", tc.ago, got, tc.want)
}
})
}
}
// TestHostTile_OverlayLink asserts the tile includes the tile-link <a>
// that makes the whole card clickable. The action button stays a
// sibling element, so CSS (z-index) keeps it on top of the overlay.
//
// Heartbeat must be fresh because canStart now gates on LastSeenAt —
// an offline host renders a disabled button (no form), which is
// covered by TestHostTile_DisabledStartWhenOffline below.
func TestHostTile_OverlayLink(t *testing.T) {
now := time.Now()
data := TileData{
Host: model.Host{ID: 42, Name: "tile-test", MAC: "aa:bb:cc:dd:ee:ff"},
LastSeenAt: &now,
}
var buf strings.Builder
if err := HostTile(data).Render(context.Background(), &buf); err != nil {
t.Fatalf("render: %v", err)
}
html := buf.String()
if !strings.Contains(html, `href="/hosts/42"`) {
t.Fatalf("tile missing overlay href: %s", html)
}
if !strings.Contains(html, `class="tile-link"`) {
t.Fatalf("tile missing tile-link class: %s", html)
}
// Fresh heartbeat + no run → Start form must render.
if !strings.Contains(html, `/hosts/42/start`) {
t.Fatalf("expected Start vetting form in tile: %s", html)
}
// Dropped content that used to live on the tile — confirm it has
// actually moved off so the slim-down is real.
for _, dropped := range []string{`tile-meta`, `tile-log`, `tile-actions`, `tile-hold`} {
if strings.Contains(html, dropped) {
t.Errorf("slim tile still contains dropped class %q", dropped)
}
}
}
// TestHostTile_DisabledStartWhenOffline: no heartbeat → disabled button
// with the quick.sh tooltip, not a submittable form. Mirrors the
// server-side StartRun 409 so the UI matches the handler.
func TestHostTile_DisabledStartWhenOffline(t *testing.T) {
data := TileData{
Host: model.Host{ID: 42, Name: "tile-test", MAC: "aa:bb:cc:dd:ee:ff"},
}
var buf strings.Builder
if err := HostTile(data).Render(context.Background(), &buf); err != nil {
t.Fatalf("render: %v", err)
}
html := buf.String()
if strings.Contains(html, `/hosts/42/start`) {
t.Fatalf("offline host should not expose a Start form: %s", html)
}
if !strings.Contains(html, `disabled`) || !strings.Contains(html, `quick.sh`) {
t.Fatalf("expected disabled Start button with quick.sh tooltip: %s", html)
}
}
func TestLastSeenLabelAndClass(t *testing.T) {
if got := lastSeenLabel(nil); got != "never" {
t.Fatalf("label nil = %q, want never", got)
}
if got := lastSeenClass(nil); got != "offline" {
t.Fatalf("class nil = %q, want offline", got)
}
recent := time.Now().Add(-5 * time.Second)
if got := lastSeenClass(&recent); got != "online" {
t.Fatalf("class recent = %q, want online", got)
}
stale := time.Now().Add(-10 * time.Minute)
if got := lastSeenClass(&stale); got != "stale" {
t.Fatalf("class stale = %q, want stale", got)
}
}