mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-06-29 22:07:12 +00:00
B
This commit is contained in:
+28
-12
@@ -65,21 +65,23 @@ func TestHandleUpdate_NewChat(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
// userID 123 is the configured owner; any other ID is a regular user.
|
||||||
userID int64
|
userID int64
|
||||||
isOwner bool
|
// wantSubstr must appear in both the Telegram-sent text and the DB-stored
|
||||||
wantResp string
|
// response. Owners (model:set scope) see the raw API error; regular users
|
||||||
|
// get the generic fallback. Substring (not exact) so the test stays robust
|
||||||
|
// against the SDK's evolving error wording for non-API errors.
|
||||||
|
wantSubstr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Owner First Message",
|
name: "Owner First Message",
|
||||||
userID: 123, // owner's ID
|
userID: 123,
|
||||||
isOwner: true,
|
wantSubstr: "Anthropic call failed:",
|
||||||
wantResp: "I'm sorry, I'm having trouble processing your request right now.",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Regular User First Message",
|
name: "Regular User First Message",
|
||||||
userID: 456,
|
userID: 456,
|
||||||
isOwner: false,
|
wantSubstr: "I'm sorry, I'm having trouble processing your request right now.",
|
||||||
wantResp: "I'm sorry, I'm having trouble processing your request right now.",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +90,7 @@ func TestHandleUpdate_NewChat(t *testing.T) {
|
|||||||
// Setup mock response expectations for error case to test fallback messages
|
// Setup mock response expectations for error case to test fallback messages
|
||||||
mockTgClient.SendMessageFunc = func(ctx context.Context, params *bot.SendMessageParams) (*models.Message, error) {
|
mockTgClient.SendMessageFunc = func(ctx context.Context, params *bot.SendMessageParams) (*models.Message, error) {
|
||||||
assert.Equal(t, tc.userID, params.ChatID)
|
assert.Equal(t, tc.userID, params.ChatID)
|
||||||
assert.Equal(t, tc.wantResp, params.Text)
|
assert.Contains(t, params.Text, tc.wantSubstr)
|
||||||
return &models.Message{}, nil
|
return &models.Message{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,10 +114,13 @@ func TestHandleUpdate_NewChat(t *testing.T) {
|
|||||||
err := db.Where("chat_id = ? AND user_id = ? AND text = ?", tc.userID, tc.userID, "Hello").First(&storedMsg).Error
|
err := db.Where("chat_id = ? AND user_id = ? AND text = ?", tc.userID, tc.userID, "Hello").First(&storedMsg).Error
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Verify response was stored
|
// Verify response was stored (most recent assistant message in this chat).
|
||||||
var respMsg Message
|
var respMsg Message
|
||||||
err = db.Where("chat_id = ? AND is_user = ? AND text = ?", tc.userID, false, tc.wantResp).First(&respMsg).Error
|
err = db.Where("chat_id = ? AND is_user = ?", tc.userID, false).
|
||||||
|
Order("timestamp DESC").
|
||||||
|
First(&respMsg).Error
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, respMsg.Text, tc.wantSubstr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -720,11 +725,22 @@ func TestAnthropicErrorResponse(t *testing.T) { //NOSONAR go:S100 -- underscore
|
|||||||
wantMissing: "/set_model",
|
wantMissing: "/set_model",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "owner receives generic message for non-model error",
|
// Non-model errors (network, plain errors, API errors other than 404)
|
||||||
|
// surface to anyone with model:set scope so admins/owners can diagnose.
|
||||||
|
name: "owner receives elevated detail for non-API error",
|
||||||
err: otherErr,
|
err: otherErr,
|
||||||
userID: 123,
|
userID: 123,
|
||||||
|
wantSubstr: "Anthropic call failed:",
|
||||||
|
wantMissing: "I'm sorry",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Regular users keep getting the generic fallback for any non-model error
|
||||||
|
// to avoid leaking internal details.
|
||||||
|
name: "regular user receives generic message for non-model error",
|
||||||
|
err: otherErr,
|
||||||
|
userID: 789,
|
||||||
wantSubstr: "I'm sorry",
|
wantSubstr: "I'm sorry",
|
||||||
wantMissing: "/set_model",
|
wantMissing: "Anthropic call failed",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user