fix(weknora): 同内容重复上传改为认领而非死信

This commit is contained in:
姚章浩
2026-09-15 15:02:13 +08:00
parent e8dae5c7f7
commit 40d0c511d6
2 changed files with 71 additions and 2 deletions
@@ -402,3 +402,55 @@ func TestUploadHandlerAdoptsSameSHAItemOnDuplicateConflict(t *testing.T) {
t.Fatalf("knowledge=%q", store.recordedKnowledgeID)
}
}
func TestUploadHandlerAdoptsUniqueSameSHAItemWhenOwnRecoveryFails(t *testing.T) {
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "objects"), 0o700); err != nil {
t.Fatal(err)
}
body := []byte("uploading-state duplicate\n")
if err := os.WriteFile(filepath.Join(root, "objects", "artifact.md"), body, 0o600); err != nil {
t.Fatal(err)
}
digest := sha256.Sum256(body)
sha := hex.EncodeToString(digest[:])
var uploads atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodPost:
uploads.Add(1)
http.Error(w, "must not upload while recovering", http.StatusInternalServerError)
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/knowledge-bases/kb-1/knowledge":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":true,"data":[{"id":"knowledge-sibling","knowledge_base_id":"kb-1","metadata":{"publication_id":"publication-sibling","content_sha256":"` + sha + `"}}],"total":1,"page":1,"page_size":100}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/knowledge/knowledge-sibling":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"knowledge-sibling","knowledge_base_id":"kb-1","metadata":{"publication_id":"publication-sibling","content_sha256":"` + sha + `"}}}`))
default:
http.NotFound(w, r)
}
}))
defer server.Close()
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-key", Timeout: time.Second})
if err != nil {
t.Fatal(err)
}
store := &uploadStoreStub{candidate: postgresstore.PublicationForUpload{
ID: "publication-1", TestKBID: "kb-1", IngestState: governance.IngestUploading,
ProcessingState: governance.ProcessingPending, SHA256: sha, SizeBytes: int64(len(body)),
ValidatedFormat: "md", LocalSpoolRef: "objects/artifact.md", Title: "Safe title",
}}
handler, err := NewUploadHandler(UploadHandlerConfig{
Store: store, Client: client, SpoolRoot: root, MaxUploadBytes: 50 * 1024 * 1024,
FirstPollDelay: time.Second, Now: func() time.Time { return time.Now() },
})
if err != nil {
t.Fatal(err)
}
if err := handler.Handle(context.Background(), jobs.Job{Payload: []byte(`{"publication_id":"publication-1"}`)}); err != nil {
t.Fatalf("Handle() error = %v, want sibling adoption", err)
}
if uploads.Load() != 0 || store.recordedKnowledgeID != "knowledge-sibling" {
t.Fatalf("uploads=%d knowledge=%q", uploads.Load(), store.recordedKnowledgeID)
}
}