Skip to content

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.

4.2 KB
Raw Blame
// Dùng chung cho các test gói mẫu (packs/test/t*.mjs). Chạy: node packs/test/<file>.mjs
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
export const JOBS = path.join(ROOT, 'jobs');

let failed = 0;
export function check(ok, name, detail = '') {
  if (!ok) failed++;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${name}${detail ? '  — ' + detail : ''}`);
  return ok;
}
export function info(line) { console.log('      ' + line); }
export function done(title) {
  console.log(failed ? `\n${title}: ${failed} FAIL` : `\n${title}: tất cả PASS`);
  process.exit(failed ? 1 : 0);
}

/** Máy chủ đếm kết nối (mọi request HTTP, upgrade WebSocket, CONNECT của proxy). */
export function counterServer() {
  return new Promise((resolve) => {
    const hits = [];
    const srv = http.createServer((req, res) => { hits.push(`${req.method} ${req.url}`); res.writeHead(204); res.end(); });
    srv.on('upgrade', (req, sock) => { hits.push(`UPGRADE ${req.url}`); sock.destroy(); });
    srv.on('connect', (req, sock) => { hits.push(`CONNECT ${req.url}`); sock.destroy(); });
    srv.on('connection', () => hits.push('tcp'));
    srv.listen(0, '127.0.0.1', () => resolve({ srv, port: srv.address().port, hits, close: () => new Promise((r) => srv.close(r)) }));
  });
}

/** Cảnh tối thiểu hợp lệ theo AUTHORING (chữ + nền đổi màu theo t). body: chèn thêm vào cuối <body>. */
export function minimalScene({ head = '', body = '', duration = 8 } = {}) {
  return `<!doctype html>
<html lang="vi">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=1080, height=1920" />
<link rel="stylesheet" href="./lib/fonts/fonts.css" />
<script src="./lib/vendor/gsap.min.js"></script>
<script src="./lib/runtime/scene.js"></script>
${head}
<style>
  html, body { margin:0; width:1080px; height:1920px; overflow:hidden; background:#076B87; }
  #root { position:relative; width:1080px; height:1920px; overflow:hidden; }
  #t { position:absolute; left:0; top:860px; width:1080px; text-align:center; color:#FCE802; font-family:'Be Vietnam Pro', sans-serif; font-weight:800; font-size:120px; }
</style>
</head>
<body>
<div id="root" data-composition-id="main" data-start="0" data-duration="${duration}" data-fps="30" data-width="1080" data-height="1920">
  <div id="t">Thử khoá</div>
</div>
<script>
  VideoScene.define({
    duration: ${duration}, fps: 30, width: 1080, height: 1920,
    render(t) {
      document.getElementById('t').style.opacity = String(0.4 + 0.6 * VideoScene.prog(t, 0, ${duration}));
      document.getElementById('root').style.background = 'rgb(7,' + Math.round(80 + 20 * t) + ',135)';
    },
  });
</script>
${body}
</body>
</html>
`;
}

/** Tạo thư mục job thử trong jobs/ (đúng độ sâu jobs/<id>/ để lệnh ../../engine/preview.mjs chạy được). */
export function makeTestJob(prefix, html) {
  const id = `${prefix}-${Date.now().toString(36)}`;
  const dir = path.join(JOBS, id);
  fs.mkdirSync(path.join(dir, 'scene'), { recursive: true });
  if (html) fs.writeFileSync(path.join(dir, 'scene', 'index.html'), html);
  return { id, dir };
}

/** Đọc claude.stream.jsonl → {toolUses:[{id,name,input}], results:Map(id→{isError,text}), denials:[], final} */
export function readStream(file) {
  const toolUses = [];
  const results = new Map();
  let final = null;
  for (const line of fs.readFileSync(file, 'utf8').split('\n')) {
    let ev;
    try { ev = JSON.parse(line); } catch { continue; }
    if (ev.type === 'assistant') for (const c of ev.message?.content || []) if (c.type === 'tool_use') toolUses.push({ id: c.id, name: c.name, input: c.input });
    if (ev.type === 'user') for (const c of ev.message?.content || []) {
      if (c.type !== 'tool_result') continue;
      const text = Array.isArray(c.content) ? c.content.map((x) => x.text || '').join('\n') : String(c.content ?? '');
      results.set(c.tool_use_id, { isError: !!c.is_error, text });
    }
    if (ev.type === 'result') final = ev;
  }
  return { toolUses, results, denials: final?.permission_denials || [], final };
}