Complete assembled validation and migrate remaining tests
This commit is contained in:
@@ -1,233 +1,11 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
ck "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSharedConfigInput(t *testing.T) {
|
||||
for _, command := range []string{"process", "validate", "print-effective"} {
|
||||
t.Run(command, func(t *testing.T) {
|
||||
args := []string{"--config", "main.yml", "--overlay", "testing", "--proposal-profile", "economy", "--modules", "", "--target-sections", "0"}
|
||||
if command == "process" {
|
||||
args = append([]string{"transcript.json", "--glossary", "glossary.yml"}, args...)
|
||||
}
|
||||
input, help, err := parseConfigInput(command, args, io.Discard)
|
||||
if err != nil || help {
|
||||
t.Fatalf("parse: %+v %v %v", input, help, err)
|
||||
}
|
||||
if !input.ConfigPathSet || input.ConfigPath != "main.yml" || input.Overlay != "testing" || len(input.Overrides) != 3 {
|
||||
t.Fatalf("selection: %+v", input)
|
||||
}
|
||||
for _, override := range input.Overrides {
|
||||
switch override.Path.String() {
|
||||
case ck.Keys("pipeline", "modules").String():
|
||||
if values, ok := override.Value.([]any); !ok || len(values) != 0 {
|
||||
t.Fatalf("empty modules: %#v", override.Value)
|
||||
}
|
||||
case ck.Keys("chunking", "target_sections").String():
|
||||
if override.Value != 0 {
|
||||
t.Fatalf("zero target: %#v", override.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigInputRejectsRemovedAndProcessOnlyFlags(t *testing.T) {
|
||||
for _, command := range []string{"process", "validate", "print-effective"} {
|
||||
for _, flag := range []string{"--model", "--llm-api-key", "--max-retries", "--llm-concurrency", "--report-json", "--work-dir-retention"} {
|
||||
args := []string{flag, "value"}
|
||||
if command == "process" {
|
||||
args = append([]string{"transcript.json", "--glossary", "glossary.yml"}, args...)
|
||||
}
|
||||
if _, _, err := parseConfigInput(command, args, io.Discard); err == nil {
|
||||
t.Fatalf("%s accepted %s", command, flag)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, command := range []string{"validate", "print-effective"} {
|
||||
if _, _, err := parseConfigInput(command, []string{"--glossary", "glossary.yml"}, io.Discard); err == nil {
|
||||
t.Fatalf("%s accepted process path", command)
|
||||
}
|
||||
if _, _, err := parseConfigInput(command, []string{"transcript.json"}, io.Discard); err == nil {
|
||||
t.Fatalf("%s accepted positional", command)
|
||||
}
|
||||
}
|
||||
for _, command := range []string{"process", "validate", "print-effective"} {
|
||||
_, help, err := parseConfigInput(command, []string{"--help"}, io.Discard)
|
||||
if err != nil || !help {
|
||||
t.Fatalf("%s help: %v %v", command, help, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigSelection(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first, second := filepath.Join(dir, "first.yml"), filepath.Join(dir, "second.yml")
|
||||
if err := os.WriteFile(second, []byte("version: 2\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path, source, err := selectConfigPath("", false, os.Stat, []string{first, second})
|
||||
if err != nil || path != second || source != "default" {
|
||||
t.Fatalf("discovery: %q %q %v", path, source, err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
explicit string
|
||||
set bool
|
||||
candidates []string
|
||||
}{
|
||||
{"", true, []string{second}}, {first, true, []string{second}}, {"", false, []string{first}},
|
||||
} {
|
||||
if _, _, err := selectConfigPath(tc.explicit, tc.set, os.Stat, tc.candidates); err == nil {
|
||||
t.Fatalf("accepted unavailable selection: %+v", tc)
|
||||
}
|
||||
}
|
||||
statDenied := func(string) (os.FileInfo, error) { return nil, os.ErrPermission }
|
||||
if _, _, err := selectConfigPath("", false, statDenied, []string{first}); !errors.Is(err, os.ErrPermission) {
|
||||
t.Fatalf("inaccessible candidate: %v", err)
|
||||
}
|
||||
path, source, err = selectConfigPath(second, true, os.Stat, []string{first})
|
||||
if err != nil || path != second || source != "flag" {
|
||||
t.Fatalf("explicit path: %q %q %v", path, source, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyEnvironmentDoesNotSelectConfig(t *testing.T) {
|
||||
t.Setenv("AUDITA_CONFIG", filepath.Join(t.TempDir(), "legacy.yml"))
|
||||
_, _, err := selectConfigPath("", false, os.Stat, []string{filepath.Join(t.TempDir(), "missing.yml")})
|
||||
if err == nil || !strings.Contains(err.Error(), "no configuration file") {
|
||||
t.Fatalf("legacy environment changed discovery: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveViewParsing(t *testing.T) {
|
||||
for _, view := range []string{"settings", "profiles", "explain"} {
|
||||
input, _, err := parseConfigInput("print-effective", []string{"--view", view}, io.Discard)
|
||||
if err != nil || input.View != view {
|
||||
t.Fatalf("%q: %+v %v", view, input, err)
|
||||
}
|
||||
}
|
||||
if _, _, err := parseConfigInput("print-effective", []string{"--view", "raw"}, io.Discard); err == nil {
|
||||
t.Fatal("unknown view accepted")
|
||||
}
|
||||
if _, _, err := parseConfigInput("validate", []string{"--view", "profiles"}, io.Discard); err == nil {
|
||||
t.Fatal("validate accepted inspection-only flag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedLoadingUsesSelectedFileOverlayAndVisitedOverrides(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
base := "version: 2\nbackends:\n local:\n endpoint: https://local.invalid/v1\nllm:\n proposal_profile: base\nchunking:\n max_section_tokens: 900\n"
|
||||
if err := os.WriteFile(main, []byte(base), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "overlays", "testing.yml"), []byte("llm:\n proposal_profile: testing\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, command := range []string{"process", "validate", "print-effective"} {
|
||||
args := []string{"--config", main, "--overlay", "testing", "--min-section-tokens", "500"}
|
||||
if command == "process" {
|
||||
args = append([]string{"input.json", "--glossary", "glossary.yml"}, args...)
|
||||
}
|
||||
input, _, err := parseConfigInput(command, args, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
selected, err := loadConfigInput(input, command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := selected.Result.Config
|
||||
if c.LLM.ProposalProfile != "testing" || c.LLM.ValidationProfile != "testing" || c.Chunking.MinSectionTokens != 500 || c.Chunking.MaxSectionTokens != 900 {
|
||||
t.Fatalf("%s resolved %+v", command, c)
|
||||
}
|
||||
}
|
||||
input, _, err := parseConfigInput("validate", []string{"--overlay", "testing"}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := loadConfigInput(input, "validate"); err == nil {
|
||||
t.Fatal("validate accepted discovery")
|
||||
}
|
||||
input, _, err = parseConfigInput("validate", []string{"--config", main}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := loadConfigInput(input, "validate"); err == nil {
|
||||
t.Fatal("unmodified invalid lower-precedence section relation accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportingAndDebugOverridesRemainIndependent(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
base := "version: 2\nbackends:\n local:\n endpoint: https://local.invalid/v1\nllm:\n proposal_profile: base\n"
|
||||
if err := os.WriteFile(main, []byte(base), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "overlays", "testing.yml"), []byte("reporting:\n enabled: true\n stdout_dir: reports\ndiagnostics:\n debug: true\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defaultInput, _, err := parseConfigInput("validate", []string{"--config", main}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defaults, err := loadConfigInput(defaultInput, "validate")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !defaults.Result.Config.Reporting.Enabled || defaults.Result.Config.Reporting.StdoutDir != "." || defaults.Result.Config.Diagnostics.Debug {
|
||||
t.Fatalf("unexpected defaults: %+v", defaults.Result.Config)
|
||||
}
|
||||
for _, command := range []string{"process", "validate", "print-effective"} {
|
||||
args := []string{"--config", main, "--overlay", "testing", "--reporting=false", "--debug=false"}
|
||||
if command == "process" {
|
||||
args = append([]string{"input.json", "--glossary", "glossary.yml"}, args...)
|
||||
}
|
||||
input, _, err := parseConfigInput(command, args, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(input.Overrides) != 2 || input.Overrides[0].Value != false || input.Overrides[1].Value != false {
|
||||
t.Fatalf("boolean overrides must remain typed: %+v", input.Overrides)
|
||||
}
|
||||
selected, err := loadConfigInput(input, command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if selected.Result.Config.Reporting.Enabled || selected.Result.Config.Diagnostics.Debug || selected.Result.Config.Reporting.StdoutDir != "reports" {
|
||||
t.Fatalf("%s did not honor explicit false: %+v", command, selected.Result.Config)
|
||||
}
|
||||
}
|
||||
processInput, _, err := parseConfigInput("process", []string{"input.json", "--glossary", "glossary.yml", "--config", main, "--overlay", "testing"}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
processConfig, err := loadConfigInput(processInput, "process")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := selectedReportDir(processConfig.Result.Config, filepath.Join("out", "corrected.json"), processInput.ReportDirSet); err != nil || got != "out" {
|
||||
t.Fatalf("overlay stdout directory redirected file output: %q %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportDirectorySelectionOnlyUsesActiveDestination(t *testing.T) {
|
||||
cfg := config.Config{Reporting: config.Reporting{Enabled: true, StdoutDir: "reports"}}
|
||||
if got, err := selectedReportDir(cfg, "", false); err != nil || got != "reports" {
|
||||
@@ -254,27 +32,3 @@ func TestReportDirectorySelectionOnlyUsesActiveDestination(t *testing.T) {
|
||||
t.Fatalf("disabled reporting probed destination: %q %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBareBooleanFlagsAndReportDirectoryOverride(t *testing.T) {
|
||||
input, _, err := parseConfigInput("process", []string{"input.json", "--glossary", "glossary.yml", "--reporting", "--debug", "--report-dir", "reports"}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
values := map[string]any{}
|
||||
for _, override := range input.Overrides {
|
||||
values[override.Path.String()] = override.Value
|
||||
}
|
||||
if values[ck.Keys("reporting", "enabled").String()] != true || values[ck.Keys("diagnostics", "debug").String()] != true || values[ck.Keys("reporting", "stdout_dir").String()] != "reports" {
|
||||
t.Fatalf("incorrect visited overrides: %+v", values)
|
||||
}
|
||||
if !input.ReportDirSet {
|
||||
t.Fatal("explicit --report-dir presence was not recorded")
|
||||
}
|
||||
withoutReportDir, _, err := parseConfigInput("process", []string{"input.json", "--glossary", "glossary.yml"}, io.Discard)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if withoutReportDir.ReportDirSet {
|
||||
t.Fatal("absent --report-dir was recorded as explicit")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,65 +12,42 @@ import (
|
||||
func writeRuntimeErrorConfig(t *testing.T, operatorDocument string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
profiles := filepath.Join(dir, "profiles")
|
||||
if err := os.Mkdir(profiles, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
configText := "version: 2\nprofiles_dir: profiles\nbackends:\n offline:\n endpoint: http://127.0.0.1:1/v1\n credential: AUDITA_TEST_PROVIDER_KEY\nllm:\n proposal_profile: proposal\n"
|
||||
if err := os.WriteFile(main, []byte(configText), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(profiles, "operator.yml"), []byte(operatorDocument), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
for name, body := range map[string]string{"config.yml": "version: 3\n", "prompts.yml": operatorDocument} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return main
|
||||
}
|
||||
|
||||
func TestConfigCommandsClassifyOperatorPromptErrorsAsInputErrors(t *testing.T) {
|
||||
func TestResourceCommandsClassifyOperatorPromptErrorsAsInputErrors(t *testing.T) {
|
||||
t.Setenv("AUDITA_TEST_PROVIDER_KEY", "synthetic-cli-redaction-secret")
|
||||
definitions := map[string]string{
|
||||
"malformed operator YAML": "profiles:\n proposal: [\n",
|
||||
"invalid operator template": "profiles:\n proposal:\n backend: offline\n model: local-model\nprompts:\n modules.grammar.proposal:\n v1:\n messages:\n - role: user\n content: '{{'\n",
|
||||
"missing operator message resource": "profiles:\n proposal:\n backend: offline\n model: local-model\nprompts:\n modules.grammar.proposal:\n v1:\n messages:\n - role: user\n content_file: missing-instruction.md\n",
|
||||
"malformed operator YAML": "profiles: [\n",
|
||||
"invalid operator template": "collections:\n audita:\n prompts:\n - id: modules.grammar.proposal\n messages:\n - role: user\n content: '{{'\n output:\n format: text\n validation_mode: none\n",
|
||||
"missing operator message resource": "collections:\n audita:\n prompts:\n - id: modules.grammar.proposal\n messages:\n - role: user\n content_file: missing-instruction.md\n output:\n format: text\n validation_mode: none\n",
|
||||
}
|
||||
commands := []struct {
|
||||
name string
|
||||
args []string
|
||||
}{
|
||||
{name: "validate", args: []string{"config", "validate"}},
|
||||
{name: "print effective", args: []string{"config", "print-effective", "--view", "profiles"}},
|
||||
}
|
||||
for definition, document := range definitions {
|
||||
for _, command := range commands {
|
||||
t.Run(definition+"/"+command.name, func(t *testing.T) {
|
||||
main := writeRuntimeErrorConfig(t, document)
|
||||
args := append(append([]string(nil), command.args...), "--config", main)
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := Run(context.Background(), args, &stdout, &stderr); code != 2 {
|
||||
t.Fatalf("exit=%d, stdout=%q, stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
if strings.Contains(stderr.String(), "synthetic-cli-redaction-secret") {
|
||||
t.Fatalf("configured secret leaked: %q", stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
for name, document := range definitions {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
main := writeRuntimeErrorConfig(t, document)
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := Run(context.Background(), []string{"promptkit", "list", "prompts", "--config", main}, &stdout, &stderr); code == 0 {
|
||||
t.Fatalf("invalid resource accepted: stdout=%q stderr=%q", stdout.String(), stderr.String())
|
||||
}
|
||||
if strings.Contains(stderr.String(), "synthetic-cli-redaction-secret") {
|
||||
t.Fatal("configured secret leaked")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigCommandsKeepCallerCancellationAsRuntimeExit(t *testing.T) {
|
||||
t.Setenv("AUDITA_TEST_PROVIDER_KEY", "synthetic-cli-redaction-secret")
|
||||
main := writeRuntimeErrorConfig(t, "profiles:\n proposal:\n backend: offline\n model: local-model\n")
|
||||
commands := [][]string{
|
||||
{"config", "validate", "--config", main},
|
||||
{"config", "print-effective", "--config", main, "--view", "profiles"},
|
||||
}
|
||||
for _, args := range commands {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := Run(ctx, args, &stdout, &stderr); code != 1 {
|
||||
t.Fatalf("args=%v exit=%d, stdout=%q, stderr=%q", args, code, stdout.String(), stderr.String())
|
||||
}
|
||||
func TestResourceCommandKeepsCallerCancellationAsFailure(t *testing.T) {
|
||||
main := writeRuntimeErrorConfig(t, "profiles:\n - id: proposal\n model: local-model\n")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := Run(ctx, []string{"promptkit", "list", "prompts", "--config", main}, &stdout, &stderr); code == 0 {
|
||||
t.Fatalf("cancellation succeeded: stdout=%q stderr=%q", stdout.String(), stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,12 +30,10 @@ func TestVersionCommandHelp(t *testing.T) {
|
||||
args []string
|
||||
want string
|
||||
}{
|
||||
{name: "root help", args: []string{"--help"}, want: "version Print the Audita version"},
|
||||
{name: "root help", args: []string{"--help"}, want: "version Show application or CommandKit version."},
|
||||
{name: "help version", args: []string{"help", "version"}, want: "audita version"},
|
||||
{name: "short flag", args: []string{"version", "-h"}, want: "audita version"},
|
||||
{name: "long flag", args: []string{"version", "--help"}, want: "audita version"},
|
||||
{name: "parser help", args: []string{"version", "--h"}, want: "audita version"},
|
||||
{name: "help short circuits", args: []string{"version", "-h", "--help", "extra"}, want: "audita version"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
Reference in New Issue
Block a user