Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 1m40s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 42m16s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
299 lines
9.3 KiB
Go
299 lines
9.3 KiB
Go
package model
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRequestDetailsUserProjection(t *testing.T) {
|
|
other := NewLogOther()
|
|
other.SetPublic("model_ratio", 1)
|
|
other.SetAdmin("request_details", map[string]any{"session_id": "private-session", "correlation_key": "private-key"})
|
|
other.SetAdmin("request_policy", []any{map[string]any{"candidates": []string{"private-channel"}}})
|
|
log := &Log{CorrelationKey: "private-key", Other: other.JSONString()}
|
|
formatUserLogs([]*Log{log}, 0)
|
|
encoded, err := common.Marshal(log)
|
|
require.NoError(t, err)
|
|
require.NotContains(t, string(encoded), "private-")
|
|
require.NotContains(t, string(encoded), "request_details")
|
|
require.NotContains(t, string(encoded), "request_policy")
|
|
require.Contains(t, log.Other, "model_ratio")
|
|
}
|
|
|
|
func TestAstraRejectionUserProjectionShowsModelWithoutRoute(t *testing.T) {
|
|
other := NewLogOther()
|
|
other.SetPublic("error_code", "response_model_rejected")
|
|
other.SetAdmin("request_details", map[string]any{
|
|
"session_id": "private-session",
|
|
"model_guard": map[string]any{"attempts": []map[string]any{
|
|
{"expected_model": "gpt-6-astra", "returned_model": "gpt-5.6-luna", "reason": "model_mismatch", "channel_id": 1},
|
|
{"expected_model": "gpt-6-astra", "returned_model": "gpt-5.6-luna", "reason": "model_mismatch", "channel_id": 1},
|
|
}},
|
|
})
|
|
log := &Log{Other: other.JSONString()}
|
|
formatUserLogs([]*Log{log}, 0)
|
|
var projected map[string]any
|
|
require.NoError(t, common.UnmarshalJsonStr(log.Other, &projected))
|
|
assert.Equal(t, "model_mismatch", projected["model_guard_result"])
|
|
assert.Equal(t, "gpt-5.6-luna", projected["response_model"].(map[string]any)["returned_model"])
|
|
assert.NotContains(t, log.Other, "admin_info")
|
|
assert.NotContains(t, log.Other, "private-session")
|
|
assert.NotContains(t, log.Other, "channel_id")
|
|
|
|
other.SetAdmin("request_details", map[string]any{"model_guard": map[string]any{"attempts": []map[string]any{
|
|
{"expected_model": "gpt-6-astra", "reason": "confirmation_timeout", "channel_id": 1},
|
|
}}})
|
|
log.Other = other.JSONString()
|
|
formatUserLogs([]*Log{log}, 0)
|
|
projected = nil
|
|
require.NoError(t, common.UnmarshalJsonStr(log.Other, &projected))
|
|
assert.Equal(t, "confirmation_timeout", projected["model_guard_result"])
|
|
assert.NotContains(t, projected, "response_model")
|
|
|
|
other.SetPublic("error_code", "upstream_error")
|
|
log.Other = other.JSONString()
|
|
formatUserLogs([]*Log{log}, 0)
|
|
projected = nil
|
|
require.NoError(t, common.UnmarshalJsonStr(log.Other, &projected))
|
|
assert.NotContains(t, projected, "model_guard_result")
|
|
assert.NotContains(t, projected, "response_model")
|
|
}
|
|
|
|
// TestFormatUserLogsStripsQuotaSaturation verifies the admin-only quota
|
|
// saturation marker (nested under other.admin_info) is removed for non-admin
|
|
// log views, since formatUserLogs strips the whole admin_info object.
|
|
func TestFormatUserLogsStripsQuotaSaturation(t *testing.T) {
|
|
other := common.MapToJsonStr(map[string]any{
|
|
"model_price": 0.004,
|
|
"admin_info": map[string]any{
|
|
"quota_saturation": map[string]any{
|
|
"op": "QuotaFromDecimal",
|
|
"kind": "overflow",
|
|
"clamped": common.MaxQuota,
|
|
},
|
|
},
|
|
})
|
|
logs := []*Log{{Other: other}}
|
|
|
|
formatUserLogs(logs, 0)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
_, hasAdminInfo := parsed["admin_info"]
|
|
require.False(t, hasAdminInfo, "admin_info (and nested quota_saturation) must be stripped for non-admin views")
|
|
// Non-admin billing fields remain visible.
|
|
require.Contains(t, parsed, "model_price")
|
|
}
|
|
|
|
func TestTaskPluginLogVisibilityIsRoleSeparated(t *testing.T) {
|
|
other := common.MapToJsonStr(map[string]any{
|
|
"model_price": 1.25,
|
|
"admin_info": map[string]any{
|
|
"task_plugin": map[string]any{
|
|
"key": "document-parser",
|
|
"name": "Document Parser",
|
|
"version": "1.2.3",
|
|
},
|
|
},
|
|
"root_info": map[string]any{
|
|
"upstream_task_id": "upstream-private",
|
|
"task_plugin": map[string]any{
|
|
"generation": 42,
|
|
},
|
|
},
|
|
})
|
|
|
|
t.Run("user", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
formatUserLogs(logs, 0)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.NotContains(t, parsed, "admin_info")
|
|
assert.NotContains(t, parsed, "root_info")
|
|
assert.Equal(t, 1.25, parsed["model_price"])
|
|
})
|
|
|
|
t.Run("admin", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
FormatAdminLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, parsed, "admin_info")
|
|
assert.NotContains(t, parsed, "root_info")
|
|
})
|
|
|
|
t.Run("root", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
FormatRootLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, parsed, "admin_info")
|
|
assert.Contains(t, parsed, "root_info")
|
|
})
|
|
}
|
|
|
|
func TestLegacyLogOtherVisibilityIsRoleSeparated(t *testing.T) {
|
|
other := common.MapToJsonStr(map[string]any{
|
|
"request_path": "/v1/chat/completions",
|
|
"channel_id": 202,
|
|
"channel_name": "legacy-secret-channel",
|
|
"channel_type": 1,
|
|
"reject_reason": "legacy-policy-rejection",
|
|
"admin_info": map[string]any{
|
|
"existing_admin_field": "preserved",
|
|
},
|
|
"root_info": map[string]any{
|
|
"upstream_request_id": "upstream-private",
|
|
},
|
|
"audit_info": map[string]any{
|
|
"method": "POST",
|
|
},
|
|
})
|
|
|
|
t.Run("user", func(t *testing.T) {
|
|
logs := []*Log{{
|
|
Id: 99,
|
|
ChannelId: 77,
|
|
ChannelName: "resolved-secret-channel",
|
|
Other: other,
|
|
}}
|
|
|
|
formatUserLogs(logs, 10)
|
|
|
|
assert.Equal(t, 11, logs[0].Id)
|
|
assert.Equal(t, 77, logs[0].ChannelId)
|
|
assert.Empty(t, logs[0].ChannelName)
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "/v1/chat/completions", parsed["request_path"])
|
|
for _, key := range []string{
|
|
"channel_id",
|
|
"channel_name",
|
|
"channel_type",
|
|
"reject_reason",
|
|
"admin_info",
|
|
"root_info",
|
|
"audit_info",
|
|
} {
|
|
assert.NotContains(t, parsed, key)
|
|
}
|
|
})
|
|
|
|
t.Run("admin", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
|
|
FormatAdminLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "legacy-secret-channel", parsed["channel_name"])
|
|
assert.NotContains(t, parsed, "reject_reason")
|
|
assert.NotContains(t, parsed, "root_info")
|
|
assert.Contains(t, parsed, "audit_info")
|
|
adminInfo, ok := parsed["admin_info"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "preserved", adminInfo["existing_admin_field"])
|
|
assert.Equal(t, "legacy-policy-rejection", adminInfo["reject_reason"])
|
|
})
|
|
|
|
t.Run("root", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
|
|
FormatRootLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "legacy-secret-channel", parsed["channel_name"])
|
|
assert.NotContains(t, parsed, "reject_reason")
|
|
assert.Contains(t, parsed, "root_info")
|
|
assert.Contains(t, parsed, "audit_info")
|
|
adminInfo, ok := parsed["admin_info"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "preserved", adminInfo["existing_admin_field"])
|
|
assert.Equal(t, "legacy-policy-rejection", adminInfo["reject_reason"])
|
|
})
|
|
}
|
|
|
|
func TestLegacyRejectReasonDoesNotOverrideScopedValue(t *testing.T) {
|
|
other := common.MapToJsonStr(map[string]any{
|
|
"reject_reason": "legacy-value",
|
|
"admin_info": map[string]any{
|
|
"reject_reason": "scoped-value",
|
|
},
|
|
})
|
|
logs := []*Log{{Other: other}}
|
|
|
|
FormatRootLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.NotContains(t, parsed, "reject_reason")
|
|
adminInfo, ok := parsed["admin_info"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "scoped-value", adminInfo["reject_reason"])
|
|
}
|
|
|
|
func TestLegacyRejectReasonHandlesNullAdminInfo(t *testing.T) {
|
|
logs := []*Log{{Other: `{"reject_reason":"legacy-value","admin_info":null}`}}
|
|
|
|
FormatAdminLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.NotContains(t, parsed, "reject_reason")
|
|
adminInfo, ok := parsed["admin_info"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "legacy-value", adminInfo["reject_reason"])
|
|
}
|
|
|
|
func TestLogFormattingPreservesLargeIntegerLexemes(t *testing.T) {
|
|
const other = `{"public_id":9007199254740993,"admin_info":{"admin_id":9007199254740995},"root_info":{"generation":18446744073709551615}}`
|
|
|
|
t.Run("user", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
|
|
formatUserLogs(logs, 0)
|
|
|
|
assert.Contains(t, logs[0].Other, `"public_id":9007199254740993`)
|
|
assert.NotContains(t, logs[0].Other, "admin_id")
|
|
assert.NotContains(t, logs[0].Other, "generation")
|
|
})
|
|
|
|
t.Run("admin", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
|
|
FormatAdminLogs(logs)
|
|
|
|
assert.Contains(t, logs[0].Other, `"public_id":9007199254740993`)
|
|
assert.Contains(t, logs[0].Other, `"admin_id":9007199254740995`)
|
|
assert.NotContains(t, logs[0].Other, "generation")
|
|
})
|
|
|
|
t.Run("root", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
|
|
FormatRootLogs(logs)
|
|
|
|
assert.Equal(t, other, logs[0].Other)
|
|
})
|
|
|
|
t.Run("unprivileged", func(t *testing.T) {
|
|
const unprivileged = `{"public_id":9007199254740993,"model_price":0.004}`
|
|
|
|
userLogs := []*Log{{Other: unprivileged}}
|
|
formatUserLogs(userLogs, 0)
|
|
assert.Equal(t, unprivileged, userLogs[0].Other)
|
|
|
|
adminLogs := []*Log{{Other: unprivileged}}
|
|
FormatAdminLogs(adminLogs)
|
|
assert.Equal(t, unprivileged, adminLogs[0].Other)
|
|
})
|
|
}
|