File size: 3,809 Bytes
8d3471e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package admin

import (
	"context"
	"net/http"
	"testing"

	"ds2api/internal/account"
	"ds2api/internal/auth"
	"ds2api/internal/config"
	dsclient "ds2api/internal/deepseek/client"
	adminaccounts "ds2api/internal/httpapi/admin/accounts"
	adminconfig "ds2api/internal/httpapi/admin/configmgmt"
	adminsettings "ds2api/internal/httpapi/admin/settings"
	adminshared "ds2api/internal/httpapi/admin/shared"
)

var intFrom = adminshared.IntFrom

func toAccount(m map[string]any) config.Account { return adminshared.ToAccount(m) }
func fieldString(m map[string]any, key string) string {
	return adminshared.FieldString(m, key)
}
func maskSecretPreview(secret string) string { return adminshared.MaskSecretPreview(secret) }
func boolFrom(v any) bool                    { return adminsettings.BoolFrom(v) }

func newAdminTestHandler(t *testing.T, raw string) *Handler {
	t.Helper()
	t.Setenv("DS2API_CONFIG_JSON", raw)
	store := config.LoadStore()
	return &Handler{
		Store: store,
		Pool:  account.NewPool(store),
	}
}

type testingDSMock struct {
	loginToken                 string
	deleteAllSessionsError     error
	deleteAllSessionsErrorOnce bool
	sessionCount               *dsclient.SessionStats
	loginCalls                 int
	deleteAllCalls             int
}

func (m *testingDSMock) Login(_ context.Context, _ config.Account) (string, error) {
	m.loginCalls++
	if m.loginToken == "" {
		return "token", nil
	}
	return m.loginToken, nil
}

func (m *testingDSMock) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) {
	return "session-id", nil
}

func (m *testingDSMock) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) {
	return "pow", nil
}

func (m *testingDSMock) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) {
	return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil
}

func (m *testingDSMock) DeleteAllSessionsForToken(_ context.Context, _ string) error {
	m.deleteAllCalls++
	if m.deleteAllSessionsError != nil {
		err := m.deleteAllSessionsError
		if m.deleteAllSessionsErrorOnce {
			m.deleteAllSessionsError = nil
		}
		return err
	}
	return nil
}

func (m *testingDSMock) GetSessionCountForToken(_ context.Context, _ string) (*dsclient.SessionStats, error) {
	if m.sessionCount != nil {
		return m.sessionCount, nil
	}
	return &dsclient.SessionStats{}, nil
}

func (h *Handler) configHandler() *adminconfig.Handler {
	return &adminconfig.Handler{Store: h.Store, Pool: h.Pool, DS: h.DS, OpenAI: h.OpenAI, ChatHistory: h.ChatHistory}
}

func (h *Handler) settingsHandler() *adminsettings.Handler {
	return &adminsettings.Handler{Store: h.Store, Pool: h.Pool, DS: h.DS, OpenAI: h.OpenAI, ChatHistory: h.ChatHistory}
}

func (h *Handler) getConfig(w http.ResponseWriter, r *http.Request) {
	h.configHandler().GetConfig(w, r)
}

func (h *Handler) updateConfig(w http.ResponseWriter, r *http.Request) {
	h.configHandler().UpdateConfig(w, r)
}

func (h *Handler) configImport(w http.ResponseWriter, r *http.Request) {
	h.configHandler().ConfigImport(w, r)
}

func (h *Handler) batchImport(w http.ResponseWriter, r *http.Request) {
	h.configHandler().BatchImport(w, r)
}

func (h *Handler) getSettings(w http.ResponseWriter, r *http.Request) {
	h.settingsHandler().GetSettings(w, r)
}

func (h *Handler) updateSettings(w http.ResponseWriter, r *http.Request) {
	h.settingsHandler().UpdateSettings(w, r)
}

func (h *Handler) updateSettingsPassword(w http.ResponseWriter, r *http.Request) {
	h.settingsHandler().UpdateSettingsPassword(w, r)
}

func runAccountTestsConcurrently(accounts []config.Account, maxConcurrency int, testFn func(int, config.Account) map[string]any) []map[string]any {
	return adminaccounts.RunAccountTestsConcurrently(accounts, maxConcurrency, testFn)
}