增加调试前端页面

This commit is contained in:
andy committed 2026-09-06 00:25:25 +08:00
1 parent 3080b17d02
commit ee9d2d7cf1
22 files changed
+1442 -59

No files matched your search

+21
View File
@@ -37,6 +37,18 @@ func New(ctx context.Context, cfg config.Config) (*Application, error) {
routerOptions := handler.RouterOptions{}
closeDependencies := func() {}
if cfg.Chat.PageEnabled {
if !cfg.Chat.Enabled {
return nil, fmt.Errorf("initialize chat page: chat must be enabled")
}
if cfg.Chat.CompatAppID == "" {
return nil, fmt.Errorf("initialize chat page: compatibility app ID is required")
}
if len(cfg.Chat.AllowedOrigins) == 0 {
return nil, fmt.Errorf("initialize chat page: at least one allowed origin is required")
}
}
if cfg.Chat.Enabled {
if !cfg.SuperAgent.Enabled {
return nil, fmt.Errorf("initialize chat: SuperAgent must be enabled")
@@ -96,6 +108,15 @@ func New(ctx context.Context, cfg config.Config) (*Application, error) {
}
routerOptions.DashScopeChat = dashScopeChatHandler
}
if cfg.Chat.PageEnabled {
chatPageHandler, err := handler.NewChatPageHandler(handler.ChatPageOptions{
AppID: cfg.Chat.CompatAppID,
})
if err != nil {
return nil, fmt.Errorf("initialize chat page handler: %w", err)
}
routerOptions.ChatPage = chatPageHandler
}
}
if cfg.MCP.Enabled {
+45
View File
@@ -39,11 +39,48 @@ func TestNewKeepsExternalIntegrationsDisabledByDefault(t *testing.T) {
if chatResponse.Code != http.StatusNotFound {
t.Fatalf("chat status = %d, want %d while disabled", chatResponse.Code, http.StatusNotFound)
}
for _, path := range []string{"/chat/", "/chat/app.js", "/chat/app.css"} {
chatPageRequest := httptest.NewRequest(http.MethodGet, path, nil)
chatPageResponse := httptest.NewRecorder()
application.server.Handler.ServeHTTP(chatPageResponse, chatPageRequest)
if chatPageResponse.Code != http.StatusNotFound {
t.Fatalf("%s status = %d, want %d while disabled", path, chatPageResponse.Code, http.StatusNotFound)
}
}
if application.server.ReadHeaderTimeout <= 0 || application.server.ReadTimeout <= 0 || application.server.WriteTimeout <= 0 || application.server.IdleTimeout <= 0 {
t.Fatalf("HTTP timeouts are incomplete: %#v", application.server)
}
}
func TestNewRejectsIncompleteChatPageConfiguration(t *testing.T) {
tests := []struct {
name string
chat config.ChatConfig
}{
{
name: "chat disabled",
chat: config.ChatConfig{PageEnabled: true, CompatAppID: "fire-safety-app", AllowedOrigins: []string{"https://fire.example.test"}},
},
{
name: "missing app ID",
chat: config.ChatConfig{Enabled: true, PageEnabled: true, AllowedOrigins: []string{"https://fire.example.test"}},
},
{
name: "missing allowed origin",
chat: config.ChatConfig{Enabled: true, PageEnabled: true, CompatAppID: "fire-safety-app"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := New(context.Background(), config.Config{HTTPAddress: ":0", Chat: tt.chat})
if err == nil || !strings.Contains(err.Error(), "initialize chat page") {
t.Fatalf("New() error = %v, want chat page configuration error", err)
}
})
}
}
func TestNewWiresChatAPIToSuperAgent(t *testing.T) {
var applicationLogs bytes.Buffer
originalLogOutput := log.Writer()
@@ -85,10 +122,12 @@ func TestNewWiresChatAPIToSuperAgent(t *testing.T) {
},
Chat: config.ChatConfig{
Enabled: true,
PageEnabled: true,
AuthToken: "delivered-key",
AllowLegacyShortToken: true,
SubjectID: "app-chat-test-subject",
CompatAppID: "fire-safety-app",
AllowedOrigins: []string{"https://fire.example.test"},
MaxBodyBytes: 4096,
RunTimeout: time.Minute,
SessionTTL: time.Minute,
@@ -121,6 +160,12 @@ func TestNewWiresChatAPIToSuperAgent(t *testing.T) {
!strings.Contains(compatResponse.Body.String(), `"finish_reason":"stop"`) || !strings.Contains(compatResponse.Body.String(), "测试回答") {
t.Fatalf("compat chat status=%d body=%s", compatResponse.Code, compatResponse.Body.String())
}
pageRequest := httptest.NewRequest(http.MethodGet, "/chat/", nil)
pageResponse := httptest.NewRecorder()
application.server.Handler.ServeHTTP(pageResponse, pageRequest)
if pageResponse.Code != http.StatusOK {
t.Fatalf("chat page status=%d body=%s", pageResponse.Code, pageResponse.Body.String())
}
if !strings.Contains(applicationLogs.String(), "legacy short chat auth token compatibility is enabled") {
t.Fatalf("application logs did not contain legacy compatibility warning: %s", applicationLogs.String())
}