68 lines
3.4 KiB
Go
68 lines
3.4 KiB
Go
|
|
package scriptorium
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestInspectRequirementsForwardsContextAndDecodesPartialReport(t *testing.T) {
|
||
|
|
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||
|
|
t.Setenv("SCRIPTORIUM_HELPER_MODE", "requirements")
|
||
|
|
t.Setenv("CUSTOM_PROVIDER_KEY", "synthetic-secret")
|
||
|
|
record := filepath.Join(t.TempDir(), "args.json")
|
||
|
|
t.Setenv("SCRIPTORIUM_HELPER_RECORD_PATH", record)
|
||
|
|
loaded, err := configkit.Load[struct {
|
||
|
|
Credentials []configkit.Secret `yaml:"credentials"`
|
||
|
|
}](configkit.Bytes("test.yml", []byte("credentials: [CUSTOM_PROVIDER_KEY]\n")))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
req := RequirementsRequest{Binary: writeScriptoriumHelperWrapper(t), ConfigPath: "/native.yml", PromptID: "qualified.prompt", ProfileID: "qualified.profile", Vars: map[string]string{"note": "", "mode": "full,more=yes"}, Credentials: loaded.Config.Credentials, WorkingDir: t.TempDir(), Timeout: 2 * time.Second}
|
||
|
|
report, err := NewSubprocessRunner().InspectRequirements(context.Background(), req)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if report.PromptID != req.PromptID || report.ProfileID != req.ProfileID || report.Complete || len(report.Inputs) != 1 || report.Inputs[0].ContentType != nil || report.Inputs[0].Description == nil || *report.Inputs[0].Description != "" || len(report.Variables) != 1 || !report.Variables[0].Supplied || len(report.Issues) != 1 {
|
||
|
|
t.Fatalf("report = %+v", report)
|
||
|
|
}
|
||
|
|
rec := readScriptoriumHelperRecord(t, record)
|
||
|
|
want := []string{"promptkit", "requirements", "show", "qualified.prompt", "--config", "/native.yml", "--profile", "qualified.profile", "--var", "mode=full,more=yes", "--var", "note=", "--format", "json"}
|
||
|
|
if !reflect.DeepEqual(rec.Args, want) || rec.Env["CUSTOM_PROVIDER_KEY"] != "synthetic-secret" {
|
||
|
|
t.Fatalf("invocation = %+v", rec)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestInspectRequirementsRejectsFailedProcessAndRedactsStderr(t *testing.T) {
|
||
|
|
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||
|
|
t.Setenv("SCRIPTORIUM_HELPER_MODE", "requirements_failed")
|
||
|
|
t.Setenv("CUSTOM_PROVIDER_KEY", "synthetic-secret")
|
||
|
|
loaded, err := configkit.Load[struct {
|
||
|
|
Credentials []configkit.Secret `yaml:"credentials"`
|
||
|
|
}](configkit.Bytes("test.yml", []byte("credentials: [CUSTOM_PROVIDER_KEY]\n")))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
_, err = NewSubprocessRunner().InspectRequirements(context.Background(), RequirementsRequest{Binary: writeScriptoriumHelperWrapper(t), PromptID: "example", Credentials: loaded.Config.Credentials, Timeout: 2 * time.Second})
|
||
|
|
if err == nil || strings.Contains(err.Error(), "synthetic-secret") || !strings.Contains(err.Error(), "<redacted>") {
|
||
|
|
t.Fatalf("error = %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDecodeRequirementsRequiresOneCompleteReportShape(t *testing.T) {
|
||
|
|
valid := `{"prompt_id":"x","complete":true,"inputs":[],"variables":[],"issues":[],"future_field":true}`
|
||
|
|
if report, err := decodeRequirements([]byte(valid)); err != nil || !report.Complete || report.Inputs == nil {
|
||
|
|
t.Fatalf("report = %+v, error = %v", report, err)
|
||
|
|
}
|
||
|
|
for _, data := range []string{"", "null", "[]", `{}`, `{"prompt_id":null,"complete":true,"inputs":[],"variables":[],"issues":[]}`, `{"prompt_id":"x","complete":null,"inputs":[],"variables":[],"issues":[]}`, `{"prompt_id":"x","complete":true,"inputs":null,"variables":[],"issues":[]}`, valid + ` {}`, valid[:len(valid)-1]} {
|
||
|
|
if _, err := decodeRequirements([]byte(data)); err == nil {
|
||
|
|
t.Errorf("accepted %q", data)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|