package control

import (
	"context"
	"strings"
	"testing"
)

func TestMonthlyBillingRequiresVerifiedFrozenInputsBeforeWorker(t *testing.T) {
	runner := DirectRunner{Python: "/does-not-exist"}
	closeRun := &CloseRun{ID: "close-fixture", Steps: map[string]*StepState{}}
	attempt := &Attempt{ID: "attempt-fixture"}
	for _, state := range []*StepState{
		nil,
		{Execution: ExecutionSucceeded, Verification: VerificationNotChecked},
		{Execution: ExecutionFailed, Verification: VerificationPassed},
		{Execution: ExecutionSucceeded, Verification: VerificationPassed, Receipt: map[string]any{}},
		{Execution: ExecutionSucceeded, Verification: VerificationPassed, Receipt: map[string]any{"input_manifest": map[string]string{"uri": "gs://fixture/manifest.json", "generation": "latest", "sha256": strings.Repeat("a", 64)}}},
	} {
		closeRun.Steps["freeze_billing_inputs"] = state
		_, err := runner.runMonthlyBilling(context.Background(), closeRun, StepDefinition{Key: "run_monthly_cloud_billing"}, attempt)
		if err == nil || strings.Contains(err.Error(), "monthly billing failed") {
			t.Fatalf("worker invoked without a verified immutable input reference: %v", err)
		}
	}
}
