106 lines
4.3 KiB
Go
106 lines
4.3 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNativeSessionModelPermissionsGenerationAndAbort(t *testing.T) {
|
|
var created, prompted, deleted, aborted bool
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.URL.Query().Get("directory") != "/repo" {
|
|
t.Error("workspace lost")
|
|
}
|
|
switch r.Method + " " + r.URL.Path {
|
|
case "POST /session":
|
|
var body struct {
|
|
Model map[string]string
|
|
Permission []map[string]string
|
|
}
|
|
json.NewDecoder(r.Body).Decode(&body)
|
|
if body.Model["id"] != "model" || body.Model["providerID"] != "provider" || body.Permission[0]["action"] != "deny" || body.Permission[1]["permission"] != "bash" {
|
|
t.Error("model or scoped permissions lost", body)
|
|
}
|
|
created = true
|
|
w.Write([]byte(`{"id":"ses_native"}`))
|
|
case "POST /session/ses_native/message":
|
|
var body struct {
|
|
Model map[string]string
|
|
Parts []map[string]string
|
|
Format map[string]any
|
|
}
|
|
json.NewDecoder(r.Body).Decode(&body)
|
|
if body.Model["modelID"] != "model" || body.Parts[0]["text"] != "exact prompt" {
|
|
t.Error("prompt or message model changed", body)
|
|
}
|
|
if body.Format["type"] != "json_schema" || body.Format["retryCount"] != float64(0) {
|
|
t.Error("structured-output format or no-replay retry count lost", body.Format)
|
|
}
|
|
prompted = true
|
|
w.Write([]byte(`{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}`))
|
|
case "GET /session/ses_native/message":
|
|
w.Write([]byte(`[{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}]`))
|
|
case "POST /session/ses_native/abort":
|
|
aborted = true
|
|
w.Write([]byte("true"))
|
|
case "DELETE /session/ses_native":
|
|
deleted = true
|
|
w.Write([]byte("true"))
|
|
default:
|
|
t.Error("unexpected native operation", r.Method, r.URL.Path)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
c := &Connection{Backend: "v1", URL: server.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}
|
|
ctx := context.Background()
|
|
_, err := c.Call(ctx, "post", "/api/session", map[string]any{"model": map[string]string{"providerID": "provider", "id": "model"}, "permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}, {"action": "shell", "resource": "go test ./...", "effect": "allow"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := c.Call(ctx, "post", "/api/session/ses_native/generate", map[string]any{"prompt": "exact prompt", "format": map[string]any{"type": "json_schema", "schema": map[string]any{"type": "object"}, "retryCount": 0}})
|
|
if err != nil || !strings.Contains(string(data), "result") {
|
|
t.Fatal(string(data), err)
|
|
}
|
|
if _, err = c.Call(ctx, "post", "/api/experimental/session/ses_native/wait", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = c.Call(ctx, "get", "/api/session/ses_native/message?order=desc", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = c.Call(ctx, "post", "/api/session/ses_native/interrupt", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = c.Call(ctx, "delete", "/api/session/ses_native", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !created || !prompted || !deleted || !aborted {
|
|
t.Fatal("native lifecycle incomplete")
|
|
}
|
|
}
|
|
|
|
func TestHTMLBridgeIsNotAnAPIAndErrorsAreRedacted(t *testing.T) {
|
|
for _, status := range []int{200, 500} {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.WriteHeader(status)
|
|
w.Write([]byte("secret-token HTML"))
|
|
}))
|
|
c := &Connection{URL: server.URL}
|
|
_, err := c.Native(context.Background(), "get", "/api/session", nil)
|
|
server.Close()
|
|
if err == nil || strings.Contains(err.Error(), "secret-token") {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, body := range []string{`{"info":{"role":"assistant","finish":"tool-calls","time":{"completed":1}},"parts":[]}`, `{"info":{"role":"assistant","finish":"stop"},"parts":[{"type":"text","text":"unfinished"}]}`, `{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"error":{"message":"bad"}},"parts":[{"type":"text","text":"failed"}]}`} {
|
|
if _, err := messageText([]byte(body)); err == nil {
|
|
t.Fatal("unfinished/error response accepted")
|
|
}
|
|
}
|
|
}
|