package main

import (
	"testing"

	"gitea.d-ma.be/mathias/tapir/internal/config"
)

// TestBuildProcessorNilOnIncompleteConfig asserts the queue-only fallback: when a
// required input is missing, buildProcessor returns (nil, nil) — never an error —
// so the web UI degrades to queue-only instead of failing to start.
func TestBuildProcessorNilOnIncompleteConfig(t *testing.T) {
	// A complete config (the fields buildProcessor gates on). The store is nil:
	// buildProcessor must not touch it on the incomplete paths, and the complete
	// path only stores the pointer (no connection), so nil is fine for this test.
	complete := config.Config{
		GatewayURL:     "http://gw/v1",
		YTClientID:     "id",
		YTClientSecret: "secret",
		SecretsFile:    "/tmp/secrets.json",
	}

	tests := []struct {
		name    string
		mutate  func(config.Config) config.Config
		wantNil bool
	}{
		{"complete", func(c config.Config) config.Config { return c }, false},
		{"no gateway url", func(c config.Config) config.Config { c.GatewayURL = ""; return c }, true},
		{"no yt client id", func(c config.Config) config.Config { c.YTClientID = ""; return c }, true},
		{"no yt client secret", func(c config.Config) config.Config { c.YTClientSecret = ""; return c }, true},
		{"no secrets file", func(c config.Config) config.Config { c.SecretsFile = ""; return c }, true},
		{"empty config", func(config.Config) config.Config { return config.Config{} }, true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			engine, err := buildProcessor(tt.mutate(complete), nil)
			if err != nil {
				t.Fatalf("buildProcessor returned an error, want nil: %v", err)
			}
			if (engine == nil) != tt.wantNil {
				t.Fatalf("engine == nil is %v, want %v", engine == nil, tt.wantNil)
			}
		})
	}
}
