package inject

import (
	"os"
	"path/filepath"
	"testing"
)

func TestInjectorWriteBranches(t *testing.T) {
	dir := t.TempDir()
	boot := filepath.Join(dir, "boot")
	root := filepath.Join(dir, "root")
	inj := &Injector{BootPath: boot, RootPath: root}
	files := []FileSpec{
		{Path: "boot.txt", Content: []byte("boot"), Mode: 0o644, RootFS: false},
		{Path: "root.txt", Content: []byte("root"), Mode: 0o600, RootFS: true},
	}
	if err := inj.Write(files); err != nil {
		t.Fatalf("Write: %v", err)
	}
	if got, err := os.ReadFile(filepath.Join(boot, "boot.txt")); err != nil || string(got) != "boot" {
		t.Fatalf("boot write = %q err=%v", got, err)
	}
	if got, err := os.ReadFile(filepath.Join(root, "root.txt")); err != nil || string(got) != "root" {
		t.Fatalf("root write = %q err=%v", got, err)
	}

	block := filepath.Join(dir, "blocked")
	if err := os.WriteFile(block, []byte("file"), 0o644); err != nil {
		t.Fatal(err)
	}
	inj = &Injector{BootPath: block}
	if err := inj.Write([]FileSpec{{Path: "x", Content: []byte("x")}}); err == nil {
		t.Fatal("expected mkdir error")
	}

	targetDir := filepath.Join(boot, "already-dir")
	if err := os.MkdirAll(targetDir, 0o755); err != nil {
		t.Fatal(err)
	}
	inj = &Injector{BootPath: boot}
	if err := inj.Write([]FileSpec{{Path: "already-dir", Content: []byte("x"), Mode: 0o644}}); err == nil {
		t.Fatal("expected write error when target path is a directory")
	}
}
