|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + binaryName = "gh-models-test" |
| 16 | + timeoutDuration = 30 * time.Second |
| 17 | +) |
| 18 | + |
| 19 | +// getBinaryPath returns the path to the compiled gh-models binary |
| 20 | +func getBinaryPath(t *testing.T) string { |
| 21 | + wd, err := os.Getwd() |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + // Binary should be in the parent directory |
| 25 | + binaryPath := filepath.Join(filepath.Dir(wd), binaryName) |
| 26 | + |
| 27 | + // Check if binary exists |
| 28 | + if _, err := os.Stat(binaryPath); os.IsNotExist(err) { |
| 29 | + t.Skipf("Binary %s not found. Run 'script/build' first.", binaryPath) |
| 30 | + } |
| 31 | + |
| 32 | + return binaryPath |
| 33 | +} |
| 34 | + |
| 35 | +// runCommand executes the gh-models binary with given arguments |
| 36 | +func runCommand(t *testing.T, args ...string) (stdout, stderr string, err error) { |
| 37 | + binaryPath := getBinaryPath(t) |
| 38 | + |
| 39 | + cmd := exec.Command(binaryPath, args...) |
| 40 | + cmd.Env = os.Environ() |
| 41 | + |
| 42 | + // Set timeout |
| 43 | + done := make(chan error, 1) |
| 44 | + var stdoutBytes, stderrBytes []byte |
| 45 | + |
| 46 | + go func() { |
| 47 | + stdoutBytes, err = cmd.Output() |
| 48 | + if err != nil { |
| 49 | + if exitError, ok := err.(*exec.ExitError); ok { |
| 50 | + stderrBytes = exitError.Stderr |
| 51 | + } |
| 52 | + } |
| 53 | + done <- err |
| 54 | + }() |
| 55 | + |
| 56 | + select { |
| 57 | + case err = <-done: |
| 58 | + return string(stdoutBytes), string(stderrBytes), err |
| 59 | + case <-time.After(timeoutDuration): |
| 60 | + if cmd.Process != nil { |
| 61 | + cmd.Process.Kill() |
| 62 | + } |
| 63 | + t.Fatalf("Command timed out after %v", timeoutDuration) |
| 64 | + return "", "", nil |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestList(t *testing.T) { |
| 69 | + stdout, stderr, err := runCommand(t, "list") |
| 70 | + if err != nil { |
| 71 | + t.Logf("List command failed. stdout: %s, stderr: %s", stdout, stderr) |
| 72 | + // If the command fails due to auth issues, skip the test |
| 73 | + if strings.Contains(stderr, "authentication") || strings.Contains(stderr, "token") { |
| 74 | + t.Skip("Skipping - authentication issue") |
| 75 | + } |
| 76 | + require.NoError(t, err, "List command should succeed with valid auth") |
| 77 | + } |
| 78 | + |
| 79 | + // Basic verification that list command produces expected output format |
| 80 | + require.NotEmpty(t, stdout, "List should produce output") |
| 81 | + // Should contain some indication of models or table headers |
| 82 | + lowerOut := strings.ToLower(stdout) |
| 83 | + hasExpectedContent := strings.Contains(lowerOut, "openai/gpt-4.1") |
| 84 | + require.True(t, hasExpectedContent, "List output should contain model information") |
| 85 | +} |
| 86 | + |
| 87 | +// TestRun tests the run command with a simple prompt |
| 88 | +// This test is more limited since it requires actual model inference |
| 89 | +func TestRun(t *testing.T) { |
| 90 | + stdout, _, err := runCommand(t, "run", "openai/gpt-4.1-nano", "say 'pain' in french") |
| 91 | + require.NoError(t, err, "Run should work") |
| 92 | + require.Contains(t, strings.ToLower(stdout), "pain") |
| 93 | +} |
| 94 | + |
| 95 | +// TestIntegrationRunWithOrg tests the run command with --org flag |
| 96 | +func TestRunWithOrg(t *testing.T) { |
| 97 | + // Test run command with --org flag (using help to avoid expensive API calls) |
| 98 | + stdout, _, err := runCommand(t, "run", "openai/gpt-4.1-nano", "say 'pain' in french", "--org", "github") |
| 99 | + require.NoError(t, err, "Run should work") |
| 100 | + require.Contains(t, strings.ToLower(stdout), "pain") |
| 101 | +} |
0 commit comments