Use native Scriptorium generation and capture its output
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package scriptorium
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -36,8 +37,8 @@ func TestSubprocessRunnerRunSuccessBuildsDeterministicArgsAndCapturesLogs(t *tes
|
||||
ConfigPath: "/etc/scriptorium/config.yml",
|
||||
PromptID: "dnd.session_recap",
|
||||
ProfileID: "local-quality",
|
||||
InputPaths: map[string]string{"transcript": filepath.Join(dir, "polished.json"), "other": filepath.Join(dir, "other.md")},
|
||||
Vars: map[string]string{"session_id": "2026-05-03", "campaign_name": "Icewind Dale"},
|
||||
InputPaths: map[string]string{"transcript": filepath.Join(dir, "polished.json"), "other": filepath.Join(dir, "other,=notes.md")},
|
||||
Vars: map[string]string{"session_id": "2026-05-03", "campaign_name": "Icewind,Dale=North"},
|
||||
OutputPath: filepath.Join(dir, "artifacts", "session_recap.md"),
|
||||
StdoutLogPath: filepath.Join(dir, "logs", "scriptorium.run.stdout.log"),
|
||||
StderrLogPath: filepath.Join(dir, "logs", "scriptorium.run.stderr.log"),
|
||||
@@ -65,22 +66,30 @@ func TestSubprocessRunnerRunSuccessBuildsDeterministicArgsAndCapturesLogs(t *tes
|
||||
t.Fatalf("Duration = %s, want > 0", res.Duration)
|
||||
}
|
||||
|
||||
assertFileNonEmpty(t, req.OutputPath)
|
||||
assertFileContains(t, req.StdoutLogPath, "scriptorium helper stdout")
|
||||
data, err := os.ReadFile(req.OutputPath)
|
||||
if err != nil || !bytes.Equal(data, []byte{'a', 0, 255, '\n'}) {
|
||||
t.Fatalf("artifact = %v, %v", data, err)
|
||||
}
|
||||
if res.StdoutLogPath != "" {
|
||||
t.Fatalf("unexpected stdout log %q", res.StdoutLogPath)
|
||||
}
|
||||
if _, err := os.Stat(req.StdoutLogPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("unexpected stdout diagnostic: %v", err)
|
||||
}
|
||||
assertFileContains(t, req.StderrLogPath, "scriptorium helper stderr")
|
||||
|
||||
rec := readScriptoriumHelperRecord(t, recordPath)
|
||||
wantArgs := []string{
|
||||
"run",
|
||||
"--prompt", "dnd.session_recap",
|
||||
"dnd.session_recap",
|
||||
"--config", "/etc/scriptorium/config.yml",
|
||||
"--profile", "local-quality",
|
||||
"--input", "other=" + req.InputPaths["other"],
|
||||
"--input", "transcript=" + req.InputPaths["transcript"],
|
||||
"--var", "campaign_name=Icewind Dale",
|
||||
"--input-file", "other=" + req.InputPaths["other"],
|
||||
"--input-file", "transcript=" + req.InputPaths["transcript"],
|
||||
"--var", "campaign_name=Icewind,Dale=North",
|
||||
"--var", "session_id=2026-05-03",
|
||||
"--timeout", "2s",
|
||||
"--out", req.OutputPath,
|
||||
"--run-timeout", "2s",
|
||||
"--format", "text",
|
||||
}
|
||||
if strings.Join(rec.Args, "\n") != strings.Join(wantArgs, "\n") {
|
||||
t.Fatalf("args = %#v, want %#v", rec.Args, wantArgs)
|
||||
@@ -96,6 +105,20 @@ func TestSubprocessRunnerRunSuccessBuildsDeterministicArgsAndCapturesLogs(t *tes
|
||||
if strings.Contains(string(cfgData), "super-secret") {
|
||||
t.Fatalf("generated config must not include credential value")
|
||||
}
|
||||
if !strings.Contains(string(cfgData), "stdout_destination:") || !strings.Contains(string(cfgData), req.OutputPath) || !strings.Contains(string(cfgData), "--input-file") || !strings.Contains(string(cfgData), "OPENAI_KEY_SOURCE") {
|
||||
t.Fatalf("invocation record = %s", cfgData)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRejectsNonRegularOutput(t *testing.T) {
|
||||
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||||
req := runReqForTest(t, writeScriptoriumHelperWrapper(t))
|
||||
if err := os.MkdirAll(req.OutputPath, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := NewSubprocessRunner().RunArtifact(context.Background(), req); err == nil {
|
||||
t.Fatal("expected output open failure")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRunExitCodeOneFails(t *testing.T) {
|
||||
@@ -136,9 +159,9 @@ func TestSubprocessRunnerRunExitCodeOneFails(t *testing.T) {
|
||||
assertFileContains(t, req.StderrLogPath, "scriptorium helper failure")
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRunExitCodeTwoReturnsValidationFailureAndPreservesOutput(t *testing.T) {
|
||||
func TestSubprocessRunnerRunExitCodeFourReturnsValidationFailureAndPreservesOutput(t *testing.T) {
|
||||
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_MODE", "fail_exit2_with_output")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_MODE", "fail_exit4_with_output")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_RECORD_PATH", filepath.Join(t.TempDir(), "record.json"))
|
||||
|
||||
runner := NewSubprocessRunner()
|
||||
@@ -147,8 +170,8 @@ func TestSubprocessRunnerRunExitCodeTwoReturnsValidationFailureAndPreservesOutpu
|
||||
if err == nil {
|
||||
t.Fatal("RunArtifact() error = nil, want non-nil")
|
||||
}
|
||||
if res.ExitCode != 2 {
|
||||
t.Fatalf("ExitCode = %d, want 2", res.ExitCode)
|
||||
if res.ExitCode != 4 {
|
||||
t.Fatalf("ExitCode = %d, want 4", res.ExitCode)
|
||||
}
|
||||
if !res.ValidationFailed {
|
||||
t.Fatalf("ValidationFailed = false, want true")
|
||||
@@ -159,6 +182,18 @@ func TestSubprocessRunnerRunExitCodeTwoReturnsValidationFailureAndPreservesOutpu
|
||||
assertFileNonEmpty(t, req.OutputPath)
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRunExitCodeTwoIsNotContentValidation(t *testing.T) {
|
||||
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_MODE", "fail_exit2_with_output")
|
||||
runner := NewSubprocessRunner()
|
||||
req := runReqForTest(t, writeScriptoriumHelperWrapper(t))
|
||||
res, err := runner.RunArtifact(context.Background(), req)
|
||||
if err == nil || res.ExitCode != 2 || res.ValidationFailed {
|
||||
t.Fatalf("result = %+v, error = %v", res, err)
|
||||
}
|
||||
assertFileNonEmpty(t, req.OutputPath)
|
||||
}
|
||||
|
||||
func TestSubprocessRunnerRunMissingOutputOnSuccessFails(t *testing.T) {
|
||||
t.Setenv("GO_WANT_SCRIPTORIUM_HELPER", "1")
|
||||
t.Setenv("SCRIPTORIUM_HELPER_MODE", "run_success_missing_output")
|
||||
@@ -261,12 +296,10 @@ func TestScriptoriumSubprocessHelper(t *testing.T) {
|
||||
mode := os.Getenv("SCRIPTORIUM_HELPER_MODE")
|
||||
switch mode {
|
||||
case "run_success":
|
||||
writeScriptoriumHelperFile(outputPath, "generated artifact\n")
|
||||
_, _ = os.Stdout.WriteString("scriptorium helper stdout\n")
|
||||
_, _ = os.Stdout.Write([]byte{'a', 0, 255, '\n'})
|
||||
_, _ = os.Stderr.WriteString("scriptorium helper stderr\n")
|
||||
os.Exit(0)
|
||||
case "run_success_missing_output":
|
||||
_, _ = os.Stdout.WriteString("scriptorium helper stdout missing output\n")
|
||||
_, _ = os.Stderr.WriteString("scriptorium helper stderr missing output\n")
|
||||
os.Exit(0)
|
||||
case "fail_exit1":
|
||||
@@ -274,9 +307,13 @@ func TestScriptoriumSubprocessHelper(t *testing.T) {
|
||||
_, _ = os.Stderr.WriteString(os.Getenv("CUSTOM_PROVIDER_KEY"))
|
||||
os.Exit(1)
|
||||
case "fail_exit2_with_output":
|
||||
writeScriptoriumHelperFile(outputPath, "validation failed artifact\n")
|
||||
_, _ = os.Stderr.WriteString("scriptorium helper validation failure\n")
|
||||
_, _ = os.Stdout.WriteString("partial output\n")
|
||||
_, _ = os.Stderr.WriteString("scriptorium usage failure\n")
|
||||
os.Exit(2)
|
||||
case "fail_exit4_with_output":
|
||||
_, _ = os.Stdout.WriteString("validation failed artifact\n")
|
||||
_, _ = 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")
|
||||
|
||||
Reference in New Issue
Block a user