37d8761721
build / build (push) Successful in 2m35s
- 变基到 CYWL-38 的 reconciliationMessages 之后:沿用 runtime_key_forbidden / runtime_unreachable 公共码,不再另造一套。 - HTTP 仍按本任务裁定用 502 区分上游拒绝/失败与绑定不可用 503。 - 页面文案统一由 reconciliationMessages 输出;按新 Key 来源改写「平台只读 Key」措辞。 - 同步 monitoring 原因码与相关测试;make verify 17 项全绿。
223 lines
8.0 KiB
Go
223 lines
8.0 KiB
Go
package reconciliation
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
func staticConnection(baseURL, apiKey string) func(context.Context) (string, string, error) {
|
|
return func(context.Context) (string, string, error) { return baseURL, apiKey, nil }
|
|
}
|
|
|
|
func TestRuntimeProviderMapsQueuesAndTruncatesFailures(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("X-API-Key") != "runtime-key" {
|
|
t.Fatalf("missing runtime key header")
|
|
}
|
|
switch r.URL.Path {
|
|
case "/api/v1/system/admin/runtime/queues":
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"available": true, "queues_error": false, "workers_error": false, "timestamp": 7,
|
|
"queues": []map[string]any{{"name": "default", "pool": "core", "pending": 3, "active": 1, "failed": 2}},
|
|
})
|
|
case "/api/v1/system/admin/runtime/documents/doc-1":
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"success": true, "data": map[string]any{
|
|
"document": map[string]any{"id": "doc-1", "parse_status": "failed", "ingestion_status": "failed", "wiki_status": "none",
|
|
"stages": []map[string]any{{"name": "parse", "status": "failed", "error_code": "x"}}},
|
|
"failures": []map[string]any{{"task_type": "parse", "fail_count": 2, "failed_at": "2026-12-01T00:00:00Z", "last_error": strings.Repeat("错", 600)}},
|
|
"has_more_failures": false,
|
|
}})
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
provider := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: staticConnection(server.URL, "runtime-key"),
|
|
Timeout: 5 * time.Second,
|
|
})
|
|
queues, err := provider.Queues(context.Background())
|
|
if err != nil || !queues.Available || len(queues.Queues) != 1 || queues.Queues[0].Pending != 3 {
|
|
t.Fatalf("queues=%#v err=%v", queues, err)
|
|
}
|
|
progress, err := provider.DocumentProgress(context.Background(), "doc-1")
|
|
if err != nil || progress.ParseStatus != "failed" || progress.DocumentID != "doc-1" || len(progress.Stages) != 1 {
|
|
t.Fatalf("progress=%#v err=%v", progress, err)
|
|
}
|
|
if len([]rune(progress.Failures[0].LastError)) != 500 {
|
|
t.Fatalf("last_error runes = %d", len([]rune(progress.Failures[0].LastError)))
|
|
}
|
|
}
|
|
|
|
// The binding is editable from the console, so the provider must ask for the
|
|
// key on every request instead of freezing the first one it sees.
|
|
func TestRuntimeProviderResolvesKeyOnEveryRequest(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var seen []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
seen = append(seen, r.Header.Get("X-API-Key"))
|
|
mu.Unlock()
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"available": true, "queues": []map[string]any{}})
|
|
}))
|
|
defer server.Close()
|
|
|
|
mu.Lock()
|
|
key := "old-key"
|
|
mu.Unlock()
|
|
provider := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: func(context.Context) (string, string, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return server.URL, key, nil
|
|
},
|
|
Timeout: 5 * time.Second,
|
|
})
|
|
if _, err := provider.Queues(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mu.Lock()
|
|
key = "new-key"
|
|
mu.Unlock()
|
|
if _, err := provider.Queues(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(seen) != 2 || seen[0] != "old-key" || seen[1] != "new-key" {
|
|
t.Fatalf("request keys = %v, want [old-key new-key]", seen)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProviderUnavailableAndNotFound(t *testing.T) {
|
|
unavailable := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: func(context.Context) (string, string, error) { return "", "", nil },
|
|
})
|
|
if _, err := unavailable.Queues(context.Background()); !errors.Is(err, ErrRuntimeUnavailable) {
|
|
t.Fatalf("unconfigured queues err=%v", err)
|
|
}
|
|
broken := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: func(context.Context) (string, string, error) {
|
|
return "", "", errors.New("binding storage is not ready")
|
|
},
|
|
})
|
|
if _, err := broken.DocumentProgress(context.Background(), "doc-1"); !errors.Is(err, ErrRuntimeUnavailable) {
|
|
t.Fatalf("broken binding err=%v", err)
|
|
}
|
|
server := httptest.NewServer(http.NotFoundHandler())
|
|
defer server.Close()
|
|
provider := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: staticConnection(server.URL, "runtime-key"),
|
|
Timeout: 5 * time.Second,
|
|
})
|
|
if _, err := provider.DocumentProgress(context.Background(), "missing"); err == nil || weknora.StatusCode(err) != 404 {
|
|
t.Fatalf("missing document err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeErrorCodeDistinguishesStates(t *testing.T) {
|
|
cases := map[string]error{
|
|
"runtime_read_unavailable": ErrRuntimeUnavailable,
|
|
"runtime_key_forbidden": ErrRuntimeForbidden,
|
|
"runtime_unreachable": ErrRuntimeUnreachable,
|
|
"": errors.New("unclassified"),
|
|
}
|
|
for want, err := range cases {
|
|
if got := RuntimeErrorCode(err); got != want {
|
|
t.Fatalf("RuntimeErrorCode(%v) = %q, want %q", err, got, want)
|
|
}
|
|
}
|
|
if got := RuntimeErrorCode(nil); got != "" {
|
|
t.Fatalf("RuntimeErrorCode(nil) = %q", got)
|
|
}
|
|
}
|
|
|
|
// The progress passthrough is a projection, not a proxy: whatever extra fields
|
|
// the platform endpoint returns (keys, authorization headers, token echoes) must
|
|
// never reach the browser. This guards the shape if someone later switches to
|
|
// forwarding the upstream body.
|
|
func TestRuntimeProviderDropsSensitiveUpstreamFields(t *testing.T) {
|
|
const secret = "leak-sentinel"
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"success": true, "data": map[string]any{
|
|
"document": map[string]any{
|
|
"id": "doc-1", "parse_status": "completed", "ingestion_status": "completed", "wiki_status": "none",
|
|
"api_key": secret, "apiKey": secret, "authorization": "Bearer " + secret,
|
|
"headers": map[string]any{"X-API-Key": secret},
|
|
},
|
|
"failures": []map[string]any{},
|
|
"has_more_failures": false,
|
|
"api_key": secret,
|
|
}})
|
|
}))
|
|
defer server.Close()
|
|
provider := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: staticConnection(server.URL, "runtime-key"),
|
|
Timeout: 5 * time.Second,
|
|
})
|
|
progress, err := provider.DocumentProgress(context.Background(), "doc-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
payload, err := json.Marshal(progress)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(payload), secret) || strings.Contains(string(payload), "Bearer ") {
|
|
t.Fatalf("progress leaked upstream credentials: %s", payload)
|
|
}
|
|
if progress.DocumentID != "doc-1" || progress.ParseStatus != "completed" {
|
|
t.Fatalf("projection dropped real fields: %#v", progress)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProviderClassifiesForbiddenAndUnreachable(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
status int
|
|
want error
|
|
}{
|
|
{"forbidden", http.StatusForbidden, ErrRuntimeForbidden},
|
|
{"unauthorized", http.StatusUnauthorized, ErrRuntimeForbidden},
|
|
{"upstream-500", http.StatusInternalServerError, ErrRuntimeUnreachable},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(tc.status)
|
|
}))
|
|
defer server.Close()
|
|
provider := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: staticConnection(server.URL, "runtime-key"),
|
|
Timeout: 5 * time.Second,
|
|
})
|
|
if _, err := provider.DocumentProgress(context.Background(), "doc-1"); !errors.Is(err, tc.want) {
|
|
t.Fatalf("status %d err=%v, want %v", tc.status, err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProviderClassifiesClosedListenerAsUnreachable(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"success": true, "data": map[string]any{}})
|
|
}))
|
|
url := server.URL
|
|
server.Close()
|
|
provider := NewRuntimeProvider(RuntimeConfig{
|
|
Resolve: staticConnection(url, "runtime-key"), Timeout: 2 * time.Second,
|
|
})
|
|
if _, err := provider.DocumentProgress(context.Background(), "doc-1"); !errors.Is(err, ErrRuntimeUnreachable) {
|
|
t.Fatalf("closed listener err=%v, want %v", err, ErrRuntimeUnreachable)
|
|
}
|
|
}
|