package chat import ( "context" "testing" "xianyu-go/internal/db" ) // fakeRepository 是验证聊天服务窄 repository 依赖的内存替身。 type fakeRepository struct { // accountIDs 保存模拟的用户账号归属结果。 accountIDs []string } // ListOwnedIDs 返回内存替身中的账号归属结果。 func (r *fakeRepository) ListOwnedIDs(context.Context, int64) ([]string, error) { return r.accountIDs, nil } // DeleteSession 模拟删除聊天会话。 func (*fakeRepository) DeleteSession(context.Context, string, string) error { return nil } // UpsertSession 模拟写入聊天会话。 func (*fakeRepository) UpsertSession(context.Context, db.ChatSession) error { return nil } // SyncSessionSummary 模拟同步聊天会话摘要。 func (*fakeRepository) SyncSessionSummary(context.Context, string, string, string, int64, int64, int) error { return nil } // SaveMessage 模拟幂等保存聊天消息。 func (*fakeRepository) SaveMessage(_ context.Context, _ db.ChatSession, message db.ChatMessage, _ bool) (*db.ChatMessage, bool, error) { return &message, true, nil } // UpdateMessageType 模拟更新历史消息类型。 func (*fakeRepository) UpdateMessageType(context.Context, string, string, string) error { return nil } // UpdateMessageStatus 模拟更新外发消息状态。 func (*fakeRepository) UpdateMessageStatus(_ context.Context, _ string, _ string, status string) (*db.ChatMessage, error) { return &db.ChatMessage{Status: status}, nil } // TestServiceUsesNarrowRepository 验证聊天服务可以脱离完整 db.Store 运行。 func TestServiceUsesNarrowRepository(t *testing.T) { // repository 是只实现聊天所需方法的内存替身。 repository := &fakeRepository{accountIDs: []string{"account-1", "account-2"}} // service 保存service,供当前处理流程使用 service := NewWithRepository(repository) // cancel、err 保存cancel、err,供当前处理流程使用 _, cancel, err := service.Subscribe(context.Background(), 42) if err != nil { t.Fatalf("Subscribe() error = %v", err) } cancel() }