package main import ( "bytes" "context" "log/slog" "os" "path/filepath" "strings" "testing" ) func TestToolVerbClosed(t *testing.T) { for _, args := range [][]string{nil, {"serve"}, {"tool", "secret"}, {"tool"}} { var out, log bytes.Buffer if run(context.Background(), args, strings.NewReader(""), &out, &log, func(string) string { return "/missing" }) != 1 || strings.Contains(log.String(), "secret") || out.Len() != 0 { t.Fatal("unsafe tool failure", args) } } } func TestRunnerServiceVerbs(t *testing.T) { for _, verb := range []string{"run", "preflight", "enrol", "verify-installed"} { var out, log bytes.Buffer if run(context.Background(), []string{verb}, strings.NewReader(""), &out, &log, func(string) string { return "" }) != 1 { t.Fatal("missing config accepted", verb) } } var out, log bytes.Buffer if run(context.Background(), []string{"version"}, strings.NewReader(""), &out, &log, func(string) string { return "" }) != 0 || out.Len() == 0 { t.Fatal("version unavailable") } } func TestRunnerDiagnosticIsCredentialFree(t *testing.T) { var out, log bytes.Buffer code := run(context.Background(), []string{"run"}, strings.NewReader(""), &out, &log, func(string) string { return "https://user:secret@example.com" }) if code != 1 || !strings.Contains(log.String(), "EQUIPO_ORIGIN") || !strings.Contains(log.String(), "runner.config.origin") || strings.Contains(log.String(), "secret") { t.Fatal("missing or unsafe diagnostic", log.String()) } } // run's first line says what starts and where it connects: the version, // the platform, the role, the origin's host alone, the machine's label // and the env file it read. It comes before anything that could hang. func TestRunLogsItsStart(t *testing.T) { root, err := filepath.EvalSymlinks(t.TempDir()) if err != nil { t.Fatal(err) } vars := map[string]string{"EQUIPO_ORIGIN": "https://equipo.example.com", "EQUIPO_INSTANCE_ID": "i1", "EQUIPO_FAKE_PLATFORM": "darwin/arm64", "EQUIPO_MACHINE_LABEL": `"Test Mac"`, "EQUIPO_AMADAN_PATH": "/bin/sh"} for _, v := range []string{"BWRAP_PATH", "CLAUDE_PATH", "CODEX_CODE_MODE_HOST_PATH", "RUNNER_STATE_DIR", "AMADAN_CREDENTIAL_PATH", "ACCOUNT_BINDINGS_PATH", "CODEX_PATH", "GO_ROOT", "GO_BINARY", "WORKSPACE_ROOT", "SCRATCH_ROOT", "GIT_PATH"} { vars["EQUIPO_"+v] = filepath.Join(root, strings.ToLower(v)) } var lines []string for k, v := range vars { lines = append(lines, k+"="+v) } envFile := filepath.Join(root, "runner.env") if err := os.WriteFile(envFile, []byte(strings.Join(lines, "\n")+"\n"), 0600); err != nil { t.Fatal(err) } var out, log bytes.Buffer // No credential in the state directory: the run ends at once, after // its first line. run(context.Background(), []string{"run", "--env-file", envFile}, strings.NewReader(""), &out, &log, func(string) string { return "" }) first, _, _ := strings.Cut(log.String(), "\n") for _, want := range []string{`level=INFO msg="runner starting" version=`, " os=" + goos() + " arch=" + goarch() + " role=worker origin=equipo.example.com", `label="Test Mac"`, "env_file=" + envFile} { if !strings.Contains(first, want) { t.Fatalf("start line lacks %q:\n%s", want, log.String()) } } if strings.Contains(first, "https://") { t.Fatalf("start line names more than the origin's host: %s", first) } } // The fetcher verb reads no configuration: it needs none, and the // sandbox it runs in has no environment. func TestWebreadVerbNeedsNoConfiguration(t *testing.T) { var out, log bytes.Buffer getenv := func(k string) string { t.Fatalf("webread read %s", k); return "" } if code := run(context.Background(), []string{"webread"}, strings.NewReader(""), &out, &log, getenv); code != 2 || !strings.Contains(log.String(), "webread ") { t.Fatalf("exit %d: %s", code, log.String()) } } // The render sandbox's first process reads no configuration either, and // takes no arguments. func TestRenderSandboxVerbNeedsNoConfiguration(t *testing.T) { var out, log bytes.Buffer getenv := func(k string) string { t.Fatalf("render-sandbox read %s", k); return "" } if code := run(context.Background(), []string{"render-sandbox", "/site"}, strings.NewReader(""), &out, &log, getenv); code != 2 || out.Len() != 0 { t.Fatalf("exit %d: %s", code, log.String()) } } // install-opencode takes one absolute, clean root and nothing else. func TestInstallOpenCodeArguments(t *testing.T) { for _, args := range [][]string{nil, {"relative"}, {"/a/../b"}, {"/a", "/b"}} { var out, errout strings.Builder if code := installOpenCode(context.Background(), args, &out, &errout); code != 2 || out.Len() != 0 || !strings.Contains(errout.String(), "install-opencode") { t.Fatalf("%q: %d %q %q", args, code, out.String(), errout.String()) } } } // The runner logs at info unless EQUIPO_RUNNER_LOG_LEVEL=debug asks for // its debug lines too (an Assistant turn's phases among them). func TestRunnerLogLevel(t *testing.T) { for v, want := range map[string]slog.Level{"": slog.LevelInfo, "info": slog.LevelInfo, "verbose": slog.LevelInfo, "debug": slog.LevelDebug} { o := logOptions(func(k string) string { if k == "EQUIPO_RUNNER_LOG_LEVEL" { return v } return "" }) if got := o.Level.Level(); got != want { t.Errorf("EQUIPO_RUNNER_LOG_LEVEL=%q: level %v, want %v", v, got, want) } } }