github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/shared/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  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"gopkg.in/yaml.v3"
    14  )
    15  
    16  func TestMarshalMetadata(t *testing.T) {
    17  	var metadata Metadata
    18  	var metadataInBytes = []byte(`
    19  links:
    20    - product: chrome-64
    21      url: https://external.com/item
    22      results:
    23      - test: a.html
    24        label: labelA
    25      - test: a.html
    26    - product: firefox-2
    27      url: https://bug.com/item
    28      results:
    29      - test: b.html
    30        subtest: Something should happen
    31        status: FAIL
    32      - test: c.html
    33    - url: https://github-issue.com/1234
    34      label: labelB
    35      results:
    36      - test: d.html
    37      - test: e.html
    38  `)
    39  	yaml.Unmarshal(metadataInBytes, &metadata)
    40  	acutalInBytes, _ := yaml.Marshal(metadata)
    41  
    42  	var actual Metadata
    43  	yaml.Unmarshal(acutalInBytes, &actual)
    44  
    45  	assert.Equal(t, metadata, actual)
    46  }
    47  
    48  func TestParseMetadata(t *testing.T) {
    49  	var path = "foo/bar"
    50  	var metadataByteMap = make(map[string][]byte)
    51  	metadataByteMap[path] = []byte(`
    52  links:
    53    - product: chrome-64
    54      url: https://external.com/item
    55      results:
    56      - test: a.html
    57    - product: firefox-2
    58      url: https://bug.com/item
    59      results:
    60      - test: b.html
    61        subtest: Something should happen
    62        status: FAIL
    63      - test: c.html
    64    - label: labelA
    65      results:
    66      - test: c.html
    67      - test: e.html
    68  `)
    69  
    70  	metadatamap := parseMetadata(metadataByteMap, NewNilLogger())
    71  
    72  	assert.Len(t, metadatamap, 1)
    73  	assert.Len(t, metadatamap[path].Links, 3)
    74  	assert.Equal(t, "chrome", metadatamap[path].Links[0].Product.BrowserName)
    75  	assert.Equal(t, "64", metadatamap[path].Links[0].Product.BrowserVersion)
    76  	assert.Equal(t, "a.html", metadatamap[path].Links[0].Results[0].TestPath)
    77  	assert.Equal(t, "https://external.com/item", metadatamap[path].Links[0].URL)
    78  	assert.Equal(t, "firefox", metadatamap[path].Links[1].Product.BrowserName)
    79  	assert.Equal(t, "2", metadatamap[path].Links[1].Product.BrowserVersion)
    80  	assert.Equal(t, "b.html", metadatamap[path].Links[1].Results[0].TestPath)
    81  	assert.Equal(t, "Something should happen", *(metadatamap[path].Links[1].Results[0].SubtestName))
    82  	assert.Equal(t, TestStatusFail, *(metadatamap[path].Links[1].Results[0].Status))
    83  	assert.Equal(t, "https://bug.com/item", metadatamap[path].Links[1].URL)
    84  	assert.Len(t, metadatamap[path].Links[1].Results, 2)
    85  	assert.Equal(t, "b.html", metadatamap[path].Links[1].Results[0].TestPath)
    86  	assert.Equal(t, "Something should happen", *(metadatamap[path].Links[1].Results[0].SubtestName))
    87  	assert.Equal(t, TestStatusFail, *(metadatamap[path].Links[1].Results[0].Status))
    88  	assert.Equal(t, "labelA", metadatamap[path].Links[2].Label)
    89  	assert.Equal(t, "c.html", metadatamap[path].Links[2].Results[0].TestPath)
    90  	assert.Equal(t, "e.html", metadatamap[path].Links[2].Results[1].TestPath)
    91  }
    92  
    93  func TestConstructMetadataResponse_OneLink(t *testing.T) {
    94  	productSpecs := []ProductSpec{
    95  		ParseProductSpecUnsafe("Firefox-54"),
    96  		ParseProductSpecUnsafe("Chrome"),
    97  	}
    98  	subtestName := "Something should happen"
    99  	fail := TestStatusFail
   100  	label := "labelA"
   101  	labelB := "labelB"
   102  	metadataMap := map[string]Metadata{
   103  		"foo/bar": Metadata{
   104  			Links: []MetadataLink{
   105  				MetadataLink{
   106  					Product: ParseProductSpecUnsafe("ChrOme"),
   107  					URL:     "https://external.com/item",
   108  					Results: []MetadataTestResult{{
   109  						TestPath: "a.html",
   110  					}},
   111  				},
   112  				MetadataLink{
   113  					Product: ParseProductSpecUnsafe("Firefox"),
   114  					URL:     "https://bug.com/item",
   115  					Results: []MetadataTestResult{{
   116  						TestPath:    "a.html",
   117  						SubtestName: &subtestName,
   118  						Status:      &fail,
   119  					}},
   120  				},
   121  				MetadataLink{
   122  					Label: label,
   123  					Results: []MetadataTestResult{{
   124  						TestPath: "a.html",
   125  					}},
   126  				},
   127  				MetadataLink{
   128  					Label: labelB,
   129  					Results: []MetadataTestResult{
   130  						{
   131  							TestPath: "a.html",
   132  						},
   133  						{
   134  							TestPath: "e.html",
   135  						},
   136  					},
   137  				},
   138  			},
   139  		},
   140  	}
   141  
   142  	MetadataResults := constructMetadataResponse(productSpecs, true, metadataMap)
   143  
   144  	assert.Equal(t, 2, len(MetadataResults))
   145  	assert.Equal(t, 4, len(MetadataResults["/foo/bar/a.html"]))
   146  	assert.Equal(t, "https://external.com/item", MetadataResults["/foo/bar/a.html"][0].URL)
   147  	assert.True(t, ParseProductSpecUnsafe("chrome").MatchesProductSpec(MetadataResults["/foo/bar/a.html"][0].Product))
   148  	assert.Equal(t, "https://bug.com/item", MetadataResults["/foo/bar/a.html"][1].URL)
   149  	assert.True(t, ParseProductSpecUnsafe("firefox").MatchesProductSpec(MetadataResults["/foo/bar/a.html"][1].Product))
   150  	assert.Equal(t, label, MetadataResults["/foo/bar/a.html"][2].Label)
   151  	assert.Equal(t, labelB, MetadataResults["/foo/bar/a.html"][3].Label)
   152  	assert.Equal(t, labelB, MetadataResults["/foo/bar/e.html"][0].Label)
   153  
   154  	// Remove test-level metadata.
   155  	MetadataResults = constructMetadataResponse(productSpecs, false, metadataMap)
   156  	assert.Equal(t, 1, len(MetadataResults))
   157  	assert.Equal(t, 2, len(MetadataResults["/foo/bar/a.html"]))
   158  	assert.Equal(t, "https://external.com/item", MetadataResults["/foo/bar/a.html"][0].URL)
   159  	assert.True(t, ParseProductSpecUnsafe("chrome").MatchesProductSpec(MetadataResults["/foo/bar/a.html"][0].Product))
   160  	assert.Equal(t, "https://bug.com/item", MetadataResults["/foo/bar/a.html"][1].URL)
   161  	assert.True(t, ParseProductSpecUnsafe("firefox").MatchesProductSpec(MetadataResults["/foo/bar/a.html"][1].Product))
   162  }
   163  
   164  func TestConstructMetadataResponse_NoMatchingLink(t *testing.T) {
   165  	productSpecs := []ProductSpec{
   166  		ParseProductSpecUnsafe("Firefox-54"),
   167  		ParseProductSpecUnsafe("Firefox"),
   168  	}
   169  	metadataMap := map[string]Metadata{
   170  		"foo/bar": Metadata{
   171  			Links: []MetadataLink{
   172  				MetadataLink{
   173  					Product: ParseProductSpecUnsafe("ChrOme"),
   174  					URL:     "https://external.com/item",
   175  					Results: []MetadataTestResult{{
   176  						TestPath: "a.html",
   177  					}},
   178  				},
   179  				MetadataLink{
   180  					Product: ParseProductSpecUnsafe("safari"),
   181  					URL:     "https://bug.com/item",
   182  					Results: []MetadataTestResult{{
   183  						TestPath: "a.html",
   184  					}},
   185  				},
   186  			},
   187  		},
   188  	}
   189  
   190  	MetadataResults := constructMetadataResponse(productSpecs, true, metadataMap)
   191  	assert.Equal(t, 0, len(MetadataResults))
   192  
   193  	// There is no test-level metadata here, so it should return the same results
   194  	// whether we pass true or false.
   195  	MetadataResults = constructMetadataResponse(productSpecs, false, metadataMap)
   196  	assert.Equal(t, 0, len(MetadataResults))
   197  }
   198  
   199  func TestConstructMetadataResponse_MultipleLinks(t *testing.T) {
   200  	productSpecs := []ProductSpec{
   201  		ParseProductSpecUnsafe("Firefox-54"),
   202  		ParseProductSpecUnsafe("Chrome"),
   203  	}
   204  	metadataMap := map[string]Metadata{
   205  		"foo/bar": Metadata{
   206  			Links: []MetadataLink{
   207  				MetadataLink{
   208  					Product: ProductSpec{},
   209  					URL:     "https://test.com/item",
   210  					Results: []MetadataTestResult{{
   211  						TestPath: "c.html",
   212  					}},
   213  				},
   214  				MetadataLink{
   215  					Product: ParseProductSpecUnsafe("ChrOme"),
   216  					URL:     "https://external.com/item",
   217  					Results: []MetadataTestResult{{
   218  						TestPath: "b.html",
   219  					}},
   220  				},
   221  				MetadataLink{
   222  					Product: ParseProductSpecUnsafe("Firefox"),
   223  					URL:     "https://bug.com/item",
   224  					Results: []MetadataTestResult{{
   225  						TestPath: "a.html",
   226  					}},
   227  				},
   228  			},
   229  		},
   230  	}
   231  
   232  	MetadataResults := constructMetadataResponse(productSpecs, true, metadataMap)
   233  	assert.Equal(t, 3, len(MetadataResults))
   234  	assert.Equal(t, MetadataResults["/foo/bar/a.html"][0].URL, "https://bug.com/item")
   235  	assert.Equal(t, MetadataResults["/foo/bar/b.html"][0].URL, "https://external.com/item")
   236  	assert.Equal(t, MetadataResults["/foo/bar/c.html"][0].URL, "https://test.com/item")
   237  
   238  	// The test-level issue should only match if we pass includeTestLevel.
   239  	MetadataResults = constructMetadataResponse(productSpecs, false, metadataMap)
   240  	assert.Equal(t, 2, len(MetadataResults))
   241  	assert.Equal(t, MetadataResults["/foo/bar/a.html"][0].URL, "https://bug.com/item")
   242  	assert.Equal(t, MetadataResults["/foo/bar/b.html"][0].URL, "https://external.com/item")
   243  }
   244  
   245  func TestConstructMetadataResponse_OneMatchingBrowserVersion(t *testing.T) {
   246  	productSpecs := []ProductSpec{
   247  		ParseProductSpecUnsafe("Firefox-54"),
   248  		ParseProductSpecUnsafe("Chrome-1"),
   249  	}
   250  	metadataMap := map[string]Metadata{
   251  		"foo/bar": Metadata{
   252  			Links: []MetadataLink{
   253  				MetadataLink{
   254  					Product: ParseProductSpecUnsafe("ChrOme-2"),
   255  					URL:     "https://external.com/item",
   256  					Results: []MetadataTestResult{{
   257  						TestPath: "b.html",
   258  					}},
   259  				},
   260  				MetadataLink{
   261  					Product: ParseProductSpecUnsafe("Firefox-54"),
   262  					URL:     "https://bug.com/item",
   263  					Results: []MetadataTestResult{{
   264  						TestPath: "a.html",
   265  					}},
   266  				},
   267  			},
   268  		},
   269  	}
   270  
   271  	MetadataResults := constructMetadataResponse(productSpecs, true, metadataMap)
   272  	assert.Equal(t, 1, len(MetadataResults))
   273  	assert.Equal(t, MetadataResults["/foo/bar/a.html"][0].URL, "https://bug.com/item")
   274  
   275  	// There is no test-level metadata here, so it should return the same results
   276  	// whether we pass true or false.
   277  	MetadataResults = constructMetadataResponse(productSpecs, false, metadataMap)
   278  	assert.Equal(t, 1, len(MetadataResults))
   279  	assert.Equal(t, MetadataResults["/foo/bar/a.html"][0].URL, "https://bug.com/item")
   280  }
   281  
   282  func TestConstructMetadataResponse_TestIssueMetadata(t *testing.T) {
   283  	productSpecs := []ProductSpec{
   284  		ParseProductSpecUnsafe("Firefox-54"),
   285  		ParseProductSpecUnsafe("Chrome"),
   286  		ParseProductSpecUnsafe("Safari"),
   287  	}
   288  	metadataMap := map[string]Metadata{
   289  		"foo/bar": Metadata{
   290  			Links: []MetadataLink{
   291  				MetadataLink{
   292  					Product: ProductSpec{},
   293  					URL:     "https://bug.com/item",
   294  					Results: []MetadataTestResult{{
   295  						TestPath: "a.html",
   296  					},
   297  						{
   298  							TestPath: "e.html",
   299  						},
   300  					},
   301  				},
   302  			},
   303  		},
   304  	}
   305  
   306  	MetadataResults := constructMetadataResponse(productSpecs, true, metadataMap)
   307  	assert.Equal(t, 2, len(MetadataResults))
   308  	assert.Equal(t, 1, len(MetadataResults["/foo/bar/a.html"]))
   309  	assert.Equal(t, "https://bug.com/item", MetadataResults["/foo/bar/a.html"][0].URL)
   310  	assert.Equal(t, "https://bug.com/item", MetadataResults["/foo/bar/e.html"][0].URL)
   311  
   312  	MetadataResults = constructMetadataResponse(productSpecs, false, metadataMap)
   313  	assert.Equal(t, 0, len(MetadataResults))
   314  }
   315  
   316  func TestGetWPTTestPath(t *testing.T) {
   317  	actual := GetWPTTestPath("foo", "bar")
   318  	assert.Equal(t, "/foo/bar", actual)
   319  }
   320  
   321  func TestGetWPTTestPath_EmptyFolder(t *testing.T) {
   322  	actual := GetWPTTestPath("", "bar")
   323  	assert.Equal(t, "/bar", actual)
   324  }
   325  
   326  func TestSplitWPTTestPath_InvalidPath(t *testing.T) {
   327  	folderPath, testPath := SplitWPTTestPath("foo/bar")
   328  	assert.Equal(t, "", folderPath)
   329  	assert.Equal(t, "", testPath)
   330  }
   331  
   332  func TestSplitWPTTestPath_EmptyPath(t *testing.T) {
   333  	folderPath, testPath := SplitWPTTestPath("/")
   334  	assert.Equal(t, "", folderPath)
   335  	assert.Equal(t, "", testPath)
   336  }
   337  
   338  func TestSplitWPTTestPath_NoFolderPath(t *testing.T) {
   339  	folderPath, testPath := SplitWPTTestPath("/foo")
   340  	assert.Equal(t, "", folderPath)
   341  	assert.Equal(t, "foo", testPath)
   342  }
   343  
   344  func TestSplitWPTTestPath_Success(t *testing.T) {
   345  	folderPath, testPath := SplitWPTTestPath("/foo/bar/foo1")
   346  	assert.Equal(t, "foo/bar", folderPath)
   347  	assert.Equal(t, "foo1", testPath)
   348  
   349  	folderPath, testPath = SplitWPTTestPath("/foo/bar")
   350  	assert.Equal(t, "foo", folderPath)
   351  	assert.Equal(t, "bar", testPath)
   352  }
   353  
   354  func TestGetMetadataFilePath(t *testing.T) {
   355  	actual := GetMetadataFilePath("")
   356  	assert.Equal(t, "META.yml", actual)
   357  
   358  	actual = GetMetadataFilePath("foo")
   359  	assert.Equal(t, "foo/META.yml", actual)
   360  }
   361  
   362  func TestPrepareLinkFilter(t *testing.T) {
   363  	subtestName := "Something should happen"
   364  	fail := TestStatusFail
   365  	metadataResults := map[string]MetadataLinks{
   366  		"/foo/bar/a.html": []MetadataLink{
   367  			{
   368  				URL: "https://bug.com/item",
   369  				Results: []MetadataTestResult{{
   370  					SubtestName: &subtestName,
   371  					Status:      &fail,
   372  				}},
   373  			},
   374  			{
   375  				URL:   "",
   376  				Label: "LabelA",
   377  				Results: []MetadataTestResult{{
   378  					SubtestName: &subtestName,
   379  				}},
   380  			},
   381  		},
   382  	}
   383  
   384  	metaddataMap := PrepareLinkFilter(metadataResults)
   385  
   386  	assert.Equal(t, 1, len(metaddataMap))
   387  	assert.Equal(t, 1, len(metaddataMap["/foo/bar/a.html"]))
   388  	assert.Equal(t, "https://bug.com/item", metaddataMap["/foo/bar/a.html"][0])
   389  }
   390  
   391  func TestPrepareTestLabelFilter(t *testing.T) {
   392  	label := "labelA"
   393  	labelb := "labelB"
   394  	fail := TestStatusFail
   395  	metadataResults := map[string]MetadataLinks{
   396  		"/foo/bar/a.html": []MetadataLink{
   397  			{
   398  				Product: ProductSpec{},
   399  				Label:   label,
   400  			},
   401  			{
   402  				Product: ProductSpec{},
   403  				Label:   labelb,
   404  			},
   405  			{
   406  				URL: "https://bug.com/item",
   407  				Results: []MetadataTestResult{{
   408  					Status: &fail,
   409  				}},
   410  			},
   411  		},
   412  	}
   413  
   414  	metaddataMap := PrepareTestLabelFilter(metadataResults)
   415  
   416  	assert.Equal(t, 1, len(metaddataMap))
   417  	assert.Equal(t, 2, len(metaddataMap["/foo/bar/a.html"]))
   418  	assert.Equal(t, "labelA", metaddataMap["/foo/bar/a.html"][0])
   419  	assert.Equal(t, "labelB", metaddataMap["/foo/bar/a.html"][1])
   420  }