fix: address audit findings across websearch, notify, and channel pricing

Backend fixes:
- Fix balance notify ignoring percentage threshold type (was treating
  percentage value as fixed USD amount)
- Remove dead code parseJSONStringArray
- Add ImageOutputTokens to tryModelFilePricing calculation
- Unify zero-value check: cost == 0 → cost <= 0 in calculateTokenStatsCost
- Use MarshalNotifyEmails instead of json.Marshal for consistency
- Rename quotaDim.oldUsed → currentUsed for clarity
- Extract HTML email templates to const variables (function ≤30 lines)

Test fixes:
- Rewrite account_websearch_test.go for GetWebSearchEmulationMode tri-state
- Add 6 tryModelFilePricing test cases

Frontend fixes:
- Replace hardcoded '未命名' with i18n key
- Extract getBillingModeLabel/getBillingModeBadgeClass to shared utils
- Replace inline type with imported NotifyEmailEntry
- Pass platform to AccountStats pricing rules via inferRulePlatform()
- Add billing mode constants (BILLING_MODE_TOKEN/PER_REQUEST/IMAGE)
This commit is contained in:
erio
2026-04-13 12:07:09 +08:00
parent 1262654d97
commit a68df457d8
12 changed files with 275 additions and 121 deletions

View File

@@ -1,3 +1,5 @@
//go:build unit
package service
import (
@@ -6,66 +8,98 @@ import (
"github.com/stretchr/testify/require"
)
func TestAccount_IsWebSearchEmulationEnabled_Enabled(t *testing.T) {
func TestGetWebSearchEmulationMode_Enabled(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: "enabled"},
}
require.Equal(t, WebSearchModeEnabled, a.GetWebSearchEmulationMode())
}
func TestGetWebSearchEmulationMode_Disabled(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: "disabled"},
}
require.Equal(t, WebSearchModeDisabled, a.GetWebSearchEmulationMode())
}
func TestGetWebSearchEmulationMode_Default(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: "default"},
}
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestGetWebSearchEmulationMode_UnknownString(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: "unknown"},
}
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestGetWebSearchEmulationMode_OldBoolTrue(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: true},
}
require.True(t, a.IsWebSearchEmulationEnabled())
// bool is not a string, type assertion fails → default
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestAccount_IsWebSearchEmulationEnabled_Disabled(t *testing.T) {
func TestGetWebSearchEmulationMode_OldBoolFalse(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: false},
}
require.False(t, a.IsWebSearchEmulationEnabled())
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestAccount_IsWebSearchEmulationEnabled_MissingField(t *testing.T) {
func TestGetWebSearchEmulationMode_NilAccount(t *testing.T) {
var a *Account
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestGetWebSearchEmulationMode_NilExtra(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: nil,
}
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestGetWebSearchEmulationMode_MissingField(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{},
}
require.False(t, a.IsWebSearchEmulationEnabled())
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestAccount_IsWebSearchEmulationEnabled_WrongType(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: "true"},
}
require.False(t, a.IsWebSearchEmulationEnabled())
}
func TestAccount_IsWebSearchEmulationEnabled_NilExtra(t *testing.T) {
a := &Account{Platform: PlatformAnthropic, Type: AccountTypeAPIKey, Extra: nil}
require.False(t, a.IsWebSearchEmulationEnabled())
}
func TestAccount_IsWebSearchEmulationEnabled_NilAccount(t *testing.T) {
var a *Account
require.False(t, a.IsWebSearchEmulationEnabled())
}
func TestAccount_IsWebSearchEmulationEnabled_NonAnthropicPlatform(t *testing.T) {
func TestGetWebSearchEmulationMode_NonAnthropicPlatform(t *testing.T) {
a := &Account{
Platform: PlatformOpenAI,
Type: AccountTypeAPIKey,
Extra: map[string]any{featureKeyWebSearchEmulation: true},
Extra: map[string]any{featureKeyWebSearchEmulation: "enabled"},
}
require.False(t, a.IsWebSearchEmulationEnabled())
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}
func TestAccount_IsWebSearchEmulationEnabled_NonAPIKeyType(t *testing.T) {
func TestGetWebSearchEmulationMode_NonAPIKeyType(t *testing.T) {
a := &Account{
Platform: PlatformAnthropic,
Type: AccountTypeOAuth,
Extra: map[string]any{featureKeyWebSearchEmulation: true},
Extra: map[string]any{featureKeyWebSearchEmulation: "enabled"},
}
require.False(t, a.IsWebSearchEmulationEnabled())
require.Equal(t, WebSearchModeDefault, a.GetWebSearchEmulationMode())
}