test(shangwutong): cover CID rename reliability (#7)

Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
Rogee
2026-08-13 09:31:25 +08:00
committed by GitHub
co-authored by rogee
parent 0555b296cc
commit fff3dce1bd
2 changed files with 32 additions and 0 deletions
@@ -2,6 +2,7 @@ package v1
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
@@ -18,6 +19,7 @@ import (
channelmodel "github.com/gochat/gochat/internal/model/channel"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/internal/worker"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
@@ -137,6 +139,21 @@ func TestShangwutongConnectorContactMetadataIsScopedAndIdempotent(t *testing.T)
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
require.NoError(t, db.First(ci, ci.ID).Error)
require.JSONEq(t, `{"cid":"cid-1"}`, string(ci.ChannelMetadata))
workers := worker.NewWorkerPool(db)
dispatcher := channel.NewDispatcher(workers)
dispatcher.Register(service.NewShangwutongContactListener(db, workers))
contacts := service.NewContactService(repository.NewContactRepo(db), nil, nil)
contacts.SetWorkerPool(workers)
_, err := contacts.Update(context.Background(), inbox.AccountID, contact.ID, service.UpdateContactRequest{Name: "Renamed"})
require.NoError(t, err)
processed, err := workers.ProcessOne(context.Background())
require.NoError(t, err)
require.True(t, processed)
var job model.BackgroundJob
require.NoError(t, db.Where("job_type = ?", service.TaskTypeShangwutongWebhookDelivery).First(&job).Error)
require.Contains(t, string(job.Payload), `"cid":"cid-1"`)
require.Contains(t, string(job.Payload), `"contact_name":"Renamed"`)
}
func setupShangwutongConnectorAPI(t *testing.T) (*gin.Engine, *gorm.DB, string, *model.Inbox, *model.Inbox) {
@@ -31,6 +31,21 @@ func TestContactUpdateQueuesDurableShangwutongEvent(t *testing.T) {
require.Equal(t, contact.ID, event.ContactID)
}
func TestContactUpdateRollsBackWhenDurableEventCannotBeQueued(t *testing.T) {
db := setupServiceTestDB(t)
account := createTestAccount(t, db)
contact := &model.Contact{AccountID: account.ID, Name: "旧昵称"}
require.NoError(t, db.Create(contact).Error)
svc := NewContactService(repository.NewContactRepo(db), nil, repository.NewNoteRepo(db))
svc.SetWorkerPool(worker.NewWorkerPool(db))
require.NoError(t, db.Migrator().DropTable(&model.BackgroundJob{}))
_, err := svc.Update(context.Background(), account.ID, contact.ID, UpdateContactRequest{Name: "新昵称"})
require.Error(t, err)
require.NoError(t, db.First(contact, contact.ID).Error)
require.Equal(t, "旧昵称", contact.Name)
}
func TestShangwutongContactListenerQueuesOnlyCIDBoundInboxes(t *testing.T) {
db := setupServiceTestDB(t)
account := createTestAccount(t, db)