// Copyright 2022 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice, this
//     list of conditions and the following disclaimer.
//
//  2. Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//  3. Neither the name of the copyright holder nor the names of its
//     contributors may be used to endorse or promote products derived from
//     this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package expectations

import (
	"testing"

	"dawn.googlesource.com/dawn/tools/src/cts/query"
	"dawn.googlesource.com/dawn/tools/src/cts/result"
	"github.com/stretchr/testify/require"
)

var Q = query.Parse

/*******************************************************************************
 * removeUnknownTags tests
 ******************************************************************************/

// Tests that unknown tags are properly removed.
func TestRemoveUnknownTags(t *testing.T) {
	header := `# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER
`

	content, err := Parse("expectations.txt", header)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android", "android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win", "win10", "intel", "intel-gen-9", "intel-0x3e9b"),
			Status: result.Failure,
		},
	}

	content.removeUnknownTags(&resultList)

	expectedList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win10"),
			Status: result.Failure,
		},
	}

	require.Equal(t, expectedList, resultList)
}

/*******************************************************************************
 * reduceTagsToMostExplicitOnly tests
 ******************************************************************************/

// Tests that result tags are properly filtered to only include the most
// explicit tags based on the tag sets in the header.
func TestReduceTagsToMostExplicitOnly(t *testing.T) {
	header := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER
`

	content, err := Parse("expectations.txt", header)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android", "android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win", "win10", "intel", "intel-gen-9", "intel-0x3e9b"),
			Status: result.Failure,
		},
	}

	content.reduceTagsToMostExplicitOnly(&resultList)

	expectedList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win10", "intel-0x3e9b"),
			Status: result.Failure,
		},
	}

	require.Equal(t, expectedList, resultList)
}

/*******************************************************************************
 * reduceTagsToBroadGroups tests
 ******************************************************************************/

func getBroadTagSuccessHeader() string {
	return `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# END TAG HEADER
`
}

func TestReduceTagsToBroadGroups_Success(t *testing.T) {
	header := getBroadTagSuccessHeader()
	content, err := Parse("expectations.txt", header)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		// Android, Fuchsia, and ChromeOS should only use the device tag and should
		// still reduce properly with only the device tag available.
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android", "android-t", "qualcomm", "qualcomm-0x41333430", "android-pixel-4", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-pixel-4", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("chromeos", "intel", "intel-0x3e92", "chromeos-board-volteer", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("chromeos-board-volteer", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("fuchsia", "intel", "intel-0x3e9b", "fuchsia-board-astro", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("fuchsia-board-astro", "unused"),
			Status: result.Failure,
		},
		// Other platforms should use OS + GPU.
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("linux", "ubuntu", "amd", "amd-0x7340", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("mac", "ventura", "apple", "apple-apple-m2", "unused"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win", "win10", "nvidia", "nvidia-0x2184", "unused"),
			Status: result.Failure,
		},
	}

	err = content.reduceTagsToBroadGroups(&resultList)
	require.NoErrorf(t, err, "Failed to reduce tags: %v", err)

	expectedList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-pixel-4"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-pixel-4"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("chromeos-board-volteer"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("chromeos-board-volteer"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("fuchsia-board-astro"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("fuchsia-board-astro"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("linux", "amd"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("mac", "apple"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("win", "nvidia"),
			Status: result.Failure,
		},
	}

	require.Equal(t, expectedList, resultList)
}

func TestReduceTagsToBroadGroups_FailToGetBroadTagSets(t *testing.T) {
	header := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# END TAG HEADER
`
	content, err := Parse("expectations.txt", header)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{}
	err = content.reduceTagsToBroadGroups(&resultList)
	require.ErrorContains(t, err, "Did not find expected tag sets.")
	require.ErrorContains(t, err, "osTagSet: []")
}

func TestReduceTagsToBroadGroups_FailToFindOsTag(t *testing.T) {
	header := getBroadTagSuccessHeader()
	content, err := Parse("expectations.txt", header)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("qualcomm", "qualcomm-0x41333430", "unused"),
			Status: result.Failure,
		},
	}

	err = content.reduceTagsToBroadGroups(&resultList)
	require.ErrorContains(t, err, "did not have exactly one overlap with [qualcomm, unused]")
}

