package main

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

func TestAutoStartServiceContentWaitsForGraphicalTarget(t *testing.T) {
	appPath := filepath.Join("/opt", "tgk-touch", "m9zCtrlTty")
	content := autoStartServiceContent(appPath)

	for _, want := range []string{
		"Wants=network-online.target",
		"After=network-online.target graphical.target display-manager.service",
		"Environment=DISPLAY=:0",
		"WantedBy=graphical.target",
	} {
		if !strings.Contains(content, want) {
			t.Fatalf("service content missing %q:\n%s", want, content)
		}
	}
}

func TestAutoStartServiceMatchesRejectsOldNetworkOnlyService(t *testing.T) {
	appPath := filepath.Join("/opt", "tgk-touch", "m9zCtrlTty")
	oldContent := `[Unit]
Description=m9zCtrlTty Background Service
After=network.target

[Service]
Type=simple
ExecStart=` + appPath + `
Restart=on-failure
RestartSec=10
User=root
WorkingDirectory=` + filepath.Dir(appPath) + `

[Install]
WantedBy=multi-user.target
`

	if autoStartServiceMatches(oldContent, appPath) {
		t.Fatal("old network-only service should be treated as mismatched")
	}
}

func TestAutoStartServiceMatchesGeneratedContent(t *testing.T) {
	appPath := filepath.Join("/opt", "tgk-touch", "m9zCtrlTty")
	content := autoStartServiceContent(appPath)

	if !autoStartServiceMatches(content, appPath) {
		t.Fatalf("generated service content should match:\n%s", content)
	}
}
