diff --git a/settings/qt6-app/qml/pages/PagePersonalisation.qml b/settings/qt6-app/qml/pages/PagePersonalisation.qml index aa5e84f..dc30dd4 100644 --- a/settings/qt6-app/qml/pages/PagePersonalisation.qml +++ b/settings/qt6-app/qml/pages/PagePersonalisation.qml @@ -29,11 +29,14 @@ Item { width: parent.width spacing: 12 + // Accept rate and time saved come from the engine's in-memory + // counters (metrics.json), which reset when fcitx5 restarts — + // hence "this session" and no per-day bars for those two. Repeater { model: [ - { label: "Words learned", value: CorpusStats.words.toLocaleString(), bars: [20, 30, 25, 40, 38, 50, CorpusStats.words > 0 ? 100 : 0] }, - { label: "Accept rate · 7d", value: "0%", bars: [0, 0, 0, 0, 0, 0, 0] }, - { label: "Time saved · 7d", value: "0 s", bars: [0, 0, 0, 0, 0, 0, 0] }, + { label: "Words learned", value: CorpusStats.words.toLocaleString(), bars: CorpusStats.wordsPerDay }, + { label: "Accept rate · this session", value: Math.round(Engine.acceptRate * 100) + "%", bars: [] }, + { label: "Time saved · this session", value: root.formatDuration(Engine.secondsSaved), bars: [] }, ] delegate: Rectangle { required property var modelData @@ -63,6 +66,7 @@ Item { } Item { width: 6 } XBars { + opacity: modelData.bars.length > 0 ? 1 : 0 // keep the slot so labels stay aligned values: modelData.bars width: 56; height: 24 anchors.verticalCenter: parent.verticalCenter @@ -307,6 +311,15 @@ Item { } } + // Time saved is an estimate: accepted characters at an assumed typing + // speed (EngineProbe::kCharsPerSecond). + function formatDuration(secs) { + const s = Math.round(secs) + if (s < 60) return s + " s" + if (s < 3600) return Math.round(s / 60) + " min" + return (s / 3600).toFixed(1) + " h" + } + function exportProfile() { const src = Qt.resolvedUrl("file://" + Qt.platform.os) // placeholder // In a real app: QFileDialog exposed via invokable; for now show toast @@ -325,13 +338,14 @@ Item { Rectangle { anchors.centerIn: parent - width: 320; height: 160 + width: 320; height: confirmCol.implicitHeight + 40 radius: Theme.radius.card color: Qt.rgba(0.08, 0.06, 0.13, 0.97) border.color: Theme.ink12; border.width: 1 Column { - anchors { fill: parent; margins: 20 } + id: confirmCol + anchors { left: parent.left; right: parent.right; top: parent.top; margins: 20 } spacing: 16 Text { @@ -342,7 +356,7 @@ Item { } Text { width: parent.width - text: "This removes all learned text. A backup is saved automatically." + text: "This permanently deletes everything XType learned from your typing. The engine stops using it within 5 minutes." font.family: Theme.sansFamily; font.pixelSize: 12 color: Theme.ink65; wrapMode: Text.WordWrap renderType: Text.NativeRendering @@ -355,9 +369,9 @@ Item { variant: "primary" onClicked: { resetConfirm.visible = false - // xtype-corpus wipe --yes is destructive — run and refresh stats - Qt.openUrlExternally("xtype-corpus://wipe") // no-op placeholder - CorpusStats.refresh() + const removed = CorpusStats.wipe() + StyleProfile.refresh() + wipeText.text = removed < 0 ? "Could not delete some corpus files" : "Corpus wiped" wipeToast.show() } } diff --git a/settings/qt6-app/src/corpus_stats.cpp b/settings/qt6-app/src/corpus_stats.cpp index 3b816ac..1a28a17 100644 --- a/settings/qt6-app/src/corpus_stats.cpp +++ b/settings/qt6-app/src/corpus_stats.cpp @@ -1,54 +1,158 @@ #include "corpus_stats.h" -#include -#include -#include +#include +#include +#include + +// Counts whitespace-separated tokens; byte-level so multi-MB corpora stay cheap. +static int countWords(const QByteArray &line) { + int n = 0; + bool inWord = false; + for (char c : line) { + const bool space = c == ' ' || c == '\t' || c == '\r' || c == '\n' + || c == '\v' || c == '\f'; + if (!space && !inWord) ++n; + inWord = !space; + } + return n; +} + +static QString timestampsPath(const QString &corpusPath) { + const QFileInfo fi(corpusPath); + return fi.dir().filePath(fi.completeBaseName() + "_timestamps.txt"); +} CorpusStats::CorpusStats(QObject *parent) : QObject(parent) { - _timeout.setSingleShot(true); - _timeout.setInterval(5000); - connect(&_timeout, &QTimer::timeout, this, &CorpusStats::onTimeout); - connect(&_proc, QOverload::of(&QProcess::finished), - this, &CorpusStats::onFinished); - _proc.setProcessChannelMode(QProcess::MergedChannels); + _counts.wordsPerDay = QList(kDays, 0); + _timer.setInterval(3000); + connect(&_timer, &QTimer::timeout, this, &CorpusStats::refresh); + _timer.start(); + setCorpusPath("~/.local/share/xtype/corpus.txt"); +} +QVariantList CorpusStats::wordsPerDay() const { + QVariantList out; + for (int n : _counts.wordsPerDay) out.append(n); + return out; +} + +void CorpusStats::setCorpusPath(const QString &path) { + QString p = path; + if (p.startsWith("~/")) p = QDir::homePath() + p.mid(1); + if (p == _path) return; + _path = p; + _signature.clear(); refresh(); } +QStringList CorpusStats::corpusFiles(const QString &corpusPath) { + const QFileInfo fi(corpusPath); + QStringList out; + if (fi.exists()) out << fi.absoluteFilePath(); + // Rotated siblings written by CorpusCollector::rotateIfNeeded(); skip the + // transient .tmp the engine uses for atomic pruning. + const QStringList rotated = fi.dir().entryList({fi.fileName() + ".*"}, QDir::Files, QDir::Name); + for (const QString &name : rotated) + if (!name.endsWith(".tmp")) out << fi.dir().filePath(name); + return out; +} + +QString CorpusStats::fileSignature() const { + QString sig; + QStringList inputs = corpusFiles(_path); + inputs << timestampsPath(_path); + for (const QString &f : inputs) { + const QFileInfo fi(f); + if (!fi.exists()) continue; + sig += f + ':' + QString::number(fi.size()) + ':' + + QString::number(fi.lastModified().toMSecsSinceEpoch()) + ';'; + } + return sig; +} + void CorpusStats::refresh() { - if (_running) return; - _running = true; - _proc.start("xtype-corpus", {"stats", "--json"}); - if (!_proc.waitForStarted(500)) { - _running = false; - return; // binary not found — stats stay at 0 + if (_path.isEmpty()) return; + const QDate today = QDate::currentDate(); + const QString sig = fileSignature(); + if (sig == _signature && today == _countedOn) return; + _signature = sig; + _countedOn = today; + + const CorpusCounts c = count(_path, QDateTime::currentDateTime()); + if (c.sentences != _counts.sentences || c.words != _counts.words + || c.bytes != _counts.bytes || c.wordsPerDay != _counts.wordsPerDay) { + _counts = c; + emit statsChanged(); } - _timeout.start(); } -void CorpusStats::onFinished(int code, QProcess::ExitStatus) { - _timeout.stop(); - _running = false; +CorpusCounts CorpusStats::count(const QString &corpusPath, const QDateTime &now) { + CorpusCounts c; + c.wordsPerDay = QList(kDays, 0); - if (code != 0) return; + // The engine appends one timestamp line per corpus line (same order), so + // line i of the live corpus file was harvested at timestamps[i]. Rotated + // files have no timestamps and only contribute to the totals. + QList stamps; + { + QFile tf(timestampsPath(corpusPath)); + if (tf.open(QIODevice::ReadOnly)) { + for (const QByteArray &l : tf.readAll().split('\n')) + stamps.append(l.trimmed().toLongLong()); // malformed -> 0 (undated) + } + } - const QByteArray raw = _proc.readAll(); - const QJsonObject obj = QJsonDocument::fromJson(raw).object(); - if (obj.isEmpty()) return; + const QString live = QFileInfo(corpusPath).absoluteFilePath(); + const QDate today = now.date(); - const int s = obj.value("sentences").toInt(); - const int w = obj.value("words").toInt(); - const double m = obj.value("mb").toDouble(); + for (const QString &path : corpusFiles(corpusPath)) { + QFile f(path); + if (!f.open(QIODevice::ReadOnly)) continue; + const QByteArray data = f.readAll(); + c.bytes += data.size(); - if (s != _sentences || w != _words || m != _mb) { - _sentences = s; - _words = w; - _mb = m; - emit statsChanged(); + const bool dated = (path == live); + const QList lines = data.split('\n'); + for (qsizetype i = 0; i < lines.size(); ++i) { + const int w = countWords(lines[i]); + if (w == 0) continue; + ++c.sentences; + c.words += w; + + if (!dated || i >= stamps.size() || stamps[i] <= 0) continue; + const QDate day = QDateTime::fromSecsSinceEpoch(stamps[i]).date(); + const qint64 ago = day.daysTo(today); + if (ago >= 0 && ago < kDays) + c.wordsPerDay[kDays - 1 - static_cast(ago)] += w; + } } + return c; } -void CorpusStats::onTimeout() { - _proc.kill(); - _running = false; +int CorpusStats::wipe() { + const int removed = wipeFiles(_path); + _signature.clear(); + refresh(); + return removed; +} + +int CorpusStats::wipeFiles(const QString &corpusPath) { + if (corpusPath.isEmpty()) return -1; + const QFileInfo fi(corpusPath); + // style_profile.json sits next to the corpus (XTypeEngine derives it from + // corpus_path's parent directory). + QStringList targets = corpusFiles(corpusPath); + targets << timestampsPath(corpusPath) + << fi.dir().filePath(fi.fileName() + ".tmp") // pruneOldEntries() scratch files + << timestampsPath(corpusPath) + ".tmp" + << fi.dir().filePath("style_profile.json"); + + int removed = 0; + bool failed = false; + for (const QString &t : targets) { + if (!QFileInfo::exists(t)) continue; + if (QFile::remove(t)) ++removed; + else failed = true; + } + return failed ? -1 : removed; } diff --git a/settings/qt6-app/src/corpus_stats.h b/settings/qt6-app/src/corpus_stats.h index eccb628..ed16342 100644 --- a/settings/qt6-app/src/corpus_stats.h +++ b/settings/qt6-app/src/corpus_stats.h @@ -1,36 +1,61 @@ #pragma once +#include +#include #include -#include +#include #include +#include + +struct CorpusCounts { + int sentences = 0; + int words = 0; + qint64 bytes = 0; + QList wordsPerDay; // kDays entries, oldest first; last entry is today +}; class CorpusStats : public QObject { Q_OBJECT - Q_PROPERTY(int sentences READ sentences NOTIFY statsChanged) - Q_PROPERTY(int words READ words NOTIFY statsChanged) - Q_PROPERTY(double mb READ mb NOTIFY statsChanged) + Q_PROPERTY(int sentences READ sentences NOTIFY statsChanged) + Q_PROPERTY(int words READ words NOTIFY statsChanged) + Q_PROPERTY(double mb READ mb NOTIFY statsChanged) + Q_PROPERTY(QVariantList wordsPerDay READ wordsPerDay NOTIFY statsChanged) public: + static constexpr int kDays = 7; + explicit CorpusStats(QObject *parent = nullptr); - int sentences() const { return _sentences; } - int words() const { return _words; } - double mb() const { return _mb; } + int sentences() const { return _counts.sentences; } + int words() const { return _counts.words; } + double mb() const { return _counts.bytes / (1024.0 * 1024.0); } + QVariantList wordsPerDay() const; + + // Accepts a leading "~/" (as stored in config.toml). + void setCorpusPath(const QString &path); Q_INVOKABLE void refresh(); + // Deletes everything learned from typing: the corpus, its rotated copies, + // the timestamp file and style_profile.json. Returns the number of files + // removed, or -1 if any existing file could not be removed. + Q_INVOKABLE int wipe(); + static int wipeFiles(const QString &corpusPath); + + // Reads the corpus file plus its rotated siblings (corpus.txt.1, ...) and + // the engine's per-line timestamp file. `now` anchors the 7-day window. + static CorpusCounts count(const QString &corpusPath, const QDateTime &now); + signals: void statsChanged(); -private slots: - void onFinished(int code, QProcess::ExitStatus status); - void onTimeout(); - private: - int _sentences = 0; - int _words = 0; - double _mb = 0.0; - QProcess _proc; - QTimer _timeout; - bool _running = false; + static QStringList corpusFiles(const QString &corpusPath); + QString fileSignature() const; + + QString _path; + QString _signature; // size+mtime of every input; recount only on change + QDate _countedOn; // day buckets shift at midnight even without writes + CorpusCounts _counts; + QTimer _timer; }; diff --git a/settings/qt6-app/src/engine_probe.cpp b/settings/qt6-app/src/engine_probe.cpp index 2249a44..3a0e309 100644 --- a/settings/qt6-app/src/engine_probe.cpp +++ b/settings/qt6-app/src/engine_probe.cpp @@ -3,8 +3,9 @@ #include #include #include +#include +#include #include -#include #include #include @@ -170,16 +171,30 @@ void EngineProbe::poll() { QFile mf(QDir::homePath() + "/.local/share/xtype/metrics.json"); if (mf.open(QIODevice::ReadOnly)) { - QByteArray data = mf.readAll(); + const auto m = parseMetrics(mf.readAll()); mf.close(); - QRegularExpression re(R"("latency_p50_ms"\s*:\s*(\d+))"); - auto m = re.match(QString::fromUtf8(data)); - if (m.hasMatch()) { - int p50 = m.captured(1).toInt(); - if (p50 != _latencyP50) { - _latencyP50 = p50; - emit latencyP50Changed(); - } + if (!m) return; + if (m->latencyP50 != _latencyP50) { + _latencyP50 = m->latencyP50; + emit latencyP50Changed(); + } + if (m->generated != _generated || m->accepted != _accepted + || m->charsAccepted != _charsAccepted) { + _generated = m->generated; + _accepted = m->accepted; + _charsAccepted = m->charsAccepted; + emit metricsChanged(); } } } + +std::optional EngineProbe::parseMetrics(const QByteArray &json) { + const QJsonObject obj = QJsonDocument::fromJson(json).object(); + if (obj.isEmpty()) return std::nullopt; + Metrics m; + m.latencyP50 = obj.value("latency_p50_ms").toInt(); + m.generated = obj.value("suggestions_generated").toInteger(); + m.accepted = obj.value("suggestions_accepted").toInteger(); + m.charsAccepted = obj.value("chars_accepted").toInteger(); + return m; +} diff --git a/settings/qt6-app/src/engine_probe.h b/settings/qt6-app/src/engine_probe.h index bebd3c6..5deef6a 100644 --- a/settings/qt6-app/src/engine_probe.h +++ b/settings/qt6-app/src/engine_probe.h @@ -5,6 +5,7 @@ #include #include #include +#include class EngineProbe : public QObject { Q_OBJECT @@ -13,8 +14,17 @@ class EngineProbe : public QObject { Q_PROPERTY(QString state READ state NOTIFY stateChanged) Q_PROPERTY(QString stateMessage READ stateMessage NOTIFY stateMessageChanged) Q_PROPERTY(int latencyP50 READ latencyP50 NOTIFY latencyP50Changed) + // Engine counters from metrics.json. They live in the fcitx5 process and + // reset whenever fcitx5 restarts. + Q_PROPERTY(qint64 suggestionsGenerated READ suggestionsGenerated NOTIFY metricsChanged) + Q_PROPERTY(qint64 suggestionsAccepted READ suggestionsAccepted NOTIFY metricsChanged) + Q_PROPERTY(qint64 charsAccepted READ charsAccepted NOTIFY metricsChanged) + Q_PROPERTY(double acceptRate READ acceptRate NOTIFY metricsChanged) // 0..1 + Q_PROPERTY(double secondsSaved READ secondsSaved NOTIFY metricsChanged) public: + // Typing speed assumed for the time-saved estimate: 40 WPM x 5 chars/word. + static constexpr double kCharsPerSecond = 40.0 * 5.0 / 60.0; explicit EngineProbe(QObject *parent = nullptr); int cpuPct() const { return _cpuPct; } @@ -23,12 +33,29 @@ class EngineProbe : public QObject { QString stateMessage() const { return _stateMessage; } int latencyP50() const { return _latencyP50; } + qint64 suggestionsGenerated() const { return _generated; } + qint64 suggestionsAccepted() const { return _accepted; } + qint64 charsAccepted() const { return _charsAccepted; } + double acceptRate() const { return _generated > 0 ? double(_accepted) / double(_generated) : 0.0; } + double secondsSaved() const { return double(_charsAccepted) / kCharsPerSecond; } + + struct Metrics { + int latencyP50 = 0; + qint64 generated = 0; + qint64 accepted = 0; + qint64 charsAccepted = 0; + }; + // Parses metrics.json as written by XTypeEngine::writeMetrics(). + // Returns nullopt for unreadable/partial JSON so the last good values stay. + static std::optional parseMetrics(const QByteArray &json); + signals: void cpuPctChanged(); void ramGbChanged(); void stateChanged(); void stateMessageChanged(); void latencyP50Changed(); + void metricsChanged(); private slots: void poll(); @@ -47,6 +74,9 @@ private slots: QString _state = "paused"; QString _stateMessage; int _latencyP50 = 0; + qint64 _generated = 0; + qint64 _accepted = 0; + qint64 _charsAccepted = 0; // CPU delta tracking int _lastPid = -1; diff --git a/settings/qt6-app/src/main.cpp b/settings/qt6-app/src/main.cpp index 2b969af..d08533a 100644 --- a/settings/qt6-app/src/main.cpp +++ b/settings/qt6-app/src/main.cpp @@ -70,6 +70,9 @@ int main(int argc, char** argv) { static AppsKnown appsKnown; configStore.load(); + corpusStats.setCorpusPath(configStore.corpusPath()); + QObject::connect(&configStore, &ConfigStore::corpusPathChanged, &corpusStats, + [] { corpusStats.setCorpusPath(configStore.corpusPath()); }); qmlRegisterSingletonInstance ("XType.Settings", 1, 0, "Config", &configStore); qmlRegisterSingletonInstance ("XType.Settings", 1, 0, "Engine", &engineProbe); qmlRegisterSingletonInstance ("XType.Settings", 1, 0, "Reloader", &reloader); diff --git a/settings/qt6-app/tests/CMakeLists.txt b/settings/qt6-app/tests/CMakeLists.txt index 04d1f26..c4c96da 100644 --- a/settings/qt6-app/tests/CMakeLists.txt +++ b/settings/qt6-app/tests/CMakeLists.txt @@ -2,7 +2,10 @@ find_package(Catch2 3 REQUIRED) add_executable(xtype-settings-tests test_config_store.cpp + test_stats.cpp ${CMAKE_SOURCE_DIR}/src/config_store.cpp + ${CMAKE_SOURCE_DIR}/src/corpus_stats.cpp + ${CMAKE_SOURCE_DIR}/src/engine_probe.cpp ) target_include_directories(xtype-settings-tests PRIVATE diff --git a/settings/qt6-app/tests/test_stats.cpp b/settings/qt6-app/tests/test_stats.cpp new file mode 100644 index 0000000..d979669 --- /dev/null +++ b/settings/qt6-app/tests/test_stats.cpp @@ -0,0 +1,145 @@ +#include + +#include +#include +#include + +#include "corpus_stats.h" +#include "engine_probe.h" + +static void writeFile(const QString &path, const QByteArray &data) { + QFile f(path); + REQUIRE(f.open(QIODevice::WriteOnly | QIODevice::Truncate)); + f.write(data); +} + +static QByteArray stamp(const QDateTime &t) { + return QByteArray::number(t.toSecsSinceEpoch()) + '\n'; +} + +// ── CorpusStats::count ─────────────────────────────────────────────────────── + +TEST_CASE("count: missing corpus yields zeros and 7 empty day buckets", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.sentences == 0); + CHECK(c.words == 0); + CHECK(c.bytes == 0); + CHECK(c.wordsPerDay == QList(CorpusStats::kDays, 0)); +} + +TEST_CASE("count: sentences, words and bytes; blank lines are not sentences", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QByteArray data = "hello there world\n\n \nsecond line\twith tabs\n"; + writeFile(tmp.filePath("corpus.txt"), data); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.sentences == 2); + CHECK(c.words == 7); + CHECK(c.bytes == data.size()); +} + +TEST_CASE("count: UTF-8 words are counted as whole words", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + writeFile(tmp.filePath("corpus.txt"), "não sei se a configuração está certa\n"); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.words == 7); +} + +TEST_CASE("count: words are bucketed per day from the timestamps file", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QDateTime now(QDate(2026, 10, 14), QTime(12, 0)); + + writeFile(tmp.filePath("corpus.txt"), + "one two three\n" // today + "four five\n" // today + "six\n" // yesterday + "seven eight nine ten\n" // 6 days ago (oldest bucket) + "eleven twelve\n" // 7 days ago (outside window) + "thirteen\n" // malformed timestamp + "fourteen fifteen\n"); // no timestamp line at all + writeFile(tmp.filePath("corpus_timestamps.txt"), + stamp(now) + stamp(now.addSecs(-3600)) + stamp(now.addDays(-1)) + + stamp(now.addDays(-6)) + stamp(now.addDays(-7)) + "garbage\n"); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), now); + CHECK(c.words == 15); + CHECK(c.wordsPerDay == QList{4, 0, 0, 0, 0, 1, 5}); +} + +TEST_CASE("count: rotated files add to totals but not to day buckets; .tmp is ignored", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QDateTime now = QDateTime::currentDateTime(); + + writeFile(tmp.filePath("corpus.txt"), "live line here\n"); + writeFile(tmp.filePath("corpus_timestamps.txt"), stamp(now)); + writeFile(tmp.filePath("corpus.txt.1"), "rotated one\nrotated two\n"); + writeFile(tmp.filePath("corpus.txt.tmp"), "transient prune file\n"); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), now); + CHECK(c.sentences == 3); + CHECK(c.words == 7); + CHECK(c.wordsPerDay.last() == 3); +} + +TEST_CASE("wipeFiles: removes corpus, rotations, timestamps, scratch files and profile", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + const QStringList learned = { + "corpus.txt", "corpus.txt.1", "corpus.txt.tmp", + "corpus_timestamps.txt", "corpus_timestamps.txt.tmp", "style_profile.json", + }; + for (const QString &f : learned) writeFile(tmp.filePath(f), "x\n"); + // Engine state that is not learned text must survive. + for (const QString &f : {QString("metrics.json"), QString("fcitx5.log")}) + writeFile(tmp.filePath(f), "keep\n"); + + CHECK(CorpusStats::wipeFiles(tmp.filePath("corpus.txt")) == learned.size()); + for (const QString &f : learned) + CHECK_FALSE(QFile::exists(tmp.filePath(f))); + CHECK(QFile::exists(tmp.filePath("metrics.json"))); + CHECK(QFile::exists(tmp.filePath("fcitx5.log"))); + + const auto c = CorpusStats::count(tmp.filePath("corpus.txt"), QDateTime::currentDateTime()); + CHECK(c.words == 0); +} + +TEST_CASE("wipeFiles: nothing to delete is not an error", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + CHECK(CorpusStats::wipeFiles(tmp.filePath("corpus.txt")) == 0); +} + +TEST_CASE("wipeFiles: reports failure when a file cannot be removed", "[corpus_stats]") { + QTemporaryDir tmp; REQUIRE(tmp.isValid()); + writeFile(tmp.filePath("corpus.txt"), "x\n"); + QFile dir(tmp.path()); + REQUIRE(dir.setPermissions(QFile::ReadOwner | QFile::ExeOwner)); // read-only dir + CHECK(CorpusStats::wipeFiles(tmp.filePath("corpus.txt")) == -1); + dir.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); +} + +// ── EngineProbe::parseMetrics ──────────────────────────────────────────────── + +TEST_CASE("parseMetrics: reads the counters the engine writes", "[engine_probe]") { + // Verbatim shape of XTypeEngine::writeMetrics() output. + const QByteArray json = + R"({"latency_p50_ms":110,"latency_p95_ms":292,"suggestions_generated":147,)" + R"("suggestions_accepted":14,"chars_accepted":47,"updated_at":1792011683})" "\n"; + + const auto m = EngineProbe::parseMetrics(json); + REQUIRE(m.has_value()); + CHECK(m->latencyP50 == 110); + CHECK(m->generated == 147); + CHECK(m->accepted == 14); + CHECK(m->charsAccepted == 47); +} + +TEST_CASE("parseMetrics: empty or truncated file is rejected", "[engine_probe]") { + CHECK_FALSE(EngineProbe::parseMetrics("").has_value()); + CHECK_FALSE(EngineProbe::parseMetrics(R"({"latency_p50_ms":11)").has_value()); +} + +TEST_CASE("time-saved estimate assumes 40 WPM", "[engine_probe]") { + // 200 accepted chars at 40 WPM x 5 chars/word = one minute of typing. + CHECK(200.0 / EngineProbe::kCharsPerSecond == 60.0); +}