MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / TestNodeLifecycleAndProxyEndpoints

Function TestNodeLifecycleAndProxyEndpoints

internal/serverapi/api_test.go:13–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestNodeLifecycleAndProxyEndpoints(t *testing.T) {
14 hub := &fakeHub{handlers: map[string]func(body any) (json.RawMessage, error){
15 "Runtime": func(any) (json.RawMessage, error) {
16 return json.RawMessage(`{"available":true,"version":"v1.13.3"}`), nil
17 },
18 "Status": func(any) (json.RawMessage, error) {
19 return json.RawMessage(`{"running":true}`), nil
20 },
21 "Start": func(b any) (json.RawMessage, error) {
22 req, ok := b.(nodes.ConfigRequest)
23 if !ok || req.Config == "" {
24 return nil, &nodeAPIError{"bad request"}
25 }
26 return json.RawMessage(`{"running":true}`), nil
27 },
28 }}
29
30 store := nodes.NewMemoryStore()
31 api := New(store)
32 api.clientFactory = fakeHubClientFactory(hub)
33 mux := http.NewServeMux()
34 api.Register(mux)
35
36 upsertBody := []byte(`{"id":"node-1","name":"node 1","base_url":"http://node.test"}`)
37 rec := httptest.NewRecorder()
38 req := httptest.NewRequest(http.MethodPost, "/v1/nodes", bytes.NewReader(upsertBody))
39 mux.ServeHTTP(rec, req)
40 if rec.Code != http.StatusOK {
41 t.Fatalf("upsert node status = %d", rec.Code)
42 }
43
44 rec = httptest.NewRecorder()
45 req = httptest.NewRequest(http.MethodGet, "/v1/nodes", nil)
46 mux.ServeHTTP(rec, req)
47 if rec.Code != http.StatusOK {
48 t.Fatalf("list nodes status = %d", rec.Code)
49 }
50
51 rec = httptest.NewRecorder()
52 req = httptest.NewRequest(http.MethodGet, "/v1/nodes/node-1/runtime", nil)
53 mux.ServeHTTP(rec, req)
54 if rec.Code != http.StatusOK {
55 t.Fatalf("runtime status = %d body=%s", rec.Code, rec.Body.String())
56 }
57
58 // runtime/usage 在 hub 模式下走按需 hub 拉取(fakeHub 默认返回 {}),应返回 200。
59 rec = httptest.NewRecorder()
60 req = httptest.NewRequest(http.MethodGet, "/v1/nodes/node-1/runtime/usage", nil)
61 mux.ServeHTTP(rec, req)
62 if rec.Code != http.StatusOK {
63 t.Fatalf("usage status = %d body=%s", rec.Code, rec.Body.String())
64 }
65
66 rec = httptest.NewRecorder()
67 req = httptest.NewRequest(http.MethodPost, "/v1/nodes/node-1/runtime/start", bytes.NewReader([]byte(`{"config":"{}"}`)))
68 mux.ServeHTTP(rec, req)
69 if rec.Code != http.StatusOK {
70 t.Fatalf("start status = %d body=%s", rec.Code, rec.Body.String())

Callers

nothing calls this directly

Calls 6

NewMemoryStoreFunction · 0.92
fakeHubClientFactoryFunction · 0.85
ServeHTTPMethod · 0.80
NewFunction · 0.70
RegisterMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected