90 lines
2.9 KiB
JavaScript
90 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
require("dotenv").config();
|
|
const axios = require("axios");
|
|
|
|
const baseUrl = (process.env.CAPTION_TEST_BASE_URL || process.env.BASE_URL || "http://localhost:3000").replace(/\/+$/, "");
|
|
const ingestUrl = `${baseUrl}/live-captions/ingest`;
|
|
const intervalMs = 6000;
|
|
|
|
const samples = [
|
|
{
|
|
original: "Bienvenidos a nuestro servicio de adoracion.",
|
|
es: "Bienvenidos a nuestro servicio de adoracion.",
|
|
en: "Welcome to our worship service.",
|
|
fr: "Bienvenue a notre service de louange.",
|
|
},
|
|
{
|
|
original: "Leamos juntos en el Salmo 23.",
|
|
es: "Leamos juntos en el Salmo 23.",
|
|
en: "Let us read together in Psalm 23.",
|
|
fr: "Lisons ensemble le Psaume 23.",
|
|
},
|
|
{
|
|
original: "Dios es fiel en todo tiempo.",
|
|
es: "Dios es fiel en todo tiempo.",
|
|
en: "God is faithful at all times.",
|
|
fr: "Dieu est fidele en tout temps.",
|
|
},
|
|
{
|
|
original: "Tomemos un momento para orar.",
|
|
es: "Tomemos un momento para orar.",
|
|
en: "Let us take a moment to pray.",
|
|
fr: "Prenons un moment pour prier.",
|
|
},
|
|
];
|
|
|
|
let sampleIndex = 0;
|
|
let timer = null;
|
|
|
|
const postPayload = async (payload) => {
|
|
const kind = payload?.draft ? "draft" : "final";
|
|
|
|
try {
|
|
const response = await axios.post(ingestUrl, payload, {
|
|
headers: { "Content-Type": "application/json" },
|
|
timeout: 10000,
|
|
});
|
|
const seq = response?.data?.caption?.sequence || response?.data?.latestSequence || "?";
|
|
const text = payload?.draft || payload?.original || "";
|
|
console.log(`[live-captions:test-sender] sent ${kind} sequence=${seq} text="${text}"`);
|
|
} catch (error) {
|
|
const status = error?.response?.status;
|
|
const body = error?.response?.data;
|
|
const message = error?.message || "request failed";
|
|
console.error("[live-captions:test-sender] send failed", { status, body, message });
|
|
}
|
|
};
|
|
|
|
const sendNextSample = async () => {
|
|
const payload = samples[sampleIndex];
|
|
|
|
const draftWords = String(payload?.original || "").split(" ").filter(Boolean);
|
|
if (draftWords.length > 2) {
|
|
await postPayload({ draft: draftWords.slice(0, 2).join(" ") });
|
|
await new Promise((resolve) => setTimeout(resolve, 550));
|
|
await postPayload({ draft: draftWords.slice(0, 4).join(" ") });
|
|
await new Promise((resolve) => setTimeout(resolve, 550));
|
|
}
|
|
|
|
await postPayload(payload);
|
|
sampleIndex = (sampleIndex + 1) % samples.length;
|
|
};
|
|
|
|
const start = async () => {
|
|
console.log(`[live-captions:test-sender] posting to ${ingestUrl} every ${intervalMs / 1000}s`);
|
|
await sendNextSample();
|
|
timer = setInterval(sendNextSample, intervalMs);
|
|
};
|
|
|
|
const shutdown = () => {
|
|
if (timer) clearInterval(timer);
|
|
console.log("[live-captions:test-sender] stopped");
|
|
process.exit(0);
|
|
};
|
|
|
|
process.on("SIGINT", shutdown);
|
|
process.on("SIGTERM", shutdown);
|
|
|
|
start();
|