Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make changes to support testing individual commands, add a few tests #15

Merged
merged 29 commits into from
Oct 14, 2024

Conversation

cheshire137
Copy link
Member

@cheshire137 cheshire137 commented Oct 11, 2024

For https://github.com/github/models/issues/330. This adds a few basic tests covering the happy path for the view, run, and list commands. It also adds a new Actions workflow for running tests.

To support testing without hitting the actual Azure APIs, I added a new command.Config type to bundle together an Azure client and output streams. I renamed azuremodels.Client to azuremodels.AzureClient and made a new azuremodels.Client that's an interface. That let me add an azuremodels.MockClient type for use in tests, allowing us to stub out functions like ListModels to avoid making real HTTP requests.

Current test output:

	github.com/github/gh-models		coverage: 0.0% of statements
	github.com/github/gh-models/cmd		coverage: 0.0% of statements
ok  	github.com/github/gh-models/cmd/list	1.014s	coverage: 17.4% of statements
ok  	github.com/github/gh-models/cmd/run	1.025s	coverage: 32.5% of statements
	github.com/github/gh-models/internal/azuremodels		coverage: 0.0% of statements
	github.com/github/gh-models/internal/sse		coverage: 0.0% of statements
	github.com/github/gh-models/internal/ux		coverage: 0.0% of statements
	github.com/github/gh-models/pkg/command		coverage: 0.0% of statements
	github.com/github/gh-models/pkg/util		coverage: 0.0% of statements
ok  	github.com/github/gh-models/cmd/view	1.101s	coverage: 26.9% of statements

@cheshire137 cheshire137 self-assigned this Oct 11, 2024
@@ -2,17 +2,25 @@ name: "go-linter"

on:
pull_request:
types: [opened, synchronize, reopened]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run the linter on fewer PR events, such as when the PR is labelled.

merge_group:
workflow_dispatch:
push:
branches:
- 'main'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also lint what gets merged into the main branch.


permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancel an existing run of the linter for the same commit.

jobs:
lint:
strategy:
fail-fast: false
runs-on: ubuntu-latest-xl
runs-on: ubuntu-latest
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably don't need the XL version.

