mirror of
https://github.com/mattermost/mattermost.git
synced 2026-08-26 13:17:29 -05:00
* Switch Cypress to use shared ESLint config * Run --fix in Cypress * Manually fix remaining lint issues in Cypress * Switch Playwright to use shared ESLint config * Run --fix in Playwright * Manually fix remaining lint issues in Playwright * Install and cache web app deps during Cypress CI builds This also caches the types and client package. That isn't needed currently since it uses prepackaged versions of those, but I imagine we might change that at some point. * Run e2e-tests-check when ESLint plugin is updated * Change E2E test GHA caching to cache all of web app node_modules * Fix mismatch between cache save and restore * Try bumping cache keys * Copy step to install dependencies to server.run_cypress.sh I don't know how this must've worked before, but if this fixes the issue, it seems like neither Cypress nor Playwright actually use the cached depenendencies. * Try disabling caching entirely for Cypress tests * Try bypassing makefile? * Try also manually building dependencies in run_specs.sh I don't know why this appears to duplicate run_cypress.sh and run_playwright.sh, both of which are called run_test.sh which might not be used any more as best I can tell. * Try installing the web app dependencies in yet another place * Disable the extra steps in server.prepare.sh specifically for Cypress * Revert changes to update cache key and disable web app depenedency cache on Cypress builds
175 lines
5.5 KiB
JavaScript
175 lines
5.5 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');
|
|
const {URLSearchParams} = require('url');
|
|
|
|
const PORT = Number(process.env.PORT) || 3010;
|
|
|
|
if (process.argv[2]) {
|
|
process.title = process.argv[2];
|
|
}
|
|
|
|
// Each language lists all other languages as targets so the Mattermost autotranslation
|
|
// service considers every pair translatable when it queries GET /languages.
|
|
const LANGUAGE_CODES = ['en', 'es', 'fr', 'de'];
|
|
const LANGUAGE_NAMES = {en: 'English', es: 'Spanish', fr: 'French', de: 'German'};
|
|
|
|
const LANGUAGES = LANGUAGE_CODES.map((code) => ({
|
|
code,
|
|
name: LANGUAGE_NAMES[code],
|
|
targets: LANGUAGE_CODES.filter((c) => c !== code),
|
|
}));
|
|
|
|
// 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 setCorsHeaders(res) {
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
}
|
|
|
|
function parseBody(req) {
|
|
return new Promise((resolve, reject) => {
|
|
let body = '';
|
|
req.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
req.on('end', () => {
|
|
if (!body) {
|
|
resolve({});
|
|
return;
|
|
}
|
|
const contentType = req.headers['content-type'] || '';
|
|
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
// Parse form-encoded body (LibreTranslate accepts both JSON and form data)
|
|
try {
|
|
const params = new URLSearchParams(body);
|
|
const obj = {};
|
|
for (const [key, value] of params.entries()) {
|
|
obj[key] = value;
|
|
}
|
|
resolve(obj);
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
} else {
|
|
// Default: parse as JSON
|
|
try {
|
|
resolve(JSON.parse(body));
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
}
|
|
});
|
|
req.on('error', reject);
|
|
});
|
|
}
|
|
|
|
function sendJson(res, statusCode, data) {
|
|
setCorsHeaders(res);
|
|
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];
|
|
|
|
// Handle CORS preflight
|
|
if (method === 'OPTIONS') {
|
|
setCorsHeaders(res);
|
|
res.writeHead(204);
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
console.log(`[mock-libretranslate] ${method} ${path}`);
|
|
|
|
if (method === 'GET' && path === '/') {
|
|
sendJson(res, 200, {
|
|
message: 'LibreTranslate mock',
|
|
endpoints: ['GET /', 'POST /translate', 'POST /detect', 'GET /languages', 'POST /__control/source'],
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (method === 'POST' && path === '/__control/source') {
|
|
let body;
|
|
try {
|
|
body = await parseBody(req);
|
|
} catch {
|
|
sendJson(res, 400, {error: 'Invalid body'});
|
|
return;
|
|
}
|
|
if (typeof body.language === 'string') {
|
|
sourceLanguage = body.language;
|
|
}
|
|
console.log(`[mock-libretranslate] source language set to: ${sourceLanguage}`);
|
|
sendJson(res, 200, {ok: true, language: sourceLanguage});
|
|
return;
|
|
}
|
|
|
|
if (method === 'GET' && path === '/languages') {
|
|
sendJson(res, 200, LANGUAGES);
|
|
return;
|
|
}
|
|
|
|
if (method === 'POST' && path === '/detect') {
|
|
let body;
|
|
try {
|
|
body = await parseBody(req);
|
|
} catch {
|
|
sendJson(res, 400, {error: 'Invalid body'});
|
|
return;
|
|
}
|
|
console.log(`[mock-libretranslate] detect: q="${(body.q || '').slice(0, 40)}..." → ${sourceLanguage}`);
|
|
sendJson(res, 200, [{language: sourceLanguage, confidence: 95}]);
|
|
return;
|
|
}
|
|
|
|
if (method === 'POST' && path === '/translate') {
|
|
let body;
|
|
try {
|
|
body = await parseBody(req);
|
|
} catch {
|
|
sendJson(res, 400, {error: 'Invalid body'});
|
|
return;
|
|
}
|
|
|
|
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 : `${q} [translated to ${target}]`;
|
|
|
|
console.log(
|
|
`[mock-libretranslate] translate: source=${actualSource} target=${target} → "${translatedText.slice(0, 60)}..."`,
|
|
);
|
|
|
|
const response = {translatedText};
|
|
if (source === 'auto') {
|
|
response.detectedLanguage = {language: sourceLanguage, confidence: 90};
|
|
}
|
|
sendJson(res, 200, response);
|
|
return;
|
|
}
|
|
|
|
console.log(`[mock-libretranslate] 404: ${method} ${path}`);
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
});
|
|
|
|
server.listen(PORT, '0.0.0.0', () => {
|
|
console.log(`LibreTranslate mock listening on port ${PORT}!`);
|
|
});
|