095aa3ed68
build / build (push) Failing after 26s
- 为什么:白盒复检 P1——平台侧文件详情抽屉 503 只有文案,需求 §8 要求跳转链接;/monitor/tasks 行没有 URL。 - 做什么:抽出 KnowledgeBaseConsoleURL,finding 与 /monitor/tasks 共用同一拼接;handler 用当前生效测试端点填 weknora_url,前端抽屉透传给 WeKnoraProgress;openapi/需求 §7 同步。 - 补证据:stage=weknora 真库合并用例;对账扫描改为真实 HTTP 桩断言零写;progress 投影断言上游密钥字段不外泄。 - 验证:make verify 17 项 success;go test ./... exit 0;web 648 测试 + build;monitor_tasks 真库集成用例 PASS。
162 lines
5.8 KiB
Go
162 lines
5.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
type monitorTasksTestStore struct {
|
|
fakeJobStore
|
|
filter postgresstore.MonitorTaskFilter
|
|
items []postgresstore.MonitorTask
|
|
}
|
|
|
|
func (s *monitorTasksTestStore) ListMonitorTasks(_ context.Context, f postgresstore.MonitorTaskFilter) (postgresstore.MonitorTaskPage, error) {
|
|
s.filter = f
|
|
items := s.items
|
|
if items == nil {
|
|
items = []postgresstore.MonitorTask{}
|
|
}
|
|
return postgresstore.MonitorTaskPage{Items: items, Total: 145, Limit: f.Limit, Offset: f.Offset}, nil
|
|
}
|
|
func TestMonitorTasksRoute(t *testing.T) {
|
|
s := &monitorTasksTestStore{}
|
|
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?status=active&limit=20&offset=40", nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
|
|
w := httptest.NewRecorder()
|
|
h, err := NewHandler(Config{Store: s})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.ServeHTTP(w, req)
|
|
if w.Code != 200 {
|
|
t.Fatalf("status %d: %s", w.Code, w.Body.String())
|
|
}
|
|
if s.filter.Status != "active" || s.filter.Offset != 40 {
|
|
t.Fatalf("filter %#v", s.filter)
|
|
}
|
|
// §8 merges 解析/索引增强 into one "WeKnora 处理中" facet.
|
|
merged := httptest.NewRequest("GET", "/api/v1/monitor/tasks?stage=weknora", nil).WithContext(req.Context())
|
|
wm := httptest.NewRecorder()
|
|
h.ServeHTTP(wm, merged)
|
|
if wm.Code != 200 || s.filter.Stage != "weknora" {
|
|
t.Fatalf("weknora stage: %d filter %#v", wm.Code, s.filter)
|
|
}
|
|
for _, query := range []string{"status=garbage", "stage=internal&scope=internal", "limit=101", "offset=-1", "since=bad"} {
|
|
r := httptest.NewRequest("GET", "/api/v1/monitor/tasks?"+query, nil).WithContext(req.Context())
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, r)
|
|
if rr.Code != 400 {
|
|
t.Fatalf("invalid %s: %d", query, rr.Code)
|
|
}
|
|
}
|
|
unauth := httptest.NewRecorder()
|
|
h.ServeHTTP(unauth, httptest.NewRequest("GET", "/api/v1/monitor/tasks", nil))
|
|
if unauth.Code != 401 {
|
|
t.Fatalf("unauthenticated %d", unauth.Code)
|
|
}
|
|
removal := httptest.NewRecorder()
|
|
h.ServeHTTP(removal, httptest.NewRequest("POST", "/api/v1/test-publications/example/removal-retry", nil).WithContext(req.Context()))
|
|
if removal.Code != 403 {
|
|
t.Fatalf("viewer removal %d", removal.Code)
|
|
}
|
|
}
|
|
|
|
// F15:筛选里的“当前绑定”必须来自持久化绑定(与业务同一真源),不是 env 旧值,
|
|
// 也不能让前端拿本页数据猜全局。
|
|
func TestMonitorTasksReportsEffectiveTargetBindings(t *testing.T) {
|
|
store := &monitorTasksTestStore{}
|
|
handler, err := NewHandler(Config{
|
|
Store: store,
|
|
InternalRoutingKnowledgeBaseID: "env-old-internal",
|
|
ExternalRoutingKnowledgeBaseID: "env-old-external",
|
|
RoutingBindings: &bindingStoreFake{internal: "current-internal", external: "current-external"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?limit=20", nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, req)
|
|
if recorder.Code != 200 {
|
|
t.Fatalf("status %d: %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var payload struct {
|
|
Data struct {
|
|
Bindings struct {
|
|
Internal string `json:"internal"`
|
|
External string `json:"external"`
|
|
} `json:"bindings"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if payload.Data.Bindings.Internal != "current-internal" || payload.Data.Bindings.External != "current-external" {
|
|
t.Fatalf("bindings = %#v, want the persisted current bindings", payload.Data.Bindings)
|
|
}
|
|
}
|
|
|
|
// The platform drawer's 503 fallback needs the same jump link a finding carries.
|
|
// The handler, not the browser, decides the origin: it must use the effective
|
|
// test endpoint, and an unwired resolver must leave the field empty instead of
|
|
// inventing a host.
|
|
func TestMonitorTasksAddsWeKnoraConsoleURL(t *testing.T) {
|
|
store := &monitorTasksTestStore{items: []postgresstore.MonitorTask{{ID: "mt-1", TargetKB: "kb-1"}}}
|
|
handler, err := NewHandler(Config{
|
|
Store: store,
|
|
WeKnoraWebBase: func(context.Context) string { return "https://weknora.test/" },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?limit=20", nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, req)
|
|
if recorder.Code != 200 {
|
|
t.Fatalf("status %d: %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var payload struct {
|
|
Data struct {
|
|
Items []struct {
|
|
WeKnoraURL string `json:"weknora_url"`
|
|
} `json:"items"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(payload.Data.Items) != 1 || payload.Data.Items[0].WeKnoraURL != "https://weknora.test/platform/knowledge-bases/kb-1" {
|
|
t.Fatalf("weknora_url = %#v", payload.Data.Items)
|
|
}
|
|
|
|
store.items = []postgresstore.MonitorTask{{ID: "mt-1", TargetKB: "kb-1"}}
|
|
unwired, err := NewHandler(Config{Store: store})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recorder = httptest.NewRecorder()
|
|
unwired.ServeHTTP(recorder, req)
|
|
if recorder.Code != 200 {
|
|
t.Fatalf("unwired status %d: %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
payload = struct {
|
|
Data struct {
|
|
Items []struct {
|
|
WeKnoraURL string `json:"weknora_url"`
|
|
} `json:"items"`
|
|
} `json:"data"`
|
|
}{}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(payload.Data.Items) != 1 || payload.Data.Items[0].WeKnoraURL != "" {
|
|
t.Fatalf("unwired weknora_url = %#v", payload.Data.Items)
|
|
}
|
|
}
|