|
| 1 | +const assert = require('assert') |
| 2 | +const hre = require('hardhat') |
| 3 | +const storage = require('ethernaut-common/src/io/storage') |
| 4 | +const { setEnvVar } = require('ethernaut-common/src/io/env') |
| 5 | + |
| 6 | +describe('setAgoraKey task', function () { |
| 7 | + let originalEnv, originalSetEnvVar, originalSaveConfig, originalReadConfig |
| 8 | + |
| 9 | + beforeEach(function () { |
| 10 | + // Save original environment variable value |
| 11 | + originalEnv = process.env.AGORA_API_KEY |
| 12 | + |
| 13 | + // Stub setEnvVar to immediately update process.env |
| 14 | + originalSetEnvVar = setEnvVar |
| 15 | + require('ethernaut-common/src/io/env').setEnvVar = (key, value) => { |
| 16 | + process.env[key] = value |
| 17 | + } |
| 18 | + |
| 19 | + // Stub storage functions to prevent file I/O |
| 20 | + originalSaveConfig = storage.saveConfig |
| 21 | + originalReadConfig = storage.readConfig |
| 22 | + storage.saveConfig = () => {} // no-op |
| 23 | + storage.readConfig = () => ({}) |
| 24 | + }) |
| 25 | + |
| 26 | + afterEach(function () { |
| 27 | + // Restore original environment variable and function references |
| 28 | + process.env.AGORA_API_KEY = originalEnv |
| 29 | + require('ethernaut-common/src/io/env').setEnvVar = originalSetEnvVar |
| 30 | + storage.saveConfig = originalSaveConfig |
| 31 | + storage.readConfig = originalReadConfig |
| 32 | + }) |
| 33 | + |
| 34 | + it('should set the Agora API Key and instruct to restart', async function () { |
| 35 | + const testKey = 'TEST_API_KEY' |
| 36 | + |
| 37 | + const result = await hre.run( |
| 38 | + { scope: 'optigov', task: 'agorakey' }, |
| 39 | + { apiKey: testKey }, |
| 40 | + ) |
| 41 | + |
| 42 | + // Verify the output contains the new key and the restart message. |
| 43 | + assert(result.includes(`- Agora API Key set to ${testKey}`)) |
| 44 | + assert( |
| 45 | + result.includes( |
| 46 | + 'Please restart the tool for the new API key to take effect.', |
| 47 | + ), |
| 48 | + ) |
| 49 | + // Also verify process.env was updated |
| 50 | + assert.strictEqual(process.env.AGORA_API_KEY, testKey) |
| 51 | + }) |
| 52 | +}) |
0 commit comments