package integration_test

import (
	"strings"
	"testing"

	"gitea.maximumdirect.net/eric/notarius/internal/core/config"
)

func TestItemOccurrenceConfigurationRequiresEarlierItemRegistry(t *testing.T) {
	registries := productionNPCRegistries(t)
	cfg, err := configFromYAML(`
version: 4
pipelines:
  items:
    input: seriatim
    chunk: generic
    steps:
      - id: identify-items
        artifacts:
          registry:
            extract: dnd/item-registry
            merge: appendorder
            normalize: dnd/item-registry
      - id: record-occurrences
        references:
          item_registry:
            artifact:
              step: identify-items
              lane: registry
        artifacts:
          occurrences:
            extract: dnd/item-occurrences
            merge: appendorder
            normalize: dnd/item-occurrences
`)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := cfg.Resolve(config.ResolveInput{PipelineID: "items", Catalog: moduleCatalog(registries)}); err != nil {
		t.Fatalf("Resolve() error = %v", err)
	}

	broken, err := configFromYAML(strings.Replace(`
version: 4
pipelines:
  items:
    input: seriatim
    chunk: generic
    steps:
      - id: record-occurrences
        artifacts:
          occurrences:
            extract: dnd/item-occurrences
`, "record-occurrences", "record-occurrences", 1))
	if err != nil {
		t.Fatal(err)
	}
	if _, err := broken.Resolve(config.ResolveInput{PipelineID: "items", Catalog: moduleCatalog(registries)}); err == nil {
		t.Fatal("Resolve() error = nil, want required item registry")
	}
}
