package catalogue import ( "reflect" "strings" "testing" ) // Defends novox/hq ADR 0210 ยง3: a contribution is a dependency on the seat that receives it. func TestAContributionDependsOnTheSeatThatPlacesIt(t *testing.T) { env := mod("theme", nil, nil, nil) env.Environment = &Environment{Variables: map[string]string{"GTK_THEME": "Adwaita:dark"}} path := mod("toolchain", nil, nil, nil) path.Environment = &Environment{Path: []PathEntry{{Entry: "/opt/x/bin"}}} shell := mod("prompt", nil, nil, nil) shell.Shell = []ShellCode{{For: "zsh", Slot: "first", Code: "true"}} session := mod("wallpaper", nil, nil, nil) session.Shell = []ShellCode{{For: "xinitrc", Slot: "normal", Code: "true"}} resources := mod("bar", nil, nil, nil) resources.Shell = []ShellCode{{For: "xresources", Slot: "normal", Code: "x: y"}} empty := mod("nothing", nil, nil, nil) empty.Environment = &Environment{} cases := map[string]struct { m Manifest want []string }{ "a variable": {env, []string{EnvironmentSeat}}, "a path entry": {path, []string{EnvironmentSeat}}, "shell code": {shell, []string{LoginShellSeat}}, "the session's start": {session, []string{DisplayServerSeat}}, "the session's X resources": {resources, []string{DisplayServerSeat}}, "an empty environment": {empty, nil}, } for name, c := range cases { got := DependsOn(c.m) if len(got) == 0 { got = nil } if !reflect.DeepEqual(got, c.want) { t.Errorf("%s depends on %v, want %v", name, got, c.want) } } } func TestAContributionIsMetByAHolderOnTheNodeAndRefusedWithout(t *testing.T) { holder := mod("node-env", nil, nil, nil, Claim{Name: EnvironmentSeat}) contributor := mod("theme", nil, nil, nil) contributor.Environment = &Environment{Variables: map[string]string{"GTK_THEME": "Adwaita:dark"}} catalogue := map[string]Manifest{"node-env": holder, "theme": contributor} if _, err := AssignRefusal(catalogue, "laptop", []string{"node-env"}, []string{"theme"}); err != nil { t.Fatalf("a contributor beside the holder is refused: %v", err) } _, err := AssignRefusal(catalogue, "laptop", nil, []string{"theme"}) if err == nil { t.Fatal("a contributor on a node without the holder was accepted, and its contribution would be written nowhere") } if !strings.Contains(err.Error(), EnvironmentSeat) || !strings.Contains(err.Error(), "node-env") { t.Errorf("the refusal names neither the seat nor its holder: %v", err) } if _, err := AssignRefusal(catalogue, "laptop", nil, []string{"theme", "node-env"}); err != nil { t.Errorf("the contributor and the holder assigned together are refused: %v", err) } } func TestAHolderMeetsItsOwnContribution(t *testing.T) { // The display server's module contributes nothing to its own seat today, but the zsh module's // environment contributions do go to another seat: a claim meets only the seat it names. zsh := mod("zsh", nil, nil, nil, Claim{Name: LoginShellSeat}) zsh.Shell = []ShellCode{{For: "zsh", Slot: "normal", Code: "true"}} zsh.Environment = &Environment{Variables: map[string]string{"EDITOR": "vim"}} catalogue := map[string]Manifest{"zsh": zsh, "node-env": mod("node-env", nil, nil, nil, Claim{Name: EnvironmentSeat})} unheld := UnheldDependencies(catalogue, "laptop", []Manifest{zsh}, nil) if len(unheld) != 1 || unheld[0].Seat != EnvironmentSeat { t.Errorf("zsh alone: unheld %v, want only %s (its shell code is its own seat's)", unheld, EnvironmentSeat) } } func TestTwoModulesDeclaringOnePackageAreRefusedBeforeAnythingIsRecorded(t *testing.T) { pacman := withResources(mod("pacman", nil, nil, nil, Claim{Name: PackageManagerSeat}), res("package", "pacman-contrib")) bar := withResources(mod("bar", nil, nil, nil), res("package", "bar"), res("package", "pacman-contrib")) other := withResources(mod("other", nil, nil, nil), res("package", "other")) catalogue := map[string]Manifest{"pacman": pacman, "bar": bar, "other": other} err := CollisionRefusal(catalogue, "laptop", []string{"pacman"}, []string{"bar"}) if err == nil || !strings.Contains(err.Error(), "pacman-contrib") || !strings.Contains(err.Error(), "bar") { t.Fatalf("the second owner of a package was not refused by name: %v", err) } if err := CollisionRefusal(catalogue, "laptop", []string{"pacman"}, []string{"other"}); err != nil { t.Errorf("a module declaring nothing shared is refused: %v", err) } // A collision already on the node is status's, not a reason to refuse an unrelated assignment. if err := CollisionRefusal(catalogue, "laptop", []string{"pacman", "bar"}, []string{"other"}); err != nil { t.Errorf("an unrelated assignment is refused for a collision already there: %v", err) } } func TestCodeForAPowerMomentDependsOnThePowerSeat(t *testing.T) { for _, moment := range powerMoments { m := mod("laptop", nil, nil, nil) m.Shell = []ShellCode{{For: moment, Slot: "normal", Code: "true"}} if got := DependsOn(m); !reflect.DeepEqual(got, []string{PowerSeat}) { t.Errorf("code for %s depends on %v, want %s", moment, got, PowerSeat) } if p := m.shellProblems(); len(p) != 0 { t.Errorf("code for %s is refused: %v", moment, p) } } m := mod("laptop", nil, nil, nil) m.Shell = []ShellCode{{For: "after-lunch", Slot: "normal", Code: "true"}} if p := m.shellProblems(); len(p) == 0 { t.Error("code for a moment that does not exist was accepted") } if s, ok := SeatNamed(PowerSeat); !ok || s.Scope != ScopeNode { t.Errorf("%s is not a node seat of the mesh's own: %+v %v", PowerSeat, s, ok) } }