Files
qelectrotech-source-mirror/tests/qttest/tst_scriptterminaluuid.cpp
T
ispyisail 9fdd6ac333 Scripting: find a terminal by its uuid
The conductor calls name a wire end as element uuid + terminal index.
The index is the terminal's place in Element::terminals(), a sort by
position that is undefined between two terminals at the same point, and
the documentation ruled terminal uuids out as "empty for most of the
installed base". Since #1118 every terminal of an opened project has one.

- elementTerminals() ends each line with the terminal's uuid
  (Terminal::stableUuid()); the text before it is unchanged.
- terminalIndex(folio, elementUuid, terminalUuid) returns the index the
  calls take, or -1 if the element or terminal is not there, or if two of
  the element's terminals carry that uuid.
- The class documentation says what does address a terminal: its uuid
  together with its element's.

tst_scriptterminaluuid runs --run on perceuse.qet (552 elements, two
terminals at one point in some): every terminal listed with a uuid,
distinct within its element, found again at its own index; -1 for an
unknown or malformed uuid, an unknown element and a bad folio. Red when
terminalIndex() returns the wrong index. qet-mcp suite 253/253.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 11:00:16 +13:00

122 lines
4.6 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
#include <QtTest>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcess>
#include <QProcessEnvironment>
#include <QRegularExpression>
#include <QTemporaryDir>
#include <QUuid>
// qet.elementTerminals() ends each line with the terminal's uuid, and
// qet.terminalIndex(folio, element, terminalUuid) turns it back into the
// index the conductor calls take. Runs a script through the real binary's
// --run on perceuse.qet, which has symbols with two terminals at one point:
// their order in the index is undefined, their uuids are not.
class tst_scriptterminaluuid : public QObject
{
Q_OBJECT
QTemporaryDir m_dir;
// Run @p script on @p project in a sandbox of its own and return the
// JSON object it logged.
QJsonObject run(const QString &script, const QString &project)
{
const QString path = m_dir.filePath(QStringLiteral("probe.js"));
const QString home = m_dir.filePath(QStringLiteral("home"));
QDir().mkpath(home);
QFile f(path);
if (!f.open(QIODevice::WriteOnly)) return {};
f.write(script.toUtf8());
f.close();
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(QStringLiteral("QT_QPA_PLATFORM"), QStringLiteral("offscreen"));
env.insert(QStringLiteral("QET_ENABLE_SCRIPTING"), QStringLiteral("1"));
env.insert(QStringLiteral("HOME"), home);
env.insert(QStringLiteral("XDG_CONFIG_HOME"), home + QStringLiteral("/config"));
env.insert(QStringLiteral("XDG_DATA_HOME"), home + QStringLiteral("/data"));
env.insert(QStringLiteral("TMPDIR"), m_dir.path());
QProcess proc;
proc.setProcessEnvironment(env);
proc.start(QStringLiteral(QET_TEST_BINARY_PATH),
{QStringLiteral("--run"), path, project});
if (!proc.waitForFinished(120000)) return {};
const QString out = QString::fromUtf8(proc.readAllStandardOutput()
+ proc.readAllStandardError());
const QString mark = QStringLiteral("PROBE ");
for (const QString &line : out.split(QLatin1Char('\n'))) {
const int i = line.indexOf(mark);
if (i >= 0)
return QJsonDocument::fromJson(line.mid(i + mark.size()).toUtf8()).object();
}
return {};
}
private slots:
void initTestCase()
{
QVERIFY(m_dir.isValid());
QVERIFY(QFile::exists(QStringLiteral(QET_TEST_BINARY_PATH)));
}
void everyTerminalFoundByItsUuid()
{
const QJsonObject r = run(QStringLiteral(
"var rows = [], pick = null;\n"
"for (var f = 0; f < qet.folioCount(); f++) {\n"
" var els = qet.elementUuids(f);\n"
" for (var e = 0; e < els.length; e++) {\n"
" var lines = qet.elementTerminals(f, els[e]);\n"
" var back = lines.map(function (l) {\n"
" return qet.terminalIndex(f, els[e], l.substring(l.lastIndexOf(' ') + 1)); });\n"
" rows.push({lines: lines, back: back});\n"
" if (!pick && lines.length) pick = {f: f, e: els[e], t: lines[0].split(' ').pop()};\n"
" }\n"
"}\n"
"qet.log('PROBE ' + JSON.stringify({rows: rows,\n"
" unknown: qet.terminalIndex(pick.f, pick.e, '{00000000-0000-0000-0000-000000000001}'),\n"
" junk: qet.terminalIndex(pick.f, pick.e, 'not a uuid'),\n"
" noElement: qet.terminalIndex(pick.f, '{00000000-0000-0000-0000-000000000002}', pick.t),\n"
" badFolio: qet.terminalIndex(99, pick.e, pick.t)}));\n"),
QStringLiteral(QET_EXAMPLES_DIR "/perceuse.qet"));
QVERIFY2(!r.isEmpty(), "the script logged nothing");
const QRegularExpression line_re(QStringLiteral(
"^(\\d+): .* \\(\\d+ conductor\\(s\\)\\) (\\{[0-9a-f-]{36}\\})$"));
int terminals = 0;
for (const QJsonValue &row : r.value(QStringLiteral("rows")).toArray()) {
const QJsonArray lines = row.toObject().value(QStringLiteral("lines")).toArray();
const QJsonArray back = row.toObject().value(QStringLiteral("back")).toArray();
QCOMPARE(back.size(), lines.size());
QSet<QString> uuids;
for (int i = 0; i < lines.size(); ++i) {
const QString line = lines.at(i).toString();
const QRegularExpressionMatch m = line_re.match(line);
QVERIFY2(m.hasMatch(), qPrintable(line));
QCOMPARE(m.captured(1).toInt(), i);
QVERIFY(!QUuid(m.captured(2)).isNull());
uuids.insert(m.captured(2));
// the uuid finds this very terminal again
QCOMPARE(back.at(i).toInt(), i);
++terminals;
}
QCOMPARE(uuids.size(), lines.size());
}
QVERIFY(terminals > 200);
QCOMPARE(r.value(QStringLiteral("unknown")).toInt(), -1);
QCOMPARE(r.value(QStringLiteral("junk")).toInt(), -1);
QCOMPARE(r.value(QStringLiteral("noElement")).toInt(), -1);
QCOMPARE(r.value(QStringLiteral("badFolio")).toInt(), -1);
}
};
QTEST_APPLESS_MAIN(tst_scriptterminaluuid)
#include "tst_scriptterminaluuid.moc"