Skip to content

qtsone/learning / skills/tutor

tutor

Flagged

Turn this agent into a personal programming tutor with a versioned 0-to-expert curriculum (Go available; more languages coming). Use when the user wants to learn a programming language, start or resume lessons, get their exercise code graded, check learning progress, or contribute curriculum fixes — e.g. "/tutor go", "/tutor resume", "teach me Go", "continue my lessons", "grade my exercise".

FileKindSize
SKILL.mdskill md9.2 KB
curriculum/content/focus/cli-tooling/capstone/LESSON.mdmarkdown17.1 KB
curriculum/content/focus/cli-tooling/capstone/TUTOR.mdmarkdown5.4 KB
curriculum/content/focus/cli-tooling/capstone/exercise/app.goscript5.8 KB
curriculum/content/focus/cli-tooling/capstone/exercise/authors.goscript1.6 KB
curriculum/content/focus/cli-tooling/capstone/exercise/authors_test.goscript7.3 KB
curriculum/content/focus/cli-tooling/capstone/exercise/cli_test.goscript11.5 KB
curriculum/content/focus/cli-tooling/capstone/exercise/config_test.goscript11.5 KB
curriculum/content/focus/cli-tooling/capstone/exercise/go.modmanifest187 B
curriculum/content/focus/cli-tooling/capstone/exercise/go.summanifest900 B
curriculum/content/focus/cli-tooling/capstone/exercise/main.goscript2.0 KB
curriculum/content/focus/cli-tooling/capstone/exercise/render.goscript1.9 KB
curriculum/content/focus/cli-tooling/capstone/exercise/root.goscript2.4 KB
curriculum/content/focus/cli-tooling/capstone/exercise/scan.goscript2.5 KB
curriculum/content/focus/cli-tooling/capstone/exercise/scan_test.goscript4.0 KB
curriculum/content/focus/cli-tooling/capstone/exercise/signal_unix_test.goscript1.4 KB
curriculum/content/focus/cli-tooling/capstone/exercise/version.goscript942 B
curriculum/content/focus/cli-tooling/capstone/quiz.jsonconfig5.0 KB
curriculum/content/focus/cli-tooling/capstone/solution/app.goscript9.9 KB
curriculum/content/focus/cli-tooling/capstone/solution/authors.goscript2.9 KB
curriculum/content/focus/cli-tooling/capstone/solution/render.goscript2.6 KB
curriculum/content/focus/cli-tooling/capstone/solution/root.goscript4.7 KB
curriculum/content/focus/cli-tooling/capstone/solution/scan.goscript3.7 KB
curriculum/content/focus/cli-tooling/cobra/LESSON.mdmarkdown12.8 KB
curriculum/content/focus/cli-tooling/cobra/TUTOR.mdmarkdown5.9 KB
curriculum/content/focus/cli-tooling/cobra/exercise/app.goscript1.7 KB
curriculum/content/focus/cli-tooling/cobra/exercise/go.modmanifest193 B
curriculum/content/focus/cli-tooling/cobra/exercise/go.summanifest900 B
curriculum/content/focus/cli-tooling/cobra/exercise/main.goscript422 B
curriculum/content/focus/cli-tooling/cobra/exercise/notes_test.goscript9.8 KB
curriculum/content/focus/cli-tooling/cobra/exercise/root.goscript919 B
curriculum/content/focus/cli-tooling/cobra/exercise/store.goscript1.7 KB
curriculum/content/focus/cli-tooling/cobra/quiz.jsonconfig5.1 KB
curriculum/content/focus/cli-tooling/cobra/solution/app.goscript2.4 KB
curriculum/content/focus/cli-tooling/cobra/solution/root.goscript4.2 KB
curriculum/content/focus/cli-tooling/distribution/LESSON.mdmarkdown17.2 KB
curriculum/content/focus/cli-tooling/distribution/TUTOR.mdmarkdown7.0 KB
curriculum/content/focus/cli-tooling/distribution/exercise/check.shexecutablescript12.0 KB
curriculum/content/focus/cli-tooling/distribution/exercise/go.modmanifest35 B
curriculum/content/focus/cli-tooling/distribution/exercise/internal/buildinfo/buildinfo.goscript1.4 KB
curriculum/content/focus/cli-tooling/distribution/exercise/main.goscript1.2 KB
curriculum/content/focus/cli-tooling/distribution/quiz.jsonconfig5.0 KB
curriculum/content/focus/cli-tooling/distribution/solution/.goreleaser.yamlconfig2.1 KB
curriculum/content/focus/cli-tooling/distribution/solution/internal/buildinfo/buildinfo.goscript1.3 KB
curriculum/content/focus/cli-tooling/files-processes/LESSON.mdmarkdown18.8 KB
curriculum/content/focus/cli-tooling/files-processes/TUTOR.mdmarkdown7.8 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/atomicfile.goscript772 B
curriculum/content/focus/cli-tooling/files-processes/exercise/atomicfile_test.goscript2.6 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/exec.goscript3.8 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/exec_test.goscript10.7 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/go.modmanifest44 B
curriculum/content/focus/cli-tooling/files-processes/exercise/main.goscript1.9 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/paths.goscript1.6 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/paths_test.goscript3.1 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/signal.goscript1.6 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/signal_test.goscript1.6 KB
curriculum/content/focus/cli-tooling/files-processes/exercise/signal_unix_test.goscript2.4 KB
curriculum/content/focus/cli-tooling/files-processes/quiz.jsonconfig7.9 KB
curriculum/content/focus/cli-tooling/files-processes/solution/atomicfile.goscript2.1 KB
curriculum/content/focus/cli-tooling/files-processes/solution/exec.goscript5.4 KB
curriculum/content/focus/cli-tooling/files-processes/solution/paths.goscript2.2 KB
curriculum/content/focus/cli-tooling/files-processes/solution/signal.goscript2.1 KB
curriculum/content/focus/cli-tooling/flags-config/LESSON.mdmarkdown12.3 KB
curriculum/content/focus/cli-tooling/flags-config/TUTOR.mdmarkdown6.7 KB
curriculum/content/focus/cli-tooling/flags-config/exercise/config.goscript2.4 KB
curriculum/content/focus/cli-tooling/flags-config/exercise/config_test.goscript3.1 KB
curriculum/content/focus/cli-tooling/flags-config/exercise/flagvalue.goscript769 B
curriculum/content/focus/cli-tooling/flags-config/exercise/flagvalue_test.goscript1.9 KB
curriculum/content/focus/cli-tooling/flags-config/exercise/go.modmanifest41 B
curriculum/content/focus/cli-tooling/flags-config/exercise/loader.goscript5.5 KB
curriculum/content/focus/cli-tooling/flags-config/exercise/loader_test.goscript12.4 KB
curriculum/content/focus/cli-tooling/flags-config/exercise/main.goscript1.2 KB
curriculum/content/focus/cli-tooling/flags-config/quiz.jsonconfig5.3 KB
curriculum/content/focus/cli-tooling/flags-config/solution/config.goscript3.2 KB
curriculum/content/focus/cli-tooling/flags-config/solution/flagvalue.goscript957 B
curriculum/content/focus/cli-tooling/flags-config/solution/loader.goscript9.0 KB
curriculum/content/focus/cli-tooling/terminal-ux/LESSON.mdmarkdown14.8 KB
curriculum/content/focus/cli-tooling/terminal-ux/TUTOR.mdmarkdown6.2 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/color.goscript1.6 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/color_test.goscript2.3 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/go.modmanifest40 B
curriculum/content/focus/cli-tooling/terminal-ux/exercise/main.goscript3.6 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/progress.goscript1.9 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/progress_test.goscript1.9 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/prompt.goscript2.3 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/prompt_test.goscript4.7 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/render.goscript3.8 KB
curriculum/content/focus/cli-tooling/terminal-ux/exercise/render_test.goscript6.1 KB
curriculum/content/focus/cli-tooling/terminal-ux/quiz.jsonconfig5.9 KB
curriculum/content/focus/cli-tooling/terminal-ux/solution/color.goscript1.8 KB
curriculum/content/focus/cli-tooling/terminal-ux/solution/progress.goscript2.0 KB
curriculum/content/focus/cli-tooling/terminal-ux/solution/prompt.goscript2.2 KB
curriculum/content/focus/cli-tooling/terminal-ux/solution/render.goscript4.3 KB
curriculum/content/focus/containers/capstone/LESSON.mdmarkdown20.0 KB
curriculum/content/focus/containers/capstone/TUTOR.mdmarkdown6.7 KB
curriculum/content/focus/containers/capstone/exercise/.dockerignoreother190 B
curriculum/content/focus/containers/capstone/exercise/.github/workflows/release.ymlconfig2.4 KB
curriculum/content/focus/containers/capstone/exercise/Dockerfileother903 B
curriculum/content/focus/containers/capstone/exercise/RUNBOOK.mdmarkdown1.6 KB
curriculum/content/focus/containers/capstone/exercise/check.shscript62.6 KB
curriculum/content/focus/containers/capstone/exercise/deploy/base/deployment.yamlconfig2.8 KB
curriculum/content/focus/containers/capstone/exercise/deploy/base/kustomization.yamlconfig845 B
curriculum/content/focus/containers/capstone/exercise/deploy/base/service.yamlconfig316 B
curriculum/content/focus/containers/capstone/exercise/deploy/overlays/dev/kustomization.yamlconfig1.3 KB
curriculum/content/focus/containers/capstone/exercise/deploy/overlays/dev/resources-patch.yamlconfig906 B
curriculum/content/focus/containers/capstone/exercise/deploy/overlays/prod/kustomization.yamlconfig899 B
curriculum/content/focus/containers/capstone/exercise/deploy/overlays/prod/resources-patch.yamlconfig341 B
curriculum/content/focus/containers/capstone/exercise/go.modmanifest48 B
curriculum/content/focus/containers/capstone/exercise/main.goscript6.4 KB
curriculum/content/focus/containers/capstone/quiz.jsonconfig6.5 KB
curriculum/content/focus/containers/capstone/solution/.github/workflows/release.ymlconfig6.4 KB
curriculum/content/focus/containers/capstone/solution/Dockerfileother1.2 KB
curriculum/content/focus/containers/capstone/solution/RUNBOOK.mdmarkdown7.7 KB
curriculum/content/focus/containers/capstone/solution/deploy/overlays/prod/kustomization.yamlconfig1.4 KB
curriculum/content/focus/containers/capstone/solution/deploy/overlays/prod/resources-patch.yamlconfig1.1 KB
curriculum/content/focus/containers/compose/LESSON.mdmarkdown13.5 KB
curriculum/content/focus/containers/compose/TUTOR.mdmarkdown5.2 KB
curriculum/content/focus/containers/compose/exercise/.env.exampleother348 B
curriculum/content/focus/containers/compose/exercise/app/Dockerfileother962 B
curriculum/content/focus/containers/compose/exercise/app/go.modmanifest40 B
curriculum/content/focus/containers/compose/exercise/app/main.goscript2.2 KB
curriculum/content/focus/containers/compose/exercise/check.shscript24.5 KB
curriculum/content/focus/containers/compose/exercise/compose.yamlconfig877 B
curriculum/content/focus/containers/compose/quiz.jsonconfig4.5 KB
curriculum/content/focus/containers/compose/solution/.envconfig203 B
curriculum/content/focus/containers/compose/solution/compose.yamlconfig1.6 KB
curriculum/content/focus/containers/deploying-go/LESSON.mdmarkdown21.1 KB
curriculum/content/focus/containers/deploying-go/TUTOR.mdmarkdown6.5 KB
curriculum/content/focus/containers/deploying-go/exercise/.dockerignoreother150 B
curriculum/content/focus/containers/deploying-go/exercise/Dockerfileother851 B
curriculum/content/focus/containers/deploying-go/exercise/SHUTDOWN.mdmarkdown1.4 KB
curriculum/content/focus/containers/deploying-go/exercise/check.shscript43.5 KB
curriculum/content/focus/containers/deploying-go/exercise/configmap.yamlconfig526 B
curriculum/content/focus/containers/deploying-go/exercise/deployment.yamlconfig2.5 KB
curriculum/content/focus/containers/deploying-go/exercise/go.modmanifest41 B
curriculum/content/focus/containers/deploying-go/exercise/main.goscript6.1 KB
curriculum/content/focus/containers/deploying-go/exercise/service.yamlconfig347 B
curriculum/content/focus/containers/deploying-go/exercise/shutdown_test.goscript3.3 KB
curriculum/content/focus/containers/deploying-go/quiz.jsonconfig6.5 KB
curriculum/content/focus/containers/deploying-go/solution/SHUTDOWN.mdmarkdown5.4 KB
curriculum/content/focus/containers/deploying-go/solution/configmap.yamlconfig541 B
curriculum/content/focus/containers/deploying-go/solution/deployment.yamlconfig4.0 KB
curriculum/content/focus/containers/deploying-go/solution/main.goscript6.1 KB
curriculum/content/focus/containers/docker-fundamentals/LESSON.mdmarkdown14.4 KB
curriculum/content/focus/containers/docker-fundamentals/TUTOR.mdmarkdown5.4 KB
curriculum/content/focus/containers/docker-fundamentals/exercise/Dockerfileother261 B
curriculum/content/focus/containers/docker-fundamentals/exercise/NOTES.mdmarkdown1.4 KB
curriculum/content/focus/containers/docker-fundamentals/exercise/check.shscript10.7 KB
curriculum/content/focus/containers/docker-fundamentals/exercise/go.modmanifest38 B
curriculum/content/focus/containers/docker-fundamentals/exercise/local.envconfig218 B
curriculum/content/focus/containers/docker-fundamentals/exercise/main.goscript780 B
curriculum/content/focus/containers/docker-fundamentals/exercise/scratch/notes.mdmarkdown153 B
curriculum/content/focus/containers/docker-fundamentals/quiz.jsonconfig5.5 KB
curriculum/content/focus/containers/docker-fundamentals/solution/.dockerignoreother230 B
curriculum/content/focus/containers/docker-fundamentals/solution/Dockerfileother504 B
curriculum/content/focus/containers/docker-fundamentals/solution/NOTES.mdmarkdown3.8 KB
curriculum/content/focus/containers/dockerizing-go/LESSON.mdmarkdown14.0 KB
curriculum/content/focus/containers/dockerizing-go/TUTOR.mdmarkdown5.4 KB
curriculum/content/focus/containers/dockerizing-go/exercise/.dockerignoreother143 B
curriculum/content/focus/containers/dockerizing-go/exercise/Dockerfile.naiveother512 B
curriculum/content/focus/containers/dockerizing-go/exercise/NOTES.mdmarkdown1.1 KB
curriculum/content/focus/containers/dockerizing-go/exercise/check.shscript14.2 KB
curriculum/content/focus/containers/dockerizing-go/exercise/go.modmanifest43 B
curriculum/content/focus/containers/dockerizing-go/exercise/go.summanifest0 B
curriculum/content/focus/containers/dockerizing-go/exercise/main.goscript1.7 KB
curriculum/content/focus/containers/dockerizing-go/quiz.jsonconfig4.5 KB
curriculum/content/focus/containers/dockerizing-go/solution/Dockerfileother755 B
curriculum/content/focus/containers/dockerizing-go/solution/NOTES.mdmarkdown2.7 KB
curriculum/content/focus/containers/helm-kustomize/LESSON.mdmarkdown19.1 KB
curriculum/content/focus/containers/helm-kustomize/TUTOR.mdmarkdown9.5 KB
curriculum/content/focus/containers/helm-kustomize/exercise/NOTES.mdmarkdown1.5 KB
curriculum/content/focus/containers/helm-kustomize/exercise/chart/Chart.yamlconfig518 B
curriculum/content/focus/containers/helm-kustomize/exercise/chart/templates/_helpers.tplother1.8 KB
curriculum/content/focus/containers/helm-kustomize/exercise/chart/templates/configmap.yamlconfig392 B
curriculum/content/focus/containers/helm-kustomize/exercise/chart/templates/deployment.yamlconfig3.2 KB
curriculum/content/focus/containers/helm-kustomize/exercise/chart/templates/service.yamlconfig512 B
curriculum/content/focus/containers/helm-kustomize/exercise/chart/values.yamlconfig1.4 KB
curriculum/content/focus/containers/helm-kustomize/exercise/check.shscript55.4 KB
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/base/deployment.yamlconfig2.1 KB
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/base/kustomization.yamlconfig798 B
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/base/service.yamlconfig347 B
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/overlays/dev/kustomization.yamlconfig616 B
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/overlays/dev/resources-patch.yamlconfig506 B
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/overlays/prod/kustomization.yamlconfig477 B
curriculum/content/focus/containers/helm-kustomize/exercise/kustomize/overlays/prod/resources-patch.yamlconfig614 B
curriculum/content/focus/containers/helm-kustomize/quiz.jsonconfig7.4 KB
curriculum/content/focus/containers/helm-kustomize/solution/NOTES.mdmarkdown3.3 KB
curriculum/content/focus/containers/helm-kustomize/solution/chart/Chart.yamlconfig366 B
curriculum/content/focus/containers/helm-kustomize/solution/chart/templates/configmap.yamlconfig547 B
curriculum/content/focus/containers/helm-kustomize/solution/chart/templates/deployment.yamlconfig2.5 KB
curriculum/content/focus/containers/helm-kustomize/solution/chart/values.yamlconfig1.2 KB
curriculum/content/focus/containers/helm-kustomize/solution/kustomize/base/kustomization.yamlconfig915 B
curriculum/content/focus/containers/helm-kustomize/solution/kustomize/overlays/dev/kustomization.yamlconfig820 B
curriculum/content/focus/containers/helm-kustomize/solution/kustomize/overlays/dev/resources-patch.yamlconfig515 B
curriculum/content/focus/containers/helm-kustomize/solution/kustomize/overlays/prod/kustomization.yamlconfig267 B
curriculum/content/focus/containers/helm-kustomize/solution/kustomize/overlays/prod/resources-patch.yamlconfig649 B
curriculum/content/focus/containers/kubernetes-config/LESSON.mdmarkdown19.4 KB
curriculum/content/focus/containers/kubernetes-config/TUTOR.mdmarkdown6.3 KB
curriculum/content/focus/containers/kubernetes-config/exercise/NOTES.mdmarkdown1.3 KB
curriculum/content/focus/containers/kubernetes-config/exercise/app/Dockerfileother1.0 KB

