Files
new-grok-glance/internal/hub/ring_test.go
T
iceBear67andClaude Opus 5 051efe8fec grok-glance: web control plane for grok's /rc remote control
A single Go binary that grok dials out to over a WebSocket, and a HeroUI
web UI for driving the session it is attached to.

The roles are inverted relative to the terminal: over the /rc link grok
is the ACP Agent and glance is the Client. That makes glance a stock ACP
client and the web Stop button a real session/cancel rather than a
bespoke control message.

Both notification rails are mirrored. The stable session/update rail
carries correctness; x.ai/session_notification is presentation only and
degrades rather than erroring, because its ~60 variants are grok
internal and drift with every upstream sync. _meta is forwarded
byte for byte so viewers can dedup and order.

Permissions race: the terminal and any browser may answer, first
responder wins, and the loser's UI retracts by itself. All three
interaction methods go through that path, not just permissions.

Auth is TOTP only, with no accounts to have. A bootstrap token printed
at first start gates /setup, which is a 404 without it; state lives in
one 0600 JSON file and history in an in-memory ring, so there is no
database and no recovery story beyond deleting the file.

ARCHITECTURE.md covers the topology and the limits of that auth model;
CLAUDE.md covers building, the fakeagent loop, and the end-to-end
checklist that unit tests cannot replace.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 10:07:33 +00:00

81 lines
2.0 KiB
Go

package hub
import (
"encoding/json"
"fmt"
"testing"
)
func frames(r *ring) []string {
out := make([]string, 0, r.size)
for _, f := range r.snapshot() {
out = append(out, string(f))
}
return out
}
func TestRingKeepsTheMostRecentFrames(t *testing.T) {
r := newRing(3)
if got := r.snapshot(); len(got) != 0 {
t.Fatalf("empty ring snapshot = %v", got)
}
for i := 1; i <= 5; i++ {
r.push(json.RawMessage(fmt.Sprintf(`{"n":%d}`, i)))
}
// A browser opening mid-session wants the end of the transcript, not the
// beginning, so the oldest frames are what fall off.
want := []string{`{"n":3}`, `{"n":4}`, `{"n":5}`}
got := frames(r)
if len(got) != len(want) {
t.Fatalf("snapshot = %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("snapshot = %v, want %v", got, want)
}
}
// The count of dropped frames is shown in the UI, so a viewer knows the
// transcript starts mid-stream rather than at the beginning of the session.
if r.dropped != 2 {
t.Fatalf("dropped = %d, want 2", r.dropped)
}
if r.size != 3 {
t.Fatalf("size = %d, want 3", r.size)
}
}
func TestRingSnapshotIsOrderedAcrossTheWrap(t *testing.T) {
r := newRing(4)
for i := 1; i <= 4; i++ {
r.push(json.RawMessage(fmt.Sprintf(`%d`, i)))
}
// Exactly full: no wrap yet.
if got := frames(r); got[0] != "1" || got[3] != "4" {
t.Fatalf("full ring = %v", got)
}
r.push(json.RawMessage(`5`))
got := frames(r)
// Replay is only useful if it is in order; an off-by-one at the wrap point
// would show the transcript scrambled rather than truncated.
for i, want := range []string{"2", "3", "4", "5"} {
if got[i] != want {
t.Fatalf("after wrap = %v, want [2 3 4 5]", got)
}
}
}
func TestRingReset(t *testing.T) {
r := newRing(2)
r.push(json.RawMessage(`1`))
r.push(json.RawMessage(`2`))
r.push(json.RawMessage(`3`))
r.reset()
if r.size != 0 || r.dropped != 0 || len(r.snapshot()) != 0 {
t.Fatalf("reset left size=%d dropped=%d len=%d", r.size, r.dropped, len(r.snapshot()))
}
}