87 lines
3.0 KiB
Go
87 lines
3.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func TestInvocationCaptureOmitsMarkedRuntimeParameters(t *testing.T) {
|
|
for _, command := range []string{"run", "preflight"} {
|
|
for _, earlyFailure := range []bool{false, true} {
|
|
name := command + "/success"
|
|
if earlyFailure {
|
|
name = command + "/early-failure"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
probe := &runProbe{}
|
|
_, configPath, inputPath, _ := runFixture(t, probe, func(configuration string) string {
|
|
return strings.Replace(configuration, " prompts:", " extra_params: {token: configured-placeholder}\n prompts:", 1)
|
|
})
|
|
if earlyFailure {
|
|
inputPath += "-missing"
|
|
}
|
|
calls := 0
|
|
client := captureClient(func(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
calls++
|
|
if request.Target.ExtraParams["token"] != "runtime-private-placeholder" || request.Target.ExtraParams["caption"] != "ordinary-runtime-caption" {
|
|
t.Fatal("capture mutated execution parameters")
|
|
}
|
|
if productionSchemaHasProperty(request, "status") {
|
|
return &promptkit.GenerateResponse{Content: `{"status":"ok"}`}, nil
|
|
}
|
|
return probe.Generate(ctx, request)
|
|
})
|
|
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client, RunIDGenerator: func(time.Time) (string, error) { return "capture", nil }})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
root := t.TempDir()
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), []string{command, "demo", "--config", configPath, "--input-file", inputPath, "--llm-profile", "chosen", "--llm-extra-params", `{"token":"runtime-private-placeholder","caption":"ordinary-runtime-caption"}`, "--cache", "bypass", "--debug", "--debug-dir", root, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if (outcome.Status != 0) != earlyFailure || earlyFailure && calls != 0 {
|
|
t.Fatalf("outcome=%+v calls=%d stderr=%s", outcome, calls, stderr.String())
|
|
}
|
|
if !earlyFailure {
|
|
var report map[string]any
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil || calls == 0 {
|
|
t.Fatalf("report error=%v calls=%d", err, calls)
|
|
}
|
|
}
|
|
ordinaryRetained := false
|
|
err = filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !entry.Type().IsRegular() {
|
|
return nil
|
|
}
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if bytes.Contains(content, []byte("runtime-private-placeholder")) {
|
|
t.Errorf("marked runtime parameter retained in %s", path)
|
|
}
|
|
ordinaryRetained = ordinaryRetained || bytes.Contains(content, []byte("ordinary-runtime-caption"))
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !earlyFailure && !ordinaryRetained {
|
|
t.Fatal("ordinary effective parameters were lost")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|