- Add e2e tests for admin pages (archivers, extractors, rules) - Add tests for archives, links, navigation, and error handling - Add search and pagination test coverage - Implement test web server with Docker container - Add seed data and helper utilities for tests - Update webapp components for permission handling - Generate HTML reports with embedded screenshots Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
package e2eutil
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
// SeedTestLinks creates multiple test links for pagination/search tests.
|
|
// This is useful for tests that need multiple links to exist.
|
|
func SeedTestLinks(t *testing.T, dbPath string, count int, testServerURL string) error {
|
|
t.Helper()
|
|
|
|
db, err := sql.Open("sqlite3", dbPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
// Get default user ID (hako@hako.com)
|
|
var userID string
|
|
err = db.QueryRow("SELECT id FROM users WHERE email = ?", "hako@hako.com").Scan(&userID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get user ID: %w", err)
|
|
}
|
|
|
|
// Insert test links
|
|
stmt, err := db.Prepare("INSERT INTO links (id, url, user_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to prepare statement: %w", err)
|
|
}
|
|
defer stmt.Close()
|
|
|
|
now := time.Now()
|
|
for i := 0; i < count; i++ {
|
|
id := uuid.New().String()
|
|
url := fmt.Sprintf("%s/test-page-%d.html", testServerURL, i)
|
|
_, err = stmt.Exec(id, url, userID, now, now)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to insert link %d: %w", i, err)
|
|
}
|
|
}
|
|
|
|
t.Logf("Seeded %d test links", count)
|
|
return nil
|
|
}
|
|
|
|
// SeedTestCategories creates test categories.
|
|
func SeedTestCategories(t *testing.T, dbPath string) error {
|
|
t.Helper()
|
|
|
|
db, err := sql.Open("sqlite3", dbPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
categories := []struct {
|
|
name string
|
|
icon string
|
|
}{
|
|
{"Technology", "💻"},
|
|
{"News", "📰"},
|
|
{"Education", "📚"},
|
|
{"Entertainment", "🎬"},
|
|
}
|
|
|
|
stmt, err := db.Prepare("INSERT INTO categories (id, name, icon, created_at, updated_at) VALUES (?, ?, ?, ?, ?)")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to prepare statement: %w", err)
|
|
}
|
|
defer stmt.Close()
|
|
|
|
now := time.Now()
|
|
for _, cat := range categories {
|
|
id := uuid.New().String()
|
|
_, err = stmt.Exec(id, cat.name, cat.icon, now, now)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to insert category %s: %w", cat.name, err)
|
|
}
|
|
}
|
|
|
|
t.Logf("Seeded %d test categories", len(categories))
|
|
return nil
|
|
}
|
|
|
|
// SeedArchivedLinks creates links with completed archives.
|
|
func SeedArchivedLinks(t *testing.T, dbPath string, count int, testServerURL string) error {
|
|
t.Helper()
|
|
|
|
db, err := sql.Open("sqlite3", dbPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open database: %w", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
// Get default user ID
|
|
var userID string
|
|
err = db.QueryRow("SELECT id FROM users WHERE email = ?", "hako@hako.com").Scan(&userID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get user ID: %w", err)
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
// Insert links and archives
|
|
linkStmt, err := db.Prepare("INSERT INTO links (id, url, user_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to prepare link statement: %w", err)
|
|
}
|
|
defer linkStmt.Close()
|
|
|
|
archiveStmt, err := db.Prepare("INSERT INTO archives (id, link_id, user_id, status, title, created_at, completed_at) VALUES (?, ?, ?, ?, ?, ?, ?)")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to prepare archive statement: %w", err)
|
|
}
|
|
defer archiveStmt.Close()
|
|
|
|
for i := 0; i < count; i++ {
|
|
linkID := uuid.New().String()
|
|
url := fmt.Sprintf("%s/archived-page-%d.html", testServerURL, i)
|
|
|
|
// Insert link
|
|
_, err = linkStmt.Exec(linkID, url, userID, now, now)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to insert link %d: %w", i, err)
|
|
}
|
|
|
|
// Insert archive
|
|
archiveID := uuid.New().String()
|
|
title := fmt.Sprintf("Archived Page %d", i)
|
|
completedAt := now
|
|
_, err = archiveStmt.Exec(archiveID, linkID, userID, "completed", title, now, completedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to insert archive %d: %w", i, err)
|
|
}
|
|
}
|
|
|
|
t.Logf("Seeded %d archived links", count)
|
|
return nil
|
|
}
|