package inject

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

func TestWriteTargetsBootAndRootMounts(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 file = %q, err=%v", string(got), err)
	}
	if got, err := os.ReadFile(filepath.Join(root, "root.txt")); err != nil || string(got) != "root" {
		t.Fatalf("root file = %q, err=%v", string(got), err)
	}
}

func TestWriteReturnsFilesystemErrors(t *testing.T) {
	dir := t.TempDir()
	boot := filepath.Join(dir, "boot")
	rootFile := filepath.Join(dir, "root-file")
	if err := os.WriteFile(rootFile, []byte("not a dir"), 0o644); err != nil {
		t.Fatal(err)
	}
	inj := Injector{BootPath: boot, RootPath: rootFile}
	if err := inj.Write([]FileSpec{{Path: "root.txt", Content: []byte("root"), RootFS: true}}); err == nil {
		t.Fatal("expected write error for root path file")
	}
}
