//go:build linux

package main

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

func TestChooseFrontendBinaryPrefersSiblingOfBridgeExecutable(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	sibling := filepath.Join(dir, "mirage-qt-ui")
	if err := os.WriteFile(sibling, []byte("#!/bin/sh\n"), 0o755); err != nil {
		t.Fatalf("WriteFile(sibling): %v", err)
	}

	got := chooseFrontendBinaryWithBridge("", filepath.Join(dir, "mirage-qt"))
	if got != sibling {
		t.Fatalf("chooseFrontendBinaryWithBridge() = %q, want %q", got, sibling)
	}
}

func TestChooseFrontendBinaryUsesExplicitPathFirst(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	explicit := filepath.Join(dir, "custom-ui")
	if err := os.WriteFile(explicit, []byte("#!/bin/sh\n"), 0o755); err != nil {
		t.Fatalf("WriteFile(explicit): %v", err)
	}

	got := chooseFrontendBinaryWithBridge(explicit, filepath.Join(dir, "mirage-qt"))
	if got != explicit {
		t.Fatalf("chooseFrontendBinaryWithBridge() = %q, want %q", got, explicit)
	}
}
