github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/shared/triage_metadata_test.go (about) 1 // +build small 2 3 // Copyright 2019 The WPT Dashboard Project. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 package shared 8 9 import ( 10 "context" 11 "encoding/json" 12 "testing" 13 14 "github.com/stretchr/testify/assert" 15 "gopkg.in/yaml.v3" 16 ) 17 18 func TestAppendTestName(t *testing.T) { 19 var actual, expected MetadataResults 20 json.Unmarshal([]byte(`{ 21 "/foo/bar.html": [ 22 { 23 "url": "bugs.bar?id=456", 24 "product": "chrome", 25 "results": [ 26 {"status": 6 } 27 ] 28 } 29 ], 30 "/foo1/bar1.html": [ 31 { 32 "product": "chrome", 33 "url": "bugs.bar", 34 "results": [ 35 {"status": 6, "subtest": "sub-bar1" }, 36 {"status": 3 } 37 ]} 38 ] 39 }`), &actual) 40 41 json.Unmarshal([]byte(`{ 42 "/foo/bar.html": [ 43 { 44 "url": "bugs.bar?id=456", 45 "product": "chrome", 46 "results": [ 47 {"status": 6 } 48 ] 49 } 50 ], 51 "/foo1/bar1.html": [ 52 { 53 "product": "chrome", 54 "url": "bugs.bar", 55 "results": [ 56 {"status": 6, "test": "bar1.html", "subtest": "sub-bar1" }, 57 {"status": 3, "test": "bar1.html"} 58 ]} 59 ] 60 }`), &expected) 61 test := "/foo1/bar1.html" 62 63 appendTestName(test, actual) 64 65 assert.Equal(t, expected, actual) 66 } 67 68 func TestAppendTestName_EmptyResults(t *testing.T) { 69 var actual, expected MetadataResults 70 json.Unmarshal([]byte(`{ 71 "/foo1/bar1.html": [ 72 { 73 "product": "chrome", 74 "url": "bugs.bar" 75 } 76 ] 77 }`), &actual) 78 79 json.Unmarshal([]byte(`{ 80 "/foo1/bar1.html": [ 81 { 82 "product": "chrome", 83 "url": "bugs.bar", 84 "results": [ 85 {"test": "bar1.html"} 86 ]} 87 ] 88 }`), &expected) 89 test := "/foo1/bar1.html" 90 91 appendTestName(test, actual) 92 93 assert.Equal(t, expected, actual) 94 } 95 96 func TestAddToFiles_AddNewFile(t *testing.T) { 97 var amendment MetadataResults 98 json.Unmarshal([]byte(`{ 99 "/foo/foo1/bar.html": [ 100 { 101 "url": "bugs.bar?id=456", 102 "product": "chrome", 103 "results": [ 104 {"subtest": "sub-bar1"} 105 ] 106 } 107 ] 108 }`), &amendment) 109 110 var path = "a" 111 var fileMap = make(map[string]Metadata) 112 fileInBytes := []byte(` 113 links: 114 - product: chrome-64 115 url: https://external.com/item 116 results: 117 - test: a.html 118 - product: firefox 119 url: https://bug.com/item 120 results: 121 - test: b.html 122 subtest: Something should happen 123 status: FAIL 124 - test: c.html 125 `) 126 var file Metadata 127 yaml.Unmarshal(fileInBytes, &file) 128 fileMap[path] = file 129 130 actualMap := addToFiles(amendment, fileMap, NewNilLogger()) 131 132 assert.Equal(t, 1, len(actualMap)) 133 actualInBytes, ok := actualMap["foo/foo1"] 134 assert.True(t, ok) 135 136 var actual Metadata 137 yaml.Unmarshal(actualInBytes, &actual) 138 assert.Equal(t, 1, len(actual.Links)) 139 assert.Equal(t, "chrome", actual.Links[0].Product.BrowserName) 140 assert.Equal(t, "bugs.bar?id=456", actual.Links[0].URL) 141 assert.Equal(t, 1, len(actual.Links[0].Results)) 142 assert.Equal(t, "bar.html", actual.Links[0].Results[0].TestPath) 143 assert.Equal(t, "sub-bar1", *actual.Links[0].Results[0].SubtestName) 144 } 145 146 func TestAddToFiles_AddNewMetadataResult(t *testing.T) { 147 var amendment MetadataResults 148 json.Unmarshal([]byte(`{ 149 "/foo/foo1/a.html": [ 150 { 151 "url": "foo", 152 "product": "chrome", 153 "results": [ 154 {"status": 6, "subtest": "sub-a" } 155 ] 156 } 157 ] 158 }`), &amendment) 159 160 var path = "foo/foo1" 161 var fileMap = make(map[string]Metadata) 162 fileInBytes := []byte(` 163 links: 164 - product: chrome 165 url: foo 166 results: 167 - test: b.html 168 - product: firefox 169 url: https://bug.com/item 170 results: 171 - test: b.html 172 subtest: Something should happen 173 status: FAIL 174 - test: c.html 175 `) 176 var file Metadata 177 yaml.Unmarshal(fileInBytes, &file) 178 fileMap[path] = file 179 180 actualMap := addToFiles(amendment, fileMap, NewNilLogger()) 181 182 assert.Equal(t, 1, len(actualMap)) 183 actualInBytes, ok := actualMap["foo/foo1"] 184 assert.True(t, ok) 185 186 var actual Metadata 187 yaml.Unmarshal(actualInBytes, &actual) 188 assert.Equal(t, 2, len(actual.Links)) 189 assert.Equal(t, "chrome", actual.Links[0].Product.BrowserName) 190 assert.Equal(t, "foo", actual.Links[0].URL) 191 assert.Equal(t, 2, len(actual.Links[0].Results)) 192 assert.Equal(t, "b.html", actual.Links[0].Results[0].TestPath) 193 assert.Equal(t, "a.html", actual.Links[0].Results[1].TestPath) 194 assert.Equal(t, "sub-a", *actual.Links[0].Results[1].SubtestName) 195 assert.Equal(t, TestStatusFail, *actual.Links[0].Results[1].Status) 196 assert.Equal(t, "firefox", actual.Links[1].Product.BrowserName) 197 assert.Equal(t, "https://bug.com/item", actual.Links[1].URL) 198 } 199 200 func TestAddToFiles_AddNewMetadataLink(t *testing.T) { 201 var amendment MetadataResults 202 json.Unmarshal([]byte(`{ 203 "/foo/foo1/a.html": [ 204 { 205 "url": "foo1", 206 "product": "chrome", 207 "results": [ 208 {"status": 6 } 209 ] 210 } 211 ] 212 }`), &amendment) 213 214 var path = "foo/foo1" 215 var fileMap = make(map[string]Metadata) 216 fileInBytes := []byte(` 217 links: 218 - product: chrome 219 url: foo 220 results: 221 - test: b.html 222 - product: firefox 223 url: https://bug.com/item 224 results: 225 - test: b.html 226 subtest: Something should happen 227 status: FAIL 228 - test: c.html 229 `) 230 var file Metadata 231 yaml.Unmarshal(fileInBytes, &file) 232 fileMap[path] = file 233 234 actualMap := addToFiles(amendment, fileMap, NewNilLogger()) 235 236 assert.Equal(t, 1, len(actualMap)) 237 actualInBytes, ok := actualMap["foo/foo1"] 238 assert.True(t, ok) 239 240 var actual Metadata 241 yaml.Unmarshal(actualInBytes, &actual) 242 assert.Equal(t, 3, len(actual.Links)) 243 assert.Equal(t, "chrome", actual.Links[0].Product.BrowserName) 244 assert.Equal(t, "foo", actual.Links[0].URL) 245 assert.Equal(t, 1, len(actual.Links[0].Results)) 246 assert.Equal(t, "b.html", actual.Links[0].Results[0].TestPath) 247 assert.Equal(t, "firefox", actual.Links[1].Product.BrowserName) 248 assert.Equal(t, "https://bug.com/item", actual.Links[1].URL) 249 assert.Equal(t, "chrome", actual.Links[2].Product.BrowserName) 250 assert.Equal(t, "foo1", actual.Links[2].URL) 251 assert.Equal(t, "a.html", actual.Links[2].Results[0].TestPath) 252 } 253 254 func TestAddToFiles_AddNewMetadataLink_Label(t *testing.T) { 255 var amendment MetadataResults 256 json.Unmarshal([]byte(`{ 257 "/foo/foo1/abc.html": [ 258 { 259 "label": "interop" 260 } 261 ] 262 }`), &amendment) 263 264 var path = "foo/foo1" 265 var fileMap = make(map[string]Metadata) 266 fileInBytes := []byte(` 267 links: 268 - product: chrome 269 url: foo 270 results: 271 - test: b.html 272 - product: firefox 273 url: https://bug.com/item 274 results: 275 - test: b.html 276 subtest: Something should happen 277 status: FAIL 278 - test: c.html 279 `) 280 var file Metadata 281 yaml.Unmarshal(fileInBytes, &file) 282 fileMap[path] = file 283 284 actualMap := addToFiles(amendment, fileMap, NewNilLogger()) 285 286 assert.Equal(t, 1, len(actualMap)) 287 actualInBytes, ok := actualMap["foo/foo1"] 288 assert.True(t, ok) 289 290 var actual Metadata 291 yaml.Unmarshal(actualInBytes, &actual) 292 assert.Equal(t, 3, len(actual.Links)) 293 assert.Equal(t, "chrome", actual.Links[0].Product.BrowserName) 294 assert.Equal(t, "foo", actual.Links[0].URL) 295 assert.Equal(t, 1, len(actual.Links[0].Results)) 296 assert.Equal(t, "b.html", actual.Links[0].Results[0].TestPath) 297 assert.Equal(t, "firefox", actual.Links[1].Product.BrowserName) 298 assert.Equal(t, "https://bug.com/item", actual.Links[1].URL) 299 assert.Equal(t, "", actual.Links[2].Product.String()) 300 assert.Equal(t, "", actual.Links[2].URL) 301 assert.Equal(t, "interop", actual.Links[2].Label) 302 } 303 304 func TestAddToFiles_AddNewMetadataResults_Label(t *testing.T) { 305 var amendment MetadataResults 306 json.Unmarshal([]byte(`{ 307 "/foo/foo1/abc.html": [ 308 { 309 "label": "interop" 310 } 311 ] 312 }`), &amendment) 313 314 var path = "foo/foo1" 315 var fileMap = make(map[string]Metadata) 316 fileInBytes := []byte(` 317 links: 318 - label: interop 319 results: 320 - test: b.html 321 `) 322 var file Metadata 323 yaml.Unmarshal(fileInBytes, &file) 324 fileMap[path] = file 325 326 actualMap := addToFiles(amendment, fileMap, NewNilLogger()) 327 328 assert.Equal(t, 1, len(actualMap)) 329 actualInBytes, ok := actualMap["foo/foo1"] 330 assert.True(t, ok) 331 332 var actual Metadata 333 yaml.Unmarshal(actualInBytes, &actual) 334 assert.Equal(t, 1, len(actual.Links)) 335 assert.Equal(t, "", actual.Links[0].Product.BrowserName) 336 assert.Equal(t, "interop", actual.Links[0].Label) 337 assert.Equal(t, 2, len(actual.Links[0].Results)) 338 assert.Equal(t, "b.html", actual.Links[0].Results[0].TestPath) 339 assert.Equal(t, "abc.html", actual.Links[0].Results[1].TestPath) 340 } 341 342 func TestAddToFiles_AddNewMetadataLink_Asterisk(t *testing.T) { 343 var amendment MetadataResults 344 json.Unmarshal([]byte(`{ 345 "/foo/foo1/*": [ 346 { 347 "url": "foo1", 348 "product": "chrome", 349 "results": [ 350 {"status": 6 } 351 ] 352 } 353 ] 354 }`), &amendment) 355 356 var path = "foo/foo1" 357 var fileMap = make(map[string]Metadata) 358 fileInBytes := []byte(` 359 links: 360 - product: chrome 361 url: foo 362 results: 363 - test: b.html 364 - product: firefox 365 url: https://bug.com/item 366 results: 367 - test: b.html 368 subtest: Something should happen 369 status: FAIL 370 - test: c.html 371 `) 372 var file Metadata 373 yaml.Unmarshal(fileInBytes, &file) 374 fileMap[path] = file 375 376 actualMap := addToFiles(amendment, fileMap, NewNilLogger()) 377 378 assert.Equal(t, 1, len(actualMap)) 379 actualInBytes, ok := actualMap["foo/foo1"] 380 assert.True(t, ok) 381 382 var actual Metadata 383 yaml.Unmarshal(actualInBytes, &actual) 384 assert.Equal(t, 3, len(actual.Links)) 385 assert.Equal(t, "chrome", actual.Links[0].Product.BrowserName) 386 assert.Equal(t, "foo", actual.Links[0].URL) 387 assert.Equal(t, 1, len(actual.Links[0].Results)) 388 assert.Equal(t, "b.html", actual.Links[0].Results[0].TestPath) 389 assert.Equal(t, "firefox", actual.Links[1].Product.BrowserName) 390 assert.Equal(t, "https://bug.com/item", actual.Links[1].URL) 391 assert.Equal(t, "chrome", actual.Links[2].Product.BrowserName) 392 assert.Equal(t, "foo1", actual.Links[2].URL) 393 assert.Equal(t, "*", actual.Links[2].Results[0].TestPath) 394 } 395 396 func TestNewTriageMetadata_email_fallback(t *testing.T) { 397 // They won't be used in the constructor. 398 ctx := context.Background() 399 fetcher := MetadataFetcher(nil) 400 401 m := NewTriageMetadata(ctx, nil, "testuser", "testemail@example.com", fetcher).(triageMetadata) 402 assert.Equal(t, m.authorName, "testuser") 403 assert.Equal(t, m.authorEmail, "testemail@example.com") 404 405 m = NewTriageMetadata(ctx, nil, "testuser", "", fetcher).(triageMetadata) 406 assert.Equal(t, m.authorName, "testuser") 407 assert.Equal(t, m.authorEmail, "testuser@users.noreply.github.com") 408 } 409 410 func TestContainsInterop_True(t *testing.T) { 411 var amendment MetadataResults 412 json.Unmarshal([]byte(`{ 413 "/foo/foo1/abc.html": [ 414 { 415 "label": "interop-x" 416 } 417 ] 418 }`), &amendment) 419 420 actual := containsInterop(amendment) 421 422 assert.True(t, actual) 423 } 424 425 func TestContainsInterop_NotInteropLabel(t *testing.T) { 426 var amendment MetadataResults 427 json.Unmarshal([]byte(`{ 428 "/foo/foo1/abc.html": [ 429 { 430 "label": "lets-go-interoperability" 431 } 432 ] 433 }`), &amendment) 434 435 actual := containsInterop(amendment) 436 437 assert.False(t, actual) 438 } 439 440 func TestContainsInterop_False(t *testing.T) { 441 var amendment MetadataResults 442 json.Unmarshal([]byte(`{ 443 "/foo/foo1/*": [ 444 { 445 "url": "foo1", 446 "product": "chrome", 447 "results": [ 448 {"status": 6 } 449 ] 450 } 451 ] 452 }`), &amendment) 453 454 actual := containsInterop(amendment) 455 456 assert.False(t, actual) 457 } 458