package main

import (
	"io"
	"os"
	"strings"
	"testing"
)

func TestRunSuccess(t *testing.T) {
	orig := os.Args
	t.Cleanup(func() { os.Args = orig })
	os.Args = []string{"orca", "version"}
	if code := run(); code != 0 {
		t.Errorf("run() = %d, want 0", code)
	}
}

func TestRunError(t *testing.T) {
	origArgs := os.Args
	t.Cleanup(func() { os.Args = origArgs })
	os.Args = []string{"orca", "job", "run", "/nonexistent/spec.hcl"}

	r, w, err := os.Pipe()
	if err != nil {
		t.Fatalf("pipe: %v", err)
	}
	origStderr := os.Stderr
	os.Stderr = w
	t.Cleanup(func() { os.Stderr = origStderr })

	code := run()
	w.Close()
	out, _ := io.ReadAll(r)
	if code != 1 {
		t.Errorf("run() = %d, want 1", code)
	}
	if !strings.Contains(string(out), "error:") {
		t.Errorf("stderr missing 'error:' prefix: %s", out)
	}
}
