package data

import (
	"context"
	"testing"
	"voucher/internal/conf"
	"voucher/internal/pkg/mq"
)

func Test_OrderProducer(t *testing.T) {
	m := make(map[string]*conf.EventMap)

	m["order"] = &conf.EventMap{
		Topic: "order",
	}

	c := &conf.RocketMQ{
		Addr:        "http://rmq-cn-nwy3fn4ex09.cn-chengdu.rmq.aliyuncs.com:8080",
		AccessKey:   "Qecl4cea2IAZPKoD",
		SecretKey:   "Z3596KCFA9RAUR6k",
		SecretToken: "",
		EventMap:    m,
	}

	mqx, err := buildMqProducer(c)
	if err != nil {
		t.Errorf("buildMqProducer() error = %v", err)
		return
	}

	ctx := context.Background()
	orderNo := ""

	eventMap := c.EventMap["order"]
	sendOption := []mq.SendOption{
		mq.WithSendShardingKeysOption(orderNo),
	}

	if err = mqx.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil {
		t.Errorf("入队失败 error = %v", err)
		return
	}
}

func Test_NotifyProducer(t *testing.T) {
	m := make(map[string]*conf.EventMap)

	m["notify"] = &conf.EventMap{
		Topic: "notify",
	}

	c := &conf.RocketMQ{
		Addr:        "http://rmq-cn-nwy3fn4ex09.cn-chengdu.rmq.aliyuncs.com:8080",
		AccessKey:   "Qecl4cea2IAZPKoD",
		SecretKey:   "Z3596KCFA9RAUR6k",
		SecretToken: "",
		EventMap:    m,
	}

	mqx, err := buildMqProducer(c)
	if err != nil {
		t.Errorf("buildMqProducer() error = %v", err)
		return
	}

	ctx := context.Background()
	orderNo := ""

	eventMap := c.EventMap["notify"]
	sendOption := []mq.SendOption{
		mq.WithSendShardingKeysOption(orderNo),
	}

	if err = mqx.SendSync(ctx, eventMap.Topic, []byte("{}"), sendOption...); err != nil {
		t.Errorf("入队失败 error = %v", err)
		return
	}
}
