-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz_testserver_test.go
More file actions
80 lines (71 loc) · 2.27 KB
/
Copy pathzz_testserver_test.go
File metadata and controls
80 lines (71 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: AGPL-3.0-or-later
package skillinject_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/pilot-protocol/skillinject"
)
// fakeRepo is a tiny HTTP server that mimics raw.githubusercontent.com
// for the pilot-skills repo. Tests swap in different files to simulate
// drift, missing entrypoint, etc.
type fakeRepo struct {
srv *httptest.Server
manifest skillinject.Manifest
files map[string][]byte // relPath → body
}
func newFakeRepo(t *testing.T) *fakeRepo {
t.Helper()
r := &fakeRepo{files: map[string][]byte{}}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
path := strings.TrimPrefix(req.URL.Path, "/")
if path == "inject-manifest.json" {
b, _ := json.Marshal(r.manifest)
w.Write(b)
return
}
if body, ok := r.files[path]; ok {
w.Write(body)
return
}
http.NotFound(w, req)
})
r.srv = httptest.NewServer(mux)
t.Cleanup(r.srv.Close)
return r
}
// withDefault sets up a sane default manifest with one entrypoint and
// a configurable set of tools. testContent is served as the entrypoint
// SKILL.md and a benign heartbeat template is served per tool.
const testContent = "---\nname: pilotctl\n---\n\n# Test entrypoint skill\n\nbody.\n"
const testHeartbeat = "Pilot Protocol skill installed. See {{.EntrypointPath}} — overlay/NAT/peer/pilotctl."
// withTools builds a manifest with the given tool rows + serves the
// canonical entrypoint + heartbeat templates.
func (r *fakeRepo) withTools(tools []skillinject.ManifestTool) {
r.manifest = skillinject.Manifest{
Version: 1,
Entrypoint: "pilotctl",
Tools: tools,
}
r.files["skills/pilotctl/SKILL.md"] = []byte(testContent)
for _, t := range tools {
if t.HeartbeatTemplate != "" {
r.files[t.HeartbeatTemplate] = []byte(testHeartbeat)
}
}
}
// cfg returns a skillinject.Config wired to point at the fake server.
func (r *fakeRepo) cfg(home string) skillinject.Config {
return skillinject.Config{
Home: home,
ManifestURL: r.srv.URL + "/inject-manifest.json",
RepoBaseURL: r.srv.URL + "/",
}
}
// setSkillBody overrides the entrypoint SKILL.md body (for drift tests).
func (r *fakeRepo) setSkillBody(body []byte) {
r.files["skills/pilotctl/SKILL.md"] = body
}