|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/MAX-API-Next/MAX-API/common" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func withLogAuditSettings(t *testing.T, requestEnabled bool, responseEnabled bool) { |
| 11 | + t.Helper() |
| 12 | + prevRequest := common.LogRequestContentEnabled |
| 13 | + prevResponse := common.LogResponseContentEnabled |
| 14 | + common.LogRequestContentEnabled = requestEnabled |
| 15 | + common.LogResponseContentEnabled = responseEnabled |
| 16 | + t.Cleanup(func() { |
| 17 | + common.LogRequestContentEnabled = prevRequest |
| 18 | + common.LogResponseContentEnabled = prevResponse |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +func TestFormatUserLogsExposesOnlyUserAuditContent(t *testing.T) { |
| 23 | + withLogAuditSettings(t, true, true) |
| 24 | + |
| 25 | + logs := []*Log{ |
| 26 | + { |
| 27 | + Other: common.MapToJsonStr(map[string]interface{}{ |
| 28 | + "admin_info": map[string]interface{}{ |
| 29 | + "request_content": "user prompt", |
| 30 | + "request_content_truncated": true, |
| 31 | + "response_content": "model answer", |
| 32 | + "response_content_truncated": true, |
| 33 | + "local_count_tokens": true, |
| 34 | + "use_channel": []int{1, 2}, |
| 35 | + }, |
| 36 | + "audit_info": map[string]interface{}{ |
| 37 | + "request_content": "stale audit content", |
| 38 | + }, |
| 39 | + "stream_status": map[string]interface{}{ |
| 40 | + "status": "error", |
| 41 | + }, |
| 42 | + }), |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + formatUserLogs(logs, 10) |
| 47 | + |
| 48 | + other, err := common.StrToMap(logs[0].Other) |
| 49 | + require.NoError(t, err) |
| 50 | + require.NotContains(t, other, "admin_info") |
| 51 | + require.NotContains(t, other, "stream_status") |
| 52 | + require.Equal(t, 11, logs[0].Id) |
| 53 | + |
| 54 | + auditInfo, ok := other["audit_info"].(map[string]interface{}) |
| 55 | + require.True(t, ok) |
| 56 | + require.Equal(t, "user prompt", auditInfo["request_content"]) |
| 57 | + require.Equal(t, true, auditInfo["request_content_truncated"]) |
| 58 | + require.Equal(t, "model answer", auditInfo["response_content"]) |
| 59 | + require.Equal(t, true, auditInfo["response_content_truncated"]) |
| 60 | + require.NotContains(t, auditInfo, "local_count_tokens") |
| 61 | + require.NotContains(t, auditInfo, "use_channel") |
| 62 | +} |
| 63 | + |
| 64 | +func TestFormatUserLogsHidesAuditContentWhenDisabled(t *testing.T) { |
| 65 | + withLogAuditSettings(t, false, false) |
| 66 | + |
| 67 | + logs := []*Log{ |
| 68 | + { |
| 69 | + Other: common.MapToJsonStr(map[string]interface{}{ |
| 70 | + "admin_info": map[string]interface{}{ |
| 71 | + "request_content": "user prompt", |
| 72 | + "response_content": "model answer", |
| 73 | + }, |
| 74 | + }), |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + formatUserLogs(logs, 0) |
| 79 | + |
| 80 | + other, err := common.StrToMap(logs[0].Other) |
| 81 | + require.NoError(t, err) |
| 82 | + require.NotContains(t, other, "admin_info") |
| 83 | + require.NotContains(t, other, "audit_info") |
| 84 | +} |
0 commit comments