Use native prepared previews and correct diagnostic references

This commit is contained in:
2026-11-26 04:08:40 +00:00
parent fcd7cffd18
commit b790cc73ca
9 changed files with 81 additions and 107 deletions

View File

@@ -182,6 +182,20 @@ func TestSubprocessRunnerRunExitCodeFourReturnsValidationFailureAndPreservesOutp
assertFileNonEmpty(t, req.OutputPath)
}
func TestSubprocessRunnerRenderRejectsInvalidOrFailedPreview(t *testing.T) {
for _, mode := range []string{"render_invalid", "render_failure"} {
t.Run(mode, func(t *testing.T) {
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
t.Setenv("SCRIPTORIUM_HELPER_MODE", mode)
dir := t.TempDir()
req := RenderArtifactRequest{Binary: writeScriptoriumHelperWrapper(t), PromptID: "example", OutputPath: filepath.Join(dir, "preview.json"), StderrLogPath: filepath.Join(dir, "stderr.log"), Timeout: 2 * time.Second}
if _, err := NewSubprocessRunner().RenderArtifact(context.Background(), req); err == nil {
t.Fatal("expected preview error")
}
})
}
}
func TestSubprocessRunnerRunExitCodeTwoIsNotContentValidation(t *testing.T) {
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
t.Setenv("SCRIPTORIUM_HELPER_MODE", "fail_exit2_with_output")
@@ -224,8 +238,11 @@ func TestSubprocessRunnerRenderSuccess(t *testing.T) {
dir := t.TempDir()
req := RenderArtifactRequest{
Binary: wrapper,
ConfigPath: "/config.yml",
PromptID: "dnd.session_recap",
ProfileID: "local-quality",
InputPaths: map[string]string{"transcript": filepath.Join(dir, "polished.json")},
Vars: map[string]string{"session_id": "test,session=value"},
OutputPath: filepath.Join(dir, "artifacts", "session_recap.render.json"),
StdoutLogPath: filepath.Join(dir, "logs", "scriptorium.render.stdout.log"),
StderrLogPath: filepath.Join(dir, "logs", "scriptorium.render.stderr.log"),
@@ -245,15 +262,21 @@ func TestSubprocessRunnerRenderSuccess(t *testing.T) {
t.Fatalf("ExitCode = %d, want 0", res.ExitCode)
}
assertFileNonEmpty(t, req.OutputPath)
if res.StdoutLogPath != "" {
t.Fatalf("stdout log = %q", res.StdoutLogPath)
}
data, err := os.ReadFile(req.OutputPath)
if err != nil || string(data) != "{\"prepared\":true}\n" {
t.Fatalf("prepared output = %q, %v", data, err)
}
rec := readScriptoriumHelperRecord(t, recordPath)
wantArgs := []string{
"render",
"--prompt", "dnd.session_recap",
"--input", "transcript=" + req.InputPaths["transcript"],
"--timeout", "2s",
"promptkit", "show", "prepared", "dnd.session_recap",
"--config", "/config.yml", "--profile", "local-quality",
"--input-file", "transcript=" + req.InputPaths["transcript"],
"--var", "session_id=test,session=value",
"--format", "json",
"--out", req.OutputPath,
}
if strings.Join(rec.Args, "\n") != strings.Join(wantArgs, "\n") {
t.Fatalf("args = %#v, want %#v", rec.Args, wantArgs)
@@ -279,7 +302,6 @@ func TestScriptoriumSubprocessHelper(t *testing.T) {
}
cliArgs := args[start:]
outputPath := flagValue(cliArgs, "--out")
recordPath := os.Getenv("SCRIPTORIUM_HELPER_RECORD_PATH")
if strings.TrimSpace(recordPath) != "" {
rec := scriptoriumHelperRecord{
@@ -315,10 +337,15 @@ func TestScriptoriumSubprocessHelper(t *testing.T) {
_, _ = os.Stderr.WriteString("scriptorium helper validation failure\n")
os.Exit(4)
case "render_success":
writeScriptoriumHelperFile(outputPath, "{\"rendered\":true}\n")
_, _ = os.Stdout.WriteString("scriptorium helper render stdout\n")
_, _ = os.Stdout.WriteString("{\"prepared\":true}\n")
_, _ = os.Stderr.WriteString("scriptorium helper render stderr\n")
os.Exit(0)
case "render_invalid":
_, _ = os.Stdout.WriteString("{broken")
os.Exit(0)
case "render_failure":
_, _ = os.Stderr.WriteString("preview failed")
os.Exit(1)
default:
_, _ = os.Stderr.WriteString(fmt.Sprintf("unknown helper mode %q\n", mode))
os.Exit(2)
@@ -371,15 +398,6 @@ func mustParseScriptoriumDuration(t *testing.T, v string) time.Duration {
return d
}
func flagValue(args []string, name string) string {
for i := 0; i < len(args)-1; i++ {
if args[i] == name {
return args[i+1]
}
}
return ""
}
func writeScriptoriumFile(t *testing.T, path, contents string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
@@ -390,14 +408,6 @@ func writeScriptoriumFile(t *testing.T, path, contents string) {
}
}
func writeScriptoriumHelperFile(path, contents string) {
if strings.TrimSpace(path) == "" {
return
}
_ = os.MkdirAll(filepath.Dir(path), 0o755)
_ = os.WriteFile(path, []byte(contents), 0o644)
}
func assertFileNonEmpty(t *testing.T, path string) {
t.Helper()
info, err := os.Stat(path)