Files
worker/internal/webapp/handlers_peer_test.go
Gleb Tv 2c7a0236da feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
2026-07-13 17:55:14 +03:00

95 строки
3.2 KiB
Go

package webapp
import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestHandlePeerStatus_NoProbe covers the first-boot window: the
// runner has not yet produced a selfcheck verdict, so the endpoint
// must return a valid JSON body with up=null and observed_at=null.
func TestHandlePeerStatus_NoProbe(t *testing.T) {
srv := newTestServer(t, &stubRunner{id: "w-1"})
ts := newHTTPTestServer(t, srv)
resp, err := http.Get(ts.URL + "/api/peer/status")
require.NoError(t, err)
defer resp.Body.Close() //nolint:errcheck
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var got peerStatusResponse
require.NoError(t, json.Unmarshal(body, &got))
assert.Equal(t, "w-1", got.WorkerID)
assert.Nil(t, got.Up, "up must be nil before the first probe")
assert.Nil(t, got.ObservedAt, "observed_at must be nil before the first probe")
}
// TestHandlePeerStatus_UpAndDown covers the post-probe window: the
// endpoint must reflect the most recent selfcheck verdict.
func TestHandlePeerStatus_UpAndDown(t *testing.T) {
up := true
at := time.Date(2026, 7, 10, 16, 0, 0, 0, time.UTC)
srv := newTestServer(t, &stubRunner{id: "w-2", masterUp: &up, masterAt: at})
ts := newHTTPTestServer(t, srv)
resp, err := http.Get(ts.URL + "/api/peer/status")
require.NoError(t, err)
defer resp.Body.Close() //nolint:errcheck
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var got peerStatusResponse
require.NoError(t, json.Unmarshal(body, &got))
assert.Equal(t, "w-2", got.WorkerID)
require.NotNil(t, got.Up)
assert.True(t, *got.Up)
require.NotNil(t, got.ObservedAt)
assert.Equal(t, at, *got.ObservedAt)
// Flip to down and re-fetch; the handler reads from the live
// stub view, not a cached copy, so the new verdict must surface.
down := false
runner := srv.deps.Runner.(*stubRunner) //nolint:forcetypeassert // helper under test
runner.masterUp = &down
runner.masterAt = at.Add(time.Minute)
resp2, err := http.Get(ts.URL + "/api/peer/status")
require.NoError(t, err)
defer resp2.Body.Close() //nolint:errcheck
var got2 peerStatusResponse
require.NoError(t, json.NewDecoder(resp2.Body).Decode(&got2))
require.NotNil(t, got2.Up)
assert.False(t, *got2.Up)
require.NotNil(t, got2.ObservedAt)
assert.Equal(t, at.Add(time.Minute), *got2.ObservedAt)
}
// TestHandlePeerStatus_DoesNotRequireSession confirms the slice-1
// design: the endpoint sits outside /web/api/* so the basic-auth
// middleware does not intercept it and no session cookie is needed.
// The handler should still answer 200 OK when the runner is wired.
func TestHandlePeerStatus_DoesNotRequireSession(t *testing.T) {
srv := newTestServer(t, &stubRunner{id: "w-3"})
ts := newHTTPTestServer(t, srv)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, ts.URL+"/api/peer/status", nil)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close() //nolint:errcheck
assert.Equal(t, http.StatusOK, resp.StatusCode,
"peer status must be reachable without a session cookie (slice 1)")
}