Files
knowledge-governance/internal/httpapi/reconciliation_test.go
Zhero & Rin fe9d055522 fix(reconciliation): 运行时读复用知识库绑定 Key
- 为什么:运行时读另用 WEKNORA_RUNTIME_KEY_FILE(0400 文件、启动读一次),当前部署没有该文件,队列与处理详情恒不可用;换 Key 还要重启,且与绑定页不同步。
- 做什么:RuntimeProvider 改为每次请求经 kbbinding.Resolve(EnvTest) 取地址与 Key,与 reconciliationRemote 共用同一 resolver;删除配置字段/校验、ReadRuntimeKeyFile 与测试、compose/env、启动 WARN。
- 三态:绑定不可用→503 runtime_read_unavailable;上游 401/403→502 runtime_read_forbidden;超时/DNS→502 runtime_read_unreachable;overview 增 weknora_queue_error,页面三句不同文案并保留 WeKnora 跳转链接。
- 同步:openapi progress 契约与 502、需求书 §5/Q7、阶段计划、运维手册、CHANGELOG。
- 验证:make verify 17 项 success;新增换 Key 二次请求头、401/403/不可达分类、Service 透传、overview 原因码、页面三文案用例。
2026-12-02 13:09:24 +08:00

170 lines
7.4 KiB
Go

package httpapi
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/reconciliation"
)
type reconciliationServiceStub struct {
created bool
progress governance.ReconciliationProgress
err error
}
func (s *reconciliationServiceStub) CreateRun(context.Context, string, string) (governance.ReconciliationRun, error) {
s.created = true
return governance.ReconciliationRun{ID: "run-1", KBID: "kb-1", State: "running"}, s.err
}
func (s *reconciliationServiceStub) ListRuns(context.Context, governance.ReconciliationRunFilter) (governance.ReconciliationRunPage, error) {
return governance.ReconciliationRunPage{Items: []governance.ReconciliationRun{}}, s.err
}
func (s *reconciliationServiceStub) GetRun(context.Context, string) (governance.ReconciliationRun, error) {
return governance.ReconciliationRun{ID: "run-1"}, s.err
}
func (s *reconciliationServiceStub) ListFindings(context.Context, governance.ReconciliationFindingFilter) (governance.ReconciliationFindingPage, error) {
return governance.ReconciliationFindingPage{Items: []governance.ReconciliationFinding{}}, s.err
}
func (s *reconciliationServiceStub) Writeback(context.Context, string, string, string) (governance.ReconciliationFinding, error) {
return governance.ReconciliationFinding{ID: "f1", ActionState: "done"}, s.err
}
func (s *reconciliationServiceStub) Requeue(context.Context, string, string, string) (governance.ReconciliationFinding, error) {
return governance.ReconciliationFinding{ID: "f1", ActionState: "done"}, s.err
}
func (s *reconciliationServiceStub) Remove(context.Context, string, string, string) (governance.ReconciliationFinding, error) {
return governance.ReconciliationFinding{ID: "f1", ActionState: "done"}, s.err
}
func (s *reconciliationServiceStub) Progress(context.Context, string) (governance.ReconciliationProgress, error) {
return s.progress, s.err
}
func reconciliationRequest(method, path, role, body string) *http.Request {
request := httptest.NewRequest(method, path, strings.NewReader(body))
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role}))
return request
}
func TestReconciliationRoutesEnforceRolesAndWriteIntent(t *testing.T) {
service := &reconciliationServiceStub{}
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: service})
if err != nil {
t.Fatal(err)
}
viewer := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/runs", roleViewer, `{"kb_id":"kb-1"}`)
viewer.Header.Set("X-Governance-Action", "reconciliation-action")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, viewer)
if recorder.Code != http.StatusForbidden || service.created {
t.Fatalf("viewer create = %d created=%t", recorder.Code, service.created)
}
engineer := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/runs", roleEngineer, `{"kb_id":"kb-1"}`)
engineer.Header.Set("X-Governance-Action", "reconciliation-action")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, engineer)
if recorder.Code != http.StatusAccepted || !service.created {
t.Fatalf("engineer create = %d created=%t %s", recorder.Code, service.created, recorder.Body.String())
}
missingIntent := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/findings/f1/requeue", roleEngineer, "")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, missingIntent)
if recorder.Code != http.StatusForbidden {
t.Fatalf("missing write intent = %d", recorder.Code)
}
engineerRemove := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/findings/f1/remove", roleEngineer, "")
engineerRemove.Header.Set("X-Governance-Action", "reconciliation-action")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, engineerRemove)
if recorder.Code != http.StatusForbidden {
t.Fatalf("engineer remove = %d", recorder.Code)
}
adminRemove := reconciliationRequest(http.MethodPost, "/api/v1/reconciliation/findings/f1/remove", roleAdmin, "")
adminRemove.Header.Set("X-Governance-Action", "reconciliation-action")
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, adminRemove)
if recorder.Code != http.StatusOK {
t.Fatalf("admin remove = %d %s", recorder.Code, recorder.Body.String())
}
}
func TestReconciliationProgress503AndUnwiredRoute(t *testing.T) {
service := &reconciliationServiceStub{err: reconciliation.ErrRuntimeUnavailable}
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: service})
if err != nil {
t.Fatal(err)
}
request := reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/documents/doc-1/progress", roleViewer, "")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusServiceUnavailable || !strings.Contains(recorder.Body.String(), "runtime_read_unavailable") {
t.Fatalf("progress = %d %s", recorder.Code, recorder.Body.String())
}
unwired, err := NewHandler(Config{Store: &fakeJobStore{}})
if err != nil {
t.Fatal(err)
}
recorder = httptest.NewRecorder()
unwired.ServeHTTP(recorder, reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/runs", roleViewer, ""))
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("unwired runs = %d", recorder.Code)
}
}
func TestReconciliationFindingTypeValidation(t *testing.T) {
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: &reconciliationServiceStub{}})
if err != nil {
t.Fatal(err)
}
request := reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/runs/run-1/findings?type=R9", roleViewer, "")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("invalid type = %d", recorder.Code)
}
}
func TestReconciliationProgressRejectsMalformedDocumentIDWith400(t *testing.T) {
service := &reconciliationServiceStub{}
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: service})
if err != nil {
t.Fatal(err)
}
for _, path := range []string{
"/api/v1/reconciliation/documents//progress",
"/api/v1/reconciliation/documents/doc-1/extra/progress",
} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, reconciliationRequest(http.MethodGet, path, roleViewer, ""))
if recorder.Code != http.StatusBadRequest || !strings.Contains(recorder.Body.String(), "invalid document id") {
t.Fatalf("%s = %d %s, want 400 invalid document id", path, recorder.Code, recorder.Body.String())
}
}
}
func TestReconciliationProgressClassifiesRuntimeFailures(t *testing.T) {
cases := []struct {
err error
status int
code string
}{
{reconciliation.ErrRuntimeForbidden, http.StatusBadGateway, "runtime_key_forbidden"},
{reconciliation.ErrRuntimeUnreachable, http.StatusBadGateway, "runtime_unreachable"},
{reconciliation.ErrRuntimeUnavailable, http.StatusServiceUnavailable, "runtime_read_unavailable"},
}
for _, tc := range cases {
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: &reconciliationServiceStub{err: tc.err}})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/documents/doc-1/progress", roleViewer, ""))
if recorder.Code != tc.status || !strings.Contains(recorder.Body.String(), tc.code) {
t.Fatalf("%v = %d %s, want %d %s", tc.err, recorder.Code, recorder.Body.String(), tc.status, tc.code)
}
}
}