Preserve message resource origins and profile credential lookup names

This commit is contained in:
2026-10-03 01:21:48 +00:00
parent 8c68b28421
commit 62a4a20529
7 changed files with 118 additions and 39 deletions

View File

@@ -3,8 +3,11 @@ package promptkit_test
import (
"context"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"testing/fstest"
@@ -12,6 +15,60 @@ import (
"gitea.maximumdirect.net/eric/promptkit"
)
func TestProfileEnvironmentLookupAcceptsNonShellNames(t *testing.T) {
const name = "promptkit.test-api-key"
for _, test := range []struct {
name, value, direct, want string
unset, clear bool
}{
{name: "set", value: " synthetic-key ", want: "Bearer synthetic-key"},
{name: "empty"},
{name: "whitespace", value: " "},
{name: "unset", unset: true},
{name: "direct precedence", value: "synthetic-key", direct: "direct-key", want: "Bearer direct-key"},
{name: "explicit clear", value: "synthetic-key", clear: true},
} {
t.Run(test.name, func(t *testing.T) {
t.Setenv(name, test.value)
if test.unset {
if err := os.Unsetenv(name); err != nil {
t.Fatal(err)
}
}
cfg := simpleConfig("hello")
cfg.Sources.Underlays = []configkit.Underlay{{Inputs: []configkit.Input{*cfg.Sources.Main}}}
lowerName := name
cfg.Sources.Underlays = append(cfg.Sources.Underlays, configkit.Underlay{Inputs: []configkit.Input{{
Source: promptkit.ProfileSource("default credentials", map[string]promptkit.ProfileConfig{
"profile": {APIKeyEnv: &lowerName},
}),
}}})
env := " " + name + " "
if test.clear {
env = ""
}
cfg.Sources.Main = &configkit.Input{Source: promptkit.ProfileSource("credentials", map[string]promptkit.ProfileConfig{
"profile": {APIKeyEnv: &env},
})}
var calls int
var auth string
cfg.HTTPClient = &http.Client{Transport: roundTripFunc(func(request *http.Request) (*http.Response, error) {
calls++
auth = request.Header.Get("Authorization")
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"choices":[{"message":{"content":"ok"}}]}`))}, nil
})}
engine, err := promptkit.NewEngine(cfg)
if err != nil {
t.Fatal(err)
}
_, err = engine.Run(context.Background(), promptkit.RunRequest{PromptID: "prompt", APIKey: test.direct})
if err != nil || calls != 1 || auth != test.want {
t.Fatalf("run: err=%v calls=%d authorization=%q, want %q", err, calls, auth, test.want)
}
})
}
}
func TestPublicConstructionWithLocalBackendAndProgrammaticProfile(t *testing.T) {
backendID, model := "workstation", "offline-model"
prompt := configkit.Input{Source: configkit.Mapping("prompt", map[string]any{