|
| 1 | +import { expect } from 'chai'; |
| 2 | +import fs from 'fs'; |
| 3 | +import os from 'os'; |
| 4 | +import path from 'path'; |
| 5 | +import { spawn, spawnSync } from 'child_process'; |
| 6 | + |
| 7 | +function writeJson(filePath, value) { |
| 8 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 9 | + fs.writeFileSync(filePath, JSON.stringify(value, null, 2)); |
| 10 | +} |
| 11 | + |
| 12 | +function runTh(args, cwd) { |
| 13 | + return spawnSync('node', [path.resolve('packages/cli/dist/index.js'), ...args], { |
| 14 | + cwd, |
| 15 | + encoding: 'utf-8' |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function hasAnvil() { |
| 20 | + const res = spawnSync('anvil', ['--version'], { encoding: 'utf-8' }); |
| 21 | + if (res.error && res.error.code === 'ENOENT') return false; |
| 22 | + return res.status === 0; |
| 23 | +} |
| 24 | + |
| 25 | +function waitForOutput(proc, pattern, timeoutMs) { |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + const startedAt = Date.now(); |
| 28 | + let combined = ''; |
| 29 | + let done = false; |
| 30 | + |
| 31 | + function cleanup() { |
| 32 | + if (done) return; |
| 33 | + done = true; |
| 34 | + clearInterval(timer); |
| 35 | + proc.stdout?.off('data', onData); |
| 36 | + proc.stderr?.off('data', onData); |
| 37 | + } |
| 38 | + |
| 39 | + function onData(chunk) { |
| 40 | + combined += String(chunk ?? ''); |
| 41 | + if (pattern.test(combined)) { |
| 42 | + cleanup(); |
| 43 | + resolve(combined); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + proc.stdout?.on('data', onData); |
| 48 | + proc.stderr?.on('data', onData); |
| 49 | + |
| 50 | + const timer = setInterval(() => { |
| 51 | + if (Date.now() - startedAt < timeoutMs) return; |
| 52 | + cleanup(); |
| 53 | + reject(new Error(`Timed out waiting for output match: ${pattern}\nOutput:\n${combined}`)); |
| 54 | + }, 200); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +async function requestJson(url, init) { |
| 59 | + const res = await fetch(url, init); |
| 60 | + const text = await res.text(); |
| 61 | + let json = null; |
| 62 | + try { |
| 63 | + json = text ? JSON.parse(text) : null; |
| 64 | + } catch { |
| 65 | + json = null; |
| 66 | + } |
| 67 | + return { status: res.status, json, text }; |
| 68 | +} |
| 69 | + |
| 70 | +function schemaForIntegration() { |
| 71 | + return { |
| 72 | + thsVersion: '2025-12', |
| 73 | + schemaVersion: '0.0.1', |
| 74 | + app: { |
| 75 | + name: 'Integration Test App', |
| 76 | + slug: 'integration-test-app', |
| 77 | + features: { uploads: false, onChainIndexing: true } |
| 78 | + }, |
| 79 | + collections: [ |
| 80 | + { |
| 81 | + name: 'Candidate', |
| 82 | + fields: [{ name: 'name', type: 'string', required: true }], |
| 83 | + createRules: { required: ['name'], access: 'public' }, |
| 84 | + visibilityRules: { gets: ['name'], access: 'public' }, |
| 85 | + updateRules: { mutable: ['name'], access: 'owner' }, |
| 86 | + deleteRules: { softDelete: true, access: 'owner' }, |
| 87 | + transferRules: { access: 'owner' }, |
| 88 | + indexes: { unique: [], index: [] } |
| 89 | + } |
| 90 | + ] |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +describe('CLI local integration (anvil + preview + faucet)', function () { |
| 95 | + it('builds, auto-deploys in preview, serves manifest, and funds wallet via faucet endpoint', async function () { |
| 96 | + this.timeout(180000); |
| 97 | + |
| 98 | + if (!hasAnvil()) this.skip(); |
| 99 | + |
| 100 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'th-integration-')); |
| 101 | + const schemaPath = path.join(dir, 'schema.json'); |
| 102 | + const outDir = path.join(dir, 'out'); |
| 103 | + const schema = schemaForIntegration(); |
| 104 | + writeJson(schemaPath, schema); |
| 105 | + |
| 106 | + const buildRes = runTh(['build', schemaPath, '--out', outDir], process.cwd()); |
| 107 | + expect(buildRes.status, buildRes.stderr || buildRes.stdout).to.equal(0); |
| 108 | + |
| 109 | + const port = 42000 + Math.floor(Math.random() * 2000); |
| 110 | + const host = '127.0.0.1'; |
| 111 | + const baseUrl = `http://${host}:${port}`; |
| 112 | + |
| 113 | + const preview = spawn( |
| 114 | + 'node', |
| 115 | + [path.resolve('packages/cli/dist/index.js'), 'preview', outDir, '--host', host, '--port', String(port)], |
| 116 | + { cwd: process.cwd(), stdio: ['ignore', 'pipe', 'pipe'] } |
| 117 | + ); |
| 118 | + |
| 119 | + try { |
| 120 | + const previewReadyPattern = new RegExp(`http://${host}:${port}/`); |
| 121 | + await waitForOutput(preview, previewReadyPattern, 60000); |
| 122 | + |
| 123 | + const home = await requestJson(`${baseUrl}/`); |
| 124 | + expect(home.status).to.equal(200); |
| 125 | + |
| 126 | + const manifestRes = await requestJson(`${baseUrl}/.well-known/tokenhost/manifest.json`); |
| 127 | + expect(manifestRes.status).to.equal(200); |
| 128 | + expect(manifestRes.json?.deployments?.[0]?.deploymentEntrypointAddress).to.match(/^0x[0-9a-fA-F]{40}$/); |
| 129 | + expect(manifestRes.json?.deployments?.[0]?.deploymentEntrypointAddress.toLowerCase()).to.not.equal( |
| 130 | + '0x0000000000000000000000000000000000000000' |
| 131 | + ); |
| 132 | + |
| 133 | + const faucetStatus = await requestJson(`${baseUrl}/__tokenhost/faucet`); |
| 134 | + expect(faucetStatus.status).to.equal(200); |
| 135 | + expect(faucetStatus.json?.ok).to.equal(true); |
| 136 | + expect(faucetStatus.json?.enabled).to.equal(true); |
| 137 | + expect(faucetStatus.json?.chainId).to.equal(31337); |
| 138 | + |
| 139 | + const addr = '0x1111111111111111111111111111111111111111'; |
| 140 | + const faucetFund = await requestJson(`${baseUrl}/__tokenhost/faucet`, { |
| 141 | + method: 'POST', |
| 142 | + headers: { 'content-type': 'application/json' }, |
| 143 | + body: JSON.stringify({ address: addr }) |
| 144 | + }); |
| 145 | + expect(faucetFund.status).to.equal(200); |
| 146 | + expect(faucetFund.json?.ok).to.equal(true); |
| 147 | + expect(typeof faucetFund.json?.newBalanceWei).to.equal('string'); |
| 148 | + } finally { |
| 149 | + preview.kill('SIGINT'); |
| 150 | + } |
| 151 | + }); |
| 152 | +}); |
0 commit comments