mirror of
https://github.com/affaan-m/ECC.git
synced 2026-10-26 18:35:05 +00:00
fix: reject negative execution counters
(cherry picked from commit fed9eebed5)
This commit is contained in:
1 parent
3ac640f6cc
commit
8f72869987
2 files changed
+15
-2
No files matched your search
@@ -35,8 +35,8 @@ function toNullableNumber(value, fieldName) {
|
||||
}
|
||||
|
||||
const numericValue = Number(value);
|
||||
if (!Number.isFinite(numericValue)) {
|
||||
throw new Error(`${fieldName} must be a number`);
|
||||
if (!Number.isFinite(numericValue) || numericValue < 0) {
|
||||
throw new Error(`${fieldName} must be a number greater than or equal to zero`);
|
||||
}
|
||||
|
||||
return numericValue;
|
||||
|
||||
@@ -445,6 +445,12 @@ function runTests() {
|
||||
[{ ...valid, recorded_at: 'not-a-date' }, /recorded_at must be/],
|
||||
[{ ...valid, tokens_used: 'lots' }, /tokens_used must be a number/],
|
||||
[{ ...valid, duration_ms: Number.POSITIVE_INFINITY }, /duration_ms must be a number/],
|
||||
...[-1, '-1'].flatMap(value => [
|
||||
[{ ...valid, tokens_used: value }, /tokens_used must be a number/],
|
||||
[{ ...valid, tokensUsed: value }, /tokens_used must be a number/],
|
||||
[{ ...valid, duration_ms: value }, /duration_ms must be a number/],
|
||||
[{ ...valid, durationMs: value }, /duration_ms must be a number/],
|
||||
]),
|
||||
...[42, 0, false, {}, []].flatMap(value => [
|
||||
[{ ...valid, failure_reason: value }, /failure_reason must be a string or null/],
|
||||
[{ ...valid, failureReason: value }, /failure_reason must be a string or null/],
|
||||
@@ -480,6 +486,13 @@ function runTests() {
|
||||
/failure_reason must be a string or null/
|
||||
);
|
||||
assert.ok(!fs.existsSync(invalidRunsFile), 'invalid reasons must be rejected before writing a run');
|
||||
for (const invalidFields of [{ tokens_used: -1 }, { duration_ms: -1 }]) {
|
||||
assert.throws(() => tracker.recordSkillExecution({ ...valid, ...invalidFields }, { runsFilePath: invalidRunsFile }));
|
||||
assert.ok(!fs.existsSync(invalidRunsFile), 'invalid telemetry must be rejected before writing a run');
|
||||
}
|
||||
const zeroStrings = tracker.normalizeExecutionRecord({ ...valid, tokens_used: '0', duration_ms: '0' });
|
||||
assert.strictEqual(zeroStrings.tokens_used, 0);
|
||||
assert.strictEqual(zeroStrings.duration_ms, 0);
|
||||
})) passed++; else failed++;
|
||||
|
||||
if (test('uses state-store adapters for recording and reading when available', () => {
|
||||
|
||||
Reference in new issue
Block a user