fix: keep SMS smoke JSON output clean

This commit is contained in:
Codex
2026-07-03 23:33:21 +08:00
parent 5020d40636
commit 732b367909
2 changed files with 35 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import http from 'node:http';
import { spawn } from 'node:child_process';
import { runRemoteSmsLoginSmoke } from './remote-sms-login-smoke.js';
const tenantId = '00000000-0000-0000-0000-000000000001';
@@ -75,6 +76,29 @@ const server = http.createServer(async (req, res) => {
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
function runCliSmoke(address) {
return new Promise(resolve => {
const child = spawn(process.execPath, ['scripts/remote-sms-login-smoke.js', '--json'], {
cwd: process.cwd(),
env: {
...process.env,
SMS_SMOKE_API_BASE_URL: `http://127.0.0.1:${address.port}`,
SMS_SMOKE_TENANT_ID: tenantId,
SMS_SMOKE_PHONE: phone,
SMS_SMOKE_BIND_PHONE: bindPhone,
SMS_SMOKE_ORIGIN: 'https://admin.tjszsb.com',
SMS_SMOKE_CODE: '123456',
SMS_SMOKE_BIND_CODE: '654321',
},
});
let stdout = '';
let stderr = '';
child.stdout.on('data', chunk => { stdout += chunk.toString('utf8'); });
child.stderr.on('data', chunk => { stderr += chunk.toString('utf8'); });
child.on('close', status => resolve({ status, stdout, stderr }));
});
}
try {
const address = server.address();
const result = await runRemoteSmsLoginSmoke(
@@ -105,6 +129,13 @@ try {
assert.equal(seen.some(item => item.path === '/api/auth/sms/verify' && item.origin === 'https://admin.tjszsb.com'), true);
assert.equal(seen.some(item => item.path === '/api/auth/me'), true);
assert.equal(seen.some(item => item.path === '/api/auth/phone/bind'), true);
const cliResult = await runCliSmoke(address);
assert.equal(cliResult.status, 0, `CLI --json smoke should pass: ${cliResult.stdout} ${cliResult.stderr}`);
const cliSummary = JSON.parse(cliResult.stdout);
assert.equal(cliSummary.provider, 'aliyun-pnvs');
assert.equal(cliSummary.bindProvider, 'aliyun-pnvs');
assert.doesNotMatch(cliResult.stdout, /Enter received/, 'CLI --json stdout must stay machine-readable JSON');
console.log('[PASS] remote SMS login smoke script');
} finally {
await new Promise(resolve => server.close(resolve));

View File

@@ -1,5 +1,5 @@
import { createInterface } from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
import { stdin as input, stdout as output, stderr } from 'node:process';
import { fileURLToPath, pathToFileURL } from 'node:url';
const DEFAULT_TIMEOUT_MS = 15_000;
@@ -108,7 +108,7 @@ function assertOk(name, response) {
async function promptCode(config) {
if (config.code) return config.code;
const readline = createInterface({ input, output });
const readline = createInterface({ input, output: config.jsonOutput ? stderr : output });
try {
return (await readline.question('Enter received SMS verification code: ')).trim();
} finally {
@@ -118,7 +118,7 @@ async function promptCode(config) {
async function promptBindCode(config) {
if (config.bindCode) return config.bindCode;
const readline = createInterface({ input, output });
const readline = createInterface({ input, output: config.jsonOutput ? stderr : output });
try {
return (await readline.question('Enter received bind_phone SMS verification code: ')).trim();
} finally {
@@ -216,7 +216,7 @@ async function runRemoteSmsLoginSmoke(inputConfig, options = {}) {
async function main() {
const options = parseArgs(process.argv.slice(2));
try {
const result = await runRemoteSmsLoginSmoke(null, { quiet: options.quiet || options.json });
const result = await runRemoteSmsLoginSmoke({ ...buildConfig(), jsonOutput: options.json }, { quiet: options.quiet || options.json });
if (options.json) console.log(JSON.stringify(result.summary, null, 2));
} catch (error) {
if (options.json) {