168 lines
3.6 KiB
Go
168 lines
3.6 KiB
Go
package browser
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"verstak/internal/core/storage"
|
|
)
|
|
|
|
func setupDB(t *testing.T) *Store {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
db, err := storage.Open(filepath.Join(dir, "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { db.Close() })
|
|
|
|
return NewStore(db)
|
|
}
|
|
|
|
func TestInsertAndListPending(t *testing.T) {
|
|
s := setupDB(t)
|
|
|
|
events := []Event{
|
|
{ID: "e1", DeviceID: "dev-1", Type: "page_visit", URL: "https://example.com", Domain: "example.com", ActiveSeconds: 120},
|
|
{ID: "e2", DeviceID: "dev-1", Type: "page_visit", URL: "https://github.com", Domain: "github.com", ActiveSeconds: 300},
|
|
}
|
|
|
|
n, err := s.InsertEvents(events)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 2 {
|
|
t.Errorf("expected 2 inserted, got %d", n)
|
|
}
|
|
|
|
pending, err := s.ListPending(10, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(pending) != 2 {
|
|
t.Errorf("expected 2 pending, got %d", len(pending))
|
|
}
|
|
}
|
|
|
|
func TestInsertDuplicate(t *testing.T) {
|
|
s := setupDB(t)
|
|
|
|
ev := []Event{{ID: "e1", DeviceID: "dev-1", Type: "page_visit", URL: "https://example.com", Domain: "example.com"}}
|
|
n, err := s.InsertEvents(ev)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("expected 1 inserted, got %d", n)
|
|
}
|
|
|
|
// Same ID again
|
|
n, err = s.InsertEvents(ev)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 0 {
|
|
t.Errorf("expected 0 inserted (duplicate), got %d", n)
|
|
}
|
|
}
|
|
|
|
func TestAccept(t *testing.T) {
|
|
s := setupDB(t)
|
|
|
|
_, _ = s.InsertEvents([]Event{{ID: "e1", DeviceID: "d1", Type: "page_visit", URL: "https://example.com", Domain: "ex.com"}})
|
|
|
|
if err := s.Accept("e1", "wl-1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ev, err := s.Get("e1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ev.Status != "accepted" {
|
|
t.Errorf("expected status 'accepted', got %s", ev.Status)
|
|
}
|
|
if ev.WorklogID != "wl-1" {
|
|
t.Errorf("expected worklog_id 'wl-1', got %s", ev.WorklogID)
|
|
}
|
|
}
|
|
|
|
func TestAttach(t *testing.T) {
|
|
s := setupDB(t)
|
|
|
|
_, _ = s.InsertEvents([]Event{{ID: "e1", DeviceID: "d1", Type: "page_visit", URL: "https://example.com", Domain: "ex.com"}})
|
|
|
|
if err := s.Attach("e1", "node-1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ev, err := s.Get("e1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ev.Status != "attached" {
|
|
t.Errorf("expected status 'attached', got %s", ev.Status)
|
|
}
|
|
if ev.NodeID != "node-1" {
|
|
t.Errorf("expected node_id 'node-1', got %s", ev.NodeID)
|
|
}
|
|
}
|
|
|
|
func TestDismiss(t *testing.T) {
|
|
s := setupDB(t)
|
|
|
|
_, _ = s.InsertEvents([]Event{{ID: "e1", DeviceID: "d1", Type: "page_visit", URL: "https://example.com", Domain: "ex.com"}})
|
|
|
|
if err := s.Dismiss("e1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ev, err := s.Get("e1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ev.Status != "dismissed" {
|
|
t.Errorf("expected status 'dismissed', got %s", ev.Status)
|
|
}
|
|
}
|
|
|
|
func TestCountPending(t *testing.T) {
|
|
s := setupDB(t)
|
|
|
|
_, _ = s.InsertEvents([]Event{
|
|
{ID: "e1", DeviceID: "d1", Type: "page_visit", URL: "https://a.com", Domain: "a.com"},
|
|
{ID: "e2", DeviceID: "d1", Type: "page_visit", URL: "https://b.com", Domain: "b.com"},
|
|
})
|
|
|
|
count, err := s.CountPending()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 2 {
|
|
t.Errorf("expected 2 pending, got %d", count)
|
|
}
|
|
|
|
_ = s.Dismiss("e1")
|
|
count, _ = s.CountPending()
|
|
if count != 1 {
|
|
t.Errorf("expected 1 pending after dismiss, got %d", count)
|
|
}
|
|
}
|
|
|
|
func TestStoreFileAndDirPermissions(t *testing.T) {
|
|
// Verify DB file permissions match project conventions
|
|
dir := t.TempDir()
|
|
db, err := storage.Open(filepath.Join(dir, "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
info, err := os.Stat(filepath.Join(dir, "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("DB file mode: %o", info.Mode())
|
|
}
|