curriculum/content/focus/cli-tooling/capstone/exercise/cli_test.go

View on GitHub
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"

	"github.com/spf13/cobra"
)

type result struct {
	stdout string
	stderr string
	err    error
}

// newApp builds an App with a private, empty config directory, so no test can
// be steered by the real machine's user config. A test that wants a config file
// at the default location writes one into app.ConfigDir itself.
func newApp(t *testing.T, env map[string]string) *App {
	t.Helper()
	return &App{
		Getenv:    func(k string) string { return env[k] },
		ConfigDir: t.TempDir(),
	}
}

// run executes a fresh command tree in-process and captures both streams.
func run(t *testing.T, app *App, args ...string) result {
	t.Helper()
	return runCtx(t, context.Background(), app, args...)
}

func runCtx(t *testing.T, ctx context.Context, app *App, args ...string) result {
	t.Helper()
	if args == nil {
		args = []string{}
	}
	var out, errOut bytes.Buffer
	root := NewRootCmd(app)
	root.SetOut(&out)
	root.SetErr(&errOut)
	root.SetArgs(args)
	err := root.ExecuteContext(ctx)
	return result{stdout: out.String(), stderr: errOut.String(), err: err}
}

// execute goes through Execute, so it sees what the shell would: an exit code
// and whatever landed on stderr.
func execute(t *testing.T, ctx context.Context, app *App, args ...string) (int, string, string) {
	t.Helper()
	var out, errOut bytes.Buffer
	code := Execute(ctx, app, args, &out, &errOut)
	return code, out.String(), errOut.String()
}

