fix(backend): harden final MVP invariants

This commit is contained in:
2026-07-22 01:24:41 +08:00
parent c527cd6ed0
commit 26c3e6fd8a
26 changed files with 684 additions and 179 deletions

View File

@@ -220,9 +220,9 @@ func seedProjectA1(tx *gorm.DB, userID uint, projectID uint) error {
}
}
for _, plan := range []models.SenlinAgentCronPlan{
{ProjectID: projectID, CreatedBy: userID, Title: "每日 Inbox 摘要", Schedule: "0 9 * * *", NextRunAt: &nextRun, Enabled: true, LastResult: "上次执行成功"},
{ProjectID: projectID, CreatedBy: userID, Title: "每周任务风险扫描", Schedule: "0 10 * * 1", NextRunAt: &dueSoon, Enabled: true, LastResult: "等待下次执行"},
{ProjectID: projectID, CreatedBy: userID, Title: "月底知识归档提醒", Schedule: "0 18 28 * *", NextRunAt: &dueLater, Enabled: false, LastResult: "已暂停"},
{ProjectID: projectID, CreatedBy: userID, Title: "每日 Inbox 摘要", Schedule: "0 9 * * *", NextRunAt: &nextRun, Enabled: true},
{ProjectID: projectID, CreatedBy: userID, Title: "每周任务风险扫描", Schedule: "0 10 * * 1", NextRunAt: &dueSoon, Enabled: true},
{ProjectID: projectID, CreatedBy: userID, Title: "月底知识归档提醒", Schedule: "0 18 28 * *", NextRunAt: &dueLater, Enabled: false},
} {
if err := firstOrCreateCronPlan(tx, plan); err != nil {
return err
@@ -249,7 +249,14 @@ func seedCompactProject(tx *gorm.DB, userID uint, projectID uint) error {
}
func firstOrCreateTag(tx *gorm.DB, projectID uint, name string) error {
return firstOrCreate(tx, &models.SenlinAgentTag{}, "project_id = ? AND name = ?", []any{projectID, name}, models.SenlinAgentTag{ProjectID: projectID, Name: name})
candidate := models.SenlinAgentTag{ProjectID: projectID, Name: name}
if err := tx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "project_id"}, {Name: "name"}},
DoNothing: true,
}).Create(&candidate).Error; err != nil {
return err
}
return tx.Where("project_id = ? AND name = ?", projectID, name).First(&models.SenlinAgentTag{}).Error
}
func tagID(tx *gorm.DB, projectID uint, name string) (uint, error) {

View File

@@ -1,3 +1,5 @@
//go:build integration
package seed
import (
@@ -26,10 +28,8 @@ const (
)
func TestPostgresDemoSeedSerializesConcurrentRunsForSameOwner(t *testing.T) {
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
t.Skip("DATABASE_URL is not configured; skipping PostgreSQL seed concurrency test")
}
dsn := os.Getenv("TEST_DATABASE_URL")
require.NotEmpty(t, dsn, "TEST_DATABASE_URL is required for integration tests and must point to an isolated database")
database, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
TranslateError: true,
Logger: logger.Default.LogMode(logger.Silent),

View File

@@ -42,6 +42,9 @@ func TestDemoSeedCreatesFrontendWorkspaceData(t *testing.T) {
require.GreaterOrEqual(t, len(workspace.AISessions), 4)
require.GreaterOrEqual(t, len(workspace.NotesSources), 3)
require.GreaterOrEqual(t, len(workspace.CronPlans), 3)
for _, plan := range workspace.CronPlans {
require.Empty(t, plan.LastResult, "演示数据不得声称计划已执行")
}
require.Len(t, workspace.Channels, 8)
}