-
Notifications
You must be signed in to change notification settings - Fork 0
feat(nubi): add single query (-q/--query) and --async modes #108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d34e18e
feat(nubi): add single query (-q/--query) and --async modes, cleanup …
blue4209211 779c21e
fix(nubi): validate --async requires --query and pass active context …
blue4209211 0b3598f
refactor(nubi): retrieve flags dynamically from command context inste…
blue4209211 01cdbbe
fix(nubi): add WAITING status notice in single query mode and use t.C…
blue4209211 5b662a0
fix(nubi): use errors.Is for context check, set hermetic viper userna…
blue4209211 b5f3bbb
fix(nubi): remove redundant cancel assignment and format resume comma…
blue4209211 d89bb74
fix(nubi): trim query whitespace, return context error on cancellatio…
blue4209211 8014652
fix(nubi): use ConversationID in GetUsageMetrics and deduplicate lipg…
blue4209211 78abb12
fix(nubi): prevent interactive fallback on empty query flag and resto…
blue4209211 a5c3d2d
fix(nubi): simplify signal handler goroutine to call cancel() and ret…
blue4209211 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/nudgebee/nbctl/pkg/testutil" | ||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func resetNubiFlags() { | ||
| if f := nubiCmd.Flags().Lookup("query"); f != nil { | ||
| _ = f.Value.Set("") | ||
| f.Changed = false | ||
| } | ||
| if f := nubiCmd.Flags().Lookup("async"); f != nil { | ||
| _ = f.Value.Set("false") | ||
| f.Changed = false | ||
| } | ||
| viper.Set("username", "") | ||
| } | ||
|
|
||
| func TestNubiCmd_AsyncQuery(t *testing.T) { | ||
| resetNubiFlags() | ||
| viper.Set("username", "test-user") | ||
| t.Cleanup(resetNubiFlags) | ||
|
|
||
|
blue4209211 marked this conversation as resolved.
|
||
| mockResponse := map[string]interface{}{ | ||
| "ai_execute_investigation": map[string]interface{}{ | ||
| "data": map[string]interface{}{ | ||
| "response": "ok", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| output, err := testutil.RunWithSimpleGraphQL(mockResponse, nubiCmd, []string{"nubi", "test-account-id", "-q", "hello", "--async"}) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Contains(t, output, "Investigation triggered asynchronously.") | ||
| assert.Contains(t, output, "Session ID:") | ||
| } | ||
|
|
||
| func TestNubiCmd_AsyncWithoutQuery(t *testing.T) { | ||
| resetNubiFlags() | ||
| viper.Set("username", "test-user") | ||
| t.Cleanup(resetNubiFlags) | ||
|
|
||
|
blue4209211 marked this conversation as resolved.
|
||
| _, err := testutil.RunWithSimpleGraphQL(nil, nubiCmd, []string{"nubi", "test-account-id", "--async"}) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "--async requires --query / -q") | ||
| } | ||
|
blue4209211 marked this conversation as resolved.
|
||
|
|
||
| func TestNubiCmd_EmptyQuery(t *testing.T) { | ||
| resetNubiFlags() | ||
| viper.Set("username", "test-user") | ||
| t.Cleanup(resetNubiFlags) | ||
|
|
||
| _, err := testutil.RunWithSimpleGraphQL(nil, nubiCmd, []string{"nubi", "test-account-id", "-q", " "}) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "query cannot be empty") | ||
| } | ||
|
|
||
| func TestNubiCmd_SyncQuery(t *testing.T) { | ||
| resetNubiFlags() | ||
| viper.Set("username", "test-user") | ||
| t.Cleanup(resetNubiFlags) | ||
|
|
||
| handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| switch r.URL.Path { | ||
| case "/api/auth/token": | ||
| _ = json.NewEncoder(w).Encode(map[string]any{"token": "fake-token", "expiry": 3600}) | ||
| case "/api/graphql": | ||
| resp := map[string]interface{}{ | ||
| "data": map[string]interface{}{ | ||
| "ai_execute_investigation": map[string]interface{}{ | ||
| "data": map[string]interface{}{ | ||
| "response": "started", | ||
| }, | ||
| }, | ||
| "ai_get_conversation_v3": map[string]interface{}{ | ||
| "conversation": map[string]interface{}{ | ||
| "id": "conv-123", | ||
| "status": "COMPLETED", | ||
| }, | ||
| "messages": []map[string]interface{}{ | ||
| { | ||
| "id": "msg-1", | ||
| "status": "COMPLETED", | ||
| "response": "System status is healthy", | ||
| "message_type": "generation", | ||
| }, | ||
| }, | ||
| }, | ||
| "ai_get_conversation_usage_metrics": map[string]interface{}{ | ||
| "data": map[string]interface{}{ | ||
| "conversation": map[string]interface{}{ | ||
| "total_cost": 0.001, | ||
| "total_input_tokens": 50, | ||
| "total_output_tokens": 100, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| _ = json.NewEncoder(w).Encode(resp) | ||
| default: | ||
| http.NotFound(w, r) | ||
| } | ||
| }) | ||
|
|
||
| defaults := map[string]any{ | ||
| "api-key": "dummy", | ||
| "username": "dummy-user", | ||
| "account-id": "dummy-account", | ||
| } | ||
| output, err := testutil.RunWithMockServer(handler, defaults, nubiCmd, []string{"nubi", "test-account-id", "-q", "system status"}) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Contains(t, output, "System status") | ||
| assert.Contains(t, output, "healthy") | ||
| assert.Contains(t, output, "Cost: $0.001000") | ||
| assert.Contains(t, output, "Response time:") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.