package zellij import ( "context" "net/http" "net/http/httptest" "os" "path/filepath" "testing" "gitlab.hive-brain.de/MDietermann/Archduino/internal/config" "gitlab.hive-brain.de/MDietermann/Archduino/internal/module" "gitlab.hive-brain.de/MDietermann/Archduino/internal/platform" ) type fakeRunner struct { runCalls [][]string 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...)) 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, errOne } return nil, nil } type eo struct{} func (eo) Error() string { return "exit 1" } var errOne = eo{} type nop struct{} func (nop) Progress(string, int, int, string) {} func (nop) Log(string, string) {} func TestZellijPlanAppliesInstallAndConfig(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("keybinds {}\n")) })) defer srv.Close() r := &fakeRunner{installed: map[string]bool{}} m := New(config.ZellijConfig{ConfigURL: srv.URL}, platform.NewPacman(r)) st, _ := m.Detect(context.Background()) p, _ := m.Plan(context.Background(), st) if err := m.Apply(context.Background(), p, nop{}); err != nil { t.Fatal(err) } got, err := os.ReadFile(filepath.Join(home, ".config", "zellij", "config.kdl")) if err != nil { t.Fatalf("config not written: %v", err) } if string(got) != "keybinds {}\n" { t.Fatalf("unexpected content %q", got) } } func TestZellijInstalledPresentEmptyPlan(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) r := &fakeRunner{installed: map[string]bool{"zellij": true}} m := New(config.ZellijConfig{ConfigURL: ""}, platform.NewPacman(r)) st, _ := m.Detect(context.Background()) p, _ := m.Plan(context.Background(), st) if !p.IsEmpty() { t.Fatalf("want empty plan, got %+v", p) } } func TestPlanForceReinstallsAndRewritesConfig(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) os.MkdirAll(filepath.Join(home, ".config", "zellij"), 0o755) os.WriteFile(filepath.Join(home, ".config", "zellij", "config.kdl"), []byte("old\n"), 0o644) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("fresh {}\n")) })) defer srv.Close() r := &fakeRunner{installed: map[string]bool{"zellij": true}} m := New(config.ZellijConfig{ConfigURL: srv.URL}, platform.NewPacman(r)) plan, err := m.PlanForce(context.Background()) if err != nil { t.Fatalf("PlanForce: %v", err) } if plan.IsEmpty() { t.Fatal("force plan must not be empty") } for _, a := range plan.Actions { if err := a.Exec(context.Background()); err != nil { t.Fatalf("Exec: %v", err) } } sawReinstall := false for _, c := range r.runCalls { if len(c) >= 4 && c[0] == "sudo" && c[1] == "pacman" && c[2] == "-S" && c[3] == "--noconfirm" { sawReinstall = true } } if !sawReinstall { t.Fatalf("no pacman reinstall call; calls=%v", r.runCalls) } got, err := os.ReadFile(filepath.Join(home, ".config", "zellij", "config.kdl")) if err != nil { t.Fatalf("config not written: %v", err) } if string(got) != "fresh {}\n" { t.Fatalf("config not overwritten: %q", got) } } var _ module.Module = Module{}