mattermost/e2e-tests/playwright/mock_libre_translate.js
yasser khan f6a3c7d20c
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Blocked by required conditions
Web App CI / check-types (push) Blocked by required conditions
Web App CI / test (platform) (push) Blocked by required conditions
Web App CI / test (mattermost-redux) (push) Blocked by required conditions
Web App CI / test (channels shard 1/4) (push) Blocked by required conditions
Web App CI / test (channels shard 2/4) (push) Blocked by required conditions
Web App CI / test (channels shard 3/4) (push) Blocked by required conditions
Web App CI / test (channels shard 4/4) (push) Blocked by required conditions
Web App CI / upload-coverage (push) Blocked by required conditions
Web App CI / build (push) Blocked by required conditions
fix: Stabilize autotranslation E2E tests for real LibreTranslate service (#35443)
2026-03-11 04:12:31 +00:00

112 lines
3.4 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable no-console */
const {createServer} = require('http'); // eslint-disable-line @typescript-eslint/no-require-imports
const PORT = Number(process.env.PORT) || 3010;
if (process.argv[2]) {
process.title = process.argv[2];
}
const LANGUAGES = [
{code: 'en', name: 'English'},
{code: 'es', name: 'Spanish'},
{code: 'fr', name: 'French'},
{code: 'de', name: 'German'},
];
// Source language to return from /translate and /detect when source=auto. Set via POST /__control/source.
// Applies to all messages until changed. Default 'es'. Both /detect and /translate use this value.
let sourceLanguage = 'es';
function parseJsonBody(req) {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
resolve(body ? JSON.parse(body) : {});
} catch (e) {
reject(e);
}
});
req.on('error', reject);
});
}
function sendJson(res, statusCode, data) {
res.setHeader('Content-Type', 'application/json');
res.writeHead(statusCode);
res.end(JSON.stringify(data));
}
const server = createServer(async (req, res) => {
const method = req.method;
const path = req.url.split('?')[0];
if (method === 'GET' && path === '/') {
return sendJson(res, 200, {
message: 'LibreTranslate mock',
endpoints: ['GET /', 'POST /translate', 'POST /detect', 'GET /languages', 'POST /__control/source'],
});
}
if (method === 'POST' && path === '/__control/source') {
let body;
try {
body = await parseJsonBody(req);
} catch {
return sendJson(res, 400, {error: 'Invalid JSON'});
}
if (typeof body.language === 'string') {
sourceLanguage = body.language;
}
return sendJson(res, 200, {ok: true, language: sourceLanguage});
}
if (method === 'GET' && path === '/languages') {
return sendJson(res, 200, LANGUAGES);
}
if (method === 'POST' && path === '/detect') {
sendJson(res, 200, [{language: sourceLanguage, confidence: 95}]);
return;
}
if (method === 'POST' && path === '/translate') {
let body;
try {
body = await parseJsonBody(req);
} catch {
return sendJson(res, 400, {error: 'Invalid JSON'});
}
const q = body.q || '';
const source = body.source || 'auto';
const target = body.target || 'en';
// Determine the actual source language for comparison
const actualSource = source === 'auto' ? sourceLanguage : source;
// Only "translate" if source differs from target (matches real LibreTranslate behavior)
const translatedText = actualSource !== target ? `${q} [translated to ${target}]` : q;
const response = {translatedText};
if (source === 'auto') {
response.detectedLanguage = {language: sourceLanguage, confidence: 90};
}
sendJson(res, 200, response);
return;
}
res.writeHead(404);
res.end('Not found');
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`LibreTranslate mock listening on port ${PORT}!`);
});