|
| 1 | +import type { SemVerVersion } from '@metamask/utils'; |
| 2 | +import assert from 'assert'; |
| 3 | +import fetchMock from 'jest-fetch-mock'; |
| 4 | + |
| 5 | +import { productionPlatformVersion } from './production-platform-version'; |
| 6 | +import { getMockSnapFiles, getSnapManifest } from '../../test-utils'; |
| 7 | + |
| 8 | +const MOCK_GITHUB_RESPONSE = JSON.stringify({ |
| 9 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 10 | + target_commitish: '5fceb7ed2ef18a3984786db1161a76ca5c8e15b9', |
| 11 | +}); |
| 12 | + |
| 13 | +const MOCK_PACKAGE_JSON = JSON.stringify({ |
| 14 | + dependencies: { |
| 15 | + '@metamask/snaps-sdk': '6.1.0', |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +describe('productionPlatformVersion', () => { |
| 20 | + beforeAll(() => { |
| 21 | + fetchMock.enableMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll(() => { |
| 25 | + fetchMock.disableMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('reports if the version is greater than the production version', async () => { |
| 29 | + fetchMock.mockResponses(MOCK_GITHUB_RESPONSE, MOCK_PACKAGE_JSON); |
| 30 | + |
| 31 | + const report = jest.fn(); |
| 32 | + assert(productionPlatformVersion.semanticCheck); |
| 33 | + |
| 34 | + await productionPlatformVersion.semanticCheck( |
| 35 | + getMockSnapFiles({ |
| 36 | + manifest: getSnapManifest({ |
| 37 | + platformVersion: '6.5.0' as SemVerVersion, |
| 38 | + }), |
| 39 | + }), |
| 40 | + { report }, |
| 41 | + ); |
| 42 | + |
| 43 | + expect(report).toHaveBeenCalledTimes(1); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does nothing if the version is less than the production version', async () => { |
| 47 | + fetchMock.mockResponses(MOCK_GITHUB_RESPONSE, MOCK_PACKAGE_JSON); |
| 48 | + |
| 49 | + const report = jest.fn(); |
| 50 | + assert(productionPlatformVersion.semanticCheck); |
| 51 | + |
| 52 | + await productionPlatformVersion.semanticCheck( |
| 53 | + getMockSnapFiles({ |
| 54 | + manifest: getSnapManifest({ |
| 55 | + platformVersion: '6.0.0' as SemVerVersion, |
| 56 | + }), |
| 57 | + }), |
| 58 | + { report }, |
| 59 | + ); |
| 60 | + |
| 61 | + expect(report).toHaveBeenCalledTimes(0); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does nothing if the version is equal to the production version', async () => { |
| 65 | + fetchMock.mockResponses(MOCK_GITHUB_RESPONSE, MOCK_PACKAGE_JSON); |
| 66 | + |
| 67 | + const report = jest.fn(); |
| 68 | + assert(productionPlatformVersion.semanticCheck); |
| 69 | + |
| 70 | + await productionPlatformVersion.semanticCheck( |
| 71 | + getMockSnapFiles({ |
| 72 | + manifest: getSnapManifest({ |
| 73 | + platformVersion: '6.1.0' as SemVerVersion, |
| 74 | + }), |
| 75 | + }), |
| 76 | + { report }, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(report).toHaveBeenCalledTimes(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('does nothing if the version is not set', async () => { |
| 83 | + const report = jest.fn(); |
| 84 | + assert(productionPlatformVersion.semanticCheck); |
| 85 | + |
| 86 | + const rawManifest = getSnapManifest(); |
| 87 | + delete rawManifest.platformVersion; |
| 88 | + |
| 89 | + const files = getMockSnapFiles({ |
| 90 | + manifest: rawManifest, |
| 91 | + }); |
| 92 | + |
| 93 | + await productionPlatformVersion.semanticCheck(files, { report }); |
| 94 | + |
| 95 | + expect(report).toHaveBeenCalledTimes(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('does nothing if the request to check the production version fails', async () => { |
| 99 | + fetchMock.mockResponse('', { status: 404 }); |
| 100 | + |
| 101 | + const report = jest.fn(); |
| 102 | + assert(productionPlatformVersion.semanticCheck); |
| 103 | + |
| 104 | + const files = getMockSnapFiles(); |
| 105 | + |
| 106 | + await productionPlatformVersion.semanticCheck(files, { report }); |
| 107 | + |
| 108 | + expect(report).toHaveBeenCalledTimes(0); |
| 109 | + }); |
| 110 | +}); |
0 commit comments