|
| 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 runTh(args, cwd) { |
| 8 | + return spawnSync('node', [path.resolve('packages/cli/dist/index.js'), ...args], { |
| 9 | + cwd, |
| 10 | + encoding: 'utf-8' |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +function runCmd(cmd, args, cwd, extraEnv = {}) { |
| 15 | + return spawnSync(cmd, args, { |
| 16 | + cwd, |
| 17 | + encoding: 'utf-8', |
| 18 | + env: { ...process.env, ...extraEnv } |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +function hasAnvil() { |
| 23 | + const res = spawnSync('anvil', ['--version'], { encoding: 'utf-8' }); |
| 24 | + if (res.error && res.error.code === 'ENOENT') return false; |
| 25 | + return res.status === 0; |
| 26 | +} |
| 27 | + |
| 28 | +function waitForOutput(proc, pattern, timeoutMs) { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + const startedAt = Date.now(); |
| 31 | + let combined = ''; |
| 32 | + let done = false; |
| 33 | + |
| 34 | + function cleanup() { |
| 35 | + if (done) return; |
| 36 | + done = true; |
| 37 | + clearInterval(timer); |
| 38 | + proc.stdout?.off('data', onData); |
| 39 | + proc.stderr?.off('data', onData); |
| 40 | + } |
| 41 | + |
| 42 | + function onData(chunk) { |
| 43 | + combined += String(chunk ?? ''); |
| 44 | + if (pattern.test(combined)) { |
| 45 | + cleanup(); |
| 46 | + resolve(combined); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + proc.stdout?.on('data', onData); |
| 51 | + proc.stderr?.on('data', onData); |
| 52 | + |
| 53 | + const timer = setInterval(() => { |
| 54 | + if (Date.now() - startedAt < timeoutMs) return; |
| 55 | + cleanup(); |
| 56 | + reject(new Error(`Timed out waiting for output match: ${pattern}\nOutput:\n${combined}`)); |
| 57 | + }, 200); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +describe('Generated app UI tests', function () { |
| 62 | + it('emits schema-aware UI smoke tests that pass against canonical job-board preview', async function () { |
| 63 | + this.timeout(240000); |
| 64 | + if (!hasAnvil()) this.skip(); |
| 65 | + |
| 66 | + const schemaPath = path.join(process.cwd(), 'apps', 'example', 'job-board.schema.json'); |
| 67 | + const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), 'th-generated-ui-tests-')); |
| 68 | + const generateOut = path.join(rootDir, 'generated'); |
| 69 | + const buildOut = path.join(rootDir, 'build'); |
| 70 | + const uiDir = path.join(generateOut, 'ui'); |
| 71 | + |
| 72 | + const generateRes = runTh(['generate', schemaPath, '--out', generateOut, '--with-tests'], process.cwd()); |
| 73 | + expect(generateRes.status, generateRes.stderr || generateRes.stdout).to.equal(0); |
| 74 | + |
| 75 | + const installRes = runCmd('pnpm', ['install'], uiDir, { NEXT_TELEMETRY_DISABLED: '1' }); |
| 76 | + expect(installRes.status, installRes.stderr || installRes.stdout).to.equal(0); |
| 77 | + |
| 78 | + const buildRes = runTh(['build', schemaPath, '--out', buildOut], process.cwd()); |
| 79 | + expect(buildRes.status, buildRes.stderr || buildRes.stdout).to.equal(0); |
| 80 | + |
| 81 | + const host = '127.0.0.1'; |
| 82 | + const port = 46000 + Math.floor(Math.random() * 1000); |
| 83 | + const baseUrl = `http://${host}:${port}`; |
| 84 | + const preview = spawn( |
| 85 | + 'node', |
| 86 | + [path.resolve('packages/cli/dist/index.js'), 'preview', buildOut, '--host', host, '--port', String(port)], |
| 87 | + { cwd: process.cwd(), stdio: ['ignore', 'pipe', 'pipe'] } |
| 88 | + ); |
| 89 | + |
| 90 | + try { |
| 91 | + await waitForOutput(preview, new RegExp(`${baseUrl}/`), 90000); |
| 92 | + const uiTestRes = runCmd('pnpm', ['run', 'test:ui'], uiDir, { |
| 93 | + NEXT_TELEMETRY_DISABLED: '1', |
| 94 | + TH_UI_BASE_URL: baseUrl |
| 95 | + }); |
| 96 | + expect(uiTestRes.status, uiTestRes.stderr || uiTestRes.stdout).to.equal(0); |
| 97 | + expect(uiTestRes.stdout).to.include('PASS ui smoke scaffold (live checks @'); |
| 98 | + } finally { |
| 99 | + preview.kill('SIGINT'); |
| 100 | + } |
| 101 | + }); |
| 102 | +}); |
0 commit comments