package cli import ( "bytes" "context" "fmt" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync/atomic" "testing" commandkit "go.maximumdirect.net/commandkit" ) func TestProcessProtectsRuntimeGlossarySources(t *testing.T) { for _, mode := range []string{"transcript", "report", "separate"} { t.Run(mode, func(t *testing.T) { dir := t.TempDir() var calls atomic.Int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls.Add(1) fmt.Fprint(w, `{"choices":[{"message":{"content":"{\"corrections\":[]}"}}]}`) })) defer server.Close() imported := "imported.yml" if mode == "report" { imported = "errors.yml" } files := map[string]string{ "config.yml": "version: 3\nllm: {proposal_profile: fixture}\npipeline: {stages: [{module: grammar}]}\n", "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, "runtime.yml": fmt.Sprintf("config:\n imports: [{path: %s, required: true}]\nparty: {entries: []}\n", imported), imported: "reference: {entries: []}\n", "prompts.yml": fmt.Sprintf("profiles:\n - id: fixture\n endpoint: %s/v1\n model: test\n api_key_required: false\n", server.URL), } for name, data := range files { if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { t.Fatal(err) } } output := filepath.Join(dir, "output.json") if mode == "transcript" { output = filepath.Join(dir, "runtime.yml") } app, err := buildApplication() if err != nil { t.Fatal(err) } var out, stderr bytes.Buffer result := app.Execute(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossary-file", filepath.Join(dir, "runtime.yml"), "--output", output, filepath.Join(dir, "input.json")}, commandkit.Streams{Out: &out, Err: &stderr}) if mode == "separate" { if result.Status != 0 || calls.Load() != 1 { t.Fatalf("status=%d calls=%d error=%s", result.Status, calls.Load(), stderr.String()) } } else if result.Status == 0 || calls.Load() != 0 || !strings.Contains(stderr.String(), "conflicts with input") { t.Fatalf("status=%d calls=%d error=%s", result.Status, calls.Load(), stderr.String()) } for _, name := range []string{"runtime.yml", imported} { raw, err := os.ReadFile(filepath.Join(dir, name)) if err != nil || string(raw) != files[name] { t.Fatalf("source %s changed: %s %v", name, raw, err) } } }) } } func TestProcessRuntimeGlossaryFailures(t *testing.T) { dir := t.TempDir() files := map[string]string{"config.yml": "version: 3\nglossaries: [party]\npipeline: {stages: [{module: glossary}]}\n", "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, "empty.yml": "party: {entries: []}\n", "bad.yml": "party: [\n"} for name, data := range files { if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { t.Fatal(err) } } for _, tc := range []struct { name string status int diagnostic string }{ {"missing.yml", 1, "source.missing"}, {"bad.yml", 2, "yaml.invalid"}, {"empty.yml", 2, "no glossary entries"}, } { var stdout, stderr bytes.Buffer output := filepath.Join(dir, "output.json") status := Run(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossary-file", filepath.Join(dir, tc.name), "--output", output, filepath.Join(dir, "input.json")}, &stdout, &stderr) if status != tc.status || !strings.Contains(stderr.String(), tc.diagnostic) { t.Fatalf("%s: status=%d stderr=%s", tc.name, status, stderr.String()) } for _, name := range []string{"output.json", "errors.yml", "recoveries.yml", "report.md"} { if _, err := os.Stat(filepath.Join(dir, name)); !os.IsNotExist(err) { t.Fatalf("%s: output %s prepared: %v", tc.name, name, err) } } } } func TestNativeCommandsUseRuntimeGlossary(t *testing.T) { dir := t.TempDir() for name, data := range map[string]string{ "left.yml": "version: 3\ncontext: {description: left}\nglossary_collections: {party: {entries: [{id: a, name: Left, category: C}]}}\n", "right.yml": "version: 3\ncontext: {description: right}\nglossary_collections: {party: {entries: [{id: b, name: Right, category: C}]}}\n", "input.yml": "party: {entries: [{id: c, name: Runtime, category: C}]}\n", "bad.yml": "party: [\n", } { if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { t.Fatal(err) } } app, err := buildApplication() if err != nil { t.Fatal(err) } run := func(args ...string) (int, string, string) { t.Helper() var out, stderr bytes.Buffer result := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr}) return result.Status, out.String(), stderr.String() } main := filepath.Join(dir, "left.yml") file := filepath.Join(dir, "input.yml") code, out, stderr := run("config", "show", "--scope=app", "--config", main, "--glossary-file", file) if code != 0 || !strings.Contains(out, "Runtime") || strings.Contains(out, "name: Left") { t.Fatalf("show %d %s %s", code, out, stderr) } code, out, stderr = run("config", "explain", "--scope=app", "--config", main, "--glossary-file", file, "--path=/glossary_collections/party/entries") if code != 0 || !strings.Contains(out, "--glossary-file") { t.Fatalf("explain %d %s %s", code, out, stderr) } code, out, stderr = run("config", "diff", "--details", "--scope=app", "--config", main, "--against-config", filepath.Join(dir, "right.yml"), "--glossary-file", file) if code != 0 || !strings.Contains(out, "left") || !strings.Contains(out, "right") || strings.Contains(out, "glossary_collections") { t.Fatalf("diff %d %s %s", code, out, stderr) } for _, args := range [][]string{{"config", "show", "--scope=promptkit"}, {"promptkit", "list", "prompts"}} { valid := append(append([]string(nil), args...), "--config", main, "--glossary-file", file) code, _, stderr = run(valid...) if code != 0 { t.Fatalf("%v: %d %s", valid, code, stderr) } args = append(args, "--config", main, "--glossary-file", filepath.Join(dir, "bad.yml")) code, _, stderr = run(args...) if code == 0 || !strings.Contains(stderr, "bad.yml") { t.Fatalf("%v: %d %s", args, code, stderr) } } for _, args := range [][]string{{"--help"}, {"version"}, {"config", "scopes"}, {"promptkit", "--help"}} { code, _, stderr = run(args...) if code != 0 { t.Fatalf("%v: %d %s", args, code, stderr) } } } func TestRuntimeGlossaryUsesInvocationDirectory(t *testing.T) { root := t.TempDir() project := filepath.Join(root, "project") invocation := filepath.Join(root, "invocation") for _, dir := range []string{project, invocation, filepath.Join(project, "overlays"), filepath.Join(project, "secrets")} { if err := os.MkdirAll(dir, 0700); err != nil { t.Fatal(err) } } t.Setenv("AUDITA_GLOSSARY_CONTEXT_TEST", "") files := map[string]string{ filepath.Join(project, "config.yml"): "version: 3\nconfig: {secrets_dir: secrets, secrets_overwrite: true}\ncontext: {description: main}\n", filepath.Join(project, "secrets", "AUDITA_GLOSSARY_CONTEXT_TEST"): "loaded", filepath.Join(project, "glossary.yml"): "reference: {entries: [{id: r, name: Companion, category: C}]}\n", filepath.Join(project, "overlays", "chosen.yml"): "context: {description: overlay}\n", filepath.Join(invocation, "input.yml"): "config: {imports: [{path: imported.yml}]}\nparty: {entries: [{id: a, name: Incoming, category: C}]}\n", filepath.Join(invocation, "imported.yml"): "lore: {entries: [{id: b, name: Imported, category: C}]}\n", } for path, data := range files { if err := os.WriteFile(path, []byte(data), 0600); err != nil { t.Fatal(err) } } t.Chdir(invocation) var out, stderr bytes.Buffer code := Run(context.Background(), []string{"config", "show", "--scope=app", "--config", filepath.Join(project, "config.yml"), "--overlay", "chosen", "--glossary-file", "input.yml"}, &out, &stderr) if code != 0 || !strings.Contains(out.String(), "Incoming") || !strings.Contains(out.String(), "Imported") || !strings.Contains(out.String(), "Companion") || !strings.Contains(out.String(), "overlay") || os.Getenv("AUDITA_GLOSSARY_CONTEXT_TEST") != "loaded" { t.Fatalf("status=%d out=%s err=%s", code, out.String(), stderr.String()) } }