|
| 1 | +// test/goeE2ETest.mjs |
| 2 | +import 'dotenv/config'; |
| 3 | +import path from 'path'; |
| 4 | +import fs from 'fs'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { runCommand } from "./runCommand.mjs"; |
| 7 | + |
| 8 | +const DIST_CLI = path.resolve('./dist/cli/index.js'); |
| 9 | +const CHAIN_ID = '11155111'; |
| 10 | + |
| 11 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 12 | +const PROJECT_ROOT = path.resolve(__dirname, '../test'); |
| 13 | +const TEMP_DIR = path.join(PROJECT_ROOT, '/.tmp'); |
| 14 | + |
| 15 | +const PASSWORD = process.env.GOE_TEST_PASSWORD; |
| 16 | + |
| 17 | +if (!PASSWORD) { |
| 18 | + console.error('Please set "GOE_TEST_PASSWORD" in .env'); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +/* ---------------- utils ---------------- */ |
| 25 | + |
| 26 | +const randomRepoName = () => |
| 27 | + `goe-e2e-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`; |
| 28 | + |
| 29 | +const stripAnsi = (s) => |
| 30 | + s.replace(/\x1B\[[0-9;]*m/g, ''); |
| 31 | + |
| 32 | + |
| 33 | +/* ------------------- Wallet ------------------- */ |
| 34 | +async function checkWalletExists() { |
| 35 | + const out = await runCommand('node', [DIST_CLI, 'wallet', 'list'], { capture: true }); |
| 36 | + if (out.includes('No wallets found')) { |
| 37 | + throw new Error('No wallet exists. Create and fund one first.'); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function unlockWallet() { |
| 42 | + console.log('Unlocking wallet using GOE_TEST_PASSWORD...'); |
| 43 | + await runCommand( |
| 44 | + 'node', |
| 45 | + [DIST_CLI, 'wallet', 'unlock'], |
| 46 | + { |
| 47 | + env: { |
| 48 | + GOE_TEST_MODE: '1', |
| 49 | + GOE_TEST_PASSWORD: PASSWORD, |
| 50 | + }, |
| 51 | + } |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +const lockWallet = () => |
| 56 | + runCommand('node', [DIST_CLI, 'wallet', 'lock']); |
| 57 | + |
| 58 | +/* ------------------- Repo ------------------- */ |
| 59 | +async function createRepo() { |
| 60 | + const name = randomRepoName(); |
| 61 | + const out = await runCommand( |
| 62 | + 'node', |
| 63 | + [DIST_CLI, 'repo', 'create', name, '--chain-id', CHAIN_ID], |
| 64 | + { capture: true } |
| 65 | + ); |
| 66 | + const match = stripAnsi(out).match(/0x[a-fA-F0-9]{40}/); |
| 67 | + if (!match) { |
| 68 | + throw new Error('Repo address not found'); |
| 69 | + } |
| 70 | + |
| 71 | + return { name, address: match[0] }; |
| 72 | +} |
| 73 | + |
| 74 | +const listRepos = () => |
| 75 | + runCommand('node', [DIST_CLI, 'repo', 'list', '--chain-id', CHAIN_ID]); |
| 76 | + |
| 77 | +const listBranches = (addr) => |
| 78 | + runCommand('node', [DIST_CLI, 'repo', 'branches', addr, '--chain-id', CHAIN_ID],); |
| 79 | + |
| 80 | +const setDefaultBranch = (addr) => |
| 81 | + runCommand('node', [DIST_CLI, 'repo', 'default-branch', addr, 'main', '--chain-id', CHAIN_ID],); |
| 82 | + |
| 83 | +/* ------------------- Git flow ------------------- */ |
| 84 | +async function gitFlow(goe) { |
| 85 | + const repoPath = path.join(TEMP_DIR, 'flow'); |
| 86 | + fs.mkdirSync(repoPath, { recursive: true }); |
| 87 | + process.chdir(repoPath); |
| 88 | + |
| 89 | + // init + remote |
| 90 | + await runCommand('git', ['init']); |
| 91 | + await runCommand('git', ['remote', 'add', 'origin', goe]); |
| 92 | + await runCommand('git', ['checkout', '-b', 'main']); |
| 93 | + |
| 94 | + // init commit |
| 95 | + fs.writeFileSync('README.md', '# GoE E2E\n'); |
| 96 | + await runCommand('git', ['add', '.']); |
| 97 | + await runCommand('git', ['commit', '-m', 'init']); |
| 98 | + await runCommand('git', ['push', 'origin', 'main']); |
| 99 | + |
| 100 | + // feature branch |
| 101 | + await runCommand('git', ['checkout', '-b', 'feature']); |
| 102 | + fs.writeFileSync('feature.txt', 'feature\n'); |
| 103 | + await runCommand('git', ['add', '.']); |
| 104 | + await runCommand('git', ['commit', '-m', 'feature']); |
| 105 | + await runCommand('git', ['push', 'origin', 'feature']); |
| 106 | + |
| 107 | + // force push |
| 108 | + fs.writeFileSync('feature.txt', 'force\n'); |
| 109 | + await runCommand('git', ['add', '.']); |
| 110 | + await runCommand('git', ['commit', '-m', 'force-update']); |
| 111 | + await runCommand('git', ['push', '--force', 'origin', 'feature']); |
| 112 | + |
| 113 | + // delete branch |
| 114 | + await runCommand('git', ['push', 'origin', '--delete', 'feature']); |
| 115 | +} |
| 116 | + |
| 117 | +async function gitCloneVerify(goe) { |
| 118 | + const clonePath = path.join(TEMP_DIR, 'clone'); |
| 119 | + fs.mkdirSync(clonePath, { recursive: true }); |
| 120 | + process.chdir(TEMP_DIR); |
| 121 | + |
| 122 | + await runCommand('git', ['clone', goe, clonePath]); |
| 123 | + process.chdir(clonePath); |
| 124 | + |
| 125 | + const readme = fs.readFileSync(path.join(clonePath, 'README.md'), 'utf8'); |
| 126 | + if (!readme.includes('GoE E2E')) throw new Error('Clone verification failed'); |
| 127 | + |
| 128 | + const head = await runCommand('git',['symbolic-ref', 'HEAD'], { capture: true }); |
| 129 | + if (!head.includes('refs/heads/main')) throw new Error('HEAD is not refs/heads/main'); |
| 130 | + |
| 131 | + const branches = await runCommand('git', ['branch', '-r'], { capture: true }); |
| 132 | + if (branches.includes('feature')) throw new Error('Deleted branch still exists'); |
| 133 | +} |
| 134 | + |
| 135 | +async function gitRejectNonFastForward(goe) { |
| 136 | + const repoPath = path.join(TEMP_DIR, 'nff'); |
| 137 | + fs.mkdirSync(repoPath, { recursive: true }); |
| 138 | + process.chdir(repoPath); |
| 139 | + |
| 140 | + await runCommand('git', ['clone', goe, '.']); |
| 141 | + |
| 142 | + // push new file |
| 143 | + fs.writeFileSync('nff.txt', '1'); |
| 144 | + await runCommand('git', ['add', '.']); |
| 145 | + await runCommand('git', ['commit', '-m', 'nff-1']); |
| 146 | + await runCommand('git', ['push', 'origin', 'main']); |
| 147 | + |
| 148 | + // reset head |
| 149 | + await runCommand('git', ['reset', '--hard', 'HEAD~1']); |
| 150 | + |
| 151 | + // non-fast-forward push should fail |
| 152 | + try { |
| 153 | + await runCommand('git', ['push', 'origin', 'main']); |
| 154 | + throw new Error('Expected non-fast-forward push to fail'); |
| 155 | + } catch { |
| 156 | + console.log('[expected] non-fast-forward rejected'); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +async function gitMergePush(goe) { |
| 161 | + const repoPath = path.join(TEMP_DIR, 'merge'); |
| 162 | + fs.mkdirSync(repoPath, { recursive: true }); |
| 163 | + process.chdir(repoPath); |
| 164 | + |
| 165 | + await runCommand('git', ['clone', goe, '.']); |
| 166 | + |
| 167 | + // create a branch and commit |
| 168 | + await runCommand('git', ['checkout', '-b', 'merge-a']); |
| 169 | + fs.writeFileSync('a.txt', 'a'); |
| 170 | + await runCommand('git', ['add', 'a.txt']); |
| 171 | + await runCommand('git', ['commit', '-m', 'a']); |
| 172 | + |
| 173 | + // merge back to main |
| 174 | + await runCommand('git', ['checkout', 'main']); |
| 175 | + await runCommand('git', ['merge', 'merge-a']); |
| 176 | + |
| 177 | + await runCommand('git', ['push', 'origin', 'main']); |
| 178 | +} |
| 179 | + |
| 180 | +async function gitFetchUpdate(goe) { |
| 181 | + const repoPath = path.join(TEMP_DIR, 'fetch'); |
| 182 | + fs.mkdirSync(repoPath, { recursive: true }); |
| 183 | + process.chdir(repoPath); |
| 184 | + |
| 185 | + await runCommand('git', ['clone', goe, '.']); |
| 186 | + |
| 187 | + fs.writeFileSync('fetch.txt', 'x'); |
| 188 | + await runCommand('git', ['add', 'fetch.txt']); |
| 189 | + await runCommand('git', ['commit', '-m', 'fetch-update']); |
| 190 | + await runCommand('git', ['push', 'origin', 'main']); |
| 191 | + |
| 192 | + // simulate fetch from another clone |
| 193 | + const clonePath = path.join(TEMP_DIR, 'fetch-clone'); |
| 194 | + fs.mkdirSync(clonePath, { recursive: true }); |
| 195 | + await runCommand('git', ['clone', goe, clonePath]); |
| 196 | + process.chdir(clonePath); |
| 197 | + |
| 198 | + await runCommand('git', ['fetch', 'origin']); |
| 199 | + const result = await runCommand('git', ['log', 'origin/main', '--oneline', '-1'], { capture: true }); |
| 200 | + if (!result.includes('fetch-update')) throw new Error('Fetch did not update origin/main'); |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +function cleanup() { |
| 205 | + process.chdir(PROJECT_ROOT); |
| 206 | + fs.rmSync(TEMP_DIR, { recursive: true, force: true }); |
| 207 | + console.log('Local test data cleaned'); |
| 208 | +} |
| 209 | + |
| 210 | +/* ------------------- main ------------------- */ |
| 211 | +async function npmPrepareAndLink() { |
| 212 | + console.log('\nPreparing local goe-cli...'); |
| 213 | + |
| 214 | + // 1. build |
| 215 | + await runCommand('npm', ['run', 'build']); |
| 216 | + |
| 217 | + // 2. link |
| 218 | + console.log('\nLinking local goe-cli...'); |
| 219 | + await runCommand('npm', ['link']); |
| 220 | +} |
| 221 | + |
| 222 | +(async () => { |
| 223 | + fs.mkdirSync(TEMP_DIR, { recursive: true }); |
| 224 | + |
| 225 | + // goe |
| 226 | + await npmPrepareAndLink(); |
| 227 | + await checkWalletExists(); |
| 228 | + await unlockWallet(); |
| 229 | + const {name, address} = await createRepo(); |
| 230 | + await listRepos(); |
| 231 | + |
| 232 | + // git helper |
| 233 | + const goe = `goe://${name}:${CHAIN_ID}` |
| 234 | + try { |
| 235 | + await gitFlow(goe); |
| 236 | + await gitCloneVerify(goe); |
| 237 | + await gitRejectNonFastForward(goe); |
| 238 | + await gitMergePush(goe); |
| 239 | + await gitFetchUpdate(goe); |
| 240 | + } finally { |
| 241 | + cleanup(); |
| 242 | + } |
| 243 | + |
| 244 | + await listBranches(address); |
| 245 | + await setDefaultBranch(address); |
| 246 | + await lockWallet(); |
| 247 | + console.log('\n=== GoE E2E test finished successfully ==='); |
| 248 | +})().catch((e) => { |
| 249 | + console.error(e.message); |
| 250 | + process.exit(1); |
| 251 | +}); |
0 commit comments