|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @noflow |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +const {buildManifest} = require('../visual/manifest'); |
| 12 | +const { |
| 13 | + flowsTaking, |
| 14 | + judgeRetakes, |
| 15 | + retakeCandidates, |
| 16 | + retakeScreenshots, |
| 17 | + screenshotIds, |
| 18 | +} = require('../visual/retake'); |
| 19 | +const {ANDROID_ENVIRONMENT, SHA_A} = require('./__fixtures__/visual'); |
| 20 | +const fs = require('node:fs'); |
| 21 | +const os = require('node:os'); |
| 22 | +const path = require('node:path'); |
| 23 | + |
| 24 | +const baselineManifest = { |
| 25 | + images: [ |
| 26 | + {id: 'switch/basic/default', sha256: 'a'}, |
| 27 | + {id: 'switch/basic/toggled', sha256: 'b'}, |
| 28 | + ], |
| 29 | +}; |
| 30 | + |
| 31 | +const BUNNY = path.join( |
| 32 | + __dirname, |
| 33 | + '../../../packages/rn-tester/js/assets/bunny.png', |
| 34 | +); |
| 35 | + |
| 36 | +const images = [ |
| 37 | + {id: 'switch/basic/default', sha256: 'a'}, |
| 38 | + {id: 'switch/basic/toggled', sha256: 'x'}, |
| 39 | + {id: 'view/basic/default', sha256: 'n'}, |
| 40 | +]; |
| 41 | + |
| 42 | +describe('visual retake', () => { |
| 43 | + test('reads the test IDs a flow captures', () => { |
| 44 | + const flow = [ |
| 45 | + 'appId: ${APP_ID}', |
| 46 | + '---', |
| 47 | + '- takeScreenshot:', |
| 48 | + ' path: ${VISUAL_OUTPUT_DIR}/switch/basic/default', |
| 49 | + ' cropOn:', |
| 50 | + " id: 'switch-basic'", |
| 51 | + '- takeScreenshot:', |
| 52 | + " path: '${VISUAL_OUTPUT_DIR}/switch/basic/toggled'", |
| 53 | + ].join('\n'); |
| 54 | + expect(screenshotIds(flow)).toEqual([ |
| 55 | + 'switch/basic/default', |
| 56 | + 'switch/basic/toggled', |
| 57 | + ]); |
| 58 | + }); |
| 59 | + |
| 60 | + test('selects only the flows that take the given IDs', () => { |
| 61 | + const files = { |
| 62 | + '/flows/switch.yml': |
| 63 | + '- takeScreenshot: ${VISUAL_OUTPUT_DIR}/switch/basic/toggled', |
| 64 | + '/flows/text.yml': |
| 65 | + '- takeScreenshot: ${VISUAL_OUTPUT_DIR}/text/width-mode/default', |
| 66 | + '/flows/view.yml': |
| 67 | + '- takeScreenshot: ${VISUAL_OUTPUT_DIR}/view/basic/default', |
| 68 | + }; |
| 69 | + expect( |
| 70 | + flowsTaking( |
| 71 | + Object.keys(files), |
| 72 | + ['text/width-mode/default', 'switch/basic/toggled'], |
| 73 | + file => files[file], |
| 74 | + ), |
| 75 | + ).toEqual(['/flows/switch.yml', '/flows/text.yml']); |
| 76 | + }); |
| 77 | + |
| 78 | + test('retakes changed and new screenshots but not unchanged ones', () => { |
| 79 | + expect(retakeCandidates(images, baselineManifest)).toEqual({ |
| 80 | + changed: ['switch/basic/toggled'], |
| 81 | + added: ['view/basic/default'], |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + test.each([ |
| 86 | + ['matches the baseline', 'b', ['switch/basic/toggled']], |
| 87 | + ['still differs', 'y', []], |
| 88 | + ['is missing', null, []], |
| 89 | + ])('replaces a changed screenshot if its retake %s', (_, sha, replaced) => { |
| 90 | + expect( |
| 91 | + judgeRetakes({ |
| 92 | + changed: ['switch/basic/toggled'], |
| 93 | + added: [], |
| 94 | + images, |
| 95 | + baselineManifest, |
| 96 | + retakeSha: () => sha, |
| 97 | + }).replaced, |
| 98 | + ).toEqual(replaced); |
| 99 | + }); |
| 100 | + |
| 101 | + test.each([ |
| 102 | + ['is identical', 'n', []], |
| 103 | + ['differs', 'y', ['view/basic/default']], |
| 104 | + ['is missing', null, ['view/basic/default']], |
| 105 | + ])('marks a new screenshot unstable if its retake %s', (_, sha, unstable) => { |
| 106 | + expect( |
| 107 | + judgeRetakes({ |
| 108 | + changed: [], |
| 109 | + added: ['view/basic/default'], |
| 110 | + images, |
| 111 | + baselineManifest, |
| 112 | + retakeSha: () => sha, |
| 113 | + }).unstable, |
| 114 | + ).toEqual(unstable); |
| 115 | + }); |
| 116 | + |
| 117 | + test('retakes every screenshot when the environment has no baseline', async () => { |
| 118 | + // The repo's Jest config fakes timers, and the retake waits on one. |
| 119 | + jest.useRealTimers(); |
| 120 | + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'visual-test-')); |
| 121 | + const fetchSpy = jest |
| 122 | + .spyOn(global, 'fetch') |
| 123 | + .mockResolvedValue(new Response(null, {status: 404})); |
| 124 | + try { |
| 125 | + const outputDir = path.join(directory, 'out'); |
| 126 | + const png = fs.readFileSync(BUNNY); |
| 127 | + const write = (dir, id, contents) => { |
| 128 | + fs.mkdirSync(path.join(dir, path.dirname(id)), {recursive: true}); |
| 129 | + fs.writeFileSync(path.join(dir, `${id}.png`), contents); |
| 130 | + }; |
| 131 | + write(outputDir, 'view/basic/default', png); |
| 132 | + write(outputDir, 'view/basic/flaky', png); |
| 133 | + const flow = path.join(directory, 'view.yml'); |
| 134 | + fs.writeFileSync( |
| 135 | + flow, |
| 136 | + [ |
| 137 | + '- takeScreenshot: ${VISUAL_OUTPUT_DIR}/view/basic/default', |
| 138 | + '- takeScreenshot: ${VISUAL_OUTPUT_DIR}/view/basic/flaky', |
| 139 | + ].join('\n'), |
| 140 | + ); |
| 141 | + const runFlows = jest.fn((flows, retakeDir) => { |
| 142 | + write(retakeDir, 'view/basic/default', png); |
| 143 | + write(retakeDir, 'view/basic/flaky', Buffer.concat([png, png])); |
| 144 | + }); |
| 145 | + const {unstable} = await retakeScreenshots({ |
| 146 | + platform: 'android', |
| 147 | + environment: ANDROID_ENVIRONMENT, |
| 148 | + flows: [flow], |
| 149 | + outputDir, |
| 150 | + publicBaseUrl: 'https://storage.example', |
| 151 | + runFlows, |
| 152 | + candidateShas: [SHA_A], |
| 153 | + delayMs: 0, |
| 154 | + }); |
| 155 | + expect(runFlows).toHaveBeenCalledWith([flow], `${outputDir}-retake`); |
| 156 | + expect(unstable).toEqual(['view/basic/flaky']); |
| 157 | + } finally { |
| 158 | + fetchSpy.mockRestore(); |
| 159 | + fs.rmSync(directory, {recursive: true, force: true}); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + test('marks unstable screenshots in the manifest', () => { |
| 164 | + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'visual-test-')); |
| 165 | + try { |
| 166 | + const png = fs.readFileSync(BUNNY); |
| 167 | + for (const id of ['view/basic/default', 'view/basic/flaky']) { |
| 168 | + fs.mkdirSync(path.join(directory, path.dirname(id)), {recursive: true}); |
| 169 | + fs.writeFileSync(path.join(directory, `${id}.png`), png); |
| 170 | + } |
| 171 | + const manifest = buildManifest({ |
| 172 | + captureDir: directory, |
| 173 | + platform: 'android', |
| 174 | + environment: ANDROID_ENVIRONMENT, |
| 175 | + git: null, |
| 176 | + unstableIds: ['view/basic/flaky'], |
| 177 | + }); |
| 178 | + expect(manifest.images.map(({id, unstable}) => ({id, unstable}))).toEqual( |
| 179 | + [ |
| 180 | + {id: 'view/basic/default', unstable: undefined}, |
| 181 | + {id: 'view/basic/flaky', unstable: true}, |
| 182 | + ], |
| 183 | + ); |
| 184 | + } finally { |
| 185 | + fs.rmSync(directory, {recursive: true, force: true}); |
| 186 | + } |
| 187 | + }); |
| 188 | +}); |
0 commit comments