Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/repository"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
)
|
||||
|
||||
func uintPtr(v uint) *uint { return &v }
|
||||
|
||||
func setupNotificationDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
assert.NoError(t, err)
|
||||
err = db.AutoMigrate(&model.Notification{}, &model.User{}, &model.Account{}, &model.NotificationPreference{})
|
||||
assert.NoError(t, err)
|
||||
return db
|
||||
}
|
||||
|
||||
func setupNotificationHandler(t *testing.T, db *gorm.DB) *NotificationHandler {
|
||||
t.Helper()
|
||||
notifRepo := repository.NewNotificationRepo(db)
|
||||
prefRepo := repository.NewNotificationPreferenceRepo(db)
|
||||
ns := service.NewNotificationService(db, notifRepo, prefRepo)
|
||||
return NewNotificationHandler(ns)
|
||||
}
|
||||
|
||||
func setupNotificationRouter(handler *NotificationHandler) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.GET("/api/v1/accounts/:account_id/notifications", handler.List)
|
||||
router.GET("/api/v1/accounts/:account_id/notifications/:id", handler.Get)
|
||||
router.POST("/api/v1/accounts/:account_id/notifications/read_all", handler.MarkAllRead)
|
||||
router.PATCH("/api/v1/accounts/:account_id/notifications/:id/read", handler.MarkRead)
|
||||
return router
|
||||
}
|
||||
|
||||
func TestNotificationList(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/notifications", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationListEmpty(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/notifications", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationGet(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/notifications/10", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code) // notification doesn't exist
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationGetDifferentID(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/5/notifications/42", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code) // notification doesn't exist
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationReadAll(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/notifications/read_all", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationMarkRead(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PATCH", "/api/v1/accounts/1/notifications/5/read", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code) // notification doesn't exist
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationCRUDWithDB(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
|
||||
user := &model.User{Name: "Notif User", Email: "notif@example.com", Password: "pass", AccountID: 1}
|
||||
db.Create(user)
|
||||
|
||||
// Create notification
|
||||
notif := &model.Notification{
|
||||
UserID: user.ID,
|
||||
AccountID: uintPtr(1),
|
||||
NotificationType: "conversation_assignment",
|
||||
PrimaryActorType: "conversation",
|
||||
PrimaryActorID: 5,
|
||||
SecondaryActorType: "user",
|
||||
SecondaryActorID: 3,
|
||||
}
|
||||
err := db.Create(notif).Error
|
||||
assert.NoError(t, err)
|
||||
assert.NotEqual(t, 0, notif.ID)
|
||||
|
||||
// Read notification
|
||||
var fetched model.Notification
|
||||
err = db.First(&fetched, notif.ID).Error
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "conversation_assignment", fetched.NotificationType)
|
||||
assert.Nil(t, fetched.ReadAt)
|
||||
|
||||
// Mark as read
|
||||
now := time.Now()
|
||||
err = db.Model(&fetched).Update("read_at", &now).Error
|
||||
assert.NoError(t, err)
|
||||
err = db.First(&fetched, notif.ID).Error
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, fetched.ReadAt)
|
||||
|
||||
// Delete notification
|
||||
err = db.Delete(&fetched).Error
|
||||
assert.NoError(t, err)
|
||||
err = db.First(&fetched, notif.ID).Error
|
||||
assert.Error(t, err)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationReadAllWithDB(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
|
||||
user := &model.User{Name: "Notif User2", Email: "notif2@example.com", Password: "pass", AccountID: 1}
|
||||
db.Create(user)
|
||||
|
||||
// Create multiple unread notifications
|
||||
for i := 0; i < 5; i++ {
|
||||
notif := &model.Notification{
|
||||
UserID: user.ID,
|
||||
AccountID: uintPtr(1),
|
||||
NotificationType: "message_created",
|
||||
PrimaryActorType: "message",
|
||||
PrimaryActorID: uint(i + 1),
|
||||
}
|
||||
err := db.Create(notif).Error
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
// Verify all are unread
|
||||
var unreadCount int64
|
||||
db.Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", user.ID).Count(&unreadCount)
|
||||
assert.Equal(t, int64(5), unreadCount)
|
||||
|
||||
// Mark all as read
|
||||
now := time.Now()
|
||||
db.Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", user.ID).Update("read_at", &now)
|
||||
|
||||
// Verify all are read
|
||||
db.Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", user.ID).Count(&unreadCount)
|
||||
assert.Equal(t, int64(0), unreadCount)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationTypes(t *testing.T) {
|
||||
types := []string{
|
||||
"conversation_assignment",
|
||||
"conversation_created",
|
||||
"message_created",
|
||||
"assigned_conversation_new_message",
|
||||
"conversation_mention",
|
||||
}
|
||||
for _, nt := range types {
|
||||
notif := model.Notification{NotificationType: nt}
|
||||
assert.Equal(t, nt, notif.NotificationType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationModelFields(t *testing.T) {
|
||||
notif := model.Notification{
|
||||
UserID: 1,
|
||||
AccountID: uintPtr(2),
|
||||
NotificationType: "conversation_assignment",
|
||||
PrimaryActorType: "conversation",
|
||||
PrimaryActorID: 5,
|
||||
SecondaryActorType: "user",
|
||||
SecondaryActorID: 3,
|
||||
}
|
||||
assert.Equal(t, uint(1), notif.UserID)
|
||||
assert.Equal(t, uintPtr(2), notif.AccountID)
|
||||
assert.Equal(t, "conversation_assignment", notif.NotificationType)
|
||||
assert.Nil(t, notif.ReadAt)
|
||||
}
|
||||
|
||||
func TestNotificationPagination(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/notifications?page=1&per_page=20", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationListByAccount(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
|
||||
user1 := &model.User{Name: "User1", Email: "u1@example.com", Password: "pass", AccountID: 1}
|
||||
user2 := &model.User{Name: "User2", Email: "u2@example.com", Password: "pass", AccountID: 2}
|
||||
db.Create(user1)
|
||||
db.Create(user2)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
notif := &model.Notification{UserID: user1.ID, AccountID: uintPtr(1), NotificationType: "message_created", PrimaryActorType: "message", PrimaryActorID: uint(i + 1)}
|
||||
db.Create(notif)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
notif := &model.Notification{UserID: user2.ID, AccountID: uintPtr(2), NotificationType: "conversation_assignment", PrimaryActorType: "conversation", PrimaryActorID: uint(i + 1)}
|
||||
db.Create(notif)
|
||||
}
|
||||
|
||||
var account1Notifs []model.Notification
|
||||
err := db.Where("account_id = ?", 1).Find(&account1Notifs).Error
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 3, len(account1Notifs))
|
||||
|
||||
var account2Notifs []model.Notification
|
||||
err = db.Where("account_id = ?", 2).Find(&account2Notifs).Error
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 2, len(account2Notifs))
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationGetInvalidID(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
handler := setupNotificationHandler(t, db)
|
||||
router := setupNotificationRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/notifications/abc", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code) // invalid ID format
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationReadSpecificNotification(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
|
||||
user := &model.User{Name: "Specific User", Email: "specific@example.com", Password: "pass", AccountID: 1}
|
||||
db.Create(user)
|
||||
|
||||
notif := &model.Notification{UserID: user.ID, AccountID: uintPtr(1), NotificationType: "message_created", PrimaryActorType: "message", PrimaryActorID: 1}
|
||||
db.Create(notif)
|
||||
|
||||
// Read specific notification
|
||||
err := db.Model(notif).Update("ReadAt", time.Now()).Error
|
||||
assert.NoError(t, err)
|
||||
|
||||
var fetched model.Notification
|
||||
db.First(&fetched, notif.ID)
|
||||
assert.NotNil(t, fetched.ReadAt)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationFilterByReadStatus(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
|
||||
user := &model.User{Name: "Filter User", Email: "filter@example.com", Password: "pass", AccountID: 1}
|
||||
db.Create(user)
|
||||
|
||||
// Create mix of read and unread
|
||||
for i := 0; i < 5; i++ {
|
||||
var readAt *time.Time
|
||||
if i%2 == 0 {
|
||||
readAt = &time.Time{}
|
||||
}
|
||||
notif := &model.Notification{UserID: user.ID, AccountID: uintPtr(1), NotificationType: "message_created", PrimaryActorType: "message", PrimaryActorID: uint(i + 1), ReadAt: readAt}
|
||||
db.Create(notif)
|
||||
}
|
||||
|
||||
var unread []model.Notification
|
||||
err := db.Where("user_id = ? AND read_at IS NULL", user.ID).Find(&unread).Error
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 2, len(unread)) // indices 1,3 are unread
|
||||
|
||||
var read []model.Notification
|
||||
err = db.Where("user_id = ? AND read_at IS NOT NULL", user.ID).Find(&read).Error
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 3, len(read)) // indices 0,2,4 are read
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
func TestNotificationSoftDelete(t *testing.T) {
|
||||
db := setupNotificationDB(t)
|
||||
|
||||
user := &model.User{Name: "SoftDel Notif User", Email: "softdel@example.com", Password: "pass", AccountID: 1}
|
||||
db.Create(user)
|
||||
|
||||
notif := &model.Notification{UserID: user.ID, AccountID: uintPtr(1), NotificationType: "message_created", PrimaryActorType: "message", PrimaryActorID: 1}
|
||||
db.Create(notif)
|
||||
|
||||
err := db.Delete(notif).Error
|
||||
assert.NoError(t, err)
|
||||
|
||||
var found model.Notification
|
||||
err = db.First(&found, notif.ID).Error
|
||||
assert.Error(t, err)
|
||||
|
||||
err = db.Unscoped().First(&found, notif.ID).Error
|
||||
assert.NoError(t, err)
|
||||
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user