package appbackup

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

// TestSharedContentGID pins the shared content group to 1000 (FileBrowser's gid + the apps' PUID/PGID).
func TestSharedContentGID(t *testing.T) {
	if SharedContentGID != 1000 {
		t.Errorf("SharedContentGID = %d, want 1000", SharedContentGID)
	}
}

// TestUserdataSkeleton_List asserts the locked carry-list. R-75 renamed the hardcoded list to
// UserdataSkeletonCarry (it is now the non-derived carry-list) and DELIBERATELY dropped the three
// `import*` entries: carrying them would re-create a per-drive drop-zone on every drive forever, the
// dead lookalike the canonical root exists to remove. That is not a removal — nothing deletes the
// dirs an existing box has; they stop being maintained and stop appearing on fresh boxes. Every other
// entry is unchanged, which is the zero-removals promise.
func TestUserdataSkeleton_List(t *testing.T) {
	got := map[string]bool{}
	for _, s := range UserdataSkeletonCarry() {
		got[s] = true
	}
	for _, want := range []string{
		"media/movies", "media/tv", "media/music", "media/audiobooks", "media/books",
		"media/comics", "media/photos", "downloads",
		"roms", "documents",
	} {
		if !got[want] {
			t.Errorf("skeleton missing %q", want)
		}
	}
	for _, gone := range []string{"import", "import/paperless", "import/calibre"} {
		if got[gone] {
			t.Errorf("carry-list must NOT hold %q — the drop-zone is canonical on the system drive (R-75)", gone)
		}
	}
}

// TestUserdataDir confirms the userdata root is a sibling under the namespace.
func TestUserdataDir(t *testing.T) {
	if got := UserdataDir("/mnt/felhom-usb"); got != filepath.Clean("/mnt/felhom-usb/userdata") {
		t.Errorf("UserdataDir = %q", got)
	}
}

// TestEnsureUserdataSkeleton_Structure: every skeleton dir is created (chown may fail off-root, which
// is ignored — dirs + setgid still land). Runs cross-platform.
func TestEnsureUserdataSkeleton_Structure(t *testing.T) {
	ns := t.TempDir()
	dirs := BuildUserdataSkeleton(nil)   // no catalog derived → the carry-list floor
	_ = EnsureUserdataSkeleton(ns, dirs) // ignore chown error on a non-root CI host
	base := UserdataDir(ns)
	for _, sub := range append([]string{""}, dirs...) {
		p := filepath.Join(base, sub)
		if fi, err := os.Stat(p); err != nil || !fi.IsDir() {
			t.Errorf("skeleton dir missing: %s (%v)", p, err)
		}
	}
}
