| 1 | package agentcli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestAgentCLIProtocolAndInit(t *testing.T) { |
| 15 | path := filepath.Join(t.TempDir(), "profile", "config.json") |
| 16 | run := func(args ...string) (int, agentReply) { |
| 17 | t.Helper() |
| 18 | var output bytes.Buffer |
| 19 | code := Main(append(args, "--config", path), strings.NewReader(""), &output) |
| 20 | var reply agentReply |
| 21 | if json.Unmarshal(output.Bytes(), &reply) != nil || reply.Protocol != 1 { |
| 22 | t.Fatalf("not one protocol reply: %s", output.String()) |
| 23 | } |
| 24 | return code, reply |
| 25 | } |
| 26 | if code, _ := run("capabilities"); code != 0 { |
| 27 | t.Fatal(code) |
| 28 | } |
| 29 | if _, err := os.Stat(filepath.Dir(path)); !os.IsNotExist(err) { |
| 30 | t.Fatal("discovery created profile") |
| 31 | } |
| 32 | if code, _ := run("init"); code != 0 { |
| 33 | t.Fatal(code) |
| 34 | } |
| 35 | before, _ := os.ReadFile(path) |
| 36 | if code, _ := run("init"); code != 0 { |
| 37 | t.Fatal("not idempotent") |
| 38 | } |
| 39 | after, _ := os.ReadFile(path) |
| 40 | if !bytes.Equal(before, after) { |
| 41 | t.Fatal("repeat init overwrote config") |
| 42 | } |
| 43 | for _, args := range [][]string{{"call", "status", "--input", "-"}, {"wat"}, {"doctor", "--wait", "1s"}, {"capabilities", "--wait", "-1s"}, {"call"}} { |
| 44 | if code, _ := run(args...); code != 2 { |
| 45 | t.Fatalf("usage accepted %v: %d", args, code) |
| 46 | } |
| 47 | } |
| 48 | if err := os.WriteFile(path, []byte("damaged"), 0600); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if code, reply := run("init"); code != 1 || reply.Error.Code != "config_invalid" { |
| 52 | t.Fatal("damaged config silently replaced") |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestAgentCLIRejectsSecretArgumentsAndMalformedJSON(t *testing.T) { |
| 57 | for _, input := range []string{`null`, `[]`, `{} {}`, `{"password":"secret"} trailing`} { |
| 58 | var output bytes.Buffer |
| 59 | code := Main([]string{"call", "account.unlock", "--input", "-", "--config", filepath.Join(t.TempDir(), "config.json")}, strings.NewReader(input), &output) |
| 60 | if code != 2 || strings.Contains(output.String(), "secret") { |
| 61 | t.Fatal(code, output.String()) |
| 62 | } |
| 63 | } |
| 64 | var output bytes.Buffer |
| 65 | code := Main([]string{"call", "account.unlock", "--password", "do-not-echo"}, strings.NewReader(""), &output) |
| 66 | if code != 2 || strings.Contains(output.String(), "do-not-echo") { |
| 67 | t.Fatal(output.String()) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestAgentWaitTimeoutKeepsOriginalJobID(t *testing.T) { |
| 72 | data, err := agentWait(context.Background(), filepath.Join(t.TempDir(), "config.json"), "012345abcdef", time.Millisecond) |
| 73 | if err == nil || data != nil || !strings.Contains(err.Error(), "job may still be running") { |
| 74 | t.Fatal(string(data), err) |
| 75 | } |
| 76 | } |