Source-only publication of reviewed commit 64e96a7f423673dc29ad56485be9fc79bce8af1c.
Passed in isolation: focused frontend/backend tests, race checks, TypeScript typecheck, lint, Go compilation, Flowgram build, and deterministic SDK build/tests.
Not accepted as a production release: Next.js export/native assembly remains blocked by environment memory limits; browser interaction and MySQL staging acceptance remain unverified. No production server, database/schema, runtime configuration, deployment, release tag, or workflow execution was performed.
Expected source tree: 0a4a7528e9.
78 lines
2.8 KiB
Go
78 lines
2.8 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"agent-desk/internal/pkg/errorsx"
|
|
"agent-desk/internal/pkg/httpx"
|
|
"agent-desk/internal/pkg/i18nx"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mlogclub/simple/web"
|
|
)
|
|
|
|
func TestKnowledgeRequestContextPreservesLocaleAndCancellation(t *testing.T) {
|
|
parent, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Request = httptest.NewRequest("POST", "/knowledge_retrieve/debug_answer", nil).WithContext(parent)
|
|
ctx.Request.Header.Set("X-Locale", i18nx.LocaleZhCN)
|
|
ctx.Request.Header.Set("Accept-Language", i18nx.LocaleZhCN)
|
|
i18nx.SetLocale(ctx, i18nx.LocaleRuRU)
|
|
derived := knowledgeRequestContext(ctx)
|
|
if i18nx.LocaleFromContext(derived) != i18nx.LocaleRuRU {
|
|
t.Fatal("request locale was lost")
|
|
}
|
|
parentDeadline, _ := parent.Deadline()
|
|
if deadline, ok := derived.Deadline(); !ok || !deadline.Equal(parentDeadline) {
|
|
t.Fatal("request deadline was lost")
|
|
}
|
|
cancel()
|
|
if !errors.Is(derived.Err(), context.Canceled) {
|
|
t.Fatal("request cancellation was lost")
|
|
}
|
|
}
|
|
|
|
func TestKnowledgeOperationErrorsHideProviderDiagnostics(t *testing.T) {
|
|
const diagnostic = "synthetic-private-diagnostic"
|
|
for _, key := range []string{"error.knowledge.indexFailed", "error.knowledge.retrieveFailed"} {
|
|
for _, cause := range []error{
|
|
errors.New("SQL failed: " + diagnostic),
|
|
fmt.Errorf("provider request failed: %w", errors.New(diagnostic)),
|
|
web.NewError(2000, diagnostic),
|
|
errorsx.BusinessErrorI18n(0, "error.e0340", diagnostic),
|
|
errorsx.BusinessErrorI18n(0, "unknown.translation."+diagnostic),
|
|
} {
|
|
for _, locale := range []string{i18nx.LocaleEnUS, i18nx.LocaleRuRU, i18nx.LocaleZhCN} {
|
|
recorder := httptest.NewRecorder()
|
|
ctx, _ := gin.CreateTestContext(recorder)
|
|
i18nx.SetLocale(ctx, locale)
|
|
httpx.WriteJSON(ctx, safeKnowledgeOperationError(cause, key))
|
|
body := recorder.Body.String()
|
|
if strings.Contains(body, diagnostic) || !strings.Contains(body, i18nx.Getf(locale, key)) {
|
|
t.Fatalf("unsafe/unlocalized %s response: %s", key, body)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestKnowledgeOperationErrorPreservesCatalogApplicationError(t *testing.T) {
|
|
original := errorsx.InvalidParamI18n("error.e0340")
|
|
wrapped := fmt.Errorf("private provider details: %w", original)
|
|
if got := safeKnowledgeOperationError(wrapped, "error.knowledge.retrieveFailed"); got != original {
|
|
t.Fatalf("application error lost: %v", got)
|
|
}
|
|
localized := i18nx.Errorf("error.embeddingModel.noneEnabled")
|
|
got := safeKnowledgeOperationError(fmt.Errorf("private: %w", localized), "error.knowledge.indexFailed")
|
|
if got.Error() != localized.Error() || strings.Contains(got.Error(), "private") {
|
|
t.Fatalf("localized application error changed/leaked: %v", got)
|
|
}
|
|
}
|