// writeTree creates files under a fresh temp dir; keys are slash-separated
// relative paths and parent directories are created as needed.
func writeTree(t *testing.T, files map[string]string) string {
	t.Helper()
	dir := t.TempDir()
	for name, content := range files {
		path := filepath.Join(dir, filepath.FromSlash(name))
		if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
			t.Fatalf("mkdir for %s: %v", name, err)
		}
		if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
			t.Fatalf("write %s: %v", name, err)
		}
	}
	return dir
}

// sampleTree is 5 files: 2 .go (4 lines, 33 bytes), 2 .md (2 lines, 14 bytes),
// 1 extensionless (1 line, 5 bytes), plus a .git directory that must be pruned.
func sampleTree(t *testing.T) string {
	t.Helper()
	return writeTree(t, map[string]string{
		"a.go":          "package a\n",
		"pkg/b.go":      "package b\n\nfunc B() {}\n",
		"README.md":     "# scout\n",
		"docs/guide.md": "hello\n",
		"Makefile":      "all:\n",
		".git/config":   "[core]\n",
	})
}

// The report format is restated here on purpose: these tests pin the bytes a
// user sees, not whatever constants you happen to declare.
func scanHeader() string {
	return fmt.Sprintf("%-10s %6s %8s %10s\n", "ext", "files", "lines", "bytes")
}

