(t *testing.T)
| 134 | } |
| 135 | |
| 136 | func TestSearchRun(t *testing.T) { |
| 137 | const emptyCodeResponse = `{"total_count": 0, "incomplete_results": false, "items": []}` |
| 138 | |
| 139 | // stubKeywordSearch registers the HTTP stubs needed for a keyword search. |
| 140 | // searchByKeyword fires up to 3 concurrent search/code requests (path, |
| 141 | // owner, primary). Stubs are one-shot in httpmock, so we register one |
| 142 | // per request. |
| 143 | stubKeywordSearch := func(reg *httpmock.Registry, codeResponse string) { |
| 144 | for range 3 { |
| 145 | reg.Register( |
| 146 | httpmock.REST("GET", "search/code"), |
| 147 | httpmock.StringResponse(codeResponse), |
| 148 | ) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | tests := []struct { |
| 153 | name string |
| 154 | opts *SearchOptions |
| 155 | tty bool |
| 156 | httpStubs func(*httpmock.Registry) |
| 157 | wantStdout string |
| 158 | wantStderr string |
| 159 | wantErr string |
| 160 | }{ |
| 161 | { |
| 162 | name: "displays results in non-TTY", |
| 163 | tty: false, |
| 164 | opts: &SearchOptions{Query: "terraform", Page: 1, Limit: defaultLimit}, |
| 165 | httpStubs: func(reg *httpmock.Registry) { |
| 166 | stubKeywordSearch(reg, `{"total_count": 1, "incomplete_results": false, "items": [{"name": "SKILL.md", "path": "skills/terraform/SKILL.md", "repository": {"full_name": "github/awesome-skills"}}]}`) |
| 167 | }, |
| 168 | wantStdout: "github/awesome-skills\tterraform\t\t0\n", |
| 169 | }, |
| 170 | { |
| 171 | name: "deduplicates results", |
| 172 | tty: false, |
| 173 | opts: &SearchOptions{Query: "terraform", Page: 1, Limit: defaultLimit}, |
| 174 | httpStubs: func(reg *httpmock.Registry) { |
| 175 | stubKeywordSearch(reg, `{"total_count": 3, "incomplete_results": false, "items": [{"name": "SKILL.md", "path": "skills/terraform/SKILL.md", "repository": {"full_name": "github/awesome-skills"}}, {"name": "SKILL.md", "path": "skills/terraform/SKILL.md", "repository": {"full_name": "github/awesome-skills"}}, {"name": "SKILL.md", "path": "skills/terraform-aws/SKILL.md", "repository": {"full_name": "github/awesome-skills"}}]}`) |
| 176 | }, |
| 177 | wantStdout: "github/awesome-skills\tterraform\t\t0\ngithub/awesome-skills\tterraform-aws\t\t0\n", |
| 178 | }, |
| 179 | { |
| 180 | name: "no results", |
| 181 | tty: true, |
| 182 | opts: &SearchOptions{Query: "nonexistent", Page: 1, Limit: defaultLimit}, |
| 183 | httpStubs: func(reg *httpmock.Registry) { |
| 184 | stubKeywordSearch(reg, emptyCodeResponse) |
| 185 | }, |
| 186 | wantErr: `no skills found matching "nonexistent"`, |
| 187 | }, |
| 188 | { |
| 189 | name: "nested skill path", |
| 190 | tty: false, |
| 191 | opts: &SearchOptions{Query: "my-skill", Page: 1, Limit: defaultLimit}, |
| 192 | httpStubs: func(reg *httpmock.Registry) { |
| 193 | stubKeywordSearch(reg, `{"total_count": 1, "incomplete_results": false, "items": [{"name": "SKILL.md", "path": "skills/author/my-skill/SKILL.md", "repository": {"full_name": "org/repo"}}]}`) |
nothing calls this directly
no test coverage detected