8e6094e9e5
build / build (push) Successful in 2m6s
F01:新增 /api/v1/system/worker-capabilities,按 job_types 判定 test.upload 执行器是否注册, 服务卡片分「地址可达 / 凭据与库读取 / 执行器能力」三行。 F04:routing-bindings 返回 LIBRARY_SOURCE_KB_* 与一致性,设置页同屏显示入库目标与封存源。 结构:测试绑定与生产目标合并为「知识库映射」。
247 lines
10 KiB
Go
247 lines
10 KiB
Go
package testweknora
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/jobs"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"knowledge-governance/internal/platform/weknora"
|
|
)
|
|
|
|
func TestPollHandlerSchedulesNextSequenceForRemoteProgress(t *testing.T) {
|
|
remoteUpdated := time.Date(2026, time.September, 4, 3, 39, 0, 0, time.UTC)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/knowledge/knowledge-1" {
|
|
t.Fatalf("request = %s %s", r.Method, r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"knowledge-1","knowledge_base_id":"kb-1","parse_status":"finalizing","updated_at":"2026-09-04T03:39:00Z"}}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-api-key", Timeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Date(2026, time.September, 4, 3, 40, 0, 0, time.UTC)
|
|
lastProgress := now.Add(-time.Minute)
|
|
store := &pollStoreStub{publication: postgresstore.PublicationForPoll{
|
|
ID: "publication-1", TestKBID: "kb-1", KnowledgeID: "knowledge-1",
|
|
ProcessingState: governance.ProcessingProcessing, PollSequence: 2,
|
|
LastRemoteStatus: "processing", LastProgressAt: &lastProgress,
|
|
}}
|
|
handler, err := NewPollHandler(PollHandlerConfig{
|
|
Store: store, Client: client, PollDelay: 15 * time.Second,
|
|
StallAfter: 30 * time.Minute, Now: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.Handle(context.Background(), jobs.Job{Payload: []byte(`{"publication_id":"publication-1","sequence":2}`)}); err != nil {
|
|
t.Fatalf("Handle() error = %v", err)
|
|
}
|
|
if store.recordedState != governance.ProcessingFinalizing || store.recordedRemoteUpdated == nil || !store.recordedRemoteUpdated.Equal(remoteUpdated) || !store.nextPollAt.Equal(now.Add(15*time.Second)) {
|
|
t.Fatalf("store = %#v", store)
|
|
}
|
|
}
|
|
|
|
func TestPollHandlerCompletesAndSchedulesValidationWithoutNextPoll(t *testing.T) {
|
|
completedAt := time.Date(2026, time.September, 4, 3, 45, 0, 0, time.UTC)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/knowledge/knowledge-1" {
|
|
t.Fatalf("request = %s %s", r.Method, r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"knowledge-1","knowledge_base_id":"kb-1","parse_status":"completed","updated_at":"2026-09-04T03:45:00Z"}}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-api-key", Timeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Date(2026, time.September, 4, 3, 46, 0, 0, time.UTC)
|
|
lastProgress := now.Add(-time.Minute)
|
|
store := &pollStoreStub{publication: postgresstore.PublicationForPoll{
|
|
ID: "publication-1", TestKBID: "kb-1", KnowledgeID: "knowledge-1",
|
|
ProcessingState: governance.ProcessingProcessing, PollSequence: 3,
|
|
LastRemoteStatus: "processing", LastProgressAt: &lastProgress,
|
|
}}
|
|
handler, err := NewPollHandler(PollHandlerConfig{
|
|
Store: store, Client: client, PollDelay: 15 * time.Second,
|
|
StallAfter: 30 * time.Minute, Now: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.Handle(context.Background(), jobs.Job{Payload: []byte(`{"publication_id":"publication-1","sequence":3}`)}); err != nil {
|
|
t.Fatalf("Handle() error = %v", err)
|
|
}
|
|
if !store.completedValidated || store.recordedState != "" || store.recordedRemoteUpdated == nil || !store.recordedRemoteUpdated.Equal(completedAt) {
|
|
t.Fatalf("store = %#v", store)
|
|
}
|
|
}
|
|
|
|
func TestPollHandlerMarksStalledWhenNoProgressExceedsThreshold(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/knowledge/knowledge-1" {
|
|
t.Fatalf("request = %s %s", r.Method, r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"knowledge-1","knowledge_base_id":"kb-1","parse_status":"finalizing","updated_at":"2026-09-04T03:10:00Z"}}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-api-key", Timeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Date(2026, time.September, 4, 3, 50, 0, 0, time.UTC)
|
|
stalledSince := now.Add(-45 * time.Minute)
|
|
store := &pollStoreStub{publication: postgresstore.PublicationForPoll{
|
|
ID: "publication-1", TestKBID: "kb-1", KnowledgeID: "knowledge-1",
|
|
ProcessingState: governance.ProcessingProcessing, PollSequence: 4,
|
|
LastRemoteStatus: "processing", LastProgressAt: &stalledSince,
|
|
}}
|
|
handler, err := NewPollHandler(PollHandlerConfig{
|
|
Store: store, Client: client, PollDelay: 15 * time.Second,
|
|
StallAfter: 30 * time.Minute, Now: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.Handle(context.Background(), jobs.Job{Payload: []byte(`{"publication_id":"publication-1","sequence":4}`)}); err != nil {
|
|
t.Fatalf("Handle() error = %v", err)
|
|
}
|
|
if !store.stalledMarked || store.recordedState != "" || !store.stalledCutoff.Equal(now.Add(-30*time.Minute)) {
|
|
t.Fatalf("store = %#v", store)
|
|
}
|
|
}
|
|
|
|
func TestPollHandlerRejectsStaleSequenceWithoutPollingRemote(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
t.Fatal("stale sequence must not poll the remote knowledge endpoint")
|
|
}))
|
|
defer server.Close()
|
|
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-api-key", Timeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Date(2026, time.September, 4, 4, 0, 0, 0, time.UTC)
|
|
store := &pollStoreStub{publication: postgresstore.PublicationForPoll{
|
|
ID: "publication-1", TestKBID: "kb-1", KnowledgeID: "knowledge-1",
|
|
ProcessingState: governance.ProcessingProcessing, PollSequence: 5,
|
|
}}
|
|
handler, err := NewPollHandler(PollHandlerConfig{
|
|
Store: store, Client: client, PollDelay: 15 * time.Second,
|
|
StallAfter: 30 * time.Minute, Now: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.Handle(context.Background(), jobs.Job{Payload: []byte(`{"publication_id":"publication-1","sequence":2}`)}); !errors.Is(err, ErrPollStateConflict) {
|
|
t.Fatalf("Handle() error = %v, want state conflict", err)
|
|
}
|
|
if store.recordedState != "" || store.stalledMarked || store.completedValidated {
|
|
t.Fatalf("store mutated for stale sequence: %#v", store)
|
|
}
|
|
}
|
|
|
|
type pollStoreStub struct {
|
|
publication postgresstore.PublicationForPoll
|
|
recordedState governance.ProcessingState
|
|
recordedRemoteUpdated *time.Time
|
|
nextPollAt time.Time
|
|
completedValidated bool
|
|
stalledMarked bool
|
|
stalledCutoff time.Time
|
|
}
|
|
|
|
func (store *pollStoreStub) LoadPublicationForPoll(context.Context, string) (postgresstore.PublicationForPoll, error) {
|
|
return store.publication, nil
|
|
}
|
|
|
|
func (store *pollStoreStub) RecordRemoteStatusAndScheduleNextPoll(_ context.Context, _ string, _ int, state governance.ProcessingState, remoteUpdated *time.Time, _ time.Time, nextAt time.Time) (int, error) {
|
|
store.recordedState = state
|
|
store.recordedRemoteUpdated = remoteUpdated
|
|
store.nextPollAt = nextAt
|
|
return store.publication.PollSequence + 1, nil
|
|
}
|
|
|
|
func (store *pollStoreStub) RecordCompletedAndScheduleValidation(_ context.Context, _ string, _ int, remoteUpdated *time.Time, _ time.Time) error {
|
|
store.completedValidated = true
|
|
store.recordedRemoteUpdated = remoteUpdated
|
|
return nil
|
|
}
|
|
|
|
func (store *pollStoreStub) RecordProcessingFailure(context.Context, string, int, string, time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (store *pollStoreStub) MarkStalled(_ context.Context, _ string, _ int, cutoff, _ time.Time) error {
|
|
store.stalledMarked = true
|
|
store.stalledCutoff = cutoff
|
|
return nil
|
|
}
|
|
|
|
// F02 验收:停滞判定必须用“活动配置”的阈值,而不是启动时的常量。
|
|
// 同一条已经停在同一进度上的解析任务:阈值 30 分钟判停,阈值 2 小时不判停。
|
|
func TestPollHandlerStallThresholdFollowsActiveSettings(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"knowledge-1","knowledge_base_id":"kb-1","parse_status":"processing","updated_at":"2026-09-04T03:00:00Z"}}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-key", Timeout: time.Second})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Date(2026, time.September, 4, 4, 0, 0, 0, time.UTC)
|
|
progressAt := now.Add(-45 * time.Minute)
|
|
makeStore := func() *pollStoreStub {
|
|
return &pollStoreStub{publication: postgresstore.PublicationForPoll{
|
|
ID: "publication-1", TestKBID: "kb-1", KnowledgeID: "knowledge-1",
|
|
ProcessingState: governance.ProcessingProcessing, PollSequence: 3,
|
|
LastProgressAt: &progressAt, ProcessingStartedAt: &progressAt,
|
|
}}
|
|
}
|
|
payload := jobs.Job{Payload: []byte(`{"publication_id":"publication-1","sequence":3}`)}
|
|
|
|
// 启动常量 30 分钟、活动配置也是 30 分钟:45 分钟没进度 → 判停。
|
|
store := makeStore()
|
|
handler, err := NewPollHandler(PollHandlerConfig{
|
|
Store: store, Client: client, PollDelay: 15 * time.Second,
|
|
StallAfter: 30 * time.Minute, Now: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.Handle(context.Background(), payload); err != nil {
|
|
t.Fatalf("Handle() error = %v", err)
|
|
}
|
|
if !store.stalledMarked {
|
|
t.Fatal("30 分钟阈值下 45 分钟无进度应判停")
|
|
}
|
|
|
|
// 活动配置改成 2 小时:同一条任务不应再判停。
|
|
store = makeStore()
|
|
handler, err = NewPollHandler(PollHandlerConfig{
|
|
Store: store, Client: client, PollDelay: 15 * time.Second,
|
|
StallAfter: 30 * time.Minute,
|
|
StallAfterSource: func() time.Duration { return 2 * time.Hour },
|
|
Now: func() time.Time { return now },
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.Handle(context.Background(), payload); err != nil {
|
|
t.Fatalf("Handle() error = %v", err)
|
|
}
|
|
if store.stalledMarked {
|
|
t.Fatal("活动配置 2 小时阈值下不应判停:阈值没有接入实际判定")
|
|
}
|
|
}
|