Verify the complete native CLI cutover and record roadmap completion
This commit is contained in:
@@ -3,8 +3,20 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProcessIdentityAndNativeStatus(t *testing.T) {
|
||||
@@ -23,3 +35,79 @@ func TestProcessIdentityAndNativeStatus(t *testing.T) {
|
||||
t.Fatalf("canceled status=%d diagnostics=%q", status, diagnostics.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessSignalCancellation(t *testing.T) {
|
||||
if os.Getenv("SCRIPTORIUM_SIGNAL_CHILD") == "1" {
|
||||
separator := slices.Index(os.Args, "--")
|
||||
if separator < 0 {
|
||||
os.Exit(2)
|
||||
}
|
||||
os.Exit(runWithSignals(os.Args[separator+1:], os.Stdout, os.Stderr))
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("process interrupt/SIGTERM integration is Unix-specific")
|
||||
}
|
||||
for _, signal := range []os.Signal{os.Interrupt, syscall.SIGTERM} {
|
||||
t.Run(signal.String(), func(t *testing.T) {
|
||||
started, stopped, release := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
||||
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.Copy(io.Discard, r.Body)
|
||||
close(started)
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
close(stopped)
|
||||
case <-release:
|
||||
}
|
||||
}))
|
||||
defer provider.Close()
|
||||
defer close(release)
|
||||
root := t.TempDir()
|
||||
framework := filepath.Join(root, "framework.yml")
|
||||
content := fmt.Sprintf("profiles:\n - id: local\n endpoint: %q\n model: controlled\nprompts:\n - id: selected\n default_profile: local\n messages:\n - role: user\n content: Signal test\n output:\n format: text\n validation_mode: none\n", provider.URL+"/v1")
|
||||
if err := os.WriteFile(framework, []byte(content), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config := filepath.Join(root, "app.yml")
|
||||
if err := os.WriteFile(config, []byte(fmt.Sprintf("promptkit:\n configuration: %q\ntransient_retries: 0\n", framework)), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
command := exec.CommandContext(ctx, executable, "-test.run=^TestProcessSignalCancellation$", "--", "run", "selected", "--config="+config, "--timeout-seconds=0")
|
||||
command.Env = append(os.Environ(), "SCRIPTORIUM_SIGNAL_CHILD=1")
|
||||
var output, diagnostics bytes.Buffer
|
||||
command.Stdout, command.Stderr = &output, &diagnostics
|
||||
if err := command.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
cancel()
|
||||
if command.ProcessState == nil {
|
||||
_ = command.Wait()
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-started:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("child did not reach controlled provider")
|
||||
}
|
||||
if err := command.Process.Signal(signal); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = command.Wait()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 1 || output.Len() != 0 || diagnostics.Len() == 0 {
|
||||
t.Fatalf("signal outcome: %v stdout=%q stderr=%q", err, output.String(), diagnostics.String())
|
||||
}
|
||||
select {
|
||||
case <-stopped:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("signal did not cancel provider request")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user