package neovim import ( "context" "os" "path/filepath" "strings" "testing" "github.com/MDietermann/ArduinoArchSetup/internal/config" "github.com/MDietermann/ArduinoArchSetup/internal/module" "github.com/MDietermann/ArduinoArchSetup/internal/platform" ) type fakeRunner struct { runCalls, outCalls [][]string installed map[string]bool } func (f *fakeRunner) Run(_ context.Context, name string, args ...string) error { f.runCalls = append(f.runCalls, append([]string{name}, args...)) if name == "git" && len(args) >= 1 && args[0] == "clone" { dest := args[len(args)-1] os.MkdirAll(filepath.Join(dest, "lua", "plugins"), 0o755) os.WriteFile(filepath.Join(dest, "lua", "plugins", "init.lua"), []byte("return {\n \"neovim/nvim-lspconfig\",\n opts = {},\n},\n{\n \"other\",\n},\n"), 0o644) os.MkdirAll(filepath.Join(dest, "lua", "configs"), 0o755) os.WriteFile(filepath.Join(dest, "lua", "configs", "lspconfig.lua"), []byte("stub"), 0o644) } return nil } func (f *fakeRunner) Output(_ context.Context, name string, args ...string) ([]byte, error) { f.outCalls = append(f.outCalls, append([]string{name}, args...)) if name == "pacman" && len(args) == 2 && args[0] == "-Q" { if f.installed[args[1]] { return []byte("ok"), nil } return nil, errExit1 } return nil, nil } type exitOne struct{} func (exitOne) Error() string { return "exit 1" } var errExit1 = exitOne{} type nopReporter struct{} func (nopReporter) Progress(string, int, int, string) {} func (nopReporter) Log(string, string) {} func TestDetectFreshSystem(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) r := &fakeRunner{installed: map[string]bool{}} m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "gruvbox", platform.NewPacman(r), platform.GitClient{Runner: r}) st, err := m.Detect(context.Background()) if err != nil { t.Fatal(err) } s := st.(state) if s.NvimInstalled { t.Error("nvim should be absent") } if s.ChadrcPresent { t.Error("chadrc should be absent") } if s.BackupNeeded { t.Error("no backup on empty system") } } func TestDetectNonNvChadTriggersBackup(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) os.MkdirAll(filepath.Join(home, ".config", "nvim", "lua"), 0o755) os.WriteFile(filepath.Join(home, ".config", "nvim", "lua", "plugins.lua"), []byte("-- lazyvim"), 0o644) r := &fakeRunner{installed: map[string]bool{}} m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "gruvbox", platform.NewPacman(r), platform.GitClient{Runner: r}) st, _ := m.Detect(context.Background()) if !st.(state).BackupNeeded { t.Fatal("expected BackupNeeded=true") } } func TestPlanApplyInstallsEverything(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) r := &fakeRunner{installed: map[string]bool{}} m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "dracula", platform.NewPacman(r), platform.GitClient{Runner: r}) st, _ := m.Detect(context.Background()) p, _ := m.Plan(context.Background(), st) if len(p.Actions) == 0 { t.Fatal("empty plan") } if err := m.Apply(context.Background(), p, nopReporter{}); err != nil { t.Fatal(err) } sawPacman, sawClone := false, false for _, c := range r.runCalls { if len(c) >= 3 && c[0] == "sudo" && c[1] == "pacman" { sawPacman = true } if c[0] == "git" && len(c) >= 2 && c[1] == "clone" { sawClone = true } } if !sawPacman || !sawClone { t.Fatalf("missing steps; calls=%v", r.runCalls) } nvimCfg := filepath.Join(home, ".config", "nvim") chadrc, err := os.ReadFile(filepath.Join(nvimCfg, "lua", "chadrc.lua")) if err != nil { t.Fatalf("chadrc missing: %v", err) } if !strings.Contains(string(chadrc), `theme = "dracula"`) { t.Fatalf("chadrc theme not applied: %s", chadrc) } initLua, _ := os.ReadFile(filepath.Join(nvimCfg, "lua", "plugins", "init.lua")) if strings.Contains(string(initLua), "nvim-lspconfig") { t.Fatalf("nvim-lspconfig not stripped: %s", initLua) } if _, err := os.Stat(filepath.Join(nvimCfg, "lua", "configs", "lspconfig.lua")); !os.IsNotExist(err) { t.Fatal("starter lspconfig.lua should be removed") } for _, name := range []string{"arduino.lua", "dap.lua"} { if _, err := os.Stat(filepath.Join(nvimCfg, "lua", "plugins", name)); err != nil { t.Errorf("asset %s missing: %v", name, err) } } if _, err := os.Stat(filepath.Join(nvimCfg, "lua", "mappings.lua")); err != nil { t.Errorf("mappings.lua missing: %v", err) } if _, err := os.Stat(filepath.Join(nvimCfg, "lsp", "clangd.lua")); err != nil { t.Errorf("clangd.lua missing: %v", err) } } func TestPlanAfterApplyIsEmpty(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) r := &fakeRunner{installed: map[string]bool{"neovim": true}} m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "gruvbox", platform.NewPacman(r), platform.GitClient{Runner: r}) st, _ := m.Detect(context.Background()) p, _ := m.Plan(context.Background(), st) _ = m.Apply(context.Background(), p, nopReporter{}) st2, _ := m.Detect(context.Background()) p2, _ := m.Plan(context.Background(), st2) if !p2.IsEmpty() { t.Fatalf("second plan not empty: %+v", p2.Actions) } } func TestUnknownThemeFallsBackToFirst(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) r := &fakeRunner{installed: map[string]bool{"neovim": true}} m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "does-not-exist", platform.NewPacman(r), platform.GitClient{Runner: r}) st, _ := m.Detect(context.Background()) p, _ := m.Plan(context.Background(), st) if err := m.Apply(context.Background(), p, nopReporter{}); err != nil { t.Fatal(err) } chadrc, _ := os.ReadFile(filepath.Join(home, ".config", "nvim", "lua", "chadrc.lua")) // Unknown theme IDs fall back to the first entry of theming.List(), // which is sorted alphabetically by ID (currently "catppuccin"). if !strings.Contains(string(chadrc), `theme = "catppuccin"`) { t.Fatalf("fallback theme not applied: %s", chadrc) } } func TestWriteChadrcWritesFile(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) cfg := filepath.Join(home, ".config", "nvim") if err := os.MkdirAll(cfg, 0o755); err != nil { t.Fatal(err) } if err := WriteChadrc("dracula"); err != nil { t.Fatal(err) } data, err := os.ReadFile(filepath.Join(cfg, "lua", "chadrc.lua")) if err != nil { t.Fatal(err) } if !strings.Contains(string(data), `theme = "dracula"`) { t.Fatalf("theme not applied: %s", data) } } func TestWriteChadrcNoopWithoutConfigDir(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) if err := WriteChadrc("gruvbox"); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(home, ".config", "nvim", "lua", "chadrc.lua")); !os.IsNotExist(err) { t.Fatal("chadrc should not be created on a bare system") } } var _ module.Module = Module{}