more oc fixes

This commit is contained in:
Luxferre
2026-09-18 12:08:01 +03:00
parent 0b2d9f2a90
commit f23c9132ce
3 changed files with 162 additions and 26 deletions
+102 -6
View File
@@ -1147,13 +1147,15 @@ func TestCol(t *testing.T) {
// ---------- llm / AL / summarize / compact via httptest (no real network) ----------
func TestLLMNonStreamingAndHeaders(t *testing.T) {
var gotPath, gotAuth, gotUA, gotOCClient, gotOCSession string
var gotPath, gotAuth, gotUA, gotLegacy, gotAffinity, gotOCClient, gotOCReq string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotAuth = r.Header.Get("Authorization")
gotUA = r.Header.Get("User-Agent")
gotLegacy = r.Header.Get("X-Session-Id")
gotAffinity = r.Header.Get("x-session-affinity")
gotOCClient = r.Header.Get("x-opencode-client")
gotOCSession = r.Header.Get("x-opencode-session")
gotOCReq = r.Header.Get("x-opencode-request")
w.Write([]byte(`{"choices":[{"message":{"role":"assistant","content":"hi","reasoning_content":"think"}}]}`))
}))
defer srv.Close()
@@ -1175,17 +1177,111 @@ func TestLLMNonStreamingAndHeaders(t *testing.T) {
if !strings.Contains(gotUA, "opencode/1.18.31") {
t.Errorf("user-agent = %q", gotUA)
}
if gotOCClient != "cli" {
t.Errorf("x-opencode-client = %q, want cli", gotOCClient)
// Non-OpenCode endpoints keep the legacy session-affinity headers and must
// not receive any x-opencode-* headers.
if gotLegacy != opencodeSessionID() {
t.Errorf("X-Session-Id = %q, want %q", gotLegacy, opencodeSessionID())
}
if gotOCSession != opencodeSessionID() {
t.Errorf("x-opencode-session = %q, want %q", gotOCSession, opencodeSessionID())
if gotAffinity != opencodeSessionID() {
t.Errorf("x-session-affinity = %q, want %q", gotAffinity, opencodeSessionID())
}
if gotOCClient != "" || gotOCReq != "" {
t.Errorf("unexpected x-opencode headers: client=%q request=%q", gotOCClient, gotOCReq)
}
if m.Content == nil || *m.Content != "hi" || m.ReasoningContent != "think" {
t.Errorf("message = %+v", m)
}
}
func TestIsOpencodeEndpoint(t *testing.T) {
yes := []string{
"https://opencode.ai/zen/v1",
"https://opencode.ai/zen/go/v1",
"https://opencode.ai/zen/v1/chat/completions",
"https://api.opencode.ai/v1",
"https://OPENCODE.AI/zen/v1",
"opencode.ai",
}
for _, e := range yes {
if !isOpencodeEndpoint(e) {
t.Errorf("isOpencodeEndpoint(%q) = false, want true", e)
}
}
no := []string{
"https://api.kilo.ai/api/openrouter",
"http://127.0.0.1:4321/v1",
"https://api.openai.com/v1",
"https://notopencode.ai.example.com/v1",
"https://example.com/opencode.ai",
}
for _, e := range no {
if isOpencodeEndpoint(e) {
t.Errorf("isOpencodeEndpoint(%q) = true, want false", e)
}
}
}
func TestOcRequestID(t *testing.T) {
id := ocRequestID()
if !strings.HasPrefix(id, "msg_") {
t.Fatalf("request id %q does not start with msg_", id)
}
if len(id) != 30 {
t.Fatalf("request id length = %d, want 30: %q", len(id), id)
}
for _, c := range id[4:] {
if !strings.ContainsRune(opencodeIDAlphabet, c) {
t.Fatalf("invalid character %c in request id %q", c, id)
}
}
if ocRequestID() == id {
t.Fatalf("consecutive request ids should differ: %q", id)
}
}
func TestApplyLLMHeadersOpencode(t *testing.T) {
cfg := defCfg
cfg.Endpoint = "https://opencode.ai/zen/v1"
cfg.APIKey = "-"
req, err := http.NewRequest("POST", "https://opencode.ai/zen/v1/chat/completions", nil)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
applyLLMHeaders(req, &cfg, "msg_test")
if got := req.Header.Get("User-Agent"); !strings.Contains(got, "opencode/1.18.31") || !strings.Contains(got, "runtime/bun") {
t.Errorf("user-agent = %q", got)
}
if got := req.Header.Get("x-opencode-client"); got != "cli" {
t.Errorf("x-opencode-client = %q, want cli", got)
}
if got := req.Header.Get("x-opencode-project"); got != projectID() {
t.Errorf("x-opencode-project = %q, want %q", got, projectID())
}
if got := req.Header.Get("x-opencode-session"); got != opencodeSessionID() {
t.Errorf("x-opencode-session = %q, want %q", got, opencodeSessionID())
}
if got := req.Header.Get("x-opencode-request"); got != "msg_test" {
t.Errorf("x-opencode-request = %q, want msg_test", got)
}
if got := req.Header.Get("X-Session-Id"); got != "" {
t.Errorf("legacy X-Session-Id should be absent, got %q", got)
}
if got := req.Header.Get("Authorization"); got != "" {
t.Errorf("Authorization should be absent for '-' key, got %q", got)
}
}
func TestApplyLLMHeadersOpencodeAuth(t *testing.T) {
cfg := defCfg
cfg.Endpoint = "https://opencode.ai/zen/v1"
cfg.APIKey = "public"
req, _ := http.NewRequest("POST", "https://opencode.ai/zen/v1/chat/completions", nil)
applyLLMHeaders(req, &cfg, "msg_test")
if got := req.Header.Get("Authorization"); got != "Bearer public" {
t.Errorf("Authorization = %q, want Bearer public", got)
}
}
func TestOpencodeSessionID(t *testing.T) {
sid := opencodeSessionID()
if !strings.HasPrefix(sid, "ses_") {