package main

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

	"metis/pkg/sentinel"
)

func TestSentinelNsenterAndErrorBranches(t *testing.T) {
	dir := t.TempDir()
	write := func(name, body string) {
		path := filepath.Join(dir, name)
		if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\nset -eu\n"+body+"\n"), 0o755); err != nil {
			t.Fatalf("write %s: %v", name, err)
		}
	}
	write("nsenter", `while [[ "${1:-}" != "--" ]]; do shift; done
shift
exec "$@"`)
	write("hostname", `printf 'titan-13\n'`)
	write("uname", `printf '6.6.63\n'`)
	write("k3s", `printf 'v1.31.5+k3s1\n'`)
	write("containerd", `printf '1.7.99\n'`)
	write("cat", `printf 'PRETTY_NAME="Metis OS"\n'`)
	write("dpkg-query", `printf '1.0.0\n'`)
	write("rpm", `printf '1.0.0\n'`)
	t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
	t.Setenv("METIS_SENTINEL_NSENTER", "1")
	snap := sentinel.Collect()
	if snap.Hostname != "titan-13" || snap.OSImage != "Metis OS" {
		t.Fatalf("Collect via nsenter = %#v", snap)
	}
	if err := pushSnapshot("http://127.0.0.1:1", snap); err == nil {
		t.Fatal("expected pushSnapshot error")
	}
}
