package config

import (
	"os"
	"testing"
)

// example_test.go pins that the shipped example project (example/book.yaml) is RUNNABLE through the
// pair-keyed prompt seam (D39 слой 2 / D39.2 T3). Reframed to the prod pair zh→ru, LoadBook +
// LoadPipeline resolve, every stage's prompt resolves for the book's pair and exists on disk, and a
// ja-ru pair still FAILS LOUD — the seam the example's commented-out ja block documents. The old ja
// framing regressed to a fail-loud after the pair seam landed (D39.2-open №4); this is the guard that
// the reframe fixed it without silently smuggling a ja book through the zh-ru conventions.
func TestExampleProjectIsRunnableZhRu(t *testing.T) {
	book, err := LoadBook("../../example/book.yaml")
	if err != nil {
		t.Fatalf("example book must load (source_file etc. must exist): %v", err)
	}
	if book.LangPair() != "zh-ru" {
		t.Fatalf("example must be reframed to the prod pair zh→ru, got LangPair() = %q", book.LangPair())
	}
	models, err := LoadModels(book.ModelsFile)
	if err != nil {
		t.Fatalf("example models must load: %v", err)
	}
	pipe, err := LoadPipeline(book.Pipeline, models)
	if err != nil {
		t.Fatalf("example pipeline must load: %v", err)
	}
	if len(pipe.Stages) == 0 {
		t.Fatal("example pipeline has no stages")
	}
	// zh-ru: every stage resolves a prompt for the book's pair AND the resolved file exists (no
	// fail-loud, no dangling path) — the exact end-to-end the pair seam broke for the old ja framing.
	for _, st := range pipe.Stages {
		p, err := st.PromptPathFor(book.LangPair())
		if err != nil {
			t.Fatalf("stage %q: zh-ru prompt must resolve: %v", st.Name, err)
		}
		if _, err := os.Stat(p); err != nil {
			t.Fatalf("stage %q: resolved prompt %q must exist on disk: %v", st.Name, p, err)
		}
	}
	// ja-ru: the seam still bites a pair the pack does not carry (what the commented-out ja block in
	// example/book.yaml documents) — loadTemplates fails on the first such stage, so at least one must.
	jaFailed := false
	for _, st := range pipe.Stages {
		if _, err := st.PromptPathFor("ja-ru"); err != nil {
			jaFailed = true
			break
		}
	}
	if !jaFailed {
		t.Fatal("a ja-ru pair must fail loud through the example pipeline (the pair seam)")
	}
}
