fix(settings): 绑定留档名称并按旧目标重指向待入库
build / build (push) Successful in 1m48s

- 迁移 000084 保存对内/对外绑定名称快照;清除密钥后仍显示已绑定库,不再只显示裸 ID
- 测试/生产换绑只重指向同一侧的旧目标,避免测试保存误改另一环境的待入库
- 已保存连接但没有可用密钥时拒绝改绑定,禁止静默回落文件 Key
- 生产换绑返回重指向/退休数量,页面提示与测试侧一致

验证:make verify(含隔离 PostgreSQL 集成用例)15/15 通过
This commit is contained in:
狐友 GXXP & LEN
2026-11-23 12:47:22 +08:00
committed by 姚章浩
parent 8b900a9ffb
commit aac8538372
19 changed files with 633 additions and 140 deletions
+80 -10
View File
@@ -3,6 +3,7 @@ package httpapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
@@ -14,11 +15,14 @@ import (
)
type bindingStoreFake struct {
internal string
external string
saver string
internal string
external string
internalName string
externalName string
saver string
// replan records what the last save asked for, so tests can assert that the
// effective (env-fallback aware) targets were used.
// previous and effective (env-fallback aware) targets were used.
replanPrevious [2]string
replanEffective [2]string
replanResult postgresstore.RoutingBindingReplanResult
replanCallCount int
@@ -28,14 +32,15 @@ func (fake *bindingStoreFake) LoadRoutingKBBindings(context.Context) (string, st
return fake.internal, fake.external, fake.saver, nil
}
func (fake *bindingStoreFake) SaveRoutingKBBindings(_ context.Context, internal, external, updatedBy string) error {
fake.internal, fake.external, fake.saver = internal, external, updatedBy
return nil
func (fake *bindingStoreFake) LoadRoutingKBBindingNames(context.Context) (string, string, error) {
return fake.internalName, fake.externalName, nil
}
func (fake *bindingStoreFake) SaveRoutingKBBindingsWithReplan(_ context.Context, internal, external, effectiveInternal, effectiveExternal, updatedBy string, _ time.Time) (postgresstore.RoutingBindingReplanResult, error) {
fake.internal, fake.external, fake.saver = internal, external, updatedBy
fake.replanEffective = [2]string{effectiveInternal, effectiveExternal}
func (fake *bindingStoreFake) SaveRoutingKBBindingsWithReplan(_ context.Context, save postgresstore.RoutingBindingSave, previous, effective postgresstore.EffectiveKBIDs, _ time.Time) (postgresstore.RoutingBindingReplanResult, error) {
fake.internal, fake.external, fake.saver = save.InternalID, save.ExternalID, save.UpdatedBy
fake.internalName, fake.externalName = save.InternalName, save.ExternalName
fake.replanPrevious = [2]string{previous.Internal, previous.External}
fake.replanEffective = [2]string{effective.Internal, effective.External}
fake.replanCallCount++
return fake.replanResult, nil
}
@@ -134,6 +139,9 @@ func TestRoutingBindingsSaveReportsReplanAndUsesEffectiveTargets(t *testing.T) {
if store.replanEffective != [2]string{"kb-in", "env-external"} {
t.Fatalf("effective targets = %#v, want the env fallback on the unbound side", store.replanEffective)
}
if store.replanPrevious != [2]string{"env-internal", "env-external"} {
t.Fatalf("previous targets = %#v, want the pre-save effective pair", store.replanPrevious)
}
var envelope struct {
Data struct {
Replanned int `json:"replanned"`
@@ -163,3 +171,65 @@ func TestRouteCatalogFilesUsesUIBinding(t *testing.T) {
t.Fatalf("capabilities = %#v", envelope.Data)
}
}
// Clearing the API key must not blank the already-saved binding names: the
// page has no live listing then, so it reads the snapshot written at save time.
func TestRoutingBindingsReadFallsBackToStoredNamesWhenDisconnected(t *testing.T) {
store := &bindingStoreFake{internal: "kb-in", external: "kb-ex", internalName: "对内库", externalName: "对外库"}
handler := bindingsTestHandler(t, store, &kbListerFake{err: errors.New("no key")}, "", "")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/routing-bindings", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
}
var envelope struct {
Data routingBindingView `json:"data"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
t.Fatal(err)
}
if envelope.Data.InternalKBName != "对内库" || envelope.Data.ExternalKBName != "对外库" || envelope.Data.WeKnoraConnected {
t.Fatalf("view = %#v", envelope.Data)
}
}
type connectionResolverFake struct {
resolveOK bool
configured bool
}
func (fake connectionResolverFake) Resolve(context.Context, string) (string, string, bool) {
if !fake.resolveOK {
return "", "", false
}
return "http://127.0.0.1:8092", "key", true
}
func (fake connectionResolverFake) Configured(context.Context, string) (bool, error) {
return fake.configured, nil
}
// A saved row whose key was cleared must reject a binding save instead of
// silently falling back to the deployment file key or writing unverifiable ids.
func TestRoutingBindingsSaveRejectsClearedConnectionKey(t *testing.T) {
store := &bindingStoreFake{internal: "kb-old", external: "kb-old-external"}
handler, err := NewHandler(Config{
Store: &fakeJobStore{}, RoutingBindings: store,
KnowledgeBases: &kbListerFake{err: errors.New("no key")},
WeKnoraConnectionResolver: connectionResolverFake{configured: true},
InternalRoutingKnowledgeBaseID: "env-internal", ExternalRoutingKnowledgeBaseID: "env-external",
Now: func() time.Time { return time.Date(2026, time.October, 8, 9, 0, 0, 0, time.UTC) },
})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/routing-bindings",
strings.NewReader(`{"internal_kb_id":"kb-new","external_kb_id":"kb-old-external"}`)))
if recorder.Code != http.StatusConflict {
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
}
if store.replanCallCount != 0 {
t.Fatal("a cleared-key save must not reach the store")
}
}