func scanRow(ext string, files, lines int, bytes int64) string {
	return fmt.Sprintf("%-10s %6d %8d %10d\n", ext, files, lines, bytes)
}

func TestScanCommandText(t *testing.T) {
	dir := sampleTree(t)
	got := run(t, newApp(t, nil), "scan", dir)
	if got.err != nil {
		t.Fatalf("unexpected error: %v", got.err)
	}
	want := scanHeader() +
		scanRow(".go", 2, 4, 33) +
		scanRow(".md", 2, 2, 14) +
		scanRow("(none)", 1, 1, 5) +
		scanRow("total", 5, 7, 52)
	if got.stdout != want {
		t.Errorf("stdout =\n%q\nwant\n%q", got.stdout, want)
	}
	if got.stderr != "" {
		t.Errorf("stderr = %q, want empty: the report is data, not diagnostics", got.stderr)
	}
}

func TestScanCommandTopFlag(t *testing.T) {
	dir := sampleTree(t)
	got := run(t, newApp(t, nil), "scan", dir, "--top", "1")
	if got.err != nil {
		t.Fatalf("unexpected error: %v", got.err)
	}
	want := scanHeader() + scanRow(".go", 2, 4, 33) + scanRow("total", 5, 7, 52)
	if got.stdout != want {
		t.Errorf("stdout =\n%q\nwant\n%q\n(--top truncates the table; the totals still cover the whole tree)",
			got.stdout, want)
	}
}