cmd/root.go Outdated
Comment on lines 27 to 31
token, _ := auth.TokenForHost("github.com")
if token == "" {
util.WriteToOut(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n")
return nil
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of the commands (run, view, list) was doing this, so I pulled out to this earlier point to DRY things up. It also lets me test the individual commands by passing in a mock client.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does moving this affect the ability to list commands, get help from the cli unless you're signed in?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did indeed affect things! Fixed with 1b48b0f. Now when I'm not authenticated and I try to run a command, I get output like:

% ./gh-models view gpt-4o-mini
No GitHub token found. Please run 'gh auth login' to authenticate.
Error: not authenticated
Usage:
  gh models view [model] [flags]

Flags:
  -h, --help   help for view

If I just try to check usage info, I get output like:

% ./gh-models list --help
No GitHub token found. Please run 'gh auth login' to authenticate.
List available models

Usage:
  gh models list [flags]

Flags:
  -h, --help   help for list

Less reaching-through at call sites.
}

func (p *modelPrinter) render() error {
modelSummary := p.modelSummary
if modelSummary != nil {
p.printLabelledLine("Display name:", modelSummary.FriendlyName)
p.printLabelledLine("Summary name:", modelSummary.Name)
p.printLabelledLine("Model name:", modelSummary.Name)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing the 'view' command test showed me this in the output produced, making me realize we were labelling this field wrong. Yay for tests! 🎉

@@ -36,7 +36,8 @@ type ChatCompletionOptions struct {
TopP *float64 `json:"top_p,omitempty"`
}

type chatChoiceMessage struct {
// ChatChoiceMessage is a message from a choice in a chat conversation.
type ChatChoiceMessage struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made public for use in tests.

@cheshire137 cheshire137 marked this pull request as ready for review October 11, 2024 19:21
@cheshire137 cheshire137 requested a review from a team as a code owner October 11, 2024 19:21
@cheshire137 cheshire137 changed the title Make changes to support testing individual commands, and add a test Make changes to support testing individual commands, add a few tests Oct 11, 2024
Comment on lines 29 to 31
- name: Configure Go private module access
run: |
echo "machine goproxy.githubapp.com login nobody password ${{ secrets.GOPROXY_TOKEN }}" >> $HOME/.netrc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use - uses: github/[email protected]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite possibly! Let me try that out, this was copy-pasta from another github-org Go app.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped from both our test and lint workflows in 3ede78e, we'll see if anything goes awry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, both builds are fine! 🎉

cmd/root.go Outdated
Comment on lines 27 to 31
token, _ := auth.TokenForHost("github.com")
if token == "" {
util.WriteToOut(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n")
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does moving this affect the ability to list commands, get help from the cli unless you're signed in?

// GetChatCompletionStream returns a stream of chat completions using the given options.
GetChatCompletionStream(context.Context, ChatCompletionOptions) (*ChatCompletionResponse, error)
// GetModelDetails returns the details of the specified model in a particular registry.
GetModelDetails(context.Context, string, string, string) (*ModelDetails, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, use names for the parameters to understand which 3 strings this takes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Fixed in 09cd4ae.

cheshire137 and others added 3 commits October 11, 2024 15:30
e.g.,

./gh-models run gpt-4o-mini "hello"
No GitHub token found. Please run 'gh auth login' to authenticate.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xba48c6a]

goroutine 1 [running]:
github.com/github/gh-models/internal/azuremodels.(*AzureClient).ListModels(0x0, {0xd098300, 0xd5ca4a0})
	gh-models/internal/azuremodels/azure_client.go:194 +0x20a
github.com/github/gh-models/cmd/run.(*runCommandHandler).loadModels(0x4?)
	gh-models/cmd/run/run.go:382 +0x2a
github.com/github/gh-models/cmd/run.NewRunCommand.func1(0xc00021a308, {0xc0002d82e0, 0x2, 0x2})
	gh-models/cmd/run/run.go:203 +0x139
github.com/spf13/cobra.(*Command).execute(0xc00021a308, {0xc0002d82a0, 0x2, 0x2})
	go/pkg/mod/github.com/spf13/[email protected]/command.go:985 +0xaca
github.com/spf13/cobra.(*Command).ExecuteC(0xc0001fd808)
	go/pkg/mod/github.com/spf13/[email protected]/command.go:1117 +0x3ff
github.com/spf13/cobra.(*Command).ExecuteContextC(0xb6c5305?, {0xd098300?, 0xd5ca4a0?})
	go/pkg/mod/github.com/spf13/[email protected]/command.go:1050 +0x47
main.mainRun()
	gh-models/main.go:29 +0x26
main.main()
	gh-models/main.go:19 +0x13
https: //github.com//pull/15#discussion_r1797368843
Co-Authored-By: Christopher Schleiden <[email protected]>
Copy link
Collaborator

@sgoedecke sgoedecke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for refactoring this to make it testable. One nit around changed token validation behavior, not a showstopper.


var client azuremodels.Client

if token == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For gh models run, previously the code would have immediately bailed out of the command if you had no token set, but now it looks like it's going to try and keep going with a "" token (and eventually run into some 403 error).

I really like that this PR still lets you do some commands without a token, but do you think it's worth trying to preserve the old behavior for run (printing a human-readable error and immediately exiting)?

Copy link
Member Author

@cheshire137 cheshire137 Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it's worth trying to preserve the old behavior for run (printing a human-readable error and immediately exiting)?

So the effect feels the same to users. When I try to run as an unauthenticated user right now on this branch, here's what I get:

% ./gh-models run gpt-4o-mini "hello world"
No GitHub token found. Please run 'gh auth login' to authenticate.
Error: not authenticated
Usage:
  gh models run [model] [prompt] [flags]

Flags:
  -h, --help                   help for run
      --max-tokens string      Limit the maximum tokens for the model response.
      --system-prompt string   Prompt the system.
      --temperature string     Controls randomness in the response, use lower to be more deterministic.
      --top-p string           Controls text diversity by selecting the most probable words until a set probability is reached.

Are you worried about us trying to make an HTTP call with an invalid (blank) token?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really mind (actually making the HTTP call is not a problem), I was just a little worried that the error would be ugly. Given that it isn't, I have no qualms here - no need to follow up in another branch.

@cheshire137 cheshire137 merged commit 27157bc into main Oct 14, 2024
5 checks passed
@cheshire137 cheshire137 deleted the add-a-test branch October 14, 2024 15:31
@cheshire137
Copy link
Member Author

I landed the PR so we can start having some test coverage and help avoid regressions/breakages, Sean, but reply back above regarding the blank token and I can iterate further in a follow-up branch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants