|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const http = require('http'); |
| 4 | +const os = require('os'); |
| 5 | +const { spawnSync, spawn } = require('child_process'); |
| 6 | +const { writeJsonAtomic } = require('../../lib/cli-file-utils'); |
| 7 | +const { normalizeWireApi, buildModelProbeSpec } = require('../../lib/cli-models-utils'); |
| 8 | + |
| 9 | +const debug = (...args) => { |
| 10 | + if (process.env.E2E_DEBUG) { |
| 11 | + console.error('[e2e]', ...args); |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +function assert(condition, message) { |
| 16 | + if (!condition) { |
| 17 | + throw new Error(message); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function fileMode(filePath) { |
| 22 | + return fs.existsSync(filePath) ? (fs.statSync(filePath).mode & 0o777) : 0; |
| 23 | +} |
| 24 | + |
| 25 | +function captureFileState(filePath) { |
| 26 | + const state = { |
| 27 | + path: filePath, |
| 28 | + exists: false, |
| 29 | + readable: true, |
| 30 | + content: '', |
| 31 | + error: '' |
| 32 | + }; |
| 33 | + |
| 34 | + state.exists = fs.existsSync(filePath); |
| 35 | + if (!state.exists) { |
| 36 | + return state; |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + state.content = fs.readFileSync(filePath, 'utf-8'); |
| 41 | + } catch (e) { |
| 42 | + state.readable = false; |
| 43 | + state.error = e && e.message ? e.message : String(e); |
| 44 | + } |
| 45 | + return state; |
| 46 | +} |
| 47 | + |
| 48 | +function assertFileUnchanged(state, label) { |
| 49 | + if (!state || !state.readable) return; |
| 50 | + const name = label || state.path; |
| 51 | + if (state.exists) { |
| 52 | + assert(fs.existsSync(state.path), `${name} disappeared during e2e`); |
| 53 | + const current = fs.readFileSync(state.path, 'utf-8'); |
| 54 | + assert(current === state.content, `${name} changed during e2e`); |
| 55 | + return; |
| 56 | + } |
| 57 | + assert(!fs.existsSync(state.path), `${name} should not be created during e2e`); |
| 58 | +} |
| 59 | + |
| 60 | +function runSync(node, args, options = {}) { |
| 61 | + const result = spawnSync(node, args, { |
| 62 | + encoding: 'utf-8', |
| 63 | + ...options |
| 64 | + }); |
| 65 | + return result; |
| 66 | +} |
| 67 | + |
| 68 | +function runWithInput(node, args, input, options = {}) { |
| 69 | + return new Promise((resolve) => { |
| 70 | + let child; |
| 71 | + try { |
| 72 | + child = spawn(node, args, { ...options, stdio: ['pipe', 'pipe', 'pipe'] }); |
| 73 | + } catch (err) { |
| 74 | + return resolve({ |
| 75 | + status: 1, |
| 76 | + stdout: '', |
| 77 | + stderr: err && err.message ? err.message : String(err) |
| 78 | + }); |
| 79 | + } |
| 80 | + let stdout = ''; |
| 81 | + let stderr = ''; |
| 82 | + child.stdout.on('data', chunk => stdout += chunk.toString()); |
| 83 | + child.stderr.on('data', chunk => stderr += chunk.toString()); |
| 84 | + child.on('error', (err) => { |
| 85 | + resolve({ |
| 86 | + status: 1, |
| 87 | + stdout, |
| 88 | + stderr: stderr || (err && err.message ? err.message : String(err)) |
| 89 | + }); |
| 90 | + }); |
| 91 | + child.on('close', (code) => resolve({ status: code, stdout, stderr })); |
| 92 | + if (input) { |
| 93 | + child.stdin.write(input); |
| 94 | + } |
| 95 | + child.stdin.end(); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +function postJson(port, payload, timeoutMs = 2000) { |
| 100 | + return new Promise((resolve, reject) => { |
| 101 | + const data = JSON.stringify(payload); |
| 102 | + const req = http.request({ |
| 103 | + hostname: '127.0.0.1', |
| 104 | + port, |
| 105 | + path: '/api', |
| 106 | + method: 'POST', |
| 107 | + headers: { |
| 108 | + 'Content-Type': 'application/json', |
| 109 | + 'Content-Length': Buffer.byteLength(data) |
| 110 | + } |
| 111 | + }, (res) => { |
| 112 | + let body = ''; |
| 113 | + res.setEncoding('utf-8'); |
| 114 | + res.on('data', chunk => body += chunk); |
| 115 | + res.on('end', () => { |
| 116 | + try { |
| 117 | + resolve(JSON.parse(body || '{}')); |
| 118 | + } catch (e) { |
| 119 | + reject(new Error('Invalid JSON response')); |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + req.on('error', reject); |
| 125 | + req.setTimeout(timeoutMs, () => { |
| 126 | + req.destroy(new Error('Request timeout')); |
| 127 | + }); |
| 128 | + req.write(data); |
| 129 | + req.end(); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +async function waitForServer(port, retries = 20, delayMs = 200) { |
| 134 | + let lastError; |
| 135 | + for (let i = 0; i < retries; i++) { |
| 136 | + try { |
| 137 | + await postJson(port, { action: 'status' }, 1000); |
| 138 | + return; |
| 139 | + } catch (e) { |
| 140 | + lastError = e; |
| 141 | + debug(`wait retry ${i + 1}/${retries}: ${e && e.message ? e.message : e}`); |
| 142 | + await new Promise(resolve => setTimeout(resolve, delayMs)); |
| 143 | + } |
| 144 | + } |
| 145 | + throw lastError || new Error('Server not ready'); |
| 146 | +} |
| 147 | + |
| 148 | +function startLocalServer(options = {}) { |
| 149 | + const mode = options.mode || 'list'; |
| 150 | + const modelsPath = options.modelsPath || '/models'; |
| 151 | + const status = options.status || 200; |
| 152 | + return new Promise((resolve, reject) => { |
| 153 | + const server = http.createServer((req, res) => { |
| 154 | + if (req.url && req.url.startsWith(modelsPath)) { |
| 155 | + if (mode === 'none') { |
| 156 | + res.writeHead(404, { 'Content-Type': 'application/json' }); |
| 157 | + res.end(JSON.stringify({ error: 'not found' })); |
| 158 | + return; |
| 159 | + } |
| 160 | + if (mode === 'html') { |
| 161 | + res.writeHead(status, { 'Content-Type': 'text/html' }); |
| 162 | + res.end('<!doctype html><html><body>ok</body></html>'); |
| 163 | + return; |
| 164 | + } |
| 165 | + res.writeHead(status, { 'Content-Type': 'application/json' }); |
| 166 | + res.end(JSON.stringify({ |
| 167 | + data: [ |
| 168 | + { id: 'e2e2-model' }, |
| 169 | + { id: 'e2e2-model-2' } |
| 170 | + ] |
| 171 | + })); |
| 172 | + return; |
| 173 | + } |
| 174 | + res.writeHead(status, { 'Content-Type': 'application/json' }); |
| 175 | + res.end(JSON.stringify({ ok: true })); |
| 176 | + }); |
| 177 | + server.on('error', reject); |
| 178 | + server.listen(0, '127.0.0.1', () => { |
| 179 | + const address = server.address(); |
| 180 | + resolve({ server, port: address.port }); |
| 181 | + }); |
| 182 | + }); |
| 183 | +} |
| 184 | + |
| 185 | +function closeServer(server) { |
| 186 | + return new Promise((resolve) => { |
| 187 | + if (!server) return resolve(); |
| 188 | + try { |
| 189 | + server.close(() => resolve()); |
| 190 | + } catch (e) { |
| 191 | + resolve(); |
| 192 | + } |
| 193 | + }); |
| 194 | +} |
| 195 | + |
| 196 | +module.exports = { |
| 197 | + fs, |
| 198 | + path, |
| 199 | + os, |
| 200 | + debug, |
| 201 | + assert, |
| 202 | + fileMode, |
| 203 | + captureFileState, |
| 204 | + assertFileUnchanged, |
| 205 | + runSync, |
| 206 | + runWithInput, |
| 207 | + postJson, |
| 208 | + waitForServer, |
| 209 | + startLocalServer, |
| 210 | + closeServer, |
| 211 | + writeJsonAtomic, |
| 212 | + normalizeWireApi, |
| 213 | + buildModelProbeSpec |
| 214 | +}; |
0 commit comments