func TestReduceTagsToBroadGroups_FailToFindGpuTag(t *testing.T) {
	header := getBroadTagSuccessHeader()
	content, err := Parse("expectations.txt", header)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("linux", "ubuntu", "unused"),
			Status: result.Failure,
		},
	}

	err = content.reduceTagsToBroadGroups(&resultList)
	require.ErrorContains(t, err, "did not have exactly one overlap with [linux, unused]")
}

/*******************************************************************************
 * getAndAssertOneOverlappingTag tests
 ******************************************************************************/

func TestGetAndAssertOneOverlappingTag(t *testing.T) {
	tests := []struct {
		name       string
		validTags  result.Tags
		allTags    result.Tags
		want       string
		wantErr    bool
		wantErrMsg string
	}{
		{
			name:       "One overlapping tag",
			validTags:  result.NewTags("tag1", "tag2"),
			allTags:    result.NewTags("tag2", "tag3"),
			want:       "tag2",
			wantErr:    false,
			wantErrMsg: "",
		},
		{
			name:       "No overlapping tag",
			validTags:  result.NewTags("tag1", "tag2"),
			allTags:    result.NewTags("tag3", "tag4"),
			want:       "",
			wantErr:    true,
			wantErrMsg: "Tags [tag1, tag2] did not have exactly one overlap with [tag3, tag4]",
		},
		{
			name:       "Multiple overlapping tags",
			validTags:  result.NewTags("tag1", "tag2", "tag3"),
			allTags:    result.NewTags("tag2", "tag3", "tag4"),
			want:       "",
			wantErr:    true,
			wantErrMsg: "Tags [tag1, tag2, tag3] did not have exactly one overlap with [tag2, tag3, tag4]",
		},
		{
			name:       "Empty validTags",
			validTags:  result.NewTags(),
			allTags:    result.NewTags("tag2", "tag3"),
			want:       "",
			wantErr:    true,
			wantErrMsg: "Tags [] did not have exactly one overlap with [tag2, tag3]",
		},
		{
			name:       "Empty allTags",
			validTags:  result.NewTags("tag1", "tag2"),
			allTags:    result.NewTags(),
			want:       "",
			wantErr:    true,
			wantErrMsg: "Tags [tag1, tag2] did not have exactly one overlap with []",
		},
		{
			name:       "Both empty",
			validTags:  result.NewTags(),
			allTags:    result.NewTags(),
			want:       "",
			wantErr:    true,
			wantErrMsg: "Tags [] did not have exactly one overlap with []",
		},
		{
			name:      "One overlapping tag with duplicates in validTags",
			validTags: result.NewTags("tag1", "tag2", "tag2"),
			allTags:   result.NewTags("tag2", "tag3"),
			want:      "tag2",
			wantErr:   false,
		},
		{
			name:      "One overlapping tag with duplicates in allTags",
			validTags: result.NewTags("tag1", "tag2"),
			allTags:   result.NewTags("tag2", "tag3", "tag2"),
			want:      "tag2",
			wantErr:   false,
		},
		{
			name:      "One overlapping tag with duplicates in both",
			validTags: result.NewTags("tag1", "tag2", "tag2"),
			allTags:   result.NewTags("tag2", "tag3", "tag2"),
			want:      "tag2",
			wantErr:   false,
		},
		{
			name:       "Multiple overlapping tags with duplicates",
			validTags:  result.NewTags("tag1", "tag2", "tag3", "tag2"),
			allTags:    result.NewTags("tag2", "tag3", "tag4", "tag2"),
			want:       "",
			wantErr:    true,
			wantErrMsg: "Tags [tag1, tag2, tag3] did not have exactly one overlap with [tag2, tag3, tag4]",
		},
	}
	for _, testCase := range tests {
		t.Run(testCase.name, func(t *testing.T) {
			got, err := getAndAssertOneOverlappingTag(testCase.validTags, testCase.allTags)
			if (err != nil) != testCase.wantErr {
				t.Errorf("getAndAssertOneOverlappingTag() error = %v, wantErr %v", err, testCase.wantErr)
				return
			}
			if testCase.wantErr && err.Error() != testCase.wantErrMsg {
				t.Errorf("getAndAssertOneOverlappingTag() errorMsg = %v, wantErrMsg %v", err.Error(), testCase.wantErrMsg)
				return
			}
			if got != testCase.want {
				t.Errorf("getAndAssertOneOverlappingTag() = %v, want %v", got, testCase.want)
				return
			}
		})
	}
}

/*******************************************************************************
 * getBroadTagSets tests
 ******************************************************************************/

