All checks were successful
Sync infra to hosts / sync-beta (push) Successful in 8s
Sync infra to hosts / sync-prod (push) Successful in 7s
secrets-guard / encrypted (push) Successful in 8s
shell-lint / shellcheck (push) Successful in 10s
Build + push backend image (Forgejo registry) / build-push (push) Successful in 1m14s
Deploy backend to beta VPS / deploy (push) Successful in 2m4s
The gateway bot and APScheduler were long-lived stateful I/O loops running inside the async web app under a leader election. They move into a single Go binary that owns ONLY that I/O -- websocket, RESUME, heartbeat, backoff, timers. It owns no grading logic. Anything needing data calls back over a new internal-only surface (/internal/discord/grade, /internal/jobs/*). Grading depends on polars and the parquet cache; reimplementing it in Go would let the bot's grades drift from the API's. The grade route returns gateway-ready JSON and Go relays the bytes verbatim. The binary ships in the backend image and runs as a second compose service off the same tag, so the two ends of the /internal/* contract can never skew. deploy.sh rolls daemon alongside backend -- without that the service would never be created, since a single-service deploy uses --no-deps. It also probes the image first and skips the daemon when rolling a tag that predates the binary: infra tracks main while image tags are env-staged, so a host can legitimately be asked to roll an older backend image, and creating the service anyway would leave a container crash-looping on a missing binary. replicas: 1 with order: stop-first replaces the leader election -- Discord permits one gateway connection per bot token. THERMOGRAPH_INTERNAL_TOKEN is optional: both ends derive it from THERMOGRAPH_AUTH_SECRET via HMAC under a domain-separation label, so this needs no new vault entry. The derivation is pinned to a shared cross-language test vector asserted on both sides, so drift fails CI instead of 401ing every call. Fail closed when neither secret is set. Improvements over the Python: a close intended for RESUME uses 4000 rather than 1000 (Discord invalidates a session closed 1000, so the old default defeated its own resume); MESSAGE_CREATE runs on a bounded worker pool; and a malformed HELLO returns an error rather than a clean reconnect, which would otherwise reset backoff and hot-loop against the gateway. 365 Python tests pass; Go build/vet/test -race clean; shellcheck 0 findings.
278 lines
8.3 KiB
Go
278 lines
8.3 KiB
Go
// Reply-path tests: a fake Grader plus an httptest server standing in for
|
|
// Discord's REST API. No live gateway is involved anywhere.
|
|
|
|
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type fakeGrader struct {
|
|
mu sync.Mutex
|
|
resp json.RawMessage
|
|
err error
|
|
calls []string
|
|
}
|
|
|
|
func (f *fakeGrader) Grade(_ context.Context, query string) (json.RawMessage, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.calls = append(f.calls, query)
|
|
return f.resp, f.err
|
|
}
|
|
|
|
func (f *fakeGrader) callCount() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return len(f.calls)
|
|
}
|
|
|
|
type captured struct {
|
|
method, path, auth string
|
|
body []byte
|
|
}
|
|
|
|
// newTestBot wires a Bot to a Grader and an httptest stand-in for Discord.
|
|
func newTestBot(t *testing.T, g Grader, handler http.HandlerFunc) *Bot {
|
|
t.Helper()
|
|
srv := httptest.NewServer(handler)
|
|
t.Cleanup(srv.Close)
|
|
b := New("tok-123", g)
|
|
b.rest = srv.URL
|
|
return b
|
|
}
|
|
|
|
// capture records each request and replies 200 {}.
|
|
func capture(got chan<- captured) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
got <- captured{r.Method, r.URL.Path, r.Header.Get("Authorization"), body}
|
|
_, _ = w.Write([]byte(`{}`))
|
|
}
|
|
}
|
|
|
|
func TestGradeReplyIsRelayedVerbatimAsAReply(t *testing.T) {
|
|
grade := json.RawMessage(`{"embeds":[{"title":"grade:Phoenix"}]}`)
|
|
got := make(chan captured, 1)
|
|
b := newTestBot(t, &fakeGrader{resp: grade}, capture(got))
|
|
|
|
b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)
|
|
|
|
c := <-got
|
|
if c.method != http.MethodPost || c.path != "/channels/77/messages" {
|
|
t.Fatalf("want POST /channels/77/messages, got %s %s", c.method, c.path)
|
|
}
|
|
if c.auth != "Bot tok-123" {
|
|
t.Fatalf("want bot-token auth, got %q", c.auth)
|
|
}
|
|
var top map[string]json.RawMessage
|
|
if err := json.Unmarshal(c.body, &top); err != nil {
|
|
t.Fatalf("reply body not JSON: %v", err)
|
|
}
|
|
// The embed must be byte-for-byte what Python returned — the verbatim
|
|
// relay is the no-drift guarantee.
|
|
if string(top["embeds"]) != `[{"title":"grade:Phoenix"}]` {
|
|
t.Fatalf("embeds not relayed verbatim: %s", top["embeds"])
|
|
}
|
|
if string(top["message_reference"]) != `{"message_id":"42"}` {
|
|
t.Fatalf("bad message_reference: %s", top["message_reference"])
|
|
}
|
|
// The locked-down allowed_mentions is the anti-@everyone control; it must
|
|
// be present exactly.
|
|
if string(top["allowed_mentions"]) != `{"parse":[],"replied_user":true}` {
|
|
t.Fatalf("bad allowed_mentions: %s", top["allowed_mentions"])
|
|
}
|
|
}
|
|
|
|
func TestEmptyQueryRepliesWithHelpWithoutGrading(t *testing.T) {
|
|
got := make(chan captured, 1)
|
|
g := &fakeGrader{}
|
|
b := newTestBot(t, g, capture(got))
|
|
|
|
b.handleMessage(context.Background(), testMsg("<@999>", mentioning(botID)), botID)
|
|
|
|
c := <-got
|
|
var body struct {
|
|
Content string `json:"content"`
|
|
}
|
|
if err := json.Unmarshal(c.body, &body); err != nil {
|
|
t.Fatalf("help body not JSON: %v", err)
|
|
}
|
|
if body.Content != helpText {
|
|
t.Fatalf("want help text, got %q", body.Content)
|
|
}
|
|
if g.callCount() != 0 {
|
|
t.Fatal("help reply must not hit the grade callback")
|
|
}
|
|
}
|
|
|
|
func TestSilentMessagesMakeNoRequests(t *testing.T) {
|
|
var hits atomic.Int32
|
|
g := &fakeGrader{resp: json.RawMessage(`{}`)}
|
|
b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
|
|
hits.Add(1)
|
|
})
|
|
|
|
b.handleMessage(context.Background(), testMsg("just chatting"), botID)
|
|
|
|
if g.callCount() != 0 || hits.Load() != 0 {
|
|
t.Fatalf("silent message leaked: grades=%d posts=%d", g.callCount(), hits.Load())
|
|
}
|
|
}
|
|
|
|
func TestGradeFailureStaysSilent(t *testing.T) {
|
|
var hits atomic.Int32
|
|
g := &fakeGrader{err: context.DeadlineExceeded}
|
|
b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
|
|
hits.Add(1)
|
|
})
|
|
|
|
// Must log-and-return, not panic or post a broken reply.
|
|
b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)
|
|
|
|
if hits.Load() != 0 {
|
|
t.Fatal("failed grade must not produce a reply")
|
|
}
|
|
}
|
|
|
|
func TestFailedReplyIsSwallowed(t *testing.T) {
|
|
g := &fakeGrader{resp: json.RawMessage(`{"content":"x"}`)}
|
|
b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "boom", http.StatusInternalServerError)
|
|
})
|
|
|
|
// A failed reply must never kill the caller (in production: the read
|
|
// loop) — returning normally is the assertion.
|
|
b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)
|
|
}
|
|
|
|
func TestReplyRetriesOnceOn429(t *testing.T) {
|
|
var hits atomic.Int32
|
|
bodies := make(chan []byte, 2)
|
|
g := &fakeGrader{resp: json.RawMessage(`{"content":"x"}`)}
|
|
b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
bodies <- body
|
|
if hits.Add(1) == 1 {
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
_, _ = w.Write([]byte(`{"retry_after":0.01}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{}`))
|
|
})
|
|
|
|
b.handleMessage(context.Background(), testMsg("<@999> Phoenix", mentioning(botID)), botID)
|
|
|
|
if hits.Load() != 2 {
|
|
t.Fatalf("want 1 retry after 429, got %d requests", hits.Load())
|
|
}
|
|
first, second := <-bodies, <-bodies
|
|
if string(first) != string(second) {
|
|
t.Fatal("retry must resend the identical payload")
|
|
}
|
|
}
|
|
|
|
func TestNoChannelMeansNoPost(t *testing.T) {
|
|
var hits atomic.Int32
|
|
g := &fakeGrader{resp: json.RawMessage(`{"content":"x"}`)}
|
|
b := newTestBot(t, g, func(w http.ResponseWriter, r *http.Request) {
|
|
hits.Add(1)
|
|
})
|
|
|
|
b.handleMessage(context.Background(),
|
|
testMsg("<@999> Phoenix", mentioning(botID), func(m *gwMessage) { m.ChannelID = "" }), botID)
|
|
|
|
if hits.Load() != 0 {
|
|
t.Fatal("a message without a channel id must not be answered")
|
|
}
|
|
}
|
|
|
|
func TestInjectReplyFieldsWithoutMessageIDLeavesBodyAlone(t *testing.T) {
|
|
out, err := injectReplyFields([]byte(`{"content":"x"}`), "")
|
|
if err != nil {
|
|
t.Fatalf("inject: %v", err)
|
|
}
|
|
var top map[string]json.RawMessage
|
|
if err := json.Unmarshal(out, &top); err != nil {
|
|
t.Fatalf("out not JSON: %v", err)
|
|
}
|
|
if _, ok := top["message_reference"]; ok {
|
|
t.Fatal("no triggering id => no message_reference")
|
|
}
|
|
if _, ok := top["allowed_mentions"]; ok {
|
|
t.Fatal("no triggering id => no allowed_mentions")
|
|
}
|
|
}
|
|
|
|
// --- dispatch: same reply, off the read loop ---------------------------------
|
|
|
|
func TestDispatchDeliversTheSameReplyViaAGoroutine(t *testing.T) {
|
|
// Mirrors the Python test_dispatch_delivers_the_same_reply_via_a_thread:
|
|
// the concurrency offload (goroutine + semaphore here, asyncio.to_thread
|
|
// there) must not change what gets sent.
|
|
grade := json.RawMessage(`{"embeds":[{"title":"grade:Phoenix"}]}`)
|
|
got := make(chan captured, 1)
|
|
b := newTestBot(t, &fakeGrader{resp: grade}, capture(got))
|
|
sess := &session{userID: botID}
|
|
d, err := json.Marshal(testMsg("<@999> Phoenix", mentioning(botID)))
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
b.dispatch(context.Background(), payload{Op: opDispatch, T: "MESSAGE_CREATE", D: d}, sess)
|
|
|
|
select {
|
|
case c := <-got:
|
|
if c.path != "/channels/77/messages" {
|
|
t.Fatalf("want /channels/77/messages, got %s", c.path)
|
|
}
|
|
var top map[string]json.RawMessage
|
|
if err := json.Unmarshal(c.body, &top); err != nil {
|
|
t.Fatalf("reply body not JSON: %v", err)
|
|
}
|
|
if string(top["embeds"]) != `[{"title":"grade:Phoenix"}]` {
|
|
t.Fatalf("embeds not relayed verbatim: %s", top["embeds"])
|
|
}
|
|
if string(top["message_reference"]) != `{"message_id":"42"}` {
|
|
t.Fatalf("bad message_reference: %s", top["message_reference"])
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("dispatched message never produced a reply")
|
|
}
|
|
}
|
|
|
|
func TestMessageFloodIsBoundedWithoutBlockingTheReadLoop(t *testing.T) {
|
|
g := &fakeGrader{resp: json.RawMessage(`{}`)}
|
|
b := New("tok", g)
|
|
b.sem = make(chan struct{}, 1)
|
|
b.sem <- struct{}{} // occupy the only slot: simulate a reply in flight
|
|
sess := &session{userID: botID}
|
|
d, err := json.Marshal(testMsg("<@999> Phoenix", mentioning(botID)))
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
b.dispatch(context.Background(), payload{Op: opDispatch, T: "MESSAGE_CREATE", D: d}, sess)
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-done:
|
|
// dispatch returned promptly — the read loop would not have stalled
|
|
case <-time.After(time.Second):
|
|
t.Fatal("dispatch blocked on a full semaphore")
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
if g.callCount() != 0 {
|
|
t.Fatal("over-the-bound message must be dropped, not queued")
|
|
}
|
|
}
|