Wire config loading into CLI setup
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
||||
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
|
||||
@@ -61,22 +63,24 @@ func TestParseMappingsMalformed(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseRunArgsRequiredFlags(t *testing.T) {
|
||||
_, err := parseRunArgs([]string{"--profile-dir", "./profiles", "--prompt", "p", "--input", "a=b"})
|
||||
configPath := writeAppConfigFile(t, "")
|
||||
|
||||
_, err := parseRunArgs([]string{"--config", configPath, "--profile-dir", "./profiles", "--prompt", "p", "--input", "a=b"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing --prompt-dir error")
|
||||
}
|
||||
|
||||
_, err = parseRunArgs([]string{"--prompt-dir", "./prompts", "--prompt", "p", "--input", "a=b"})
|
||||
_, err = parseRunArgs([]string{"--config", configPath, "--prompt-dir", "./prompts", "--prompt", "p", "--input", "a=b"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing --profile-dir error")
|
||||
}
|
||||
|
||||
_, err = parseRunArgs([]string{"--prompt-dir", "./prompts", "--profile-dir", "./profiles", "--input", "a=b"})
|
||||
_, err = parseRunArgs([]string{"--config", configPath, "--prompt-dir", "./prompts", "--profile-dir", "./profiles", "--input", "a=b"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing --prompt error")
|
||||
}
|
||||
|
||||
_, err = parseRunArgs([]string{"--prompt-dir", "./prompts", "--profile-dir", "./profiles", "--prompt", "p"})
|
||||
_, err = parseRunArgs([]string{"--config", configPath, "--prompt-dir", "./prompts", "--profile-dir", "./profiles", "--prompt", "p"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing --input error")
|
||||
}
|
||||
@@ -140,17 +144,19 @@ func TestParseRunArgsRejectsRawLLMAPIKeyFlag(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseServeArgsRequiredFlags(t *testing.T) {
|
||||
_, err := parseServeArgs([]string{"--profile-dir", "./profiles"})
|
||||
configPath := writeAppConfigFile(t, "")
|
||||
|
||||
_, err := parseServeArgs([]string{"--config", configPath, "--profile-dir", "./profiles"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing --prompt-dir error")
|
||||
}
|
||||
|
||||
_, err = parseServeArgs([]string{"--prompt-dir", "./prompts"})
|
||||
_, err = parseServeArgs([]string{"--config", configPath, "--prompt-dir", "./prompts"})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing --profile-dir error")
|
||||
}
|
||||
|
||||
cfg, err := parseServeArgs([]string{"--prompt-dir", "./prompts", "--profile-dir", "./profiles"})
|
||||
cfg, err := parseServeArgs([]string{"--config", configPath, "--prompt-dir", "./prompts", "--profile-dir", "./profiles"})
|
||||
if err != nil {
|
||||
t.Fatalf("expected valid serve args, got %v", err)
|
||||
}
|
||||
@@ -266,6 +272,147 @@ func TestParseRenderArgsExplicitFormatsAndUnknown(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRunArgsWithExplicitConfigLoadsDirectories(t *testing.T) {
|
||||
configPath := writeAppConfigFile(t, `
|
||||
prompt_dir: ./from-config/prompts
|
||||
profile_dir: ./from-config/profiles
|
||||
schema_dir: ./from-config/schemas
|
||||
`)
|
||||
|
||||
cfg, err := parseRunArgs([]string{
|
||||
"--config", configPath,
|
||||
"--prompt", "p",
|
||||
"--input", "a=b",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("expected valid args, got %v", err)
|
||||
}
|
||||
|
||||
if cfg.promptDir != filepath.Clean("./from-config/prompts") {
|
||||
t.Fatalf("expected prompt dir from config, got %q", cfg.promptDir)
|
||||
}
|
||||
if cfg.profileDir != filepath.Clean("./from-config/profiles") {
|
||||
t.Fatalf("expected profile dir from config, got %q", cfg.profileDir)
|
||||
}
|
||||
if cfg.schemaDir != filepath.Clean("./from-config/schemas") {
|
||||
t.Fatalf("expected schema dir from config, got %q", cfg.schemaDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRunArgsMissingExplicitConfigReturnsError(t *testing.T) {
|
||||
_, err := parseRunArgs([]string{
|
||||
"--config", filepath.Join(t.TempDir(), "missing.yml"),
|
||||
"--prompt", "p",
|
||||
"--input", "a=b",
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected explicit config missing error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAppSettingsMissingImplicitConfigDoesNotError(t *testing.T) {
|
||||
fs := flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
settings, err := resolveAppSettings(fs, filepath.Join(t.TempDir(), "missing.yml"), appconfig.CLIOverrides{})
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if settings.SchemaDir != defaults.SchemaDirDefault {
|
||||
t.Fatalf("expected built-in schema dir, got %q", settings.SchemaDir)
|
||||
}
|
||||
if settings.ServerAddr != defaults.HTTPAddrDefault {
|
||||
t.Fatalf("expected built-in server addr, got %q", settings.ServerAddr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRunArgsCLIOverridesConfigDirectories(t *testing.T) {
|
||||
configPath := writeAppConfigFile(t, `
|
||||
prompt_dir: ./from-config/prompts
|
||||
profile_dir: ./from-config/profiles
|
||||
schema_dir: ./from-config/schemas
|
||||
`)
|
||||
|
||||
cfg, err := parseRunArgs([]string{
|
||||
"--config", configPath,
|
||||
"--prompt-dir", "./from-cli/prompts",
|
||||
"--profile-dir", "./from-cli/profiles",
|
||||
"--schema-dir", "./from-cli/schemas",
|
||||
"--prompt", "p",
|
||||
"--input", "a=b",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("expected valid args, got %v", err)
|
||||
}
|
||||
|
||||
if cfg.promptDir != filepath.Clean("./from-cli/prompts") {
|
||||
t.Fatalf("expected CLI prompt dir override, got %q", cfg.promptDir)
|
||||
}
|
||||
if cfg.profileDir != filepath.Clean("./from-cli/profiles") {
|
||||
t.Fatalf("expected CLI profile dir override, got %q", cfg.profileDir)
|
||||
}
|
||||
if cfg.schemaDir != filepath.Clean("./from-cli/schemas") {
|
||||
t.Fatalf("expected CLI schema dir override, got %q", cfg.schemaDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRenderArgsWithExplicitConfigLoadsDirectoriesAndFormat(t *testing.T) {
|
||||
configPath := writeAppConfigFile(t, `
|
||||
prompt_dir: ./from-config/prompts
|
||||
profile_dir: ./from-config/profiles
|
||||
defaults:
|
||||
render_format: json
|
||||
`)
|
||||
|
||||
cfg, err := parseRenderArgs([]string{
|
||||
"--config", configPath,
|
||||
"--prompt", "p",
|
||||
"--input", "a=b",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("expected valid args, got %v", err)
|
||||
}
|
||||
|
||||
if cfg.promptDir != filepath.Clean("./from-config/prompts") {
|
||||
t.Fatalf("expected prompt dir from config, got %q", cfg.promptDir)
|
||||
}
|
||||
if cfg.profileDir != filepath.Clean("./from-config/profiles") {
|
||||
t.Fatalf("expected profile dir from config, got %q", cfg.profileDir)
|
||||
}
|
||||
if cfg.outputFormat != renderformat.PreparedRunFormatJSON {
|
||||
t.Fatalf("expected render format from config, got %q", cfg.outputFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseServeArgsWithExplicitConfigLoadsSettingsAndCLIAddrOverrides(t *testing.T) {
|
||||
configPath := writeAppConfigFile(t, `
|
||||
prompt_dir: ./from-config/prompts
|
||||
profile_dir: ./from-config/profiles
|
||||
schema_dir: ./from-config/schemas
|
||||
server:
|
||||
addr: 127.0.0.1:9000
|
||||
`)
|
||||
|
||||
cfg, err := parseServeArgs([]string{
|
||||
"--config", configPath,
|
||||
"--addr", ":7777",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("expected valid args, got %v", err)
|
||||
}
|
||||
|
||||
if cfg.promptDir != filepath.Clean("./from-config/prompts") {
|
||||
t.Fatalf("expected prompt dir from config, got %q", cfg.promptDir)
|
||||
}
|
||||
if cfg.profileDir != filepath.Clean("./from-config/profiles") {
|
||||
t.Fatalf("expected profile dir from config, got %q", cfg.profileDir)
|
||||
}
|
||||
if cfg.schemaDir != filepath.Clean("./from-config/schemas") {
|
||||
t.Fatalf("expected schema dir from config, got %q", cfg.schemaDir)
|
||||
}
|
||||
if cfg.addr != ":7777" {
|
||||
t.Fatalf("expected CLI addr override, got %q", cfg.addr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetermineExitCode(t *testing.T) {
|
||||
if got := determineExitCode(errors.New("boom"), nil); got != ExitRuntimeError {
|
||||
t.Fatalf("expected runtime exit code, got %d", got)
|
||||
@@ -822,3 +969,12 @@ func newTestLLMServer(content string, hitCounter *int32) *httptest.Server {
|
||||
_, _ = w.Write([]byte(fmt.Sprintf(`{"choices":[{"message":{"role":"assistant","content":%q}}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`, content)))
|
||||
}))
|
||||
}
|
||||
|
||||
func writeAppConfigFile(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("failed to write app config fixture: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user