Files
glchat/internal/server/ready_status_test.go
T

70 lines
2.3 KiB
Go
Raw Normal View History

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// TestReadyCarriesChannelStatus проверяет, что снапшот несёт статус комнаты —
// описание и медленный режим: шапка показывает их рядом с названием, включая
// чужой сервер у инстанс-админа (AGENT.md 7.5, 11.6).
func TestReadyCarriesChannelStatus(t *testing.T) {
srv, _ := newTestServer(t)
httpServer := httptest.NewServer(srv.Handler())
t.Cleanup(httpServer.Close)
ownerCookie := registerAndLogin(t, srv, "status_owner", "status-owner@example.com")
created := doJSON(t, srv, http.MethodPost, "/api/v1/guilds", `{"name":"Статусы"}`, ownerCookie)
guild := decodeResponse[struct {
Guild struct {
ID string `json:"id"`
} `json:"guild"`
}](t, created)
channels := doJSON(t, srv, http.MethodGet, "/api/v1/guilds/"+guild.Guild.ID+"/channels", "", ownerCookie)
channel := decodeResponse[struct {
Channels []struct {
ID string `json:"id"`
} `json:"channels"`
}](t, channels)
if len(channel.Channels) == 0 {
t.Fatal("у нового сервера нет комнат")
}
patched := doJSON(t, srv, http.MethodPatch,
"/api/v1/guilds/"+guild.Guild.ID+"/channels/"+channel.Channels[0].ID,
`{"description":"Статус комнаты","slowmode_seconds":5}`, ownerCookie)
if patched.Code != http.StatusOK {
t.Fatalf("описание комнаты = %d, body = %s", patched.Code, patched.Body.String())
}
client := dialGateway(t, httpServer, ownerCookie)
var snapshot struct {
Guilds []struct {
ID string `json:"id"`
Channels []struct {
Description string `json:"description"`
Slowmode int `json:"slowmode_seconds"`
} `json:"channels"`
} `json:"guilds"`
}
if err := json.Unmarshal(client.ready.D, &snapshot); err != nil {
t.Fatalf("decode READY: %v", err)
}
described := false
for _, item := range snapshot.Guilds {
if item.ID != guild.Guild.ID {
continue
}
for _, ch := range item.Channels {
if ch.Description == "Статус комнаты" && ch.Slowmode == 5 {
described = true
}
}
}
if !described {
t.Fatalf("READY не несёт статус комнаты: %+v", snapshot.Guilds)
}
}