Every view listing runs or approvals polled on a timer: the home page made four requests every five seconds per open tab, two of them the same run list, and the needs-attention card asked for 200 runs to render five. A run or an approval request moving is now published on the hub and framed on the dashboard stream, so those views re-read on the change. A reconnect still re-reads everything, and the hub drops a reader that falls behind, which ends its stream and reconnects it.
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/internal/platform/hub"
|
|
"github.com/samcharles93/archie-core/modules/chat"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// TestStreamTellsOnlyTheConversationsOwner guards the live stream's chat
|
|
// boundary: an action settled in a conversation is announced to its owner
|
|
// and to nobody else, administrators included.
|
|
func TestStreamTellsOnlyTheConversationsOwner(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
reader string
|
|
hears bool
|
|
}{
|
|
{"alice", true},
|
|
{"bob", false},
|
|
{string(governance.SystemID), false},
|
|
} {
|
|
t.Run(tc.reader, func(t *testing.T) {
|
|
s := openStream(t, tc.reader)
|
|
s.h.Publish(hub.Note{Topic: chat.OwnerTopic("alice")})
|
|
if heard := s.hears(`"topic":"chat"`); heard != tc.hears {
|
|
t.Fatalf("%s heard the update: %v, want %v", tc.reader, heard, tc.hears)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestStreamTellsEveryReaderWhenWorkMoves guards what replaced the dashboard's
|
|
// polling: a run or an approval request moving reaches every open dashboard,
|
|
// so a view re-reads on the change instead of on a timer.
|
|
func TestStreamTellsEveryReaderWhenWorkMoves(t *testing.T) {
|
|
s := openStream(t, "bob")
|
|
s.h.Publish(hub.Note{Topic: hub.WorkTopic})
|
|
if !s.hears(`"topic":"work"`) {
|
|
t.Fatal("a run or approval moving reached no reader")
|
|
}
|
|
}
|
|
|
|
// streamReader is one open dashboard stream, reading as one person.
|
|
type streamReader struct {
|
|
h *hub.Hub
|
|
lines *bufio.Scanner
|
|
}
|
|
|
|
// openStream starts a dashboard stream as reader, which ends when the test
|
|
// does.
|
|
func openStream(t *testing.T, reader string) *streamReader {
|
|
t.Helper()
|
|
h := hub.New()
|
|
mux := http.NewServeMux()
|
|
Register(httpapi.New(mux, "test", headerAuth{}, allowAll{}), h)
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
|
|
ctx, cancel := context.WithTimeout(t.Context(), 300*time.Millisecond)
|
|
t.Cleanup(cancel)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/api/stream", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-As", reader)
|
|
res, err := srv.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = res.Body.Close() })
|
|
return &streamReader{h: h, lines: bufio.NewScanner(res.Body)}
|
|
}
|
|
|
|
// hears reports whether a frame mentioning marker arrived before the stream
|
|
// ended.
|
|
func (s *streamReader) hears(marker string) bool {
|
|
for s.lines.Scan() {
|
|
if strings.Contains(s.lines.Text(), marker) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// headerAuth signs in whoever the X-As header names; the system identity is
|
|
// the instance administrator.
|
|
type headerAuth struct{}
|
|
|
|
func (headerAuth) Authenticate(ctx context.Context, r *http.Request) (context.Context, bool, error) {
|
|
id := governance.IdentityID(r.Header.Get("X-As"))
|
|
p := governance.Principal{IdentityID: id, Org: governance.DefaultOrgID}
|
|
if id == governance.SystemID {
|
|
p = governance.SharedTokenOwner()
|
|
}
|
|
return governance.WithPrincipal(ctx, p), p.InstanceAdmin(), nil
|
|
}
|
|
|
|
// allowAll leaves every decision to the handlers under test.
|
|
type allowAll struct{}
|
|
|
|
func (allowAll) Allow(context.Context, string, string) error { return nil }
|