|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "reflect" |
| 6 | + "strings" |
5 | 7 | "testing" |
| 8 | + |
| 9 | + "github.com/firede/agent-fetch/internal/fetcher" |
| 10 | + "github.com/urfave/cli/v3" |
6 | 11 | ) |
7 | 12 |
|
8 | 13 | func TestRouteToDefaultWeb(t *testing.T) { |
| 14 | + root := newRootCommand(fetcher.DefaultConfig()) |
| 15 | + |
9 | 16 | tests := []struct { |
10 | 17 | name string |
11 | 18 | in []string |
@@ -46,14 +53,79 @@ func TestRouteToDefaultWeb(t *testing.T) { |
46 | 53 | in: []string{"agent-fetch", "--version"}, |
47 | 54 | want: []string{"agent-fetch", "--version"}, |
48 | 55 | }, |
| 56 | + { |
| 57 | + name: "unknown token treated as web shorthand", |
| 58 | + in: []string{"agent-fetch", "https://example.net"}, |
| 59 | + want: []string{"agent-fetch", "web", "https://example.net"}, |
| 60 | + }, |
49 | 61 | } |
50 | 62 |
|
51 | 63 | for _, tc := range tests { |
52 | 64 | t.Run(tc.name, func(t *testing.T) { |
53 | | - got := routeToDefaultWeb(tc.in) |
| 65 | + got := routeToDefaultWeb(tc.in, root) |
54 | 66 | if !reflect.DeepEqual(got, tc.want) { |
55 | 67 | t.Fatalf("unexpected args rewrite\ngot: %#v\nwant: %#v", got, tc.want) |
56 | 68 | } |
57 | 69 | }) |
58 | 70 | } |
59 | 71 | } |
| 72 | + |
| 73 | +func TestRouteToDefaultWeb_RegisteredSubcommandUntouched(t *testing.T) { |
| 74 | + root := newRootCommand(fetcher.DefaultConfig()) |
| 75 | + const testSubcommand = "__test_subcommand__" |
| 76 | + root.Commands = append(root.Commands, &cli.Command{Name: testSubcommand}) |
| 77 | + |
| 78 | + in := []string{"agent-fetch", testSubcommand, "arg1"} |
| 79 | + want := []string{"agent-fetch", testSubcommand, "arg1"} |
| 80 | + got := routeToDefaultWeb(in, root) |
| 81 | + if !reflect.DeepEqual(got, want) { |
| 82 | + t.Fatalf("unexpected args rewrite\ngot: %#v\nwant: %#v", got, want) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestHelpOutputContracts(t *testing.T) { |
| 87 | + t.Run("root help shows default web options", func(t *testing.T) { |
| 88 | + var out strings.Builder |
| 89 | + err := runForTest([]string{"agent-fetch", "-h"}, &out, &out) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("run help failed: %v", err) |
| 92 | + } |
| 93 | + helpText := out.String() |
| 94 | + if !strings.Contains(helpText, "DEFAULT WEB OPTIONS:") { |
| 95 | + t.Fatalf("expected default web options section, got:\n%s", helpText) |
| 96 | + } |
| 97 | + if !strings.Contains(helpText, "--format string") { |
| 98 | + t.Fatalf("expected web options in root help, got:\n%s", helpText) |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("doctor help excludes web flags", func(t *testing.T) { |
| 103 | + var out strings.Builder |
| 104 | + err := runForTest([]string{"agent-fetch", "doctor", "-h"}, &out, &out) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("run doctor help failed: %v", err) |
| 107 | + } |
| 108 | + helpText := out.String() |
| 109 | + if !strings.Contains(helpText, "--browser-path string") { |
| 110 | + t.Fatalf("expected doctor option in doctor help, got:\n%s", helpText) |
| 111 | + } |
| 112 | + if strings.Contains(helpText, "--format string") { |
| 113 | + t.Fatalf("did not expect web flag in doctor help, got:\n%s", helpText) |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("web help shows fetch flags", func(t *testing.T) { |
| 118 | + cmd := newRootCommand(fetcher.DefaultConfig()) |
| 119 | + var out strings.Builder |
| 120 | + cmd.Writer = &out |
| 121 | + cmd.ErrWriter = &out |
| 122 | + err := cmd.Run(context.Background(), routeToDefaultWeb([]string{"agent-fetch", "web", "-h"}, cmd)) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("run web help failed: %v", err) |
| 125 | + } |
| 126 | + helpText := out.String() |
| 127 | + if !strings.Contains(helpText, "--format string") { |
| 128 | + t.Fatalf("expected fetch flag in web help, got:\n%s", helpText) |
| 129 | + } |
| 130 | + }) |
| 131 | +} |
0 commit comments