func TestScanCommandJSON(t *testing.T) {
	dir := sampleTree(t)
	// --color always must lose to --json: machine output is never styled.
	got := run(t, newApp(t, nil), "scan", dir, "--json", "--color", "always")
	if got.err != nil {
		t.Fatalf("unexpected error: %v", got.err)
	}
	if strings.Contains(got.stdout, "\x1b") {
		t.Fatalf("JSON output contains an escape sequence: %q", got.stdout)
	}
	var res ScanResult
	if err := json.Unmarshal([]byte(got.stdout), &res); err != nil {
		t.Fatalf("stdout is not one JSON document: %v\ngot: %q", err, got.stdout)
	}
	if res.Root != dir {
		t.Errorf("root = %q, want %q", res.Root, dir)
	}
	if res.Files != 5 || res.Lines != 7 || res.Bytes != 52 {
		t.Errorf("totals = %d files, %d lines, %d bytes; want 5, 7, 52", res.Files, res.Lines, res.Bytes)
	}
	if len(res.Exts) != 3 || res.Exts[0].Ext != ".go" {
		t.Errorf("exts = %+v, want three buckets with .go first", res.Exts)
	}
}

func TestScanCommandEmptyDir(t *testing.T) {
	got := run(t, newApp(t, nil), "scan", t.TempDir())
	if got.err != nil {
		t.Fatalf("unexpected error: %v", got.err)
	}
	if got.stdout != "no files\n" {
		t.Errorf("stdout = %q, want %q", got.stdout, "no files\n")
	}
}

