Files
Logging-System/examples/trace/trace_test.go
T
weichen 9c5442f06d Enable canonical versioned Go module consumption
Why: Allow consumers to download an immutable module revision without nested source checkouts or local path replacements.

Evidence: SELF package tests and the prior canonical-path rejection are recorded in the Dev dependency-workspace task card; remote consumption is replayed against the committed candidate.

Impact: Change the module identity and its Go imports while preserving API behavior. Existing historical submodule revisions remain buildable.

Refs: Versioned dependencies U1/U3; feature/dependency-workspace
2026-10-13 15:06:47 +00:00

54 lines
1.7 KiB
Go

package trace
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"git-ssh.u1680832.nyat.app/weichen/Error-Code-System.git/domains"
"git-ssh.u1680832.nyat.app/weichen/Error-Code-System.git/errcode"
"git-ssh.u1680832.nyat.app/weichen/Error-Code-System.git/httpx"
"git-ssh.u1680832.nyat.app/weichen/Logging-System.git/logsys"
"git-ssh.u1680832.nyat.app/weichen/Logging-System.git/sink"
)
func TestErrorCodeReachesLogAndHTTP(t *testing.T) {
reg := errcode.NewRegistry()
domains.RegisterAll(reg)
mem := &sink.Memory{}
log := logsys.New(logsys.Options{Service: "agent-squad", Level: logsys.LevelInfo, Sinks: []logsys.Sink{mem}})
cause := errors.New("dial tcp: connection refused")
err := errcode.Wrap(cause, errcode.AgentProviderUnavailable, "模型不可达", "model", "glm-4")
const traceID = "trace-loop-0001"
log.WithTraceID(traceID).Error("provider down", err)
got := mem.Snapshot()
if len(got) != 1 {
t.Fatalf("logs %d", len(got))
}
if got[0].TraceID != traceID || got[0].Code != string(errcode.AgentProviderUnavailable) {
t.Fatalf("entry %+v", got[0])
}
if got[0].Fields["model"] != "glm-4" {
t.Fatalf("fields %+v", got[0].Fields)
}
req := httptest.NewRequest(http.MethodGet, "/v1/runs", nil)
req.Header.Set(httpx.TraceHeader, traceID)
rec := httptest.NewRecorder()
httpx.WriteError(reg, rec, req, err)
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("status %d", rec.Code)
}
var env httpx.Envelope
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
t.Fatal(err)
}
if env.Error.Code != got[0].Code || env.Error.TraceID != got[0].TraceID {
t.Fatalf("envelope %+v log code %s trace %s", env.Error, got[0].Code, got[0].TraceID)
}
}