Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.
#!/usr/bin/env node
// Tái hiện các lỗi QA của REST Edit video trên 1 server đang chạy (mặc định cổng 4416):
// A2: 300 MB multipart không có dấu phân cách → phải 400 nhanh, RAM server không phình
// A3: upload với style sai / musicStyle sai / video hỏng → 4xx và KHÔNG để lại edits/<id>/
// A4: thiếu Content-Length → 411; 2 upload cùng lúc → 1 cái 429; DELETE
// revise: id lạ → 404, lời rỗng → 400 (lỗi đồng bộ, không phải 202)
// node edit/test/http-abuse.mjs [port] [serverPid]
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { execSync } from 'node:child_process';
import { ROOT } from '../env.mjs';
const PORT = Number(process.argv[2]) || 4416;
const PID = process.argv[3];
const HOST = `127.0.0.1:${PORT}`;
const EDITS = path.join(ROOT, 'edits');
const dirsNow = () => new Set(fs.existsSync(EDITS) ? fs.readdirSync(EDITS) : []);
const rssMB = () => {
if (!PID) return null;
try {
const out = execSync(`tasklist /FI "PID eq ${PID}" /FO CSV /NH`, { encoding: 'utf8' });
return Math.round(Number(out.split('","')[4].replace(/[^\d]/g, '')) / 1024);
} catch { return null; }
};
let fails = 0;
const check = (ok, what) => { console.log(`${ok ? 'PASS' : 'FAIL'} ${what}`); if (!ok) fails++; };
function request({ method = 'POST', p = '/api/edits', headers = {}, body, stream }) {
return new Promise((resolve) => {
const t = Date.now();
const req = http.request({ host: '127.0.0.1', port: PORT, method, path: p, headers: { Host: HOST, ...headers } }, (res) => {
let d = ''; res.on('data', (c) => { d += c; }); res.on('end', () => resolve({ code: res.statusCode, body: d, ms: Date.now() - t }));
});
req.on('error', (e) => resolve({ code: -1, body: String(e.message), ms: Date.now() - t }));
if (stream) stream(req); else req.end(body);
});
}
function multipart(parts, boundary = 'XyZbOuNdArY123') {
const chunks = [];
for (const p of parts) {
chunks.push(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="${p.name}"${p.filename ? `; filename="${p.filename}"` : ''}\r\n\r\n`));
chunks.push(Buffer.isBuffer(p.data) ? p.data : Buffer.from(String(p.data)));
chunks.push(Buffer.from('\r\n'));
}
chunks.push(Buffer.from(`--${boundary}--\r\n`));
return { body: Buffer.concat(chunks), type: `multipart/form-data; boundary=${boundary}` };
}
// ---- A2 ----
{
const before = rssMB();
const total = 300 * 1024 ** 2; const chunk = Buffer.alloc(1024 * 1024, 0x41);
let peak = before;
const timer = setInterval(() => { const r = rssMB(); if (r && r > peak) peak = r; }, 500);
const r = await request({
headers: { 'Content-Type': 'multipart/form-data; boundary=XyZ', 'Content-Length': String(total) },
stream: (req) => {
let sent = 0;
const pump = () => { while (sent < total) { sent += chunk.length; if (!req.write(chunk)) { req.once('drain', pump); return; } } req.end(); };
req.on('error', () => {});
pump();
},
});
clearInterval(timer);
check((r.code === 400 || r.code === -1) && r.ms < 10000, `A2 300 MB không dấu phân cách → ${r.code} sau ${r.ms} ms (${r.body.slice(0, 80)})`);
if (before) check(peak - before < 100, `A2 RAM server: ${before} → đỉnh ${peak} MB`);
}
// ---- A3 ----
const tiny = fs.readFileSync(path.join(ROOT, 'edits', '_synth', 'synth.mp4')).subarray(0, 300000); // video cụt (hỏng)
const good = path.join(ROOT, 'edits', '_synth', 'synth.mp4');
for (const [what, parts] of [
['style sai', [{ name: 'style', data: 'khong-co' }, { name: 'video', filename: 'a.mp4', data: fs.readFileSync(good) }]],
['musicStyle sai', [{ name: 'style', data: 'sach-gon' }, { name: 'musicStyle', data: 'khong-co' }, { name: 'video', filename: 'a.mp4', data: fs.readFileSync(good) }]],
['video hỏng', [{ name: 'style', data: 'sach-gon' }, { name: 'video', filename: 'a.mp4', data: tiny }]],
['đuôi file lạ', [{ name: 'style', data: 'sach-gon' }, { name: 'video', filename: 'a.exe', data: tiny }]],
]) {
const pre = dirsNow();
const m = multipart(parts);
const r = await request({ headers: { 'Content-Type': m.type, 'Content-Length': String(m.body.length) }, body: m.body });
await new Promise((res) => setTimeout(res, 800));
const added = [...dirsNow()].filter((d) => !pre.has(d));
check(r.code >= 400 && r.code < 500 && added.length === 0, `A3 ${what} → ${r.code} ${JSON.parse(r.body || '{}').error || ''}; thư mục mới còn lại: ${added.length}`);
}
// ---- A4 ----
{
const m = multipart([{ name: 'style', data: 'sach-gon' }, { name: 'video', filename: 'a.mp4', data: tiny }]);
const r = await request({ headers: { 'Content-Type': m.type, 'Transfer-Encoding': 'chunked' }, body: m.body });
check(r.code === 411, `A4 không có Content-Length → ${r.code}`);
// 2 upload cùng lúc: cái đầu gửi chậm
const m1 = multipart([{ name: 'style', data: 'sach-gon' }, { name: 'video', filename: 'a.mp4', data: tiny }]);
const slow = request({ headers: { 'Content-Type': m1.type, 'Content-Length': String(m1.body.length) }, stream: (req) => { req.write(m1.body.subarray(0, 1000)); setTimeout(() => req.end(m1.body.subarray(1000)), 2500); } });
await new Promise((res) => setTimeout(res, 500));
const r2 = await request({ headers: { 'Content-Type': m1.type, 'Content-Length': String(m1.body.length) }, body: m1.body });
const r1 = await slow;
check(r2.code === 429, `A4 upload thứ 2 trong lúc đang nhận → ${r2.code}; cái đầu (video hỏng) → ${r1.code}`);
}
// ---- revise / delete / origin ----
{
const r = await request({ p: '/api/edits/khong-co-id/revise', headers: { 'Content-Type': 'application/json' }, body: '{"instruction":"x"}' });
check(r.code === 404, `revise id lạ → ${r.code}`);
const any = fs.readdirSync(EDITS).find((d) => fs.existsSync(path.join(EDITS, d, 'state.json')) && fs.existsSync(path.join(EDITS, d, 'job.json')));
if (any) {
const r2 = await request({ p: `/api/edits/${any}/revise`, headers: { 'Content-Type': 'application/json' }, body: '{"instruction":" "}' });
check(r2.code === 400, `revise lời rỗng → ${r2.code}`);
}
const r3 = await request({ method: 'DELETE', p: '/api/edits/khong-co-id' });
check(r3.code === 404, `DELETE id lạ → ${r3.code}`);
const r4 = await request({ method: 'DELETE', p: '/api/edits/abc', headers: { Origin: 'https://evil.example' } });
check(r4.code === 403, `DELETE từ Origin lạ → ${r4.code}`);
const r5 = await request({ method: 'GET', p: '/api/edits', headers: { Host: 'evil.example:4416' } });
check(r5.code === 421, `Host lạ (DNS rebinding) → ${r5.code}`);
}
console.log(fails ? `${fails} FAIL` : 'tất cả PASS');
process.exit(fails ? 1 : 0);