go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/test_verdict/tools/variant_utils/variant_utils.test.ts (about)

     1  // Copyright 2024 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  import { getCriticalVariantKeys } from './variant_utils';
    16  
    17  describe('getCriticalVariantKeys', () => {
    18    it('when all variants are the same', () => {
    19      const keys = getCriticalVariantKeys([
    20        { def: { key1: 'val1', key2: 'val2' } },
    21        { def: { key1: 'val1', key2: 'val2' } },
    22        { def: { key1: 'val1', key2: 'val2' } },
    23        { def: { key1: 'val1', key2: 'val2' } },
    24      ]);
    25      expect(keys).toEqual(['key1']);
    26    });
    27  
    28    it('when some variants are the different', () => {
    29      const keys = getCriticalVariantKeys([
    30        { def: { key1: 'val1', key2: 'val2' } },
    31        { def: { key1: 'val1', key2: 'val2' } },
    32        { def: { key1: 'val1', key2: 'val3' } },
    33        { def: { key1: 'val1', key2: 'val2' } },
    34      ]);
    35      expect(keys).toEqual(['key2']);
    36    });
    37  
    38    it('when some variant defs has missing keys', () => {
    39      const keys = getCriticalVariantKeys([
    40        { def: { key1: 'val1', key2: 'val2' } },
    41        { def: { key1: 'val1', key2: 'val2' } },
    42        { def: { key1: 'val1', key2: 'val2' } },
    43        { def: { key1: 'val1' } },
    44      ]);
    45      expect(keys).toEqual(['key2']);
    46    });
    47  
    48    it('when some variant values always change together', () => {
    49      const keys = getCriticalVariantKeys([
    50        {
    51          def: {
    52            test_suite: 'test-suite-1',
    53            builder: 'linux-builder',
    54            os: 'linux',
    55          },
    56        },
    57        {
    58          def: {
    59            test_suite: 'test-suite-1',
    60            builder: 'macos-builder',
    61            os: 'macos',
    62          },
    63        },
    64        {
    65          def: {
    66            test_suite: 'test-suite-2',
    67            builder: 'linux-builder',
    68            os: 'linux',
    69          },
    70        },
    71        {
    72          def: {
    73            test_suite: 'test-suite-2',
    74            builder: 'macos-builder',
    75            os: 'macos',
    76          },
    77        },
    78      ]);
    79      expect(keys).toEqual(['builder', 'test_suite']);
    80    });
    81  
    82    it("when there are additional variant keys that don't matter", () => {
    83      const keys = getCriticalVariantKeys([
    84        {
    85          def: {
    86            test_suite: 'test-suite-1',
    87            builder: 'linux-builder',
    88            os: 'linux',
    89            a_param1: 'val1',
    90          },
    91        },
    92        {
    93          def: {
    94            test_suite: 'test-suite-1',
    95            builder: 'macos-builder',
    96            os: 'macos',
    97            a_param2: 'val2',
    98          },
    99        },
   100        {
   101          def: {
   102            test_suite: 'test-suite-2',
   103            builder: 'linux-builder',
   104            os: 'linux',
   105            a_param3: 'val3',
   106          },
   107        },
   108        {
   109          def: {
   110            test_suite: 'test-suite-2',
   111            builder: 'macos-builder',
   112            os: 'macos',
   113            a_param4: 'val4',
   114          },
   115        },
   116        {
   117          def: {
   118            test_suite: 'test-suite-2',
   119            builder: 'macos-builder',
   120            os: 'macos',
   121            a_param5: 'val5',
   122          },
   123        },
   124      ]);
   125      // Having a_param4 is enough to uniquely identify all variants.
   126      expect(keys).toEqual(['builder', 'test_suite', 'a_param4']);
   127    });
   128  
   129    it('when there are multiple valid set of critical keys', () => {
   130      const keys = getCriticalVariantKeys([
   131        {
   132          def: {
   133            test_suite: 'test-suite-1',
   134            builder: 'linux-builder',
   135            os: 'linux',
   136            a_param: 'val1',
   137          },
   138        },
   139        {
   140          def: {
   141            test_suite: 'test-suite-1',
   142            builder: 'macos-builder',
   143            os: 'macos',
   144            a_param: 'val2',
   145          },
   146        },
   147        {
   148          def: {
   149            test_suite: 'test-suite-2',
   150            builder: 'linux-builder',
   151            os: 'linux',
   152            a_param: 'val3',
   153          },
   154        },
   155        {
   156          def: {
   157            test_suite: 'test-suite-2',
   158            builder: 'macos-builder',
   159            os: 'macos',
   160            a_param: 'val4',
   161          },
   162        },
   163      ]);
   164      // Having a_param is enough to uniquely identify all variants.
   165      // But we prefer keys that are known to have special meanings.
   166      expect(keys).toEqual(['builder', 'test_suite']);
   167    });
   168  });