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.
250 lines
7.6 KiB
Go
250 lines
7.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// ========== AddLabel ==========
|
|
|
|
func TestConversationLabelRepo_AddLabel(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag := newTestTag(1, "urgent")
|
|
err := tagRepo.Create(context.Background(), tag)
|
|
require.NoError(t, err)
|
|
|
|
cl := &model.ConversationLabel{
|
|
ConversationID: 1,
|
|
TagID: tag.ID,
|
|
AccountID: 1,
|
|
}
|
|
err = repo.AddLabel(context.Background(), cl)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, cl.ID)
|
|
}
|
|
|
|
// ========== AddLabels ==========
|
|
|
|
func TestConversationLabelRepo_AddLabels(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag1 := newTestTag(1, "bug")
|
|
tag2 := newTestTag(1, "feature")
|
|
err := tagRepo.Create(context.Background(), tag1)
|
|
require.NoError(t, err)
|
|
err = tagRepo.Create(context.Background(), tag2)
|
|
require.NoError(t, err)
|
|
|
|
labels := []model.ConversationLabel{
|
|
{ConversationID: 1, TagID: tag1.ID, AccountID: 1},
|
|
{ConversationID: 1, TagID: tag2.ID, AccountID: 1},
|
|
}
|
|
err = repo.AddLabels(context.Background(), labels)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, labels[0].ID)
|
|
assert.NotZero(t, labels[1].ID)
|
|
}
|
|
|
|
// ========== RemoveLabel ==========
|
|
|
|
func TestConversationLabelRepo_RemoveLabel(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag := newTestTag(1, "spam")
|
|
err := tagRepo.Create(context.Background(), tag)
|
|
require.NoError(t, err)
|
|
|
|
cl := &model.ConversationLabel{
|
|
ConversationID: 1,
|
|
TagID: tag.ID,
|
|
AccountID: 1,
|
|
}
|
|
err = repo.AddLabel(context.Background(), cl)
|
|
require.NoError(t, err)
|
|
|
|
err = repo.RemoveLabel(context.Background(), 1, tag.ID)
|
|
require.NoError(t, err)
|
|
|
|
labels, err := repo.FindByConversationID(context.Background(), 1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, labels, 0)
|
|
}
|
|
|
|
// ========== RemoveAllLabels ==========
|
|
|
|
func TestConversationLabelRepo_RemoveAllLabels(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag1 := newTestTag(1, "a")
|
|
tag2 := newTestTag(1, "b")
|
|
err := tagRepo.Create(context.Background(), tag1)
|
|
require.NoError(t, err)
|
|
err = tagRepo.Create(context.Background(), tag2)
|
|
require.NoError(t, err)
|
|
|
|
labels := []model.ConversationLabel{
|
|
{ConversationID: 1, TagID: tag1.ID, AccountID: 1},
|
|
{ConversationID: 1, TagID: tag2.ID, AccountID: 1},
|
|
}
|
|
err = repo.AddLabels(context.Background(), labels)
|
|
require.NoError(t, err)
|
|
|
|
err = repo.RemoveAllLabels(context.Background(), 1)
|
|
require.NoError(t, err)
|
|
|
|
result, err := repo.FindByConversationID(context.Background(), 1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, result, 0)
|
|
}
|
|
|
|
// ========== FindByConversationID ==========
|
|
|
|
func TestConversationLabelRepo_FindByConversationID(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag := newTestTag(1, "support")
|
|
err := tagRepo.Create(context.Background(), tag)
|
|
require.NoError(t, err)
|
|
|
|
cl := &model.ConversationLabel{
|
|
ConversationID: 1,
|
|
TagID: tag.ID,
|
|
AccountID: 1,
|
|
}
|
|
err = repo.AddLabel(context.Background(), cl)
|
|
require.NoError(t, err)
|
|
|
|
labels, err := repo.FindByConversationID(context.Background(), 1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, labels, 1)
|
|
assert.Equal(t, tag.ID, labels[0].TagID)
|
|
}
|
|
|
|
// ========== Exists ==========
|
|
|
|
func TestConversationLabelRepo_Exists(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag := newTestTag(1, "vip")
|
|
err := tagRepo.Create(context.Background(), tag)
|
|
require.NoError(t, err)
|
|
|
|
// Not attached yet
|
|
exists, err := repo.Exists(context.Background(), 1, tag.ID)
|
|
require.NoError(t, err)
|
|
assert.False(t, exists)
|
|
|
|
// Attach it
|
|
cl := &model.ConversationLabel{
|
|
ConversationID: 1,
|
|
TagID: tag.ID,
|
|
AccountID: 1,
|
|
}
|
|
err = repo.AddLabel(context.Background(), cl)
|
|
require.NoError(t, err)
|
|
|
|
exists, err = repo.Exists(context.Background(), 1, tag.ID)
|
|
require.NoError(t, err)
|
|
assert.True(t, exists)
|
|
}
|
|
|
|
// ========== ReplaceLabels ==========
|
|
|
|
func TestConversationLabelRepo_ReplaceLabels(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag1 := newTestTag(1, "old1")
|
|
tag2 := newTestTag(1, "old2")
|
|
tag3 := newTestTag(1, "new1")
|
|
err := tagRepo.Create(context.Background(), tag1)
|
|
require.NoError(t, err)
|
|
err = tagRepo.Create(context.Background(), tag2)
|
|
require.NoError(t, err)
|
|
err = tagRepo.Create(context.Background(), tag3)
|
|
require.NoError(t, err)
|
|
|
|
// Add initial labels
|
|
labels := []model.ConversationLabel{
|
|
{ConversationID: 1, TagID: tag1.ID, AccountID: 1},
|
|
{ConversationID: 1, TagID: tag2.ID, AccountID: 1},
|
|
}
|
|
err = repo.AddLabels(context.Background(), labels)
|
|
require.NoError(t, err)
|
|
|
|
// Replace with tag3 only
|
|
err = repo.ReplaceLabels(context.Background(), 1, 1, []uint{tag3.ID})
|
|
require.NoError(t, err)
|
|
|
|
result, err := repo.FindByConversationID(context.Background(), 1)
|
|
require.NoError(t, err)
|
|
assert.Len(t, result, 1)
|
|
assert.Equal(t, tag3.ID, result[0].TagID)
|
|
}
|
|
|
|
// ========== BatchAddLabels ==========
|
|
|
|
func TestConversationLabelRepo_BatchAddLabels(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag := newTestTag(1, "bulk")
|
|
err := tagRepo.Create(context.Background(), tag)
|
|
require.NoError(t, err)
|
|
|
|
convIDs := []uint{1, 2, 3}
|
|
err = repo.BatchAddLabels(context.Background(), convIDs, tag.ID, 1)
|
|
require.NoError(t, err)
|
|
|
|
for _, cid := range convIDs {
|
|
labels, err := repo.FindByConversationID(context.Background(), cid)
|
|
require.NoError(t, err)
|
|
assert.Len(t, labels, 1)
|
|
assert.Equal(t, tag.ID, labels[0].TagID)
|
|
}
|
|
}
|
|
|
|
// ========== BatchRemoveLabels ==========
|
|
|
|
func TestConversationLabelRepo_BatchRemoveLabels(t *testing.T) {
|
|
db := setupTestDB(t, &model.ConversationLabel{}, &model.Tag{}, &model.Conversation{}, &model.Account{}, &model.Inbox{}, &model.Contact{}, &model.ContactInbox{})
|
|
repo := NewConversationLabelRepo(db)
|
|
tagRepo := NewTagRepo(db)
|
|
|
|
tag := newTestTag(1, "bulk")
|
|
err := tagRepo.Create(context.Background(), tag)
|
|
require.NoError(t, err)
|
|
|
|
convIDs := []uint{1, 2, 3}
|
|
err = repo.BatchAddLabels(context.Background(), convIDs, tag.ID, 1)
|
|
require.NoError(t, err)
|
|
|
|
err = repo.BatchRemoveLabels(context.Background(), convIDs, tag.ID)
|
|
require.NoError(t, err)
|
|
|
|
for _, cid := range convIDs {
|
|
labels, err := repo.FindByConversationID(context.Background(), cid)
|
|
require.NoError(t, err)
|
|
assert.Len(t, labels, 0)
|
|
}
|
|
} |