go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/frontend/ui/console_test.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ui 16 17 import ( 18 "testing" 19 20 "go.chromium.org/luci/milo/internal/model" 21 22 . "github.com/smartystreets/goconvey/convey" 23 ) 24 25 type testBuilder struct { 26 Builder *BuilderRef 27 Category []string 28 } 29 30 // Test helpers 31 func buildVerifyRoot(name string, builders []testBuilder, expectChildren int) *Category { 32 root := NewCategory(name) 33 for _, builder := range builders { 34 root.AddBuilder(builder.Category, builder.Builder) 35 } 36 So(len(root.Children()), ShouldEqual, expectChildren) 37 So(root.Name, ShouldEqual, name) 38 return root 39 } 40 41 func verifyCategory(e ConsoleElement, expectChildren int, expectName string) *Category { 42 cat := e.(*Category) 43 So(len(cat.Children()), ShouldEqual, expectChildren) 44 So(cat.Name, ShouldEqual, expectName) 45 return cat 46 } 47 48 func TestCategory(t *testing.T) { 49 Convey("Category structure", t, func() { 50 // Test structures 51 var emptycat []string 52 cat1 := []string{"66__bbl"} 53 cat2 := []string{"test.data"} 54 deepcat := []string{"Hi", "Goodbye"} 55 br1 := &BuilderRef{ 56 ID: "test 1", 57 ShortName: "t1", 58 Build: []*model.BuildSummary{}, 59 } 60 br2 := &BuilderRef{ 61 ID: "test 2", 62 ShortName: "t2", 63 Build: []*model.BuildSummary{}, 64 } 65 66 // Tests 67 Convey("Root category", func() { 68 buildVerifyRoot("root", []testBuilder{}, 0) 69 }) 70 71 Convey("With builder", func() { 72 root := buildVerifyRoot("_root_", []testBuilder{{br1, emptycat}}, 1) 73 So(root.Children()[0].(*BuilderRef).ID, ShouldEqual, br1.ID) 74 }) 75 76 Convey("With nested categories", func() { 77 root := buildVerifyRoot("o_o", []testBuilder{{br1, deepcat}}, 1) 78 child1 := verifyCategory(root.Children()[0], 1, deepcat[0]) 79 child2 := verifyCategory(child1.Children()[0], 1, deepcat[1]) 80 So(child2.Children()[0].(*BuilderRef).ID, ShouldEqual, br1.ID) 81 }) 82 83 Convey("Multiple categories", func() { 84 root := buildVerifyRoot("@_@", []testBuilder{ 85 {br1, cat1}, 86 {br2, cat2}, 87 }, 2) 88 child1 := verifyCategory(root.Children()[0], 1, cat1[0]) 89 So(child1.Children()[0].(*BuilderRef).ID, ShouldEqual, br1.ID) 90 child2 := verifyCategory(root.Children()[1], 1, cat2[0]) 91 So(child2.Children()[0].(*BuilderRef).ID, ShouldEqual, br2.ID) 92 }) 93 94 Convey("Reusing existing categories", func() { 95 root := buildVerifyRoot("rut", []testBuilder{ 96 {br1, cat1}, 97 {br2, cat1}, 98 }, 1) 99 child := verifyCategory(root.Children()[0], 2, cat1[0]) 100 So(child.Children()[0].(*BuilderRef).ID, ShouldEqual, br1.ID) 101 So(child.Children()[1].(*BuilderRef).ID, ShouldEqual, br2.ID) 102 }) 103 104 Convey("Caches number of leaf nodes in a category", func() { 105 root := buildVerifyRoot("rut", []testBuilder{{br1, cat1}}, 1) 106 So(root.cachedNumLeafNodes, ShouldEqual, -1) 107 So(root.Children()[0].(*Category).cachedNumLeafNodes, ShouldEqual, -1) 108 So(root.NumLeafNodes(), ShouldEqual, 1) 109 So(root.cachedNumLeafNodes, ShouldEqual, 1) 110 So(root.Children()[0].(*Category).cachedNumLeafNodes, ShouldEqual, 1) 111 112 root.AddBuilder(cat1, br2) // this must invalidate cached values 113 So(root.cachedNumLeafNodes, ShouldEqual, -1) 114 So(root.Children()[0].(*Category).cachedNumLeafNodes, ShouldEqual, -1) 115 So(root.NumLeafNodes(), ShouldEqual, 2) 116 }) 117 }) 118 }