func TestScanCommandColor(t *testing.T) {
	dir := sampleTree(t)
	app := newApp(t, nil) // not a terminal …
	got := run(t, app, "scan", dir, "--color", "always")
	if got.err != nil {
		t.Fatalf("unexpected error: %v", got.err)
	}
	header := strings.TrimSuffix(scanHeader(), "\n")
	wantFirst := Bold + header + Reset
	if first, _, _ := strings.Cut(got.stdout, "\n"); first != wantFirst {
		t.Errorf("first line = %q, want %q (pad first, then paint: the reset goes before the newline)",
			first, wantFirst)
	}
	// … and with the default policy the same run must be plain.
	plain := run(t, app, "scan", dir)
	if strings.Contains(plain.stdout, "\x1b") {
		t.Errorf("auto colour wrote escapes into a non-terminal stream: %q", plain.stdout)
	}
}

func TestVersion(t *testing.T) {
	wantText := fmt.Sprintf("scout %s (commit %s, built %s, %s, %s)\n",
		"dev", "none", "unknown", runtime.Version(), runtime.GOOS+"/"+runtime.GOARCH)

	for _, args := range [][]string{{"version"}, {"--version"}} {
		got := run(t, newApp(t, nil), args...)
		if got.err != nil {
			t.Fatalf("%q: unexpected error: %v", args, got.err)
		}
		if got.stdout != wantText {
			t.Errorf("%q: stdout = %q, want %q", args, got.stdout, wantText)
		}
	}

	got := run(t, newApp(t, nil), "version", "--json")
	var info BuildInfo
	if err := json.Unmarshal([]byte(got.stdout), &info); err != nil {
		t.Fatalf("version --json: not a JSON document: %v\ngot: %q", err, got.stdout)
	}
	if info.Version != "dev" || info.Commit != "none" || info.Date != "unknown" {
		t.Errorf("version info = %+v, want the unpatched defaults dev/none/unknown", info)
	}
	if info.Platform != runtime.GOOS+"/"+runtime.GOARCH {
		t.Errorf("platform = %q, want %q", info.Platform, runtime.GOOS+"/"+runtime.GOARCH)
	}
}

