package config

import (
	"path/filepath"
	"testing"
)

func TestDefaultActivityOutbox(t *testing.T) {
	cfg := Default()
	if cfg.RocketMQ.ActivityOutbox.Topic != "hyapp_activity_outbox" {
		t.Fatalf("unexpected topic: %s", cfg.RocketMQ.ActivityOutbox.Topic)
	}
	if cfg.RocketMQ.ActivityOutbox.ConsumerGroup != "hyapp-statistics-activity-outbox" {
		t.Fatalf("unexpected consumer group: %s", cfg.RocketMQ.ActivityOutbox.ConsumerGroup)
	}
}

func TestNormalizeActivityOutboxEnablesRocketMQAndDefaultsIdentity(t *testing.T) {
	cfg := normalizeRocketMQConfig(RocketMQConfig{
		ActivityOutbox: ConsumerMQConfig{Enabled: true},
	})
	if !cfg.Enabled {
		t.Fatal("activity consumer must enable RocketMQ")
	}
	if cfg.ActivityOutbox.Topic != "hyapp_activity_outbox" || cfg.ActivityOutbox.ConsumerGroup != "hyapp-statistics-activity-outbox" {
		t.Fatalf("activity defaults not applied: %+v", cfg.ActivityOutbox)
	}
	if cfg.ActivityOutbox.ConsumerMaxReconsumeTimes != 16 {
		t.Fatalf("unexpected reconsume limit: %d", cfg.ActivityOutbox.ConsumerMaxReconsumeTimes)
	}
}

func TestShippedConfigsEnableActivityOutboxWithStableIdentity(t *testing.T) {
	for _, name := range []string{"config.yaml", "config.docker.yaml", "config.tencent.example.yaml"} {
		t.Run(name, func(t *testing.T) {
			cfg, err := Load(filepath.Join("..", "..", "configs", name))
			if err != nil {
				t.Fatalf("load %s: %v", name, err)
			}
			if !cfg.RocketMQ.ActivityOutbox.Enabled || cfg.RocketMQ.ActivityOutbox.Topic != "hyapp_activity_outbox" ||
				cfg.RocketMQ.ActivityOutbox.ConsumerGroup != "hyapp-statistics-activity-outbox" {
				t.Fatalf("activity outbox config drift in %s: %+v", name, cfg.RocketMQ.ActivityOutbox)
			}
		})
	}
}
