mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-09-11 19:05:39 +00:00
Rebase of #1881 by @SaarMesh-Bot onto current master. Their three commits are preserved, two of them cherry-picked with authorship intact; the sweep itself had to be regenerated. Opened as a new PR rather than force-pushing their branch. Closes #1881 once merged. Addresses parts 1 and 3 of #1859; part 2 landed as #1937. ## Why regenerated rather than merged The sweep in #1881 was cut on 2026-09-02 07:13 and roughly forty PRs landed after it, so it went `CONFLICTING/DIRTY`. Re-running `gofmt` on current master is cheaper and less error-prone than resolving 72 conflicts that are all whitespace. The drift it fixes also grew in the meantime: 66 files now, against 72 then, but spread differently. ## The three commits 1. **`style(#1859)`** — `gofmt -w` across the 14 modules. 66 files. 2. **`test(#1859)`** — @SaarMesh-Bot's fix for the one `go vet` copylocks finding, `cmd/ingestor/coverage_boost_test.go`: the range variable copied a `Config` embedding `sync.Once`. Cherry-picked unchanged. 3. **`ci(#1859)`** — @SaarMesh-Bot's CI step that fails on gofmt drift or vet findings, plus `.git-blame-ignore-revs`. Cherry-picked with one change, noted in the commit message: the ignore file pointed at `04bc80ee`, the sweep commit on their branch, which does not exist on this base and would make `git blame --ignore-revs-file` error. Repointed at `d3a02599`, the sweep here. ## Verification The claim "formatting only" is checked twice rather than asserted: - Every changed file is byte-identical to `gofmt(previous content)`. 0 of 66 deviate. - With line comments and all whitespace stripped, 0 of 66 files differ, so no code outside comments changed. 14 of the 66 also show doc-comment reflow. Since Go 1.19 `gofmt` re-indents indented comment blocks to tabs and inserts a blank comment line before them; the behavior matrix above `resolveHopWithContext` in `cmd/ingestor/path_resolver.go` is a clear example. That is gofmt's own output, not an edit, but it is worth naming because it makes the diff look larger than "whitespace" suggests. The gate was run locally exactly as the workflow runs it: `gofmt` clean, and `go vet` clean in all 14 modules, including `cmd/ingestor` which is what commit 2 fixes. Suites: `cmd/server` ok (80.7s), `internal/packetpath` ok (2.3s), `cmd/ingestor` passes except `TestWriteStatsAtomic_SymlinkAtDestIsReplaced`, which fails identically on bare master with "A required privilege is not held by the client" (Windows symlink privilege on my host, not code). ## Sequencing This should go last in the queue. The sweep touches 66 files, so merging it before the remaining open Go PRs gives each of them a conflict about nothing but formatting. After it lands the gate is active, and any PR with drift fails CI until it runs `gofmt -w`. Excluded from the sweep: the misnamed `Dockerfile.go`, which is a Dockerfile that gofmt cannot parse (the workflow excludes it too), and `docs/DEPLOYMENT.md`, which a case-insensitive filesystem surfaces as a spurious modification against `docs/deployment.md` and is unrelated. --------- Co-authored-by: SaarMesh-Bot <300107934+SaarMesh-Bot@users.noreply.github.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
141 lines
3.3 KiB
Go
141 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOpenAPISpecEndpoint(t *testing.T) {
|
|
_, r := setupTestServer(t)
|
|
|
|
req := httptest.NewRequest("GET", "/api/spec", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
ct := w.Header().Get("Content-Type")
|
|
if ct != "application/json; charset=utf-8" {
|
|
t.Errorf("unexpected content-type: %s", ct)
|
|
}
|
|
|
|
var spec map[string]interface{}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &spec); err != nil {
|
|
t.Fatalf("invalid JSON: %v", err)
|
|
}
|
|
|
|
// Check required OpenAPI fields
|
|
if spec["openapi"] != "3.0.3" {
|
|
t.Errorf("expected openapi 3.0.3, got %v", spec["openapi"])
|
|
}
|
|
|
|
info, ok := spec["info"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("missing info object")
|
|
}
|
|
if info["title"] != "CoreScope API" {
|
|
t.Errorf("unexpected title: %v", info["title"])
|
|
}
|
|
|
|
paths, ok := spec["paths"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("missing paths object")
|
|
}
|
|
|
|
// Should have at least 20 paths
|
|
if len(paths) < 20 {
|
|
t.Errorf("expected at least 20 paths, got %d", len(paths))
|
|
}
|
|
|
|
// Check a known path exists
|
|
if _, ok := paths["/api/nodes"]; !ok {
|
|
t.Error("missing /api/nodes path")
|
|
}
|
|
if _, ok := paths["/api/packets"]; !ok {
|
|
t.Error("missing /api/packets path")
|
|
}
|
|
|
|
// Check tags exist
|
|
tags, ok := spec["tags"].([]interface{})
|
|
if !ok || len(tags) == 0 {
|
|
t.Error("missing or empty tags")
|
|
}
|
|
|
|
// Check security schemes
|
|
components, ok := spec["components"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("missing components")
|
|
}
|
|
schemes, ok := components["securitySchemes"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("missing securitySchemes")
|
|
}
|
|
if _, ok := schemes["ApiKeyAuth"]; !ok {
|
|
t.Error("missing ApiKeyAuth security scheme")
|
|
}
|
|
|
|
// Spec should NOT contain /api/spec or /api/docs (self-referencing)
|
|
if _, ok := paths["/api/spec"]; ok {
|
|
t.Error("/api/spec should not appear in the spec")
|
|
}
|
|
if _, ok := paths["/api/docs"]; ok {
|
|
t.Error("/api/docs should not appear in the spec")
|
|
}
|
|
}
|
|
|
|
func TestSwaggerUIEndpoint(t *testing.T) {
|
|
_, r := setupTestServer(t)
|
|
|
|
req := httptest.NewRequest("GET", "/api/docs", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
ct := w.Header().Get("Content-Type")
|
|
if ct != "text/html; charset=utf-8" {
|
|
t.Errorf("unexpected content-type: %s", ct)
|
|
}
|
|
|
|
body := w.Body.String()
|
|
if len(body) < 100 {
|
|
t.Error("response too short for Swagger UI HTML")
|
|
}
|
|
if !strings.Contains(body, "swagger-ui") {
|
|
t.Error("response doesn't contain swagger-ui reference")
|
|
}
|
|
if !strings.Contains(body, "/api/spec") {
|
|
t.Error("response doesn't point to /api/spec")
|
|
}
|
|
}
|
|
|
|
func TestExtractPathParams(t *testing.T) {
|
|
tests := []struct {
|
|
path string
|
|
expect []string
|
|
}{
|
|
{"/api/nodes", nil},
|
|
{"/api/nodes/{pubkey}", []string{"pubkey"}},
|
|
{"/api/channels/{hash}/messages", []string{"hash"}},
|
|
}
|
|
for _, tt := range tests {
|
|
got := extractPathParams(tt.path)
|
|
if len(got) != len(tt.expect) {
|
|
t.Errorf("extractPathParams(%q) = %v, want %v", tt.path, got, tt.expect)
|
|
continue
|
|
}
|
|
for i := range got {
|
|
if got[i] != tt.expect[i] {
|
|
t.Errorf("extractPathParams(%q)[%d] = %q, want %q", tt.path, i, got[i], tt.expect[i])
|
|
}
|
|
}
|
|
}
|
|
}
|