aac8538372
build / build (push) Successful in 1m48s
- 迁移 000084 保存对内/对外绑定名称快照;清除密钥后仍显示已绑定库,不再只显示裸 ID - 测试/生产换绑只重指向同一侧的旧目标,避免测试保存误改另一环境的待入库 - 已保存连接但没有可用密钥时拒绝改绑定,禁止静默回落文件 Key - 生产换绑返回重指向/退休数量,页面提示与测试侧一致 验证:make verify(含隔离 PostgreSQL 集成用例)15/15 通过
224 lines
10 KiB
Go
224 lines
10 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
type productionBindingStoreFake struct {
|
|
internal, external, saver string
|
|
internalName, externalName string
|
|
savedInternal, savedExternal string
|
|
savedInternalName, savedExternalName string
|
|
previousInternal, previousExternal string
|
|
effectiveInternal, effectiveExternal string
|
|
saveCallCount int
|
|
frozen, retargetable int
|
|
replanResult postgresstore.RoutingBindingReplanResult
|
|
}
|
|
|
|
func (fake *productionBindingStoreFake) LoadProductionKBBindings(context.Context) (string, string, string, error) {
|
|
return fake.internal, fake.external, fake.saver, nil
|
|
}
|
|
|
|
func (fake *productionBindingStoreFake) LoadProductionKBBindingNames(context.Context) (string, string, error) {
|
|
return fake.internalName, fake.externalName, nil
|
|
}
|
|
|
|
func (fake *productionBindingStoreFake) SaveProductionKBBindingsWithReplan(_ context.Context, save postgresstore.ProductionBindingSave, previous, effective postgresstore.EffectiveKBIDs, _ time.Time) (postgresstore.RoutingBindingReplanResult, error) {
|
|
fake.savedInternal, fake.savedExternal = save.InternalID, save.ExternalID
|
|
fake.savedInternalName, fake.savedExternalName = save.InternalName, save.ExternalName
|
|
fake.previousInternal, fake.previousExternal = previous.Internal, previous.External
|
|
fake.effectiveInternal, fake.effectiveExternal = effective.Internal, effective.External
|
|
fake.saver = save.UpdatedBy
|
|
fake.saveCallCount++
|
|
fake.internal, fake.external = save.InternalID, save.ExternalID
|
|
fake.internalName, fake.externalName = save.InternalName, save.ExternalName
|
|
return fake.replanResult, nil
|
|
}
|
|
|
|
func (fake *productionBindingStoreFake) ProductionExecutionCounts(context.Context) (int, int, error) {
|
|
return fake.frozen, fake.retargetable, nil
|
|
}
|
|
|
|
type productionListerFake struct {
|
|
bases []weknora.KnowledgeBase
|
|
err error
|
|
baseURL string
|
|
}
|
|
|
|
func (fake *productionListerFake) ListProductionKnowledgeBases(_ context.Context, baseURL string) ([]weknora.KnowledgeBase, error) {
|
|
fake.baseURL = baseURL
|
|
return fake.bases, fake.err
|
|
}
|
|
|
|
func productionBindingsHandler(t *testing.T, store ProductionBindingStore, lister ProductionKnowledgeBaseLister, settingsURL string) http.Handler {
|
|
t.Helper()
|
|
jobStore := &fakeJobStore{}
|
|
jobStore.activeUISettingsResult = governance.UISettings{ProductionWeKnoraBaseURL: settingsURL}
|
|
handler, err := NewHandler(Config{
|
|
Store: jobStore,
|
|
ProductionBindings: store, ProductionKnowledgeBases: lister,
|
|
Now: func() time.Time { return time.Date(2026, time.October, 8, 8, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewHandler: %v", err)
|
|
}
|
|
return handler
|
|
}
|
|
|
|
func productionBases() []weknora.KnowledgeBase {
|
|
return []weknora.KnowledgeBase{
|
|
{ID: "kb-prod-internal", Name: "对内知识库-wiki", KnowledgeCount: 2069},
|
|
{ID: "kb-prod-external", Name: "对外知识库-wiki", KnowledgeCount: 526},
|
|
}
|
|
}
|
|
|
|
// The picker reads the production endpoint from 「服务配置 · 生产地址」 and reports
|
|
// the bound bases by NAME — ids never reach the page.
|
|
func TestProductionBindingsReadUsesServiceConfiguredEndpoint(t *testing.T) {
|
|
store := &productionBindingStoreFake{internal: "kb-prod-internal"}
|
|
lister := &productionListerFake{bases: productionBases()}
|
|
handler := productionBindingsHandler(t, store, lister, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if lister.baseURL != "https://weknora-test.dobot.cn/" {
|
|
t.Fatalf("lister baseURL = %q, want the operator-configured production address", lister.baseURL)
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, "对内知识库-wiki") || !strings.Contains(body, `"production_connected":true`) {
|
|
t.Fatalf("body = %s", body)
|
|
}
|
|
}
|
|
|
|
// A chosen base that does not exist on the production endpoint must be refused
|
|
// before anything is written.
|
|
func TestProductionBindingsSaveRejectsUnknownBase(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-ghost","external_kb_id":""}`)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if store.saveCallCount != 0 {
|
|
t.Fatal("a rejected binding must not be stored")
|
|
}
|
|
}
|
|
|
|
func TestProductionBindingsSaveStoresValidatedPair(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-prod-internal","external_kb_id":"kb-prod-external"}`)))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if store.savedInternal != "kb-prod-internal" || store.savedExternal != "kb-prod-external" {
|
|
t.Fatalf("saved = %q/%q", store.savedInternal, store.savedExternal)
|
|
}
|
|
if store.savedInternalName != "对内知识库-wiki" || store.savedExternalName != "对外知识库-wiki" {
|
|
t.Fatalf("saved names = %q/%q", store.savedInternalName, store.savedExternalName)
|
|
}
|
|
var envelope struct {
|
|
Data productionBindingView `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if envelope.Data.InternalKBName != "对内知识库-wiki" || !envelope.Data.ProductionConnected {
|
|
t.Fatalf("view = %#v", envelope.Data)
|
|
}
|
|
}
|
|
|
|
// A jittery production endpoint must not block the operator, but the response
|
|
// must say the binding could not be checked.
|
|
func TestProductionBindingsSaveWarnsWhenEndpointUnavailable(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{err: errors.New("dial tcp: timeout")}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-prod-internal","external_kb_id":""}`)))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if store.saveCallCount != 1 {
|
|
t.Fatal("an unverifiable (not invalid) binding must still be stored")
|
|
}
|
|
var envelope struct {
|
|
Data productionBindingView `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if envelope.Data.ProductionConnected || !strings.Contains(envelope.Data.ProductionMessage, "不可用") {
|
|
t.Fatalf("view = %#v", envelope.Data)
|
|
}
|
|
}
|
|
|
|
// 对内 and 对外 must not point at the same production base.
|
|
func TestProductionBindingsSaveRejectsIdenticalBases(t *testing.T) {
|
|
store := &productionBindingStoreFake{}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/production-kb-bindings",
|
|
strings.NewReader(`{"internal_kb_id":"kb-prod-internal","external_kb_id":"kb-prod-internal"}`)))
|
|
if recorder.Code != http.StatusBadRequest || store.saveCallCount != 0 {
|
|
t.Fatalf("status = %d saves = %d", recorder.Code, store.saveCallCount)
|
|
}
|
|
}
|
|
|
|
func TestProductionBindingOptionsReportUnavailableReason(t *testing.T) {
|
|
handler := productionBindingsHandler(t, &productionBindingStoreFake{}, &productionListerFake{err: errors.New("no key")}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings/options", nil))
|
|
if recorder.Code != http.StatusBadGateway {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
recorder = httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings/options", nil))
|
|
// and with a working endpoint the options carry names, not just ids
|
|
handler = productionBindingsHandler(t, &productionBindingStoreFake{}, &productionListerFake{bases: productionBases()}, "https://weknora-test.dobot.cn/")
|
|
recorder = httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings/options", nil))
|
|
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), "对外知识库-wiki") {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
// A cleared production key must not erase the display snapshot: the page keeps
|
|
// saying which production bases are bound even when it cannot list them.
|
|
func TestProductionBindingsReadFallsBackToStoredNamesWhenUnavailable(t *testing.T) {
|
|
store := &productionBindingStoreFake{internal: "kb-prod-internal", external: "kb-prod-external", internalName: "对内知识库-wiki", externalName: "对外知识库-wiki"}
|
|
handler := productionBindingsHandler(t, store, &productionListerFake{err: errors.New("no key")}, "https://weknora-test.dobot.cn/")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/production-kb-bindings", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var envelope struct {
|
|
Data productionBindingView `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if envelope.Data.InternalKBName != "对内知识库-wiki" || envelope.Data.ExternalKBName != "对外知识库-wiki" || envelope.Data.ProductionConnected {
|
|
t.Fatalf("view = %#v", envelope.Data)
|
|
}
|
|
}
|