github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/screenshot/model_medium_test.go (about)

     1  //go:build medium
     2  // +build medium
     3  
     4  // Copyright 2019 The WPT Dashboard Project. All rights reserved.
     5  // Use of this source code is governed by a BSD-style license that can be
     6  // found in the LICENSE file.
     7  
     8  package screenshot
     9  
    10  import (
    11  	"fmt"
    12  	"sort"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  
    19  	"github.com/web-platform-tests/wpt.fyi/shared"
    20  	"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
    21  )
    22  
    23  func TestNewScreenshot(t *testing.T) {
    24  	s := NewScreenshot("", "chrome")
    25  	assert.Equal(t, s.Labels, []string{"chrome"})
    26  }
    27  
    28  func TestKeyAndHash(t *testing.T) {
    29  	s := Screenshot{
    30  		HashMethod: "hash",
    31  		HashDigest: "0000abcd",
    32  	}
    33  	assert.Equal(t, "hash:0000abcd", s.Hash())
    34  	assert.Equal(t, "0000abcd:hash", s.Key())
    35  }
    36  
    37  func TestSetHashFromFile(t *testing.T) {
    38  	s := Screenshot{}
    39  	err := s.SetHashFromFile(strings.NewReader("Hello, world!"), "sha1")
    40  	assert.Nil(t, err)
    41  	assert.Equal(t, "sha1", s.HashMethod)
    42  	assert.Equal(t, "943a702d06f34599aee1f8da8ef9f7296031d699", s.HashDigest)
    43  }
    44  
    45  func TestSetHashFromFile_error(t *testing.T) {
    46  	s := Screenshot{}
    47  	err := s.SetHashFromFile(strings.NewReader(""), "hash")
    48  	assert.Equal(t, ErrUnsupportedHashMethod, err)
    49  }
    50  
    51  func TestStore(t *testing.T) {
    52  	ctx, done, err := sharedtest.NewAEContext(true)
    53  	assert.Nil(t, err)
    54  	defer done()
    55  	ds := shared.NewAppEngineDatastore(ctx, false)
    56  
    57  	t.Run("error", func(t *testing.T) {
    58  		s := Screenshot{}
    59  		err := s.Store(ds)
    60  		assert.Equal(t, ErrInvalidHash, err)
    61  	})
    62  	t.Run("create new screenshot", func(t *testing.T) {
    63  		s := Screenshot{
    64  			HashDigest: "fa52883da345b2525304b54c8bc7bbb1e88b5e3e",
    65  			HashMethod: "sha1",
    66  			Labels:     []string{"chrome"},
    67  		}
    68  		err := s.Store(ds)
    69  		assert.Nil(t, err)
    70  
    71  		// Check populated fields.
    72  		assert.Equal(t, 0, s.Counter)
    73  		assert.False(t, s.LastUsed.IsZero())
    74  
    75  		var s2 Screenshot
    76  		err = ds.Get(ds.NewNameKey("Screenshot", s.Key()), &s2)
    77  		assert.Nil(t, err)
    78  		assert.Equal(t, s.HashDigest, s2.HashDigest)
    79  		assert.Equal(t, s.HashMethod, s2.HashMethod)
    80  		assert.Equal(t, s.Labels, s2.Labels)
    81  		assert.Equal(t, s.Counter, s2.Counter)
    82  	})
    83  	t.Run("update a screenshot", func(t *testing.T) {
    84  		s := Screenshot{
    85  			HashDigest: "fa52883da345b2525304b54c8bc7bbb1e88b5e3e",
    86  			HashMethod: "sha1",
    87  			Labels:     []string{"firefox"},
    88  		}
    89  		err := s.Store(ds)
    90  		assert.Nil(t, err)
    91  
    92  		// Check populated fields.
    93  		assert.Equal(t, 1, s.Counter)
    94  		expectedLabels := shared.NewSetFromStringSlice([]string{"chrome", "firefox"})
    95  		labels := shared.NewSetFromStringSlice(s.Labels)
    96  		assert.True(t, expectedLabels.Equal(labels))
    97  		assert.False(t, s.LastUsed.IsZero())
    98  
    99  		var s2 Screenshot
   100  		err = ds.Get(ds.NewNameKey("Screenshot", s.Key()), &s2)
   101  		assert.Nil(t, err)
   102  		assert.Equal(t, s.Labels, s2.Labels)
   103  		assert.Equal(t, s.Counter, s2.Counter)
   104  	})
   105  }
   106  
   107  func TestRecentScreenshotHashes_filtering(t *testing.T) {
   108  	ctx, done, err := sharedtest.NewAEContext(true)
   109  	assert.Nil(t, err)
   110  	defer done()
   111  	ds := shared.NewAppEngineDatastore(ctx, false)
   112  
   113  	screenshots := []Screenshot{
   114  		// The order matters: 0001 is the perfect match, and the rest
   115  		// have have fewer and less important matching labels.
   116  		{
   117  			HashDigest: "0001",
   118  			HashMethod: "hash",
   119  			Labels:     []string{"chrome", "64", "mac", "10.13"},
   120  		},
   121  		{
   122  			HashDigest: "0002",
   123  			HashMethod: "hash",
   124  			Labels:     []string{"chrome", "64", "mac", "10.14"},
   125  		},
   126  		{
   127  			HashDigest: "0003",
   128  			HashMethod: "hash",
   129  			Labels:     []string{"chrome", "64", "windows", "10"},
   130  		},
   131  		{
   132  			HashDigest: "0004",
   133  			HashMethod: "hash",
   134  			Labels:     []string{"chrome", "65", "windows", "10"},
   135  		},
   136  		{
   137  			HashDigest: "0005",
   138  			HashMethod: "hash",
   139  			Labels:     []string{"firefox", "60", "windows", "10"},
   140  		},
   141  	}
   142  	for _, s := range screenshots {
   143  		key := ds.NewNameKey("Screenshot", s.Key())
   144  		_, err := ds.Put(key, &s)
   145  		assert.Nil(t, err)
   146  	}
   147  
   148  	for i := 1; i <= 5; i++ {
   149  		t.Run(fmt.Sprintf("%d screenshots", i), func(t *testing.T) {
   150  			hashes, err := RecentScreenshotHashes(ds, "chrome", "64", "mac", "10.13", &i)
   151  			assert.Nil(t, err)
   152  			sort.Strings(hashes)
   153  			for j, hash := range hashes {
   154  				assert.Equal(t, fmt.Sprintf("hash:000%d", j+1), hash)
   155  			}
   156  		})
   157  	}
   158  }
   159  
   160  func TestRecentScreenshotHashes_ordering(t *testing.T) {
   161  	ctx, done, err := sharedtest.NewAEContext(true)
   162  	assert.Nil(t, err)
   163  	defer done()
   164  	ds := shared.NewAppEngineDatastore(ctx, false)
   165  
   166  	screenshots := []Screenshot{
   167  		// The order matters: we want the smallest ID to have the
   168  		// oldest timestamp to avoid accidentally passing the test even
   169  		// without ordering.
   170  		{
   171  			HashDigest: "0001",
   172  			HashMethod: "hash",
   173  			LastUsed:   time.Now().Add(-time.Minute * 3),
   174  		},
   175  		{
   176  			HashDigest: "0002",
   177  			HashMethod: "hash",
   178  			LastUsed:   time.Now().Add(-time.Minute * 2),
   179  		},
   180  		{
   181  			HashDigest: "0003",
   182  			HashMethod: "hash",
   183  			LastUsed:   time.Now().Add(-time.Minute * 1),
   184  		},
   185  	}
   186  	for _, s := range screenshots {
   187  		key := ds.NewNameKey("Screenshot", s.Key())
   188  		_, err := ds.Put(key, &s)
   189  		assert.Nil(t, err)
   190  	}
   191  
   192  	two := 2
   193  	// Intentionally provide a label without any matches to test the query fallback.
   194  	hashes, err := RecentScreenshotHashes(ds, "chrome", "", "", "", &two)
   195  	assert.Nil(t, err)
   196  	sort.Strings(hashes)
   197  	assert.Equal(t, []string{"hash:0002", "hash:0003"}, hashes)
   198  }