diff --git a/.github/workflows/wallet-ci.yml b/.github/workflows/wallet-ci.yml new file mode 100644 index 0000000..81a732e --- /dev/null +++ b/.github/workflows/wallet-ci.yml @@ -0,0 +1,26 @@ +name: goe-wallet-ci + +on: + push: + branches: + - 'test' + workflow_dispatch: + +jobs: + wallet-create: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 20 + cache: 'yarn' + + - run: yarn install + + - run: yarn build + + - name: Run Wallet Create CI Test + run: node test/createWalletCI.mjs diff --git a/src/cli/wallet/utils.ts b/src/cli/wallet/utils.ts index 7240540..3fe5324 100644 --- a/src/cli/wallet/utils.ts +++ b/src/cli/wallet/utils.ts @@ -1,6 +1,14 @@ import readlineSync from 'readline-sync'; export function promptPassword(message: string): string { + if (process.env.GOE_TEST_MODE === '1') { + const pwd = process.env.GOE_TEST_PASSWORD; + if (!pwd) { + throw new Error('GOE_TEST_PASSWORD not set'); + } + return pwd; + } + return readlineSync.question(message, { hideEchoBack: true, mask: '*' diff --git a/test/createWalletCI.mjs b/test/createWalletCI.mjs new file mode 100644 index 0000000..a966035 --- /dev/null +++ b/test/createWalletCI.mjs @@ -0,0 +1,43 @@ +import path from 'path'; +import { runCommand } from "./runCommand.mjs"; + +const DIST_CLI = path.resolve('./dist/cli/index.js'); +const PASSWORD = '12345678'; + +(async () => { + try { + const output = await runCommand( + 'node', + [DIST_CLI, 'wallet', 'create'], + { + env: { + GOE_TEST_MODE: '1', + GOE_TEST_PASSWORD: PASSWORD, + }, + capture: true + } + ); + if (!output.includes('Wallet Address:')) { + throw new Error('Wallet creation failed: no address found'); + } + + console.log('Unlocking wallet using GOE_TEST_PASSWORD...'); + await runCommand( + 'node', + [DIST_CLI, 'wallet', 'unlock'], + { + env: { + GOE_TEST_MODE: '1', + GOE_TEST_PASSWORD: PASSWORD, + }, + } + ); + + await runCommand('node', [DIST_CLI, 'wallet', 'lock']); + + console.log('✅ Wallet create + unlock/lock test passed!'); + } catch (e) { + console.error('❌ Wallet create test failed:', e.message); + process.exit(1); + } +})(); diff --git a/test/runCommand.mjs b/test/runCommand.mjs new file mode 100644 index 0000000..fa4f644 --- /dev/null +++ b/test/runCommand.mjs @@ -0,0 +1,49 @@ +/* -------- exec: capture stdout -------- */ +import {spawn} from "child_process"; + +const SHELL_CMDS = new Set([ + 'npm', + 'pnpm', + 'yarn', + 'npx', +]); + + +export function runCommand(cmd, args = [], options = {}) { + const { + capture = false, + env = {}, + cwd, + } = options; + + const useShell = SHELL_CMDS.has(cmd); + return new Promise((resolve, reject) => { + const child = spawn(cmd, args, { + shell: useShell, // ⭐ + stdio: capture ? 'pipe' : 'inherit', + env: { ...process.env, ...env }, + cwd, + }); + + let stdout = ''; + let stderr = ''; + + if (capture) { + child.stdout.on('data', d => (stdout += d)); + child.stderr.on('data', d => (stderr += d)); + } + + child.on('error', reject); + + child.on('close', (code) => { + if (code !== 0) { + return reject( + new Error( + stderr.trim() || `Command failed: ${cmd} ${args.join(' ')}` + ) + ); + } + resolve(capture ? stdout : true); + }); + }); +} diff --git a/test/test.mjs b/test/test.mjs new file mode 100644 index 0000000..0e3d682 --- /dev/null +++ b/test/test.mjs @@ -0,0 +1,251 @@ +// test/goeE2ETest.mjs +import 'dotenv/config'; +import path from 'path'; +import fs from 'fs'; +import { fileURLToPath } from 'url'; +import { runCommand } from "./runCommand.mjs"; + +const DIST_CLI = path.resolve('./dist/cli/index.js'); +const CHAIN_ID = '11155111'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const PROJECT_ROOT = path.resolve(__dirname, '../test'); +const TEMP_DIR = path.join(PROJECT_ROOT, '/.tmp'); + +const PASSWORD = process.env.GOE_TEST_PASSWORD; + +if (!PASSWORD) { + console.error('Please set "GOE_TEST_PASSWORD" in .env'); + process.exit(1); +} + + + +/* ---------------- utils ---------------- */ + +const randomRepoName = () => + `goe-e2e-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`; + +const stripAnsi = (s) => + s.replace(/\x1B\[[0-9;]*m/g, ''); + + +/* ------------------- Wallet ------------------- */ +async function checkWalletExists() { + const out = await runCommand('node', [DIST_CLI, 'wallet', 'list'], { capture: true }); + if (out.includes('No wallets found')) { + throw new Error('No wallet exists. Create and fund one first.'); + } +} + +async function unlockWallet() { + console.log('Unlocking wallet using GOE_TEST_PASSWORD...'); + await runCommand( + 'node', + [DIST_CLI, 'wallet', 'unlock'], + { + env: { + GOE_TEST_MODE: '1', + GOE_TEST_PASSWORD: PASSWORD, + }, + } + ); +} + +const lockWallet = () => + runCommand('node', [DIST_CLI, 'wallet', 'lock']); + +/* ------------------- Repo ------------------- */ +async function createRepo() { + const name = randomRepoName(); + const out = await runCommand( + 'node', + [DIST_CLI, 'repo', 'create', name, '--chain-id', CHAIN_ID], + { capture: true } + ); + const match = stripAnsi(out).match(/0x[a-fA-F0-9]{40}/); + if (!match) { + throw new Error('Repo address not found'); + } + + return { name, address: match[0] }; +} + +const listRepos = () => + runCommand('node', [DIST_CLI, 'repo', 'list', '--chain-id', CHAIN_ID]); + +const listBranches = (addr) => + runCommand('node', [DIST_CLI, 'repo', 'branches', addr, '--chain-id', CHAIN_ID],); + +const setDefaultBranch = (addr) => + runCommand('node', [DIST_CLI, 'repo', 'default-branch', addr, 'main', '--chain-id', CHAIN_ID],); + +/* ------------------- Git flow ------------------- */ +async function gitFlow(goe) { + const repoPath = path.join(TEMP_DIR, 'flow'); + fs.mkdirSync(repoPath, { recursive: true }); + process.chdir(repoPath); + + // init + remote + await runCommand('git', ['init']); + await runCommand('git', ['remote', 'add', 'origin', goe]); + await runCommand('git', ['checkout', '-b', 'main']); + + // init commit + fs.writeFileSync('README.md', '# GoE E2E\n'); + await runCommand('git', ['add', '.']); + await runCommand('git', ['commit', '-m', 'init']); + await runCommand('git', ['push', 'origin', 'main']); + + // feature branch + await runCommand('git', ['checkout', '-b', 'feature']); + fs.writeFileSync('feature.txt', 'feature\n'); + await runCommand('git', ['add', '.']); + await runCommand('git', ['commit', '-m', 'feature']); + await runCommand('git', ['push', 'origin', 'feature']); + + // force push + fs.writeFileSync('feature.txt', 'force\n'); + await runCommand('git', ['add', '.']); + await runCommand('git', ['commit', '-m', 'force-update']); + await runCommand('git', ['push', '--force', 'origin', 'feature']); + + // delete branch + await runCommand('git', ['push', 'origin', '--delete', 'feature']); +} + +async function gitCloneVerify(goe) { + const clonePath = path.join(TEMP_DIR, 'clone'); + fs.mkdirSync(clonePath, { recursive: true }); + process.chdir(TEMP_DIR); + + await runCommand('git', ['clone', goe, clonePath]); + process.chdir(clonePath); + + const readme = fs.readFileSync(path.join(clonePath, 'README.md'), 'utf8'); + if (!readme.includes('GoE E2E')) throw new Error('Clone verification failed'); + + const head = await runCommand('git',['symbolic-ref', 'HEAD'], { capture: true }); + if (!head.includes('refs/heads/main')) throw new Error('HEAD is not refs/heads/main'); + + const branches = await runCommand('git', ['branch', '-r'], { capture: true }); + if (branches.includes('feature')) throw new Error('Deleted branch still exists'); +} + +async function gitRejectNonFastForward(goe) { + const repoPath = path.join(TEMP_DIR, 'nff'); + fs.mkdirSync(repoPath, { recursive: true }); + process.chdir(repoPath); + + await runCommand('git', ['clone', goe, '.']); + + // push new file + fs.writeFileSync('nff.txt', '1'); + await runCommand('git', ['add', '.']); + await runCommand('git', ['commit', '-m', 'nff-1']); + await runCommand('git', ['push', 'origin', 'main']); + + // reset head + await runCommand('git', ['reset', '--hard', 'HEAD~1']); + + // non-fast-forward push should fail + try { + await runCommand('git', ['push', 'origin', 'main']); + throw new Error('Expected non-fast-forward push to fail'); + } catch { + console.log('[expected] non-fast-forward rejected'); + } +} + +async function gitMergePush(goe) { + const repoPath = path.join(TEMP_DIR, 'merge'); + fs.mkdirSync(repoPath, { recursive: true }); + process.chdir(repoPath); + + await runCommand('git', ['clone', goe, '.']); + + // create a branch and commit + await runCommand('git', ['checkout', '-b', 'merge-a']); + fs.writeFileSync('a.txt', 'a'); + await runCommand('git', ['add', 'a.txt']); + await runCommand('git', ['commit', '-m', 'a']); + + // merge back to main + await runCommand('git', ['checkout', 'main']); + await runCommand('git', ['merge', 'merge-a']); + + await runCommand('git', ['push', 'origin', 'main']); +} + +async function gitFetchUpdate(goe) { + const repoPath = path.join(TEMP_DIR, 'fetch'); + fs.mkdirSync(repoPath, { recursive: true }); + process.chdir(repoPath); + + await runCommand('git', ['clone', goe, '.']); + + fs.writeFileSync('fetch.txt', 'x'); + await runCommand('git', ['add', 'fetch.txt']); + await runCommand('git', ['commit', '-m', 'fetch-update']); + await runCommand('git', ['push', 'origin', 'main']); + + // simulate fetch from another clone + const clonePath = path.join(TEMP_DIR, 'fetch-clone'); + fs.mkdirSync(clonePath, { recursive: true }); + await runCommand('git', ['clone', goe, clonePath]); + process.chdir(clonePath); + + await runCommand('git', ['fetch', 'origin']); + const result = await runCommand('git', ['log', 'origin/main', '--oneline', '-1'], { capture: true }); + if (!result.includes('fetch-update')) throw new Error('Fetch did not update origin/main'); +} + + +function cleanup() { + process.chdir(PROJECT_ROOT); + fs.rmSync(TEMP_DIR, { recursive: true, force: true }); + console.log('Local test data cleaned'); +} + +/* ------------------- main ------------------- */ +async function npmPrepareAndLink() { + console.log('\nPreparing local goe-cli...'); + + // 1. build + await runCommand('npm', ['run', 'build']); + + // 2. link + console.log('\nLinking local goe-cli...'); + await runCommand('npm', ['link']); +} + +(async () => { + fs.mkdirSync(TEMP_DIR, { recursive: true }); + + // goe + await npmPrepareAndLink(); + await checkWalletExists(); + await unlockWallet(); + const {name, address} = await createRepo(); + await listRepos(); + + // git helper + const goe = `goe://${name}:${CHAIN_ID}` + try { + await gitFlow(goe); + await gitCloneVerify(goe); + await gitRejectNonFastForward(goe); + await gitMergePush(goe); + await gitFetchUpdate(goe); + } finally { + cleanup(); + } + + await listBranches(address); + await setDefaultBranch(address); + await lockWallet(); + console.log('\n=== GoE E2E test finished successfully ==='); +})().catch((e) => { + console.error(e.message); + process.exit(1); +});