47 lines
1.5 KiB
Go
47 lines
1.5 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOldFormatReadbackRecoveredWithoutRewritingSession(t *testing.T) {
|
|
for _, wrongID := range []bool{false, true} {
|
|
bin := filepath.Join(t.TempDir(), "opencode")
|
|
id := "ses_old"
|
|
if wrongID {
|
|
id = "ses_unrelated"
|
|
}
|
|
export := `{"info":{"id":"` + id + `"},"messages":[{"info":{"role":"assistant","error":{"name":"StructuredOutputError"}},"parts":[{"type":"text","text":"original DSML evidence"}]}]}`
|
|
if err := os.WriteFile(bin, []byte("#!/bin/sh\n[ \"$1\" = export ] || exit 8\n[ \"$2\" = ses_old ] || exit 9\necho '"+export+"'\n"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
t.Error("evidence reader changed stored session", r.Method)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(400)
|
|
w.Write([]byte(`{"name":"BadRequest","data":{"message":"Expected OutputFormatJsonSchema at info.format"}}`))
|
|
}))
|
|
c := &Connection{Backend: "v1", URL: srv.URL, Executable: bin}
|
|
data, err := c.Messages(context.Background(), "ses_old")
|
|
srv.Close()
|
|
if wrongID {
|
|
if err == nil {
|
|
t.Fatal("unrelated session export accepted")
|
|
}
|
|
continue
|
|
}
|
|
if err != nil || !json.Valid(data) || !strings.Contains(string(data), "original DSML evidence") || c.MessageReadbackDiagnostic == "" {
|
|
t.Fatal(err, string(data))
|
|
}
|
|
}
|
|
}
|