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.
325 lines
10 KiB
Go
325 lines
10 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
func createTestMessage(t *testing.T, db *gorm.DB, conversationID, accountID, inboxID uint, content string) *model.Message {
|
|
t.Helper()
|
|
msg := &model.Message{
|
|
ConversationID: conversationID,
|
|
AccountID: accountID,
|
|
InboxID: inboxID,
|
|
SenderType: "contact",
|
|
Content: content,
|
|
ContentType: "text",
|
|
MessageType: "incoming",
|
|
}
|
|
require.NoError(t, db.Create(msg).Error)
|
|
return msg
|
|
}
|
|
|
|
func TestMessageRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgTestOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgTestInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgTestContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
msg := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "Hello world")
|
|
|
|
found, err := repo.FindByID(context.Background(), msg.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, msg.ID, found.ID)
|
|
assert.Equal(t, "Hello world", found.Content)
|
|
}
|
|
|
|
func TestMessageRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestMessageRepo_FindByConversation(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgConvOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgConvInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgConvContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "msg"+string(rune('A'+i)))
|
|
}
|
|
|
|
messages, total, err := repo.FindByConversation(context.Background(), conv.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(5), total)
|
|
assert.Len(t, messages, 5)
|
|
}
|
|
|
|
func TestMessageRepo_FindByConversation_Pagination(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgPagOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgPagInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgPagContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "msg")
|
|
}
|
|
|
|
messages, total, err := repo.FindByConversation(context.Background(), conv.ID, 5, 3)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(10), total)
|
|
assert.Len(t, messages, 3)
|
|
}
|
|
|
|
func TestMessageRepo_FindByConversation_Empty(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
messages, total, err := repo.FindByConversation(context.Background(), 1, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), total)
|
|
assert.Len(t, messages, 0)
|
|
}
|
|
|
|
func TestMessageRepo_FindByAccountAndID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgAcctOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgAcctInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgAcctContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
msg := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "Scoped msg")
|
|
|
|
found, err := repo.FindByAccountAndID(context.Background(), account.ID, msg.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, msg.ID, found.ID)
|
|
}
|
|
|
|
func TestMessageRepo_FindByAccountAndID_WrongAccount(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgAcctWrongOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgAcctWrongInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgAcctWrongContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
msg := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "Scoped msg")
|
|
|
|
found, err := repo.FindByAccountAndID(context.Background(), 9999, msg.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestMessageRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
msg := &model.Message{
|
|
ConversationID: 1,
|
|
AccountID: 1,
|
|
InboxID: 1,
|
|
SenderType: "contact",
|
|
Content: "New message",
|
|
ContentType: "text",
|
|
MessageType: "incoming",
|
|
}
|
|
|
|
err := repo.Create(context.Background(), msg)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, msg.ID)
|
|
}
|
|
|
|
func TestMessageRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgUpdateOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgUpdateInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgUpdateContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
msg := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "Original")
|
|
msg.Content = "Updated"
|
|
err := repo.Update(context.Background(), msg)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), msg.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "Updated", found.Content)
|
|
}
|
|
|
|
func TestMessageRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgDeleteOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgDeleteInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgDeleteContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
msg := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "Delete me")
|
|
|
|
err := repo.Delete(context.Background(), msg.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), msg.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestMessageRepo_CountByConversation(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgCountOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgCountInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgCountContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "count msg")
|
|
}
|
|
|
|
total, err := repo.CountByConversation(context.Background(), conv.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(5), total)
|
|
}
|
|
|
|
func TestMessageRepo_FindLastByConversation(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
account := &model.Account{Name: "MsgLastOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgLastInbox", ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact := &model.Contact{AccountID: account.ID, Name: "MsgLastContact"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
conv := &model.Conversation{
|
|
AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID,
|
|
Status: "open", ChannelType: "web_widget", Channel: "web_widget",
|
|
}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
msg1 := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "First message")
|
|
msg2 := createTestMessage(t, db, conv.ID, account.ID, inbox.ID, "Last message")
|
|
|
|
last, err := repo.FindLastByConversation(context.Background(), conv.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, msg2.ID, last.ID)
|
|
assert.Greater(t, msg2.ID, msg1.ID)
|
|
}
|
|
|
|
func TestMessageRepo_FindLastByConversation_Empty(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewMessageRepo(db)
|
|
|
|
last, err := repo.FindLastByConversation(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, last)
|
|
} |