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 项通过。
97 lines
3.6 KiB
Go
97 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
)
|
|
|
|
type capturePersistenceFake struct {
|
|
failAdvance error
|
|
failStage error
|
|
advanced []governance.LibraryJobState
|
|
stages []string
|
|
}
|
|
|
|
func (f *capturePersistenceFake) AdvanceLibraryJob(_ context.Context, _ string, state governance.LibraryJobState,
|
|
_, _, _, _, _ string, _ time.Time) error {
|
|
f.advanced = append(f.advanced, state)
|
|
return f.failAdvance
|
|
}
|
|
|
|
func (f *capturePersistenceFake) UpdateLibraryStage(_ context.Context, _, stage, state string,
|
|
_, _, _, _ *int64, _ string, _ time.Time) error {
|
|
f.stages = append(f.stages, stage+"/"+state)
|
|
return f.failStage
|
|
}
|
|
|
|
// The handler must stop before touching the source when the starting state
|
|
// cannot be persisted. This is the failure injection that the old log-and-ignore
|
|
// path hid behind a completed queue row.
|
|
func TestBeginLibraryCaptureStopsOnAdvanceFailure(t *testing.T) {
|
|
store := &capturePersistenceFake{failAdvance: errors.New("injected advance failure")}
|
|
err := beginLibraryCaptureJob(context.Background(), store, "libseal-test", time.Now())
|
|
if err == nil || !strings.Contains(err.Error(), "cannot be advanced") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobRunning {
|
|
t.Fatalf("advance calls=%v", store.advanced)
|
|
}
|
|
if len(store.stages) != 0 {
|
|
t.Fatalf("stage was written before advance failure: %v", store.stages)
|
|
}
|
|
}
|
|
|
|
func TestBeginLibraryCaptureStopsOnStageFailure(t *testing.T) {
|
|
store := &capturePersistenceFake{failStage: errors.New("injected stage failure")}
|
|
err := beginLibraryCaptureJob(context.Background(), store, "libseal-test", time.Now())
|
|
if err == nil || !strings.Contains(err.Error(), "stage cannot be advanced") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if len(store.stages) != 1 || store.stages[0] != "export/running" {
|
|
t.Fatalf("stage calls=%v", store.stages)
|
|
}
|
|
}
|
|
|
|
// Terminal persistence is two writes; either failure must be returned rather
|
|
// than turning the queue row into completed while the business job is pending.
|
|
func TestCompleteLibraryCaptureStopsOnStageFailure(t *testing.T) {
|
|
store := &capturePersistenceFake{failStage: errors.New("injected success stage failure")}
|
|
err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now())
|
|
if err == nil || !strings.Contains(err.Error(), "success stage cannot be persisted") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if len(store.advanced) != 0 {
|
|
t.Fatalf("advance must not run after stage failure: %v", store.advanced)
|
|
}
|
|
}
|
|
|
|
func TestCompleteLibraryCaptureStopsOnAdvanceFailure(t *testing.T) {
|
|
store := &capturePersistenceFake{failAdvance: errors.New("injected success advance failure")}
|
|
err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now())
|
|
if err == nil || !strings.Contains(err.Error(), "success terminal cannot be persisted") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if len(store.stages) != 1 || store.stages[0] != "export/succeeded" {
|
|
t.Fatalf("stage calls=%v", store.stages)
|
|
}
|
|
if len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobSucceeded {
|
|
t.Fatalf("advance calls=%v", store.advanced)
|
|
}
|
|
}
|
|
|
|
func TestCompleteLibraryCapturePersistsBothFacts(t *testing.T) {
|
|
store := &capturePersistenceFake{}
|
|
if err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(store.stages) != 1 || store.stages[0] != "export/succeeded" ||
|
|
len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobSucceeded {
|
|
t.Fatalf("stages=%v advanced=%v", store.stages, store.advanced)
|
|
}
|
|
}
|