File size: 1,476 Bytes
48511d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package db

import (
	"path/filepath"
	"testing"
	"time"

	"github.com/arpinfidel/p2p-llm/config"
)

func setupTestDB(t *testing.T) (*SQLiteDB, string) {
	// Create a temporary database file
	tempDir := t.TempDir()
	dbPath := filepath.Join(tempDir, "test.db")

	// Create test config
	cfg := &config.Config{
		DBPath: dbPath,
	}

	// Initialize database
	db, err := NewSQLiteDB(cfg)
	if err != nil {
		t.Fatalf("Failed to create test database: %v", err)
	}

	if err := db.Init(); err != nil {
		t.Fatalf("Failed to initialize test database: %v", err)
	}

	// Create tables
	if err := CreateTables(db); err != nil {
		t.Fatalf("Failed to create tables: %v", err)
	}

	return db, dbPath
}

func TestAPIKeyOperations(t *testing.T) {
	db, _ := setupTestDB(t)
	defer db.Close()

	repo := NewSQLiteAPIKeyRepository(db)

	// Test CreateKey
	expiresAt := time.Now().Add(24 * time.Hour)
	err := repo.CreateKey("testhash", "testkey", &expiresAt)
	if err != nil {
		t.Errorf("CreateKey failed: %v", err)
	}

	// Test GetActiveKeyHash
	hash, err := repo.GetActiveKeyHash()
	if err != nil {
		t.Errorf("GetActiveKeyHash failed: %v", err)
	}
	if hash != "testhash" {
		t.Errorf("Expected hash 'testhash', got '%s'", hash)
	}

	// Test DeactivateKey
	// Note: This would need to know the ID of the created key
	// For a complete test, we'd need to query the ID first
	// For now, we'll just verify the basic operations work
}