146ac832a2
build / build (push) Successful in 2m9s
修复 varchar 状态参数在 AdvanceLibraryJob 的 42P08;worker 开始/终态写入失败 不再吞掉。新增管理员核对路由,仅在 completed capture queue、export succeeded、 精确 test_sealed 版本、来源身份和归档复核一致时,以条件更新修复仍为 pending/running 的同一作业;已有不同快照或并发失败/取消行拒绝覆盖。保留 第一因与历史记录,不重新封存、不产生快照、不写源或目标知识。 验证:真实 varchar DDL PG 路径、handler 负例/幂等、worker 注入、web 组件; umask022 与真实隔离 kg_library_test_* 下 Go 全量与 make verify 17 项通过。
274 lines
9.8 KiB
Go
274 lines
9.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/jobs"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
func (f *fakeLibraryStore) QueueJob(_ context.Context, id string) (jobs.Job, bool, error) {
|
|
if f.queue == nil || f.queue.ID != id {
|
|
return jobs.Job{}, false, nil
|
|
}
|
|
return *f.queue, true, nil
|
|
}
|
|
|
|
func (f *fakeLibraryStore) LibraryCaptureExportEvidence(_ context.Context, _ string) (string, string, error) {
|
|
return f.stageState, "", f.stageErr
|
|
}
|
|
|
|
func (f *fakeLibraryStore) LibraryJobStage(_ context.Context, _, stage string) (string, string, error) {
|
|
if f.stageErr != nil {
|
|
return "", "", f.stageErr
|
|
}
|
|
if stage != "export" {
|
|
return "", "", errors.New("unexpected stage")
|
|
}
|
|
return f.stageState, "", nil
|
|
}
|
|
|
|
func (f *fakeLibraryStore) LibraryTestSealedVersionForJob(_ context.Context, scope, jobID string) (governance.LibraryVersion, bool, error) {
|
|
if f.versionErr != nil {
|
|
return governance.LibraryVersion{}, false, f.versionErr
|
|
}
|
|
if f.sealedVersion == nil || f.sealedVersion.Scope != scope || f.sealedVersion.SourceJobID != jobID {
|
|
return governance.LibraryVersion{}, false, nil
|
|
}
|
|
return *f.sealedVersion, true, nil
|
|
}
|
|
|
|
func reconcileFixture(t *testing.T) (*fakeLibraryStore, Config, governance.LibraryJob, []byte) {
|
|
t.Helper()
|
|
root := libraryFixtureRoot(t)
|
|
snapshotID := sealSharedVersions(t, root, 1, 4)[0]
|
|
archive, err := librarysnapshot.Open(root, 256*1024*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest, err := archive.Load(snapshotID)
|
|
archive.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
config := Config{LibraryArchiveRoot: root,
|
|
LibrarySourceProject: "fixture-project", LibrarySourceApp: "fixture-app",
|
|
LibrarySourceTenantID: manifest.TenantID, LibrarySourceInternalKB: manifest.KnowledgeBaseID}
|
|
source := configuredLibrarySource(config, "internal")
|
|
sourceID, err := source.ID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job := governance.LibraryJob{ID: "libseal-cap-1", Scope: "internal", Kind: governance.LibraryJobSeal,
|
|
State: governance.LibraryJobPending, QueueJobID: "libcap-1"}
|
|
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "cap-1"})
|
|
store := &fakeLibraryStore{
|
|
jobs: []governance.LibraryJob{job},
|
|
stageState: "succeeded",
|
|
queue: &jobs.Job{ID: "libcap-1", Type: libraryCaptureJobType, State: jobs.StateCompleted,
|
|
Payload: payload, IdempotencyKey: "library.capture:cap-1"},
|
|
sealedVersion: &governance.LibraryVersion{ID: "libver-1", Scope: "internal",
|
|
Kind: governance.LibraryVersionTestSealed, SnapshotID: snapshotID, SourceJobID: job.ID,
|
|
Evidence: map[string]any{"source_id": sourceID, "capture_job": "cap-1"}},
|
|
}
|
|
config.Store = store
|
|
return store, config, job, payload
|
|
}
|
|
|
|
func (f *fakeLibraryStore) ReconcileLibraryCaptureTerminal(_ context.Context, jobID, scope, queueJobID, snapshotID string, _ time.Time) (bool, error) {
|
|
if f.reconcileDenied {
|
|
return false, nil
|
|
}
|
|
for index := range f.jobs {
|
|
if f.jobs[index].ID != jobID || f.jobs[index].Scope != scope || f.jobs[index].Kind != governance.LibraryJobSeal ||
|
|
f.jobs[index].QueueJobID != queueJobID || (f.jobs[index].State != governance.LibraryJobPending && f.jobs[index].State != governance.LibraryJobRunning) ||
|
|
(f.jobs[index].SnapshotID != "" && f.jobs[index].SnapshotID != snapshotID) {
|
|
return false, nil
|
|
}
|
|
f.jobs[index].State = governance.LibraryJobSucceeded
|
|
f.jobs[index].SnapshotID = snapshotID
|
|
f.lastState = governance.LibraryJobSucceeded
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
func runReconcile(t *testing.T, config Config, jobID string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/"+jobID+"/reconcile-capture", nil)
|
|
req.SetPathValue("id", jobID)
|
|
rec := httptest.NewRecorder()
|
|
libraryCaptureReconcile(rec, req, config)
|
|
return rec
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRepairsExactCompletedCapture(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body["repaired"] != true || body["snapshot_id"] == "" || body["audit_operation_id"] == "" {
|
|
t.Fatalf("body=%v", body)
|
|
}
|
|
if store.lastState != governance.LibraryJobSucceeded {
|
|
t.Fatalf("advanced state=%s", store.lastState)
|
|
}
|
|
if len(store.operations) != 1 {
|
|
t.Fatalf("audit operations=%v", store.operations)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileAlreadySucceededIsReadOnly(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.State = governance.LibraryJobSucceeded
|
|
job.SnapshotID = store.sealedVersion.SnapshotID
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
|
if body["already_succeeded"] != true || body["repaired"] != false || store.lastState != "" {
|
|
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsQueueNotCompleted(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.queue.State = jobs.StateRunning
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsStageNotSucceeded(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.stageState = "failed"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsWrongVersionSource(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.Evidence["source_id"] = "other-source"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsWrongCaptureJob(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.Evidence["capture_job"] = "other-capture"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsBadArchive(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.SnapshotID = "missing-snapshot"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsFailedJob(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.State = governance.LibraryJobFailed
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileIsIdempotent(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
first := runReconcile(t, config, job.ID)
|
|
if first.Code != http.StatusOK {
|
|
t.Fatalf("first code=%d body=%s", first.Code, first.Body.String())
|
|
}
|
|
second := runReconcile(t, config, job.ID)
|
|
if second.Code != http.StatusOK {
|
|
t.Fatalf("second code=%d body=%s", second.Code, second.Body.String())
|
|
}
|
|
var body map[string]any
|
|
_ = json.Unmarshal(second.Body.Bytes(), &body)
|
|
if body["already_succeeded"] != true || len(store.operations) != 1 {
|
|
t.Fatalf("body=%v ops=%v", body, store.operations)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsMissingQueue(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.queue = nil
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsQueueScope(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.queue.Payload, _ = json.Marshal(map[string]string{"scope": "external", "capture_job": "cap-1"})
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsMissingVersion(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion = nil
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsWrongScopeVersion(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.sealedVersion.Scope = "external"
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileRejectsDifferentExistingSnapshot(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
job.SnapshotID = "other-snapshot"
|
|
store.jobs[0] = job
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestLibraryCaptureReconcileConditionalWriteRejectsConcurrentState(t *testing.T) {
|
|
store, config, job, _ := reconcileFixture(t)
|
|
store.reconcileDenied = true
|
|
rec := runReconcile(t, config, job.ID)
|
|
if rec.Code != http.StatusConflict || store.lastState != "" {
|
|
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
|
}
|
|
}
|