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.
120 lines
3.4 KiB
Plaintext
120 lines
3.4 KiB
Plaintext
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ========== Article test helpers ==========
|
|
|
|
// setupArticleService creates ArticleRepo + ArticleService test instances.
|
|
func setupArticleService(t *testing.T) (*gorm.DB, *repository.ArticleRepo, *ArticleService) {
|
|
t.Helper()
|
|
db := setupServiceTestDB(t)
|
|
repo := repository.NewArticleRepo(db)
|
|
svc := NewArticleService(repo)
|
|
return db, repo, svc
|
|
}
|
|
|
|
// createArticleServiceTestArticle creates a test article in the given portal/account context.
|
|
func createArticleServiceTestArticle(t *testing.T, db *gorm.DB, accountID, portalID uint, authorID uint, status model.ArticleStatus) *model.Article {
|
|
t.Helper()
|
|
authorIDPtr := &authorID
|
|
slug := "test-article-" + string(status)
|
|
article := &model.Article{
|
|
PortalID: portalID,
|
|
AccountID: accountID,
|
|
AuthorID: authorIDPtr,
|
|
Title: "Test Article",
|
|
Slug: slug,
|
|
Content: "Test content",
|
|
Status: string(status),
|
|
}
|
|
if err := db.Create(article).Error; err != nil {
|
|
t.Fatalf("failed to create test article: %v", err)
|
|
}
|
|
return article
|
|
}
|
|
|
|
// ========== BulkActions ==========
|
|
|
|
func TestArticleService_BulkActions_Publish(t *testing.T) {
|
|
db, _, svc := setupArticleService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// Create a portal
|
|
portal := &model.Portal{
|
|
AccountID: account.ID,
|
|
Name: "Test Portal",
|
|
Slug: "test-portal-bulk-pub",
|
|
}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
|
|
article := createArticleServiceTestArticle(t, db, account.ID, portal.ID, 1, model.ArticleStatusDraft)
|
|
|
|
err := svc.BulkAction(context.Background(), account.ID, &ArticleBulkActionRequest{
|
|
Action: "publish",
|
|
ArticleIDs: []uint{article.ID},
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
updated, err := svc.Get(context.Background(), account.ID, article.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(model.ArticleStatusPublished), updated.Status)
|
|
}
|
|
|
|
func TestArticleService_BulkActions_Archive(t *testing.T) {
|
|
db, _, svc := setupArticleService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
portal := &model.Portal{
|
|
AccountID: account.ID,
|
|
Name: "Test Portal",
|
|
Slug: "test-portal-bulk-archive",
|
|
}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
|
|
article := createArticleServiceTestArticle(t, db, account.ID, portal.ID, 1, model.ArticleStatusPublished)
|
|
|
|
err := svc.BulkAction(context.Background(), account.ID, &ArticleBulkActionRequest{
|
|
Action: "archive",
|
|
ArticleIDs: []uint{article.ID},
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
updated, err := svc.Get(context.Background(), account.ID, article.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(model.ArticleStatusArchived), updated.Status)
|
|
}
|
|
|
|
func TestArticleService_BulkActions_Delete(t *testing.T) {
|
|
db, _, svc := setupArticleService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
portal := &model.Portal{
|
|
AccountID: account.ID,
|
|
Name: "Test Portal",
|
|
Slug: "test-portal-bulk-delete",
|
|
}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
|
|
article := createArticleServiceTestArticle(t, db, account.ID, portal.ID, 1, model.ArticleStatusDraft)
|
|
|
|
err := svc.BulkAction(context.Background(), account.ID, &ArticleBulkActionRequest{
|
|
Action: "delete",
|
|
ArticleIDs: []uint{article.ID},
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = svc.Get(context.Background(), account.ID, article.ID)
|
|
assert.Error(t, err) // deleted article should not be found
|
|
} |