fafc1fe7fd
Skip repeat guard pause actions when code runs already disabled; add resume script and tests.
71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
buildPausePlan,
|
|
evaluateHealth,
|
|
isGuardPaused,
|
|
readEnvValue,
|
|
} from './agent-run-guard-lib.mjs';
|
|
|
|
test('readEnvValue reads unquoted and quoted values', () => {
|
|
const raw = [
|
|
'# comment',
|
|
'MEMIND_AGENT_CODE_RUNS_ENABLED=1',
|
|
'FOO="bar"',
|
|
].join('\n');
|
|
assert.equal(readEnvValue(raw, 'MEMIND_AGENT_CODE_RUNS_ENABLED'), '1');
|
|
assert.equal(readEnvValue(raw, 'FOO'), 'bar');
|
|
assert.equal(readEnvValue(raw, 'MISSING'), null);
|
|
});
|
|
|
|
test('isGuardPaused is true only when code runs explicitly disabled', () => {
|
|
assert.equal(isGuardPaused('MEMIND_AGENT_CODE_RUNS_ENABLED=0\n'), true);
|
|
assert.equal(isGuardPaused('MEMIND_AGENT_CODE_RUNS_ENABLED=1\n'), false);
|
|
assert.equal(isGuardPaused('# no key\n'), false);
|
|
});
|
|
|
|
test('buildPausePlan skips repeat pause when env already disabled', () => {
|
|
const envRaw = 'MEMIND_AGENT_CODE_RUNS_ENABLED=0\n';
|
|
const plan = buildPausePlan({
|
|
shouldPause: true,
|
|
envRaw,
|
|
dryRun: false,
|
|
});
|
|
assert.equal(plan.action, 'skip');
|
|
assert.equal(plan.alreadyPaused, true);
|
|
assert.match(plan.actions.join(' '), /skip_pause/);
|
|
});
|
|
|
|
test('buildPausePlan applies only on first pause trigger', () => {
|
|
const plan = buildPausePlan({
|
|
shouldPause: true,
|
|
envRaw: 'MEMIND_AGENT_CODE_RUNS_ENABLED=1\n',
|
|
dryRun: false,
|
|
});
|
|
assert.equal(plan.action, 'apply');
|
|
assert.equal(plan.alreadyPaused, false);
|
|
});
|
|
|
|
test('buildPausePlan does nothing when queue is healthy', () => {
|
|
const plan = buildPausePlan({
|
|
shouldPause: false,
|
|
envRaw: 'MEMIND_AGENT_CODE_RUNS_ENABLED=0\n',
|
|
dryRun: false,
|
|
});
|
|
assert.equal(plan.action, 'none');
|
|
});
|
|
|
|
test('evaluateHealth flags stale pending and running heartbeats', () => {
|
|
const now = Date.now();
|
|
const evaluation = evaluateHealth({
|
|
failedRecentCount: 0,
|
|
oldestPendingAgeMs: 6 * 60 * 1000,
|
|
queuedOrRetryable: 1,
|
|
oldestRunningHeartbeatAgeMs: 16 * 60 * 1000,
|
|
});
|
|
assert.equal(evaluation.shouldPause, true);
|
|
assert.ok(evaluation.reasons.some((reason) => reason.includes('oldest_pending_age_ms')));
|
|
assert.ok(evaluation.reasons.some((reason) => reason.includes('oldest_running_heartbeat_age_ms')));
|
|
assert.equal(now > 0, true);
|
|
});
|