Files
Agent-System-Backend/internal/server/task_sources_test.go
T
weichen c50f9f07e8 Guard source previews against content version substitution
Why: Opening a processed source must not show changed current bytes as the quoted version.

Evidence: SELF TaskSourceContent regression fails before and passes after expected-hash enforcement; artifact regressions and live compilation pass.

Impact: Optional content hash narrows the existing authorized artifact read; controlled fixtures select a genuinely provided quotation.

Refs: fast-slow-u2-u3 source-version acceptance
2026-10-13 02:51:07 +00:00

39 lines
1.1 KiB
Go

package server
import (
"agent-system-backend/internal/store"
core "agent-system-core"
"crypto/sha256"
"encoding/hex"
"net/http"
"net/http/httptest"
"testing"
)
func TestTaskSourceContentDoesNotSubstituteLatestVersion(t *testing.T) {
srv, st := newTestServer(t)
original := []byte("上线六月")
a := core.Artifact{Owner: store.OwnerDefault, ProjectID: "p", Name: "input.txt"}
if e := st.CreateArtifact(&a, original); e != nil {
t.Fatal(e)
}
sum := sha256.Sum256(original)
hash := hex.EncodeToString(sum[:])
get := func() *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "/api/artifacts/"+a.ID+"/content?expectedHash="+hash, nil)
r.SetPathValue("id", a.ID)
w := httptest.NewRecorder()
srv.artifactContent(w, r)
return w
}
if w := get(); w.Code != http.StatusOK || w.Body.String() != string(original) {
t.Fatal("original version not readable", w.Code, w.Body.String())
}
if e := st.UpdateArtifactContent(store.OwnerDefault, a.ID, []byte("上线七月")); e != nil {
t.Fatal(e)
}
if w := get(); w.Code != http.StatusConflict {
t.Fatal("latest content masquerades as processed version", w.Code, w.Body.String())
}
}