// --version belongs to root alone: on a subcommand it is a typo, and a typo
// must not be silently accepted.
func TestVersionFlagIsRootOnly(t *testing.T) {
	got := run(t, newApp(t, nil), "scan", t.TempDir(), "--version")
	if got.err == nil {
		t.Fatal("scan --version returned no error, want an unknown-flag failure")
	}
}

func TestExitCodesEndToEnd(t *testing.T) {
	dir := sampleTree(t)
	missing := filepath.Join(dir, "nope")

	cases := []struct {
		name     string
		args     []string
		env      map[string]string
		wantCode int
		wantErr  string // substring required on stderr, "" means stderr must be empty
	}{
		{name: "success", args: []string{"scan", dir}, wantCode: 0},
		{name: "unknown flag", args: []string{"scan", dir, "--nope"}, wantCode: 2, wantErr: "unknown flag"},
		{name: "invalid colour value", args: []string{"scan", dir, "--color", "purple"}, wantCode: 2, wantErr: `invalid color "purple"`},
		{name: "bad environment value", args: []string{"scan", dir}, env: map[string]string{"SCOUT_TOP": "abc"}, wantCode: 2, wantErr: `invalid SCOUT_TOP "abc"`},
		{name: "missing directory", args: []string{"scan", missing}, wantCode: 1, wantErr: "nope"},
		{name: "unknown command", args: []string{"frobnicate"}, wantCode: 1, wantErr: `unknown command "frobnicate"`},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			code, stdout, stderr := execute(t, context.Background(), newApp(t, c.env), c.args...)
			if code != c.wantCode {
				t.Errorf("exit code = %d, want %d (stderr: %s)", code, c.wantCode, stderr)
			}
			if c.wantErr == "" {
				if stderr != "" {
					t.Errorf("stderr = %q, want empty on success", stderr)
				}
				return
			}
			if !strings.Contains(stderr, c.wantErr) {
				t.Errorf("stderr = %q, want it to contain %q", stderr, c.wantErr)
			}
			if !strings.HasPrefix(stderr, "scout: ") {
				t.Errorf("stderr = %q, want the message prefixed with the tool name", stderr)
			}
			if strings.Count(strings.TrimSuffix(stderr, "\n"), "\n") != 0 {
				t.Errorf("stderr = %q, want exactly one line: the error is reported once", stderr)
			}
			if stdout != "" {
				t.Errorf("stdout = %q, want empty on a failed run", stdout)
			}
		})
	}
}

