Files
LangBot/web/tests/unit/token-monitoring-error.test.mjs
T
Hyu bafdaf0033 fix(monitoring): restore SQLite token statistics (#2479)
* fix(monitoring): restore SQLite token statistics

Allow the monitored SQLite strftime bucket expression through the tenant SQL guard and preserve structured backend errors in the token dashboard. Add persistence and frontend regressions.\n\nVerified-by: independent-review

* test(runtime): accept reconcile timeout in capacity stub

Keep the PostgreSQL capacity probe aligned with the runtime handler contract and assert the bounded reconcile timeout.

---------

Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
2026-08-28 11:41:42 +08:00

54 lines
1.6 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import ts from 'typescript';
import { fileURLToPath } from 'node:url';
const currentDirectory = path.dirname(fileURLToPath(import.meta.url));
const utilsPath = path.resolve(
currentDirectory,
'../../src/app/home/monitoring/utils.ts',
);
const componentPath = path.resolve(
currentDirectory,
'../../src/app/home/monitoring/components/TokenMonitoring.tsx',
);
function loadMonitoringUtils() {
const source = fs.readFileSync(utilsPath, 'utf8');
const compiled = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS },
}).outputText;
const loadedModule = { exports: {} };
new Function('require', 'module', 'exports', compiled)(
() => {
throw new Error('Monitoring utils must not have runtime imports');
},
loadedModule,
loadedModule.exports,
);
return loadedModule.exports;
}
const { getErrorMessage } = loadMonitoringUtils();
test('token monitoring extracts messages from structured API errors', () => {
assert.equal(
getErrorMessage({
code: 500,
msg: 'SQLite aggregation failed',
data: null,
}),
'SQLite aggregation failed',
);
assert.equal(getErrorMessage(new Error('Network failed')), 'Network failed');
assert.equal(getErrorMessage('Request failed'), 'Request failed');
});
test('token monitoring uses the structured API error helper', () => {
const source = fs.readFileSync(componentPath, 'utf8');
assert.match(source, /import \{ getErrorMessage \} from '\.\.\/utils';/);
assert.match(source, /setError\(getErrorMessage\(e\)\)/);
});