refactor: pre-cleanup.

This commit is contained in:
sam committed 2026-08-06 01:40:15 +10:00
1 parent 0ceab9d897
commit e10cbbc92c
53 files changed
+3596 -648

No files matched your search

+18 -4
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"sync"
@@ -70,7 +71,7 @@ func (s *Server) chatReady(w http.ResponseWriter) (*ChatService, bool) {
http.Error(w, "chat is not configured", http.StatusNotImplemented)
return nil, false
}
if s.Chat.Router.Updates == nil && s.Chat.Updates != nil {
if !chatUpdateServiceConfigured(s.Chat.Router.Updates) && chatUpdateServiceConfigured(s.Chat.Updates) {
s.Chat.Router.Updates = s.Chat.Updates
}
if s.Chat.Router.Dangerous == nil && s.Chat.Dangerous != nil {
@@ -79,6 +80,19 @@ func (s *Server) chatReady(w http.ResponseWriter) (*ChatService, bool) {
return s.Chat, true
}
func chatUpdateServiceConfigured(updates ChatUpdateService) bool {
if updates == nil {
return false
}
value := reflect.ValueOf(updates)
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return !value.IsNil()
default:
return true
}
}
func (s *Server) handleChatSessions(w http.ResponseWriter, r *http.Request) {
chat, ok := s.chatReady(w)
if !ok {
@@ -329,7 +343,7 @@ func (s *Server) handleChatUpdate(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
if chat.Updates == nil {
if !chatUpdateServiceConfigured(chat.Updates) {
http.Error(w, "updates are not configured", http.StatusNotImplemented)
return
}
@@ -350,7 +364,7 @@ func (s *Server) handleChatUpdateDefer(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
if chat.Updates == nil {
if !chatUpdateServiceConfigured(chat.Updates) {
http.Error(w, "updates are not configured", http.StatusNotImplemented)
return
}
@@ -371,7 +385,7 @@ func (s *Server) handleChatUpdateInstall(w http.ResponseWriter, r *http.Request)
if !ok {
return
}
if chat.Updates == nil || !chat.Updates.CanInstall() {
if !chatUpdateServiceConfigured(chat.Updates) || !chat.Updates.CanInstall() {
http.Error(w, "update installation is not configured", http.StatusNotImplemented)
return
}
+14
View File
@@ -256,6 +256,20 @@ func TestChatUpdateEndpoints(t *testing.T) {
}
}
func TestChatUpdateEndpointRejectsTypedNilService(t *testing.T) {
var updates *releaseupdate.Service
sessions := gateway.NewSessionStoreMemory("test")
t.Cleanup(func() { _ = sessions.Close() })
router := gateway.NewRouter(chatStatusStub{}, nil, "web")
server := &Server{Chat: &ChatService{Router: router, Sessions: sessions, Updates: updates}}
res := httptest.NewRecorder()
server.Handler().ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/chat/update", nil))
if res.Code != http.StatusNotImplemented {
t.Fatalf("typed-nil update service status = %d, body = %s", res.Code, res.Body.String())
}
}
func TestChatDangerousApprovalEndpoints(t *testing.T) {
sessions := gateway.NewSessionStoreMemory("test")
t.Cleanup(func() { _ = sessions.Close() })