Files
portal/tools/observer-coverage-checker.test.mjs
T
Дмитрий 3ec638cbd2 fix(observer): C5 coverage driven by hook registration, drop commit ratio (COV-1)
Bug: checkCoverage flagged anomaly when "recent commits > 0 AND episodes == 0".
Two design flaws, proven in this project:
- Wrong unit: commits = work-unit (one turn → many commits via subagent
  workflow); episodes = turn-unit. A 1023-vs-19 ratio is not anomalous, it's
  expected.
- Wrong window: the 14-day commit window predated the Stop-hook's existence
  (registered 2026-05-19). For 13 of 14 days the hook didn't exist — 889
  commits were structurally impossible to mirror as episodes.

Result: the C5 indicator was either always-red (flagging the hook's birth
as anomaly) or always-green (any episode count vs huge commit count = ok).
Either way uninformative.

Fix:
- checkCoverage(episodeCount, hookRegistered) — drops the commit param.
  Warn iff hook is registered AND 0 episodes this month → the hook is
  silently failing. If the hook isn't registered, 0 episodes is correct.
- runCoverageChecker derives hookRegistered from settings.json
  (isObserverStopRegistered helper) and passes it to checkCoverage.
  No more git execFileSync — pure fs.

Tests rewritten under the new contract: 7/7 (was 6, +1 drift-hazard guard
ensuring detail strings never mention "commit"). 15/15 coverage tests green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 11:07:58 +03:00

62 lines
2.4 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import { checkCoverage, checkRegistration } from './observer-coverage-checker.mjs';
describe('checkCoverage', () => {
// COV-1 fix: the metric is driven by Stop-hook registration, NOT by recent
// commit volume. Comparing commits-to-episodes is wrong-unit + wrong-window
// (commits ≠ turns; a 14-day window mostly predates a freshly-registered
// hook). The honest signal is: "the hook is registered, so we expect
// episodes — if there are 0 this month, the hook is silently dead".
it('flags 0 episodes when the Stop-hook is registered', () => {
const r = checkCoverage(0, true);
expect(r.ok).toBe(false);
expect(r.detail).toContain('0');
expect(r.detail).toMatch(/registered|episode/i);
});
it('is ok when episodes exist and hook is registered', () => {
expect(checkCoverage(5, true).ok).toBe(true);
});
it('is ok when the hook is NOT registered (no expectation of episodes)', () => {
// If the hook was never installed in this repo, 0 episodes is correct,
// not a defect — silence here would have been a false alarm.
expect(checkCoverage(0, false).ok).toBe(true);
});
it('detail does NOT reference a commit-count ratio (drift hazard)', () => {
// The legacy "X episodes vs Y commits" wording implied commit≈episode is
// a target — misleading because commits=work-unit, episodes=turn-unit.
expect(checkCoverage(5, true).detail).not.toMatch(/commit/i);
expect(checkCoverage(0, true).detail).not.toMatch(/commit/i);
});
});
describe('checkRegistration', () => {
const goodSettings = {
hooks: { Stop: [{ hooks: [{ type: 'command', command: 'node tools/observer-stop-hook.mjs' }] }] },
};
it('is ok when the Stop-hook is registered and post-commit exists', () => {
const r = checkRegistration(goodSettings, true);
expect(r.ok).toBe(true);
});
it('flags a missing Stop-hook registration', () => {
const r = checkRegistration({ hooks: { Stop: [] } }, true);
expect(r.ok).toBe(false);
expect(r.detail).toContain('observer-stop-hook NOT registered');
});
it('flags a missing post-commit hook', () => {
const r = checkRegistration(goodSettings, false);
expect(r.ok).toBe(false);
expect(r.detail).toContain('post-commit');
});
it('handles an empty settings object', () => {
expect(checkRegistration({}, false).ok).toBe(false);
});
});