@@ -1,476 +0,0 @@
1-package core
2-
3-import (
4- "path/filepath"
5- "testing"
6-
7- "github.com/xonecas/zoea-nova/internal/provider"
8- "github.com/xonecas/zoea-nova/internal/store"
9-)
10-
11-// setupAccountTest creates a test store with accounts for testing.
12-func setupAccountTest(t *testing.T) (*store.Store, func()) {
13- t.Helper()
14-
15- tmpDir := t.TempDir()
16- dbPath := filepath.Join(tmpDir, "test.db")
17- s, err := store.Open(dbPath)
18- if err != nil {
19- t.Fatalf("Open() error: %v", err)
20- }
21-
22- // Add test accounts
23- accounts := []struct {
24- username string
25- password string
26- }{
27- {"user1", "pass1"},
28- {"user2", "pass2"},
29- {"user3", "pass3"},
30- }
31-
32- for _, acc := range accounts {
33- if _, err := s.CreateAccount(acc.username, acc.password); err != nil {
34- t.Fatalf("CreateAccount(%s) error: %v", acc.username, err)
35- }
36- }
37-
38- cleanup := func() {
39- s.Close()
40- }
41-
42- return s, cleanup
43-}
44-
45-func createTestMysis(t *testing.T, s *store.Store, name string) *store.Mysis {
46- t.Helper()
47- mysis, err := s.CreateMysis(name, "mock", "test-model", 0.7)
48- if err != nil {
49- t.Fatalf("CreateMysis() error: %v", err)
50- }
51- return mysis
52-}
53-
54-// TestMysis_AccountLifecycle tests the full account claim/release lifecycle.
55-func TestMysis_AccountLifecycle(t *testing.T) {
56- s, cleanup := setupAccountTest(t)
57- defer cleanup()
58-
59- mysis := createTestMysis(t, s, "test-mysis")
60-
61- m := &Mysis{
62- id: mysis.ID,
63- name: "test",
64- store: s,
65- }
66-
67- // Initially no account
68- if m.CurrentAccountUsername() != "" {
69- t.Errorf("Expected no account initially, got %s", m.CurrentAccountUsername())
70- }
71-
72- // Claim account
73- username := "user1"
74- m.setCurrentAccount(username, "", "")
75-
76- if m.CurrentAccountUsername() != username {
77- t.Errorf("Expected username %s, got %s", username, m.CurrentAccountUsername())
78- }
79-
80- // Verify account marked as in use in store
81- acc, err := s.GetAccount(username)
82- if err != nil {
83- t.Fatalf("GetAccount() error: %v", err)
84- }
85- if !acc.InUse {
86- t.Error("Expected account marked as in use")
87- }
88-
89- // Release account
90- m.releaseCurrentAccount()
91-
92- if m.CurrentAccountUsername() != "" {
93- t.Errorf("Expected no account after release, got %s", m.CurrentAccountUsername())
94- }
95-
96- // Verify account marked as available in store
97- acc, err = s.GetAccount(username)
98- if err != nil {
99- t.Fatalf("GetAccount() error after release: %v", err)
100- }
101- if acc.InUse {
102- t.Error("Expected account marked as available after release")
103- }
104-}
105-
106-// TestMysis_AccountSwitching tests switching between accounts.
107-func TestMysis_AccountSwitching(t *testing.T) {
108- s, cleanup := setupAccountTest(t)
109- defer cleanup()
110-
111- mysis := createTestMysis(t, s, "test-mysis")
112-
113- m := &Mysis{
114- id: mysis.ID,
115- name: "test",
116- store: s,
117- }
118-
119- // Set first account
120- m.setCurrentAccount("user1", "", "")
121-
122- if m.CurrentAccountUsername() != "user1" {
123- t.Errorf("Expected user1, got %s", m.CurrentAccountUsername())
124- }
125-
126- // Verify first account in use
127- acc1, err := s.GetAccount("user1")
128- if err != nil {
129- t.Fatalf("GetAccount(user1) error: %v", err)
130- }
131- if !acc1.InUse {
132- t.Error("Expected user1 marked as in use")
133- }
134-
135- // Switch to second account
136- m.setCurrentAccount("user2", "", "")
137-
138- if m.CurrentAccountUsername() != "user2" {
139- t.Errorf("Expected user2 after switch, got %s", m.CurrentAccountUsername())
140- }
141-
142- // Verify first account released
143- acc1, err = s.GetAccount("user1")
144- if err != nil {
145- t.Fatalf("GetAccount(user1) error after switch: %v", err)
146- }
147- if acc1.InUse {
148- t.Error("Expected user1 released after switch")
149- }
150-
151- // Verify second account in use
152- acc2, err := s.GetAccount("user2")
153- if err != nil {
154- t.Fatalf("GetAccount(user2) error: %v", err)
155- }
156- if !acc2.InUse {
157- t.Error("Expected user2 marked as in use")
158- }
159-}
160-
161-// TestMysis_AccountSwitchToSame tests switching to the same account (no-op).
162-func TestMysis_AccountSwitchToSame(t *testing.T) {
163- s, cleanup := setupAccountTest(t)
164- defer cleanup()
165-
166- mysis := createTestMysis(t, s, "test-mysis")
167-
168- m := &Mysis{
169- id: mysis.ID,
170- name: "test",
171- store: s,
172- }
173-
174- // Set account
175- m.setCurrentAccount("user1", "", "")
176-
177- // Switch to same account
178- m.setCurrentAccount("user1", "", "")
179-
180- if m.CurrentAccountUsername() != "user1" {
181- t.Errorf("Expected user1, got %s", m.CurrentAccountUsername())
182- }
183-
184- // Verify account still in use (only once)
185- acc, err := s.GetAccount("user1")
186- if err != nil {
187- t.Fatalf("GetAccount() error: %v", err)
188- }
189- if !acc.InUse {
190- t.Error("Expected user1 still marked as in use")
191- }
192-}
193-
194-// TestMysis_SetCurrentAccount_EmptyString tests setting empty username.
195-func TestMysis_SetCurrentAccount_EmptyString(t *testing.T) {
196- s, cleanup := setupAccountTest(t)
197- defer cleanup()
198-
199- mysis := createTestMysis(t, s, "test-mysis")
200-
201- m := &Mysis{
202- id: mysis.ID,
203- name: "test",
204- store: s,
205- currentAccountUsername: "user1",
206- }
207-
208- // Set to empty string (should be no-op per code)
209- m.setCurrentAccount("", "", "")
210-
211- // Verify username unchanged (empty string is ignored)
212- if m.CurrentAccountUsername() != "user1" {
213- t.Errorf("Expected user1 unchanged, got %s", m.CurrentAccountUsername())
214- }
215-}
216-
217-// TestMysis_ReleaseCurrentAccount_NoAccount tests releasing when no account is set.
218-func TestMysis_ReleaseCurrentAccount_NoAccount(t *testing.T) {
219- s, cleanup := setupAccountTest(t)
220- defer cleanup()
221-
222- mysis := createTestMysis(t, s, "test-mysis")
223-
224- m := &Mysis{
225- id: mysis.ID,
226- name: "test",
227- store: s,
228- }
229-
230- // Release when no account (should be no-op)
231- m.releaseCurrentAccount()
232-
233- if m.CurrentAccountUsername() != "" {
234- t.Errorf("Expected no account, got %s", m.CurrentAccountUsername())
235- }
236-}
237-
238-// TestMysis_ReleaseCurrentAccount_Twice tests releasing same account twice.
239-func TestMysis_ReleaseCurrentAccount_Twice(t *testing.T) {
240- s, cleanup := setupAccountTest(t)
241- defer cleanup()
242-
243- mysis := createTestMysis(t, s, "test-mysis")
244-
245- m := &Mysis{
246- id: mysis.ID,
247- name: "test",
248- store: s,
249- }
250-
251- m.setCurrentAccount("user1", "", "")
252- m.releaseCurrentAccount()
253-
254- // Release again (should be no-op)
255- m.releaseCurrentAccount()
256-
257- if m.CurrentAccountUsername() != "" {
258- t.Errorf("Expected no account after double release, got %s", m.CurrentAccountUsername())
259- }
260-
261- // Verify account still available
262- acc, err := s.GetAccount("user1")
263- if err != nil {
264- t.Fatalf("GetAccount() error: %v", err)
265- }
266- if acc.InUse {
267- t.Error("Expected user1 available after release")
268- }
269-}
270-
271-// TestMysis_AccountConcurrent tests thread safety of account operations.
272-func TestMysis_AccountConcurrent(t *testing.T) {
273- s, cleanup := setupAccountTest(t)
274- defer cleanup()
275-
276- mysis := createTestMysis(t, s, "test-mysis")
277-
278- m := &Mysis{
279- id: mysis.ID,
280- name: "test",
281- store: s,
282- }
283-
284- done := make(chan bool)
285-
286- // Run multiple goroutines switching accounts
287- accounts := []string{"user1", "user2", "user3"}
288- for i := 0; i < 10; i++ {
289- go func(idx int) {
290- username := accounts[idx%len(accounts)]
291- m.setCurrentAccount(username, "", "")
292- _ = m.CurrentAccountUsername()
293- done <- true
294- }(i)
295- }
296-
297- // Wait for all goroutines
298- for i := 0; i < 10; i++ {
299- <-done
300- }
301-
302- // No assertions needed - just verify no data races (run with -race)
303-}
304-
305-// TestMysis_Stop_ReleasesAccount tests that Stop() releases the current account.
306-func TestMysis_Stop_ReleasesAccount(t *testing.T) {
307- s, cleanup := setupAccountTest(t)
308- defer cleanup()
309-
310- // Create stored mysis
311- stored, err := s.CreateMysis("test-mysis", "mock", "test-model", 0.7)
312- if err != nil {
313- t.Fatalf("CreateMysis() error: %v", err)
314- }
315-
316- bus := NewEventBus(100)
317- defer bus.Close()
318-
319- // Use mock provider
320- mock := provider.NewMock("mock", "test response")
321- m := NewMysis(stored.ID, stored.Name, stored.CreatedAt, mock, s, bus, "")
322-
323- // Start the mysis so it enters Running state
324- if err := m.Start(); err != nil {
325- t.Fatalf("Start() error: %v", err)
326- }
327-
328- // Set account after starting
329- m.setCurrentAccount("user1", "", "")
330-
331- // Verify account in use
332- acc, err := s.GetAccount("user1")
333- if err != nil {
334- t.Fatalf("GetAccount() error: %v", err)
335- }
336- if !acc.InUse {
337- t.Error("Expected user1 marked as in use before stop")
338- }
339-
340- // Stop mysis (should release account)
341- if err := m.Stop(); err != nil {
342- t.Fatalf("Stop() error: %v", err)
343- }
344-
345- // Verify account released
346- acc, err = s.GetAccount("user1")
347- if err != nil {
348- t.Fatalf("GetAccount() error after stop: %v", err)
349- }
350- if acc.InUse {
351- t.Error("Expected user1 released after stop")
352- }
353-
354- // Verify mysis has no account
355- if m.CurrentAccountUsername() != "" {
356- t.Errorf("Expected no account after stop, got %s", m.CurrentAccountUsername())
357- }
358-}
359-
360-// TestMysis_CurrentAccountUsername_ThreadSafe tests thread safety of account getter.
361-func TestMysis_CurrentAccountUsername_ThreadSafe(t *testing.T) {
362- s, cleanup := setupAccountTest(t)
363- defer cleanup()
364-
365- mysis := createTestMysis(t, s, "test-mysis")
366-
367- m := &Mysis{
368- id: mysis.ID,
369- name: "test",
370- store: s,
371- currentAccountUsername: "user1",
372- }
373-
374- done := make(chan bool)
375-
376- // Run multiple goroutines reading account
377- for i := 0; i < 10; i++ {
378- go func() {
379- _ = m.CurrentAccountUsername()
380- done <- true
381- }()
382- }
383-
384- // Run multiple goroutines writing account
385- for i := 0; i < 10; i++ {
386- go func(idx int) {
387- accounts := []string{"user1", "user2", "user3"}
388- m.setCurrentAccount(accounts[idx%len(accounts)], "", "")
389- done <- true
390- }(i)
391- }
392-
393- // Wait for all goroutines
394- for i := 0; i < 20; i++ {
395- <-done
396- }
397-
398- // No assertions needed - just verify no data races (run with -race)
399-}
400-
401-// TestMysis_MultipleMyses_SeparateAccounts tests multiple myses with different accounts.
402-func TestMysis_MultipleMyses_SeparateAccounts(t *testing.T) {
403- s, cleanup := setupAccountTest(t)
404- defer cleanup()
405-
406- // Create actual mysis records in database
407- stored1, err := s.CreateMysis("mysis1", "mock", "test-model", 0.7)
408- if err != nil {
409- t.Fatalf("CreateMysis(mysis1) error: %v", err)
410- }
411- stored2, err := s.CreateMysis("mysis2", "mock", "test-model", 0.7)
412- if err != nil {
413- t.Fatalf("CreateMysis(mysis2) error: %v", err)
414- }
415-
416- m1 := &Mysis{
417- id: stored1.ID,
418- name: "mysis1",
419- store: s,
420- }
421-
422- m2 := &Mysis{
423- id: stored2.ID,
424- name: "mysis2",
425- store: s,
426- }
427-
428- // Each mysis claims different account
429- m1.setCurrentAccount("user1", "", "")
430- m2.setCurrentAccount("user2", "", "")
431-
432- if m1.CurrentAccountUsername() != "user1" {
433- t.Errorf("Expected mysis1 to have user1, got %s", m1.CurrentAccountUsername())
434- }
435-
436- if m2.CurrentAccountUsername() != "user2" {
437- t.Errorf("Expected mysis2 to have user2, got %s", m2.CurrentAccountUsername())
438- }
439-
440- // Verify both accounts in use
441- acc1, err := s.GetAccount("user1")
442- if err != nil {
443- t.Fatalf("GetAccount(user1) error: %v", err)
444- }
445- if !acc1.InUse {
446- t.Error("Expected user1 in use by mysis1")
447- }
448-
449- acc2, err := s.GetAccount("user2")
450- if err != nil {
451- t.Fatalf("GetAccount(user2) error: %v", err)
452- }
453- if !acc2.InUse {
454- t.Error("Expected user2 in use by mysis2")
455- }
456-
457- // Release mysis1 account
458- m1.releaseCurrentAccount()
459-
460- // Verify only user1 released
461- acc1, err = s.GetAccount("user1")
462- if err != nil {
463- t.Fatalf("GetAccount(user1) error after release: %v", err)
464- }
465- if acc1.InUse {
466- t.Error("Expected user1 released")
467- }
468-
469- acc2, err = s.GetAccount("user2")
470- if err != nil {
471- t.Fatalf("GetAccount(user2) error after release: %v", err)
472- }
473- if !acc2.InUse {
474- t.Error("Expected user2 still in use by mysis2")
475- }
476-}