2026-07-18 15:46:19 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-22 05:24:30 +00:00
|
|
|
func TestRunInvalidSyntaxCreatesNoRunState(t *testing.T) {
|
|
|
|
|
for _, flags := range [][]string{
|
|
|
|
|
{"--unknown"},
|
|
|
|
|
{"--output-dir="},
|
|
|
|
|
{"--overlay"},
|
|
|
|
|
{"--llm-temperature=invalid"},
|
|
|
|
|
{"--input-file="},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(strings.Join(flags, " "), func(t *testing.T) {
|
|
|
|
|
fixture := newStateRunFixture(t)
|
|
|
|
|
result := fixture.run(t, flags...)
|
|
|
|
|
if result.status != 2 || result.stdout != "" || result.stderr == "" {
|
|
|
|
|
t.Fatalf("syntax error=%#v", result)
|
|
|
|
|
}
|
|
|
|
|
assertAbsent(t, fixture.outputRoot)
|
|
|
|
|
assertAbsent(t, fixture.cacheRoot)
|
|
|
|
|
assertAbsent(t, fixture.debugRoot)
|
|
|
|
|
if len(fixture.client.requestsSnapshot()) != 0 {
|
|
|
|
|
t.Fatal("syntax error reached provider")
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:24:30 +00:00
|
|
|
func TestRunStateRootsIgnoreRetiredEnvironmentAndHonorNativeFlags(t *testing.T) {
|
|
|
|
|
fixture := newStateRunFixture(t)
|
|
|
|
|
environmentOutput, environmentDebug := filepath.Join(t.TempDir(), "environment-output"), filepath.Join(t.TempDir(), "environment-debug")
|
|
|
|
|
fixture.options.LookupEnv = func(name string) (string, bool) {
|
|
|
|
|
switch name {
|
|
|
|
|
case "NOTARIUS_OUTPUT_DIR":
|
|
|
|
|
return environmentOutput, true
|
|
|
|
|
case "NOTARIUS_DEBUG_DIR":
|
|
|
|
|
return environmentDebug, true
|
|
|
|
|
}
|
|
|
|
|
return "", false
|
2026-07-22 02:35:44 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
first := fixture.run(t, "--debug", "--cache", "bypass")
|
|
|
|
|
if first.status != 0 {
|
|
|
|
|
t.Fatalf("configured roots=%#v", first)
|
2026-07-22 02:35:44 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
assertFile(t, filepath.Join(first.report.OutputDirectory, "lanes", "spells.json"))
|
|
|
|
|
assertFile(t, filepath.Join(first.report.DebugDirectory, "outcome.json"))
|
|
|
|
|
flagOutput, flagDebug := filepath.Join(t.TempDir(), "flag-output"), filepath.Join(t.TempDir(), "flag-debug")
|
|
|
|
|
second := fixture.run(t, "--debug", "--cache", "bypass", "--output-dir", flagOutput, "--debug-dir", flagDebug)
|
|
|
|
|
if second.status != 0 || !strings.HasPrefix(second.report.OutputDirectory, flagOutput+string(os.PathSeparator)) || !strings.HasPrefix(second.report.DebugDirectory, flagDebug+string(os.PathSeparator)) {
|
|
|
|
|
t.Fatalf("flag roots=%#v", second)
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
assertAbsent(t, environmentOutput)
|
|
|
|
|
assertAbsent(t, environmentDebug)
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:24:30 +00:00
|
|
|
func TestRunDebugDirectoryAloneDoesNotEnableCapture(t *testing.T) {
|
|
|
|
|
fixture := newStateRunFixture(t)
|
|
|
|
|
debug := filepath.Join(t.TempDir(), "explicit-debug")
|
|
|
|
|
result := fixture.run(t, "--cache", "bypass", "--debug-dir", debug)
|
|
|
|
|
if result.status != 0 || result.report.DebugDirectory != "" {
|
|
|
|
|
t.Fatalf("run=%#v", result)
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
assertAbsent(t, debug)
|
|
|
|
|
assertAbsent(t, fixture.debugRoot)
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:24:30 +00:00
|
|
|
func TestRunUsesDeclarationRelativeOverlayRootsAndCapturesSelection(t *testing.T) {
|
|
|
|
|
fixture := newStateRunFixture(t)
|
|
|
|
|
directory := filepath.Join(filepath.Dir(fixture.configPath), "overlays")
|
|
|
|
|
path := filepath.Join(directory, "development.yml")
|
|
|
|
|
writeConfigKitFile(t, path, "output: {directory: ./overlay-output}\ndebug: {directory: ./overlay-debug}\n")
|
|
|
|
|
result := fixture.run(t, "--debug", "--cache", "bypass", "--overlay", "development")
|
|
|
|
|
if result.status != 0 || filepath.Dir(result.report.OutputDirectory) != filepath.Join(directory, "overlay-output") || filepath.Dir(result.report.DebugDirectory) != filepath.Join(directory, "overlay-debug") {
|
|
|
|
|
t.Fatalf("overlay roots=%#v", result)
|
2026-09-13 17:12:56 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
content, err := os.ReadFile(filepath.Join(result.report.DebugDirectory, "configuration", "sources.json"))
|
|
|
|
|
if err != nil || !strings.Contains(string(content), path) {
|
|
|
|
|
t.Fatalf("captured overlay selection=%s %v", content, err)
|
2026-09-13 17:12:56 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
assertAbsent(t, fixture.outputRoot)
|
|
|
|
|
assertAbsent(t, fixture.debugRoot)
|
2026-09-13 17:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:24:30 +00:00
|
|
|
func TestRunCapturesDiscoveredConfigurationPath(t *testing.T) {
|
|
|
|
|
fixture := newStateRunFixture(t)
|
|
|
|
|
root := filepath.Dir(fixture.configPath)
|
|
|
|
|
selected := filepath.Join(root, "notarius.yml")
|
|
|
|
|
content, err := os.ReadFile(fixture.configPath)
|
2026-11-17 20:53:07 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
if err := os.WriteFile(selected, content, 0600); err != nil {
|
2026-11-17 20:53:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
t.Chdir(root)
|
2026-11-17 20:53:07 +00:00
|
|
|
var stdout, stderr bytes.Buffer
|
2026-11-22 05:24:30 +00:00
|
|
|
code := RunWithOptions([]string{"run", "dnd-session", "--input-file", fixture.inputPath, "--debug", "--cache", "bypass", "--format", "json"}, &stdout, &stderr, fixture.options)
|
2026-11-17 20:53:07 +00:00
|
|
|
if code != 0 {
|
2026-11-22 05:24:30 +00:00
|
|
|
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
2026-11-22 05:24:30 +00:00
|
|
|
sourceBytes, err := os.ReadFile(filepath.Join(onlyChildDir(t, fixture.debugRoot), "configuration", "sources.json"))
|
|
|
|
|
if err != nil || !strings.Contains(string(sourceBytes), selected) {
|
|
|
|
|
t.Fatalf("selected configuration=%s %v", sourceBytes, err)
|
2026-07-18 15:46:19 +00:00
|
|
|
}
|
|
|
|
|
}
|