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.
313 lines
10 KiB
Plaintext
313 lines
10 KiB
Plaintext
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// Helper: create prerequisite Account for AssignmentPolicy tests.
|
|
func createAssignmentPolicyPrereqs(t *testing.T, db *gorm.DB) *model.Account {
|
|
t.Helper()
|
|
|
|
account := &model.Account{Name: "APTestOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
return account
|
|
}
|
|
|
|
// Helper: create a test AssignmentPolicy with all prerequisites.
|
|
func createTestAssignmentPolicy(t *testing.T, db *gorm.DB, accountID uint, name string) *model.AssignmentPolicy {
|
|
t.Helper()
|
|
|
|
policy := &model.AssignmentPolicy{
|
|
AccountID: accountID,
|
|
Name: name,
|
|
AssignmentOrder: 0,
|
|
ConversationPriority: 0,
|
|
FairDistributionLimit: 100,
|
|
FairDistributionWindow: 3600,
|
|
Enabled: true,
|
|
}
|
|
require.NoError(t, db.Create(policy).Error)
|
|
return policy
|
|
}
|
|
|
|
// ========== AssignmentPolicyRepo ==========
|
|
|
|
// --- FindByID ---
|
|
|
|
func TestAssignmentPolicyRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
|
|
found, err := repo.FindByID(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, policy.ID, found.ID)
|
|
assert.Equal(t, "TestPolicy", found.Name)
|
|
}
|
|
|
|
func TestAssignmentPolicyRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- FindByAccount ---
|
|
|
|
func TestAssignmentPolicyRepo_FindByAccount(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
createTestAssignmentPolicy(t, db, account.ID, "Policy1")
|
|
createTestAssignmentPolicy(t, db, account.ID, "Policy2")
|
|
|
|
policies, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, policies, 2)
|
|
}
|
|
|
|
func TestAssignmentPolicyRepo_FindByAccount_Pagination(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
createTestAssignmentPolicy(t, db, account.ID, "Policy1")
|
|
createTestAssignmentPolicy(t, db, account.ID, "Policy2")
|
|
createTestAssignmentPolicy(t, db, account.ID, "Policy3")
|
|
|
|
policies, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 2)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, policies, 2)
|
|
}
|
|
|
|
func TestAssignmentPolicyRepo_FindByAccount_NoMatches(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
policies, total, err := repo.FindByAccount(context.Background(), 9999, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), total)
|
|
assert.Len(t, policies, 0)
|
|
}
|
|
|
|
// --- Create ---
|
|
|
|
func TestAssignmentPolicyRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := &model.AssignmentPolicy{
|
|
AccountID: account.ID,
|
|
Name: "NewPolicy",
|
|
AssignmentOrder: 0,
|
|
ConversationPriority: 0,
|
|
FairDistributionLimit: 100,
|
|
FairDistributionWindow: 3600,
|
|
Enabled: true,
|
|
}
|
|
err := repo.Create(context.Background(), policy)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, policy.ID)
|
|
}
|
|
|
|
// --- Update ---
|
|
|
|
func TestAssignmentPolicyRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "BeforeUpdate")
|
|
|
|
policy.Name = "AfterUpdate"
|
|
err := repo.Update(context.Background(), policy)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "AfterUpdate", found.Name)
|
|
}
|
|
|
|
// --- Delete ---
|
|
|
|
func TestAssignmentPolicyRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "DeletePolicy")
|
|
|
|
err := repo.Delete(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), policy.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
func TestAssignmentPolicyRepo_Delete_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewAssignmentPolicyRepo(db)
|
|
|
|
err := repo.Delete(context.Background(), 9999)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// ========== InboxAssignmentPolicyRepo ==========
|
|
|
|
func createTestInboxForAP(t *testing.T, db *gorm.DB, accountID uint, name string) *model.Inbox {
|
|
t.Helper()
|
|
inbox := &model.Inbox{AccountID: accountID, Name: name, ChannelType: "web_widget", ChannelID: 1}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
return inbox
|
|
}
|
|
|
|
// --- FindByID ---
|
|
|
|
func TestInboxAssignmentPolicyRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
inbox := createTestInboxForAP(t, db, account.ID, "TestInbox")
|
|
|
|
iap := &model.InboxAssignmentPolicy{InboxID: inbox.ID, AssignmentPolicyID: policy.ID}
|
|
require.NoError(t, db.Create(iap).Error)
|
|
|
|
found, err := repo.FindByID(context.Background(), iap.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, iap.ID, found.ID)
|
|
assert.Equal(t, inbox.ID, found.InboxID)
|
|
assert.Equal(t, policy.ID, found.AssignmentPolicyID)
|
|
}
|
|
|
|
func TestInboxAssignmentPolicyRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
found, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- FindByInbox ---
|
|
|
|
func TestInboxAssignmentPolicyRepo_FindByInbox(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
inbox := createTestInboxForAP(t, db, account.ID, "TestInbox")
|
|
|
|
iap := &model.InboxAssignmentPolicy{InboxID: inbox.ID, AssignmentPolicyID: policy.ID}
|
|
require.NoError(t, db.Create(iap).Error)
|
|
|
|
found, err := repo.FindByInbox(context.Background(), inbox.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, policy.ID, found.AssignmentPolicyID)
|
|
}
|
|
|
|
func TestInboxAssignmentPolicyRepo_FindByInbox_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
found, err := repo.FindByInbox(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- FindByPolicy ---
|
|
|
|
func TestInboxAssignmentPolicyRepo_FindByPolicy(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
inbox1 := createTestInboxForAP(t, db, account.ID, "Inbox1")
|
|
inbox2 := createTestInboxForAP(t, db, account.ID, "Inbox2")
|
|
|
|
iap1 := &model.InboxAssignmentPolicy{InboxID: inbox1.ID, AssignmentPolicyID: policy.ID}
|
|
require.NoError(t, db.Create(iap1).Error)
|
|
iap2 := &model.InboxAssignmentPolicy{InboxID: inbox2.ID, AssignmentPolicyID: policy.ID}
|
|
require.NoError(t, db.Create(iap2).Error)
|
|
|
|
iaps, err := repo.FindByPolicy(context.Background(), policy.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, iaps, 2)
|
|
}
|
|
|
|
// --- Create ---
|
|
|
|
func TestInboxAssignmentPolicyRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
inbox := createTestInboxForAP(t, db, account.ID, "TestInbox")
|
|
|
|
iap := &model.InboxAssignmentPolicy{InboxID: inbox.ID, AssignmentPolicyID: policy.ID}
|
|
err := repo.Create(context.Background(), iap)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, iap.ID)
|
|
}
|
|
|
|
// --- Delete ---
|
|
|
|
func TestInboxAssignmentPolicyRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
inbox := createTestInboxForAP(t, db, account.ID, "TestInbox")
|
|
|
|
iap := &model.InboxAssignmentPolicy{InboxID: inbox.ID, AssignmentPolicyID: policy.ID}
|
|
require.NoError(t, db.Create(iap).Error)
|
|
|
|
err := repo.Delete(context.Background(), iap.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(context.Background(), iap.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
}
|
|
|
|
// --- DeleteByInbox ---
|
|
|
|
func TestInboxAssignmentPolicyRepo_DeleteByInbox(t *testing.T) {
|
|
db := setupTestDB(t, &model.AssignmentPolicy{}, &model.InboxAssignmentPolicy{})
|
|
repo := NewInboxAssignmentPolicyRepo(db)
|
|
|
|
account := createAssignmentPolicyPrereqs(t, db)
|
|
policy := createTestAssignmentPolicy(t, db, account.ID, "TestPolicy")
|
|
inbox := createTestInboxForAP(t, db, account.ID, "TestInbox")
|
|
|
|
iap := &model.InboxAssignmentPolicy{InboxID: inbox.ID, AssignmentPolicyID: policy.ID}
|
|
require.NoError(t, db.Create(iap).Error)
|
|
|
|
err := repo.DeleteByInbox(context.Background(), inbox.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByInbox(context.Background(), inbox.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
} |