|
| 1 | +/** |
| 2 | + * Copyright 2022 Google Inc. All Rights Reserved. |
| 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 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import {readFile} from 'fs/promises'; |
| 15 | +import {dirname, join} from 'path'; |
| 16 | +import {fileURLToPath} from 'url'; |
| 17 | + |
| 18 | +interface TestMap<T> { |
| 19 | + [key: string]: SubtestMap<T> | undefined; |
| 20 | +} |
| 21 | + |
| 22 | +interface SubtestMap<T> { |
| 23 | + [key: string]: T | undefined; |
| 24 | +} |
| 25 | + |
| 26 | +interface BrowserResults { |
| 27 | + passed: string[]; |
| 28 | + failed: string[]; |
| 29 | +} |
| 30 | + |
| 31 | +interface Diff<T> { |
| 32 | + before?: T; |
| 33 | + after?: T; |
| 34 | +} |
| 35 | + |
| 36 | +function getTargetForDescriptor<T>( |
| 37 | + testMap: TestMap<T>, |
| 38 | + descriptor: TestDescriptor, |
| 39 | + defaultValue: () => T |
| 40 | +) { |
| 41 | + const testToSubtest = (testMap[descriptor.test] = |
| 42 | + testMap[descriptor.test] || {}); |
| 43 | + const subtestToTarget = (testToSubtest[descriptor.subtest] = |
| 44 | + testToSubtest[descriptor.subtest] || defaultValue()); |
| 45 | + |
| 46 | + testMap[descriptor.test] = testToSubtest; |
| 47 | + testToSubtest[descriptor.subtest] = subtestToTarget; |
| 48 | + |
| 49 | + return subtestToTarget; |
| 50 | +} |
| 51 | + |
| 52 | +const newTestMap: TestMap<BrowserResults> = {}; |
| 53 | +for (const browser of results) { |
| 54 | + for (const version of browser.versions) { |
| 55 | + if (version.data.type === DataType.Result) { |
| 56 | + const [passed, failed] = version.data.result; |
| 57 | + const name = `${browser.name} ${version.name}`; |
| 58 | + |
| 59 | + for (const result of passed) { |
| 60 | + const target = getTargetForDescriptor(newTestMap, result, () => ({ |
| 61 | + passed: [], |
| 62 | + failed: [], |
| 63 | + })); |
| 64 | + target.passed.push(name); |
| 65 | + } |
| 66 | + |
| 67 | + for (const result of failed) { |
| 68 | + const target = getTargetForDescriptor(newTestMap, result, () => ({ |
| 69 | + passed: [], |
| 70 | + failed: [], |
| 71 | + })); |
| 72 | + target.failed.push(name); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +const baselineBuffer = await readFile( |
| 79 | + join(dirname(fileURLToPath(import.meta.url)), 'baseline.json') |
| 80 | +); |
| 81 | +const baselineTestMap: TestMap<BrowserResults> = JSON.parse( |
| 82 | + baselineBuffer.toString('utf-8') |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 84 | +) as any; |
| 85 | + |
| 86 | +const deltaTestMap: TestMap<Diff<BrowserResults>> = {}; |
| 87 | +const allTestNames = new Set( |
| 88 | + [...Object.keys(baselineTestMap), ...Object.keys(newTestMap)].sort() |
| 89 | +); |
| 90 | + |
| 91 | +for (const test in allTestNames) { |
| 92 | + const newSubtests = newTestMap[test] ?? {}; |
| 93 | + const baselineSubtests = baselineTestMap[test] ?? {}; |
| 94 | + const allSubtestNames = new Set( |
| 95 | + [...Object.keys(newSubtests), ...Object.keys(baselineSubtests)].sort() |
| 96 | + ); |
| 97 | + |
| 98 | + for (const subtest in allSubtestNames) { |
| 99 | + const target = getTargetForDescriptor<Diff<BrowserResults>>( |
| 100 | + deltaTestMap, |
| 101 | + {test, subtest}, |
| 102 | + () => ({}) |
| 103 | + ); |
| 104 | + target.before = baselineSubtests[subtest]; |
| 105 | + target.after = newSubtests[subtest]; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +console.info(JSON.stringify(deltaTestMap, null, 2)); |
0 commit comments