-
Notifications
You must be signed in to change notification settings - Fork 2
test(hub): 覆盖 target-bound dispatch 不回退红线 #1741
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
+85
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
85 changes: 85 additions & 0 deletions
85
hub-server/internal/service/dispatchsvc/agent_dispatch_target_bound_test.go
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,85 @@ | ||
| package dispatchsvc | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/agenthub/hub-server/internal/config" | ||
| "github.com/agenthub/hub-server/internal/model" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestDispatchTargetBound_RouteUnavailable_QueuesTargetTask verifies the | ||
| // architecture red line: when the route for the target device is unavailable | ||
| // (offline / no WS connection), the task is pushed to the TARGET's offline | ||
| // queue (PushPendingTargetTask), NOT the inviter desktop queue | ||
| // (PushPendingTask). See docs/architecture/01-hub-server.md §Runtime And | ||
| // Team Routing: "must not silently fallback to another Desktop/Edge when a | ||
| // target-bound device is offline." | ||
| func TestDispatchTargetBound_RouteUnavailable_QueuesTargetTask(t *testing.T) { | ||
| cache := &recordingDispatchCache{ | ||
| routes: map[string]string{}, // no route for this device → empty connID | ||
| } | ||
| ws := &recordingDispatchWS{conn: nil} // no active conn | ||
|
|
||
| ds := NewDispatchService( | ||
| nil, // db — not reached on this failure path | ||
| nil, // bus | ||
| ws, // mgr | ||
| cache, // cacheClient | ||
| nil, // relay | ||
| nil, // outbox | ||
| config.EdgeDispatchConfig{}, | ||
| nil, // edgeClient | ||
| "", // jwtSecret | ||
| ) | ||
|
|
||
| task := &model.PendingAgentTask{ | ||
| ID: "task-target-1", | ||
| TargetID: "target-desktop-1", | ||
| } | ||
| userID := "user-1" | ||
| deviceID := "device-1" | ||
| payload := []byte(`{"task_id":"task-target-1"}`) | ||
|
|
||
| delivered := ds.dispatchTargetBoundTask(context.Background(), cache, task, userID, deviceID, payload) | ||
|
|
||
| assert.False(t, delivered, "target-bound task with unavailable route must not be delivered") | ||
| assert.Len(t, cache.pushed, 1, "must push to target queue exactly once") | ||
| assert.Contains(t, cache.pushed[0], userID, "push must include userID") | ||
| assert.Contains(t, cache.pushed[0], "target-desktop-1", "push must include targetID") | ||
| assert.Contains(t, cache.pushed[0], deviceID, "push must include deviceID") | ||
| assert.Contains(t, cache.pushed[0], string(payload), "push must include the original task JSON, not an altered copy") | ||
| assert.Equal(t, 0, ws.pushed, "must NOT push to WS conn (target is offline)") | ||
| } | ||
|
|
||
| // TestDispatchTargetBound_ConnMismatch_QueuesTargetTask verifies that when | ||
| // the device route returns a connID but no matching connection is found | ||
| // (stale route / conn dropped between lookup and dispatch), the task still | ||
| // goes to the target queue, not the inviter desktop queue. | ||
| func TestDispatchTargetBound_ConnMismatch_QueuesTargetTask(t *testing.T) { | ||
| cache := &recordingDispatchCache{ | ||
| routes: map[string]string{ | ||
| "user-1:desktop:device-1": "conn-stale-1", // route exists but conn is gone | ||
| }, | ||
| } | ||
| ws := &recordingDispatchWS{conn: nil} // FindByConnID returns nil | ||
|
|
||
| ds := NewDispatchService( | ||
| nil, nil, ws, cache, nil, nil, config.EdgeDispatchConfig{}, nil, "", | ||
| ) | ||
|
|
||
| task := &model.PendingAgentTask{ | ||
| ID: "task-target-2", | ||
| TargetID: "target-desktop-2", | ||
| } | ||
| payload := []byte(`{"task_id":"task-target-2"}`) | ||
|
|
||
| delivered := ds.dispatchTargetBoundTask(context.Background(), cache, task, "user-1", "device-1", payload) | ||
|
|
||
| assert.False(t, delivered, "target-bound task with stale conn must not be delivered") | ||
| assert.Len(t, cache.pushed, 1, "must push to target queue exactly once") | ||
| assert.Contains(t, cache.pushed[0], "target-desktop-2", "push must include targetID") | ||
| assert.Contains(t, cache.pushed[0], string(payload), "push must include the original task JSON, not an altered copy") | ||
| assert.Equal(t, 0, ws.pushed, "must NOT push to WS conn (conn not found)") | ||
| } | ||
Oops, something went wrong.
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.