48 lines
2.0 KiB
Go
48 lines
2.0 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNativeStructuredResultIsPreferredOverPresentationText(t *testing.T) {
|
|
data := []byte(`{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"structured":{"outcome":"success","output":{"approval":true}}},"parts":[{"type":"text","text":"review complete"}]}`)
|
|
text, err := messageText(data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var result map[string]any
|
|
if err := json.Unmarshal([]byte(text), &result); err != nil || result["outcome"] != "success" {
|
|
t.Fatal("structured result ignored", text, err)
|
|
}
|
|
if _, err := messageText([]byte(strings.Replace(string(data), `"finish":"stop"`, `"finish":"tool-calls"`, 1))); err != nil {
|
|
t.Fatal("completed schema tool result rejected", err)
|
|
}
|
|
}
|
|
|
|
func TestStructuredAgentResponseKeepsOriginalToolEvidence(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`[{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"structured":{"outcome":"success","output":{"approval":true}}},"parts":[{"type":"tool","tool":"bash","state":{"status":"completed","input":{"command":"bash -n install.sh"},"output":"passed"}},{"type":"text","text":"review complete"}]}]`))
|
|
}))
|
|
defer srv.Close()
|
|
c := &Connection{Backend: "v1", URL: srv.URL, Directory: "/repo"}
|
|
data, err := c.Call(context.Background(), "get", "/api/session/ses_test/message?order=desc", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var result struct {
|
|
Data []struct {
|
|
Structured map[string]any
|
|
Content []map[string]any
|
|
}
|
|
}
|
|
if err := json.Unmarshal(data, &result); err != nil || len(result.Data) != 1 || result.Data[0].Structured["outcome"] != "success" || len(result.Data[0].Content) != 2 || result.Data[0].Content[0]["type"] != "tool" {
|
|
t.Fatal("structured output erased original tool evidence", string(data), err)
|
|
}
|
|
}
|