package task

import (
	"context"
	"encoding/json"
	"testing"
)

func TestRegistryIsComposableAndIdempotent(t *testing.T) {
	registry := NewRegistry()
	registry.RegisterAll([]Method{{Name: "orders.sync", Run: func(context.Context, json.RawMessage) error { return nil }}, {Name: "system.cleanup", Run: func(context.Context, json.RawMessage) error { return nil }}})
	registry.Register(Method{Name: "orders.sync", Description: "updated", Run: func(context.Context, json.RawMessage) error { return nil }})
	methods := registry.List()
	if len(methods) != 2 || methods[0].Name != "orders.sync" || methods[0].Description != "updated" {
		t.Fatalf("unexpected methods: %#v", methods)
	}
}

type testContributor struct{ name string }

func (contributor testContributor) RegisterTasks(registry *Registry) {
	registry.Register(Method{Name: contributor.name, Run: func(context.Context, json.RawMessage) error { return nil }})
}

func TestApplyCollectsModuleContributors(t *testing.T) {
	registry := NewRegistry()
	Apply(registry, testContributor{name: "system.cleanup"}, testContributor{name: "orders.expire"})
	methods := registry.List()
	if len(methods) != 2 || methods[0].Name != "orders.expire" || methods[1].Name != "system.cleanup" {
		t.Fatalf("unexpected contributed methods: %#v", methods)
	}
}
