|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | const {compareCapture, compareImages, policyFor} = require('../visual/compare'); |
12 | | -const {resolveContext, stageBaseline} = require('../visual/compare-run'); |
| 12 | +const { |
| 13 | + comparePlatform, |
| 14 | + resolveContext, |
| 15 | + stageBaseline, |
| 16 | +} = require('../visual/compare-run'); |
13 | 17 | const {buildManifest, sha256} = require('../visual/manifest'); |
14 | 18 | const {readPngDimensions} = require('../visual/png'); |
15 | 19 | const {validateCapture} = require('../visual/validate'); |
@@ -281,36 +285,107 @@ describe('visual comparison', () => { |
281 | 285 | }); |
282 | 286 | }); |
283 | 287 |
|
| 288 | + describe('comparePlatform', () => { |
| 289 | + afterEach(() => { |
| 290 | + jest.restoreAllMocks(); |
| 291 | + }); |
| 292 | + |
| 293 | + test('reads expected images from blobs for a blobs baseline', async () => { |
| 294 | + const captureDir = path.join(directory, 'capture'); |
| 295 | + const white = makePng(4, 4); |
| 296 | + const black = makePng(4, 4, () => [0, 0, 0, 255]); |
| 297 | + const manifest = writeCapture(captureDir, { |
| 298 | + 'text/changed/default': black, |
| 299 | + }); |
| 300 | + const baselineManifest = { |
| 301 | + ...manifest, |
| 302 | + imageStore: 'blobs', |
| 303 | + images: [{id: 'text/changed/default', sha256: sha256(white)}], |
| 304 | + }; |
| 305 | + const objects = new Map([ |
| 306 | + [ |
| 307 | + `baselines/${manifest.environmentKey}/${SHA_B}/manifest.json`, |
| 308 | + Buffer.from(JSON.stringify(baselineManifest)), |
| 309 | + ], |
| 310 | + [`blobs/${sha256(white)}.png`, white], |
| 311 | + ]); |
| 312 | + const fetched = []; |
| 313 | + jest.spyOn(global, 'fetch').mockImplementation(async url => { |
| 314 | + const key = url.replace('https://bucket.test/', ''); |
| 315 | + fetched.push(key); |
| 316 | + const body = objects.get(key); |
| 317 | + return body == null |
| 318 | + ? new Response(null, {status: 404}) |
| 319 | + : new Response(body, {status: 200}); |
| 320 | + }); |
| 321 | + |
| 322 | + const result = await comparePlatform({ |
| 323 | + github: { |
| 324 | + isOnBranch: async () => true, |
| 325 | + ancestry: async () => [SHA_B], |
| 326 | + }, |
| 327 | + context: {headSha: SHA_A, defaultBranch: 'main'}, |
| 328 | + captureDir, |
| 329 | + config: null, |
| 330 | + publicBaseUrl: 'https://bucket.test', |
| 331 | + outDir: path.join(directory, 'out'), |
| 332 | + prefix: 'reports/test', |
| 333 | + }); |
| 334 | + |
| 335 | + expect(fetched).toContain(`blobs/${sha256(white)}.png`); |
| 336 | + expect(result.baseline).toEqual({ |
| 337 | + sha: SHA_B, |
| 338 | + distance: 0, |
| 339 | + imageStore: 'blobs', |
| 340 | + }); |
| 341 | + expect(result.results.map(r => [r.id, r.status])).toEqual([ |
| 342 | + ['text/changed/default', 'changed'], |
| 343 | + ]); |
| 344 | + }); |
| 345 | + }); |
| 346 | + |
284 | 347 | describe('stageBaseline', () => { |
285 | | - test('leaves out unstable screenshots', () => { |
| 348 | + test('stages blobs not yet uploaded and a manifest that refers to them', async () => { |
286 | 349 | const captureDir = path.join(directory, 'capture'); |
287 | 350 | const outDir = path.join(directory, 'out'); |
| 351 | + const white = makePng(4, 4); |
| 352 | + const black = makePng(4, 4, () => [0, 0, 0, 255]); |
| 353 | + const grey = makePng(4, 4, () => [128, 128, 128, 255]); |
288 | 354 | const manifest = writeCapture(captureDir, { |
289 | | - 'text/stable/default': makePng(4, 4), |
290 | | - 'text/unstable/default': makePng(4, 4), |
| 355 | + 'text/uploaded/default': white, |
| 356 | + 'text/new/default': black, |
| 357 | + 'text/new/same': black, |
| 358 | + 'text/unstable/default': grey, |
| 359 | + }); |
| 360 | + manifest.images.find( |
| 361 | + image => image.id === 'text/unstable/default', |
| 362 | + ).unstable = true; |
| 363 | + await stageBaseline({ |
| 364 | + outDir, |
| 365 | + captureDir, |
| 366 | + manifest, |
| 367 | + sha: SHA_A, |
| 368 | + blobExists: async key => key === `blobs/${sha256(white)}.png`, |
291 | 369 | }); |
292 | | - manifest.images[1].unstable = true; |
293 | | - stageBaseline({outDir, captureDir, manifest, sha: SHA_A}); |
294 | | - const prefix = `baselines/${manifest.environmentKey}/${SHA_A}`; |
295 | 370 | const staged = JSON.parse( |
296 | 371 | fs.readFileSync( |
297 | | - path.join(outDir, 'baseline-manifests', prefix, 'manifest.json'), |
| 372 | + path.join( |
| 373 | + outDir, |
| 374 | + 'baseline-manifests', |
| 375 | + `baselines/${manifest.environmentKey}/${SHA_A}/manifest.json`, |
| 376 | + ), |
298 | 377 | 'utf8', |
299 | 378 | ), |
300 | 379 | ); |
301 | | - expect(staged.images.map(image => image.id)).toEqual([ |
302 | | - 'text/stable/default', |
| 380 | + expect(staged.imageStore).toBe('blobs'); |
| 381 | + expect(staged.images.map(image => image.id).sort()).toEqual([ |
| 382 | + 'text/new/default', |
| 383 | + 'text/new/same', |
| 384 | + 'text/uploaded/default', |
303 | 385 | ]); |
304 | 386 | expect( |
305 | | - fs.existsSync( |
306 | | - path.join( |
307 | | - outDir, |
308 | | - 'baseline-images', |
309 | | - prefix, |
310 | | - 'text/unstable/default.png', |
311 | | - ), |
312 | | - ), |
313 | | - ).toBe(false); |
| 387 | + fs.readdirSync(path.join(outDir, 'baseline-blobs', 'blobs')), |
| 388 | + ).toEqual([`${sha256(black)}.png`]); |
314 | 389 | }); |
315 | 390 | }); |
316 | 391 |
|
|
0 commit comments