skillhub/github-sync-go/internal/github/tree_test.go
XiaoSeS 2df4c913cb feat(github-sync): add continuous public repository discovery and skill sync
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-10-20 14:40:46 +08:00

34 lines
1.3 KiB
Go

package github
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
func TestFixedCommitTreeRejectsMismatchedResponses(t *testing.T) {
const commitSHA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
const treeSHA = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
client := NewClient("")
client.http.Transport = roundTripFunc(func(request *http.Request) (*http.Response, error) {
var body string
switch request.URL.Path {
case "/repos/owner/repo/git/commits/" + commitSHA:
body = `{"sha":"cccccccccccccccccccccccccccccccccccccccc","tree":{"sha":"` + treeSHA + `"}}`
case "/repos/owner/repo/git/trees/" + treeSHA:
body = `{"sha":"dddddddddddddddddddddddddddddddddddddddd","truncated":false,"tree":[]}`
default:
t.Fatalf("unexpected GitHub tree request: %s", request.URL.String())
}
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(body)), Request: request}, nil
})
if _, _, err := client.GetCommitTreeSHA(context.Background(), "owner", "repo", commitSHA); err == nil {
t.Fatal("commit response with a different SHA must be rejected")
}
if _, _, err := client.GetTree(context.Background(), "owner", "repo", treeSHA, false); err == nil {
t.Fatal("tree response with a different SHA must be rejected")
}
}