func TestGetBroadTagSets_Success(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "other",
					Tags: result.NewTags("unused"),
				},
				{
					Name: "os",
					Tags: result.NewTags("android", "linux", "special-os"),
				},
				{
					Name: "gpu",
					Tags: result.NewTags("intel", "nvidia", "special-vendor"),
				},
				{
					Name: "device",
					Tags: result.NewTags("android-pixel-4", "android-pixel-6", "special-device"),
				},
			},
		},
	}

	osTagSet, gpuTagSet, deviceTagSet, err := c.getBroadTagSets()
	require.NoError(t, err)
	require.Equal(t, result.NewTags("android", "linux", "special-os"), osTagSet)
	require.Equal(t, result.NewTags("intel", "nvidia", "special-vendor"), gpuTagSet)
	require.Equal(t, result.NewTags("android-pixel-4", "android-pixel-6", "special-device"), deviceTagSet)
}

func TestGetBroadTagSets_MissingOSTagSet(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "gpu",
					Tags: result.NewTags("intel", "nvidia"),
				},
				{
					Name: "device",
					Tags: result.NewTags("android-pixel-4", "android-pixel-6"),
				},
			},
		},
	}

	_, _, _, err := c.getBroadTagSets()
	require.Error(t, err)
	require.Contains(t, err.Error(), "Did not find expected tag sets")
	require.Contains(t, err.Error(), "osTagSet: []")
}

func TestGetBroadTagSets_MismatchedOSTagSet(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "os",
					Tags: result.NewTags("special-os"),
				},
				{
					Name: "gpu",
					Tags: result.NewTags("intel", "nvidia"),
				},
				{
					Name: "device",
					Tags: result.NewTags("android-pixel-4", "android-pixel-6"),
				},
			},
		},
	}

	_, _, _, err := c.getBroadTagSets()
	require.Error(t, err)
	require.Contains(t, err.Error(), "Did not find expected tag sets")
	require.Contains(t, err.Error(), "osTagSet: []")
}

func TestGetBroadTagSets_MissingGPUTagSet(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "os",
					Tags: result.NewTags("android", "linux"),
				},
				{
					Name: "device",
					Tags: result.NewTags("android-pixel-4", "android-pixel-6"),
				},
			},
		},
	}

	_, _, _, err := c.getBroadTagSets()
	require.Error(t, err)
	require.Contains(t, err.Error(), "Did not find expected tag sets")
	require.Contains(t, err.Error(), "gpuTagSet: []")
}

func TestGetBroadTagSets_MismatchedGPUTagSet(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "os",
					Tags: result.NewTags("android", "linux"),
				},
				{
					Name: "gpu",
					Tags: result.NewTags("special-vendor"),
				},
				{
					Name: "device",
					Tags: result.NewTags("android-pixel-4", "android-pixel-6"),
				},
			},
		},
	}

	_, _, _, err := c.getBroadTagSets()
	require.Error(t, err)
	require.Contains(t, err.Error(), "Did not find expected tag sets")
	require.Contains(t, err.Error(), "gpuTagSet: []")
}

func TestGetBroadTagSets_MissingDeviceTagSet(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "os",
					Tags: result.NewTags("android", "linux"),
				},
				{
					Name: "gpu",
					Tags: result.NewTags("intel", "nvidia"),
				},
			},
		},
	}

	_, _, _, err := c.getBroadTagSets()
	require.Error(t, err)
	require.Contains(t, err.Error(), "Did not find expected tag sets")
	require.Contains(t, err.Error(), "deviceTagSet: []")
}

func TestGetBroadTagSets_MismatchedDeviceTagSet(t *testing.T) {
	c := &Content{
		Tags: Tags{
			Sets: []TagSet{
				{
					Name: "os",
					Tags: result.NewTags("android", "linux"),
				},
				{
					Name: "gpu",
					Tags: result.NewTags("intel", "nvidia"),
				},
				{
					Name: "device",
					Tags: result.NewTags("special-device"),
				},
			},
		},
	}

	_, _, _, err := c.getBroadTagSets()
	require.Error(t, err)
	require.Contains(t, err.Error(), "Did not find expected tag sets")
	require.Contains(t, err.Error(), "deviceTagSet: []")
}

/*******************************************************************************
 * addExpectationsToMutableChunk tests
 ******************************************************************************/

// Tests that multiple mutable chunks result in an error.
func TestAddExpectationsToMutableChunkMultipleMutableChunks(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]

# ##ROLLER_AUTOGENERATED_FAILURES##
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("test"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
	}

	err = content.addExpectationsToMutableChunk(&resultList)
	require.EqualError(t, err, "Expected 1 mutable chunk, found 2")
}

// Tests that a single mutable chunk gets expectations added to it.
func TestAddExpectationsToMutableChunkSingleMutableChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
	}

	err = content.addExpectationsToMutableChunk(&resultList)
	require.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# ##ROLLER_AUTOGENERATED_FAILURES##
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	require.Equal(t, expectedContent, content.String())
}

// Tests that a lack of a mutable chunk causes a new mutable chunk to be added
// which has the new expectations.
func TestAddExpectationsToMutableChunkNoMutableChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# Immutable
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android-14", "arm"),
			Status: result.Failure,
		},
	}

	err = content.addExpectationsToMutableChunk(&resultList)
	require.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER
# OS
# tags: [ android linux win10 ]
# END TAG HEADER

# Immutable
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]

################################################################################
# Autogenerated Failure expectations. Please triage.
# ##ROLLER_AUTOGENERATED_FAILURES##
################################################################################
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	require.Equal(t, expectedContent, content.String())
}

/*******************************************************************************
 * AddExpectationsForFailingResults tests
 ******************************************************************************/

// Tests overall behavior with an existing mutable chunk using explicit tags.
func TestAddExpectationsForFailingResults_ExplicitTags_ExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList /*use_explicit_tags=*/, true /*verbose=*/, false)
	require.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
crbug.com/0000 [ intel-0x3e9b linux ] valid_test1 [ Failure ]
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	require.Equal(t, expectedContent, content.String())
}

// Tests overall behavior without an existing mutable chunk using explicit tags.
func TestAddExpectationsForFailingResults_ExplicitTags_WithoutExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList /*use_explicit_tags=*/, true /*verbose=*/, false)
	require.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

################################################################################
# Autogenerated Failure expectations. Please triage.
# ##ROLLER_AUTOGENERATED_FAILURES##
################################################################################
crbug.com/0000 [ android-14 arm ] a:b,c:d: [ Failure ]
crbug.com/0000 [ intel-0x3e9b linux ] valid_test1 [ Failure ]
crbug.com/0000 [ android-14 arm ] z:b,c:d,e:f=g [ Failure ]
`

	require.Equal(t, expectedContent, content.String())
}

// Tests overall behavior with multiple mutable chunks using explicit tags.
func TestAddExpectationsForFailingResults_ExplicitTags_MultipleExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ linux ] valid_test1 [ Failure ]
[ linux ] invalid_test [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Fully removed, immutable.
[ android ] invalid_test [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] invalid_test [ Failure ]
[ win10 ] valid_test2 [ Failure ]

# ##ROLLER_AUTOGENERATED_FAILURES##
[ android ] valid_test1 [ Failure ]
[ android ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("new_test"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z_new_test"),
			Tags:   result.NewTags("android", "android-14", "arm", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList /*use_explicit_tags=*/, true /*verbose=*/, false)
	require.EqualError(t, err, "Expected 1 mutable chunk, found 2")
}

// Tests overall behavior with an existing mutable chunk using broad tags.
func TestAddExpectationsForFailingResults_BroadTags_ExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android", "android-14", "arm", "android-pixel-4", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android", "android-14", "arm", "android-pixel-4", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList /*use_explicit_tags=*/, false /*verbose=*/, false)
	require.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
crbug.com/0000 [ android-pixel-4 ] a:b,c:d: [ Failure ]
crbug.com/0000 [ intel linux ] valid_test1 [ Failure ]
[ win10 ] valid_test1 [ Failure ]
[ win10 ] valid_test2 [ Failure ]
crbug.com/0000 [ android-pixel-4 ] z:b,c:d,e:f=g [ Failure ]
`

	require.Equal(t, expectedContent, content.String())
}

// Tests overall behavior without an existing mutable chunk using broad tags.
func TestAddExpectationsForFailingResults_BroadTags_WithoutExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("a:b,c:d"),
			Tags:   result.NewTags("android", "android-14", "arm", "android-pixel-4", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z:b,c:d,e:f=g"),
			Tags:   result.NewTags("android", "android-14", "arm", "android-pixel-4", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList /*use_explicit_tags=*/, false /*verbose=*/, false)
	require.NoErrorf(t, err, "Failed to add expectations: %v", err)

	expectedContent := `# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ android ] * [ Failure ]
[ mac ] a:* [ Failure ]
[ linux ] z:b,c:d,e:f=* [ Failure ]
[ linux ] valid_test1 [ Failure ]
[ linux ] valid_test2 [ Failure ]

################################################################################
# Autogenerated Failure expectations. Please triage.
# ##ROLLER_AUTOGENERATED_FAILURES##
################################################################################
crbug.com/0000 [ android-pixel-4 ] a:b,c:d: [ Failure ]
crbug.com/0000 [ intel linux ] valid_test1 [ Failure ]
crbug.com/0000 [ android-pixel-4 ] z:b,c:d,e:f=g [ Failure ]
`

	require.Equal(t, expectedContent, content.String())
}

// Tests overall behavior with multiple mutable chunks using broad tags.
func TestAddExpectationsForFailingResults_BroadTags_MultipleExistingChunk(t *testing.T) {
	fileContent := `
# BEGIN TAG HEADER (autogenerated, see validate_tag_consistency.py)
# OS
# tags: [ android android-oreo android-pie android-r android-s android-t
#             android-14
#         chromeos
#         fuchsia
#         linux ubuntu
#         mac highsierra mojave catalina bigsur monterey ventura sonoma sequoia
#         win win8 win10 win11 ]
# Devices
# tags: [ android-nexus-5x android-pixel-2 android-pixel-4
#             android-pixel-6 android-shield-android-tv android-sm-a135m
#             android-sm-a235m android-sm-s926b
#         chromeos-board-amd64-generic chromeos-board-eve chromeos-board-jacuzzi
#             chromeos-board-octopus chromeos-board-volteer
#         fuchsia-board-astro fuchsia-board-sherlock fuchsia-board-qemu-x64 ]
# GPU
# tags: [ amd amd-0x6613 amd-0x679e amd-0x67ef amd-0x6821 amd-0x7340
#         apple apple-apple-m1 apple-apple-m2
#             apple-angle-metal-renderer:-apple-m1
#             apple-angle-metal-renderer:-apple-m2
#         arm
#         google google-0xffff google-0xc0de
#         imagination
#         intel intel-gen-9 intel-gen-12 intel-0xa2e intel-0xd26 intel-0xa011
#             intel-0x3e92 intel-0x3e9b intel-0x4680 intel-0x5912 intel-0x9bc5
#         nvidia nvidia-0xfe9 nvidia-0x1cb3 nvidia-0x2184 nvidia-0x2783
#         qualcomm qualcomm-0x41333430 qualcomm-0x36333630 qualcomm-0x36334330 ]
# END TAG HEADER

# Partially removed, immutable.
[ linux ] valid_test1 [ Failure ]
[ linux ] invalid_test [ Failure ]
[ linux ] valid_test2 [ Failure ]

# Fully removed, immutable.
[ android ] invalid_test [ Failure ]

# Partially removed, mutable.
# ##ROLLER_AUTOGENERATED_FAILURES##
[ win10 ] valid_test1 [ Failure ]
[ win10 ] invalid_test [ Failure ]
[ win10 ] valid_test2 [ Failure ]

# ##ROLLER_AUTOGENERATED_FAILURES##
[ android ] valid_test1 [ Failure ]
[ android ] valid_test2 [ Failure ]
`

	content, err := Parse("expectations.txt", fileContent)
	require.NoErrorf(t, err, "Failed to parse expectations: %v", err)

	resultList := result.List{
		result.Result{
			Query:  query.Parse("new_test"),
			Tags:   result.NewTags("android", "android-14", "arm", "android-pixel-4", "unknown1"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("z_new_test"),
			Tags:   result.NewTags("android", "android-14", "arm", "android-pixel-4", "unknown2"),
			Status: result.Failure,
		},
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-9", "intel-0x3e9b", "unknown3"),
			Status: result.Failure,
		},
		// This should be merged with the one above after only the most explicit
		// tags remain. Multiple generations mapping to the same actual GPU
		// shouldn't happen in real life, but serves to test merging.
		result.Result{
			Query:  query.Parse("valid_test1"),
			Tags:   result.NewTags("linux", "intel", "intel-gen-12", "intel-0x3e9b", "unknown4"),
			Status: result.Failure,
		},
	}

	err = content.AddExpectationsForFailingResults(resultList /*use_explicit_tags=*/, false /*verbose=*/, false)
	require.EqualError(t, err, "Expected 1 mutable chunk, found 2")
}