// Ctrl-C is a cancelled context by the time it reaches the tool: the run stops,
// the exit code says why, and nothing half-written lands on stdout.
func TestInterruptedRun(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	code, stdout, stderr := execute(t, ctx, newApp(t, nil), "scan", sampleTree(t))
	if code != 130 {
		t.Errorf("exit code = %d, want 130 (128 + SIGINT)", code)
	}
	if stdout != "" {
		t.Errorf("stdout = %q, want empty: a cancelled scan has no report to print", stdout)
	}
	if stderr != "scout: interrupted\n" {
		t.Errorf("stderr = %q, want %q", stderr, "scout: interrupted\n")
	}
}

func TestRootHelp(t *testing.T) {
	got := run(t, newApp(t, nil))
	if got.err != nil {
		t.Fatalf("bare invocation returned an error: %v", got.err)
	}
	for _, want := range []string{"scan", "authors", "version", "--json", "--color", "--config"} {
		if !strings.Contains(got.stdout, want) {
			t.Errorf("help does not mention %q; got:\n%s", want, got.stdout)
		}
	}
}

// Every run must get its own tree: a cobra.Command stores parsed flag values on
// itself, so a package-level tree carries one run's flags into the next.
func TestTreeIsFreshPerRun(t *testing.T) {
	app := newApp(t, nil)
	dir := sampleTree(t)

	first := run(t, app, "scan", dir, "--top", "1")
	second := run(t, app, "scan", dir)
	if first.err != nil || second.err != nil {
		t.Fatalf("unexpected errors: %v / %v", first.err, second.err)
	}
	if strings.Count(second.stdout, "\n") != 5 {
		t.Errorf("the second run shows %q\nwant the full table: --top from the first run leaked into it",
			second.stdout)
	}
}

func findCmd(t *testing.T, parent *cobra.Command, name string) *cobra.Command {
	t.Helper()
	for _, c := range parent.Commands() {
		if c.Name() == name {
			return c
		}
	}
	t.Fatalf("no %q subcommand under %q", name, parent.Name())
	return nil
}

func TestFlagScopes(t *testing.T) {
	root := NewRootCmd(newApp(t, nil))
	scan := findCmd(t, root, "scan")
	authors := findCmd(t, root, "authors")

	for _, name := range []string{"json", "color", "config", "ignore"} {
		if root.LocalFlags().Lookup(name) == nil {
			t.Errorf("--%s must be declared on root as a persistent flag", name)
		}
		if scan.InheritedFlags().Lookup(name) == nil {
			t.Errorf("scan must inherit --%s from root", name)
		}
	}
	if scan.LocalFlags().Lookup("top") == nil {
		t.Error("--top must be a local flag on scan")
	}
	if authors.Flags().Lookup("top") != nil {
		t.Error("scan's local --top must not be visible on authors")
	}
	if authors.LocalFlags().Lookup("limit") == nil {
		t.Error("--limit must be a local flag on authors")
	}
	if scan.Flags().Lookup("version") != nil {
		t.Error("--version must be local to root, not inherited by subcommands")
	}
}
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 469kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 321kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 321kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 321kUpdated Claude CodeCodexVerifiedInstall