|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { getOrbitChainContractVersions } from './getOrbitChainContractVersions'; |
| 4 | +import { runDockerCommand } from './runDockerCommand'; |
| 5 | + |
| 6 | +vi.mock('./runDockerCommand', () => ({ |
| 7 | + runDockerCommand: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +describe('getOrbitChainContractVersions', () => { |
| 11 | + beforeEach(() => { |
| 12 | + vi.mocked(runDockerCommand).mockReset(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('uses the default Orbit Actions image through the internal docker runner', async () => { |
| 16 | + vi.mocked(runDockerCommand).mockResolvedValueOnce({ |
| 17 | + argv: ['docker', 'run'], |
| 18 | + stdout: |
| 19 | + '{"versions":{"Inbox":"v1.1.1","RollupProxy":"v1.1.1"},"upgradeRecommendation":{"message":"No upgrade path found"}}', |
| 20 | + stderr: '', |
| 21 | + exitCode: 0, |
| 22 | + }); |
| 23 | + |
| 24 | + const result = await getOrbitChainContractVersions({ |
| 25 | + inboxAddress: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 26 | + network: 'arb1', |
| 27 | + env: { |
| 28 | + PARENT_CHAIN_RPC: 'https://rpc.example', |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + expect(runDockerCommand).toHaveBeenCalledWith({ |
| 33 | + image: 'offchainlabs/chain-actions', |
| 34 | + entrypoint: 'yarn', |
| 35 | + command: ['--silent', 'orbit:contracts:version', '--network', 'arb1', '--no-compile'], |
| 36 | + env: { |
| 37 | + PARENT_CHAIN_RPC: 'https://rpc.example', |
| 38 | + INBOX_ADDRESS: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 39 | + JSON_OUTPUT: 'true', |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result).toEqual({ |
| 44 | + versions: { |
| 45 | + Inbox: 'v1.1.1', |
| 46 | + RollupProxy: 'v1.1.1', |
| 47 | + }, |
| 48 | + upgradeRecommendation: { |
| 49 | + message: 'No upgrade path found', |
| 50 | + }, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('uses a custom Orbit Actions image when one is provided', async () => { |
| 55 | + vi.mocked(runDockerCommand).mockResolvedValueOnce({ |
| 56 | + argv: ['docker', 'run'], |
| 57 | + stdout: |
| 58 | + '{"versions":{"Inbox":"v1.1.1","Bridge":"v1.1.2","RollupProxy":null},"upgradeRecommendation":null}', |
| 59 | + stderr: '', |
| 60 | + exitCode: 0, |
| 61 | + }); |
| 62 | + |
| 63 | + const result = await getOrbitChainContractVersions({ |
| 64 | + image: 'offchainlabs/chain-actions:custom', |
| 65 | + inboxAddress: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 66 | + network: 'arb-sepolia', |
| 67 | + }); |
| 68 | + |
| 69 | + expect(runDockerCommand).toHaveBeenCalledWith({ |
| 70 | + image: 'offchainlabs/chain-actions:custom', |
| 71 | + entrypoint: 'yarn', |
| 72 | + command: ['--silent', 'orbit:contracts:version', '--network', 'arb-sepolia', '--no-compile'], |
| 73 | + env: { |
| 74 | + INBOX_ADDRESS: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 75 | + JSON_OUTPUT: 'true', |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result).toEqual({ |
| 80 | + versions: { |
| 81 | + Inbox: 'v1.1.1', |
| 82 | + Bridge: 'v1.1.2', |
| 83 | + RollupProxy: null, |
| 84 | + }, |
| 85 | + upgradeRecommendation: null, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('throws when the docker output does not contain a valid JSON payload', async () => { |
| 90 | + vi.mocked(runDockerCommand).mockResolvedValueOnce({ |
| 91 | + argv: ['docker', 'run'], |
| 92 | + stdout: 'not json', |
| 93 | + stderr: '', |
| 94 | + exitCode: 0, |
| 95 | + }); |
| 96 | + |
| 97 | + await expect( |
| 98 | + getOrbitChainContractVersions({ |
| 99 | + inboxAddress: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 100 | + network: 'arb1', |
| 101 | + }), |
| 102 | + ).rejects.toThrow('Failed to parse Orbit chain contract versions'); |
| 103 | + }); |
| 104 | + |
| 105 | + it.each(['null', '"unexpected"', '42', 'true', '[]'])( |
| 106 | + 'throws when the parsed JSON payload is not an object: %s', |
| 107 | + async (stdout) => { |
| 108 | + vi.mocked(runDockerCommand).mockResolvedValueOnce({ |
| 109 | + argv: ['docker', 'run'], |
| 110 | + stdout, |
| 111 | + stderr: '', |
| 112 | + exitCode: 0, |
| 113 | + }); |
| 114 | + |
| 115 | + await expect( |
| 116 | + getOrbitChainContractVersions({ |
| 117 | + inboxAddress: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 118 | + network: 'arb1', |
| 119 | + }), |
| 120 | + ).rejects.toThrow('Failed to parse Orbit chain contract versions'); |
| 121 | + }, |
| 122 | + ); |
| 123 | + |
| 124 | + it('throws when the JSON payload is missing top level fields', async () => { |
| 125 | + vi.mocked(runDockerCommand).mockResolvedValueOnce({ |
| 126 | + argv: ['docker', 'run'], |
| 127 | + stdout: '{"versions":{"Inbox":"v1.1.1"}}', |
| 128 | + stderr: '', |
| 129 | + exitCode: 0, |
| 130 | + }); |
| 131 | + |
| 132 | + await expect( |
| 133 | + getOrbitChainContractVersions({ |
| 134 | + inboxAddress: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 135 | + network: 'arb1', |
| 136 | + }), |
| 137 | + ).rejects.toThrow('Failed to parse Orbit chain contract versions'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('accepts any JSON payload that includes versions and upgradeRecommendation', async () => { |
| 141 | + vi.mocked(runDockerCommand).mockResolvedValueOnce({ |
| 142 | + argv: ['docker', 'run'], |
| 143 | + stdout: '{"versions":"not-validated","upgradeRecommendation":null}', |
| 144 | + stderr: '', |
| 145 | + exitCode: 0, |
| 146 | + }); |
| 147 | + |
| 148 | + await expect( |
| 149 | + getOrbitChainContractVersions({ |
| 150 | + inboxAddress: '0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9', |
| 151 | + network: 'arb1', |
| 152 | + }), |
| 153 | + ).resolves.toEqual({ |
| 154 | + versions: 'not-validated', |
| 155 | + upgradeRecommendation: null, |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments