Files
knowledge-governance/internal/httpapi/test_publication_removal_test.go
T
姚章浩 a1b46d2b7c feat(monitor): 需下架车道可逐条移除测试库知识 —— 补齐最后一个没有页面入口的流程
背景:来源侧被判「禁止导入」后,已建好的测试库知识不会自动消失;此前只有变更集
删除路径会入队 test.remove,页面既列不出条目也没有按钮,运维无法闭环(车道只有按
库计数)。

新增:
- GET /api/v1/test-publications?removal=required:条目级列表,判据与车道计数同源
  (superseded_at IS NULL / 有 knowledge_id / completed / retrieval_state NOT IN
  (disabling,disabled) / policy_state='blocked'),过滤是叠加而不是替换。
- POST /api/v1/test-publications/{id}/removal:engineer 级写路由,要求 Idempotency-Key,
  按条目幂等(operator-remove:<id>);置 disabling 并交既有 test.remove 工作流去删除
  远端知识并读回校验。错误映射 404/409(已移除)/409(不可移除)/501(未接线)。
- 界面:需下架车道逐条显示条目 + 「下架(从测试库移除)」,页内二次确认,成功提示,
  完成后计数归零。

修掉过程中踩到的两个真问题:
1. 二次确认的确认态原先经容器 props 传给视图,一次容器改动把三个 props 丢了 ——
   视图永远渲染原按钮、确认步骤静默消失,而所有静态单元测试仍然通过。改为把确认态
   放在视图内部;并把「取消」放在原按钮位置(真实指针连点两下只会落到取消)。
2. jobs 上有部分唯一索引 UNIQUE (business_change_set_id, business_version_id,
   business_target_kb_id, business_action) WHERE business_change_set_id IS NOT NULL。
   本路径给这三列写空串(非 NULL),('','','','remove') 对所有条目是同一个键 ——
   第一条之后每次 INSERT 都撞唯一索引、被 ON CONFLICT DO NOTHING 静默吞掉,留下
   「状态已 disabling、没有任务」的永久卡死条目,而车道恰好隐藏 disabling,运维也
   看不到。改为写 NULL(语义:无变更集身份)+ 冲突目标收敛到 idempotency_key +
   校验 RowsAffected()(0 行回查既有任务;查不到就报错,不提交无任务的 disabling)。
   卡死状态在重试时可自愈。

测试:internal/platform/postgres/test_publication_removal_integration_test.go(两条发布
各自入队、business_* 为 NULL、卡死自愈;把修复退回旧写法该测试即 FAIL,已实测)、
internal/httpapi/test_publication_removal_test.go(权限/幂等键/错误映射/未接线 501/列表
过滤)、前端 testPublications.test.ts + WeKnoraMonitor.test.tsx。

验收(纯界面,两条都走完):解析监控 → 需下架(2)→ 逐条「下架」+ 二次确认 →
retrieval_state=disabled(PDF 03:57:03Z、md 04:08:55Z),jobs completed(attempt 2,
首次 test_remove_pending=远端仍在处理,重试通过;工作流以 GetKnowledge 读回 404 判
成功)→ 车道需下架(0)。核验「确认前不执行」:首次点击后仍 unpublished、移除任务数仍 1。
2026-10-10 12:10:46 +08:00

134 lines
4.6 KiB
Go

package httpapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"knowledge-governance/internal/platform/postgres"
)
// removalFakeStore 在既有 fakeJobStore 之上补出「移除入口」这一条能力,
// 用来验证路由的权限、幂等键要求与错误映射。
type removalFakeStore struct {
fakeJobStore
capturedPublicationID string
jobID string
err error
}
func (s *removalFakeStore) StartTestPublicationRemoval(_ context.Context, publicationID string, _ time.Time) (string, error) {
s.capturedPublicationID = publicationID
return s.jobID, s.err
}
// storeWithoutRemoval 只满足大接口、没有移除能力:路由必须诚实报 501,
// 而不是把"没接线"当成成功。
type storeWithoutRemoval struct{ Store }
func TestStartTestPublicationRemovalHandler(t *testing.T) {
store := &removalFakeStore{jobID: "job-remove-1"}
handler, err := NewHandler(Config{Store: store})
if err != nil {
t.Fatal(err)
}
// 缺 Idempotency-Key:写路由一律要求幂等键。
missing := httptest.NewRecorder()
handler.ServeHTTP(missing, httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/tp-1/removal", nil))
if missing.Code != http.StatusBadRequest {
t.Fatalf("missing idempotency key status=%d", missing.Code)
}
// 非法 id。
badID := httptest.NewRecorder()
badRequest := httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/bad%20id/removal", nil)
badRequest.Header.Set("Idempotency-Key", "op-remove-1")
handler.ServeHTTP(badID, badRequest)
if badID.Code != http.StatusBadRequest {
t.Fatalf("invalid id status=%d", badID.Code)
}
// 正常受理。
accepted := httptest.NewRecorder()
acceptedRequest := httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/tp-1/removal", nil)
acceptedRequest.Header.Set("Idempotency-Key", "op-remove-2")
handler.ServeHTTP(accepted, acceptedRequest)
if accepted.Code != http.StatusAccepted {
t.Fatalf("accepted status=%d body=%s", accepted.Code, accepted.Body.String())
}
var body map[string]string
if err := json.Unmarshal(accepted.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if body["publication_id"] != "tp-1" || body["job_id"] != "job-remove-1" || body["state"] != "disabling" {
t.Fatalf("accepted body=%v", body)
}
if store.capturedPublicationID != "tp-1" {
t.Fatalf("store saw %q", store.capturedPublicationID)
}
// 错误映射。
for _, testCase := range []struct {
name string
err error
status int
}{
{"not found", postgres.ErrPublicationNotFound, http.StatusNotFound},
{"already removed", postgres.ErrPublicationRemovalDone, http.StatusConflict},
{"state conflict", postgres.ErrPublicationStateConflict, http.StatusConflict},
{"unexpected", errors.New("boom"), http.StatusInternalServerError},
} {
failing := &removalFakeStore{err: testCase.err}
failingHandler, err := NewHandler(Config{Store: failing})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/tp-1/removal", nil)
request.Header.Set("Idempotency-Key", "op-remove-3")
failingHandler.ServeHTTP(recorder, request)
if recorder.Code != testCase.status {
t.Fatalf("%s status=%d want=%d", testCase.name, recorder.Code, testCase.status)
}
}
// 未接线:诚实 501。
unwired, err := NewHandler(Config{Store: storeWithoutRemoval{Store: store}})
if err != nil {
t.Fatal(err)
}
unwiredRecorder := httptest.NewRecorder()
unwiredRequest := httptest.NewRequest(http.MethodPost, "/api/v1/test-publications/tp-1/removal", nil)
unwiredRequest.Header.Set("Idempotency-Key", "op-remove-4")
unwired.ServeHTTP(unwiredRecorder, unwiredRequest)
if unwiredRecorder.Code != http.StatusNotImplemented {
t.Fatalf("unwired status=%d", unwiredRecorder.Code)
}
}
func TestListTestPublicationsRemovalFilter(t *testing.T) {
store := &fakeJobStore{}
handler, err := NewHandler(Config{Store: store})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/test-publications?removal=required", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("status=%d", recorder.Code)
}
if !store.publicationFilter.RemovalRequired {
t.Fatalf("filter=%#v, want RemovalRequired", store.publicationFilter)
}
bogus := httptest.NewRecorder()
handler.ServeHTTP(bogus, httptest.NewRequest(http.MethodGet, "/api/v1/test-publications?removal=bogus", nil))
if bogus.Code != http.StatusBadRequest {
t.Fatalf("bogus removal status=%d", bogus.Code)
}
}