Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

7.5 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import {
  MAX_CHALLENGES_PER_TASK,
  CHALLENGE_OUTCOMES,
  CHALLENGE_METADATA_KEYS,
  getChallengeCount,
  canChallenge,
  buildChallengePatch,
  buildChallengeResolutionPatch,
  classifyRecheckOutcome,
} from './cosChallenge.js';
import { resolveChallengeSchema } from '../lib/cosValidation.js';

const NOW = Date.parse('2026-07-12T12:00:00.000Z');

describe('cosChallenge', () => {
  describe('getChallengeCount', () => {
    it('returns 0 for absent / non-positive / unparseable values', () => {
      expect(getChallengeCount(undefined)).toBe(0);
      expect(getChallengeCount({})).toBe(0);
      expect(getChallengeCount({ challengeCount: 0 })).toBe(0);
      expect(getChallengeCount({ challengeCount: -1 })).toBe(0);
      expect(getChallengeCount({ challengeCount: 'nope' })).toBe(0);
    });

    it('coerces the markdown-round-tripped string form ("1") to a number', () => {
      expect(getChallengeCount({ challengeCount: '1' })).toBe(1);
      expect(getChallengeCount({ challengeCount: 2 })).toBe(2);
    });
  });

  describe('canChallenge', () => {
    it('allows a first dispute and refuses once the cap is reached', () => {
      expect(canChallenge({})).toBe(true);
      expect(canChallenge({ challengeCount: MAX_CHALLENGES_PER_TASK })).toBe(false);
      // string form (post-round-trip) is refused too
      expect(canChallenge({ challengeCount: String(MAX_CHALLENGES_PER_TASK) })).toBe(false);
    });

    it('also refuses once the shared retry budget is spent when maxTotalSpawns is supplied (#2471)', () => {
      // Under the one-shot cap but out of total spawns → refused only when the
      // budget bound is opted into; omitting maxTotalSpawns keeps the old behavior.
      expect(canChallenge({ totalSpawnCount: 5 })).toBe(true);
      expect(canChallenge({ totalSpawnCount: 5 }, { maxTotalSpawns: 5 })).toBe(false);
      expect(canChallenge({ totalSpawnCount: '5' }, { maxTotalSpawns: 5 })).toBe(false);
      expect(canChallenge({ totalSpawnCount: 4 }, { maxTotalSpawns: 5 })).toBe(true);
      // absent spawn count coerces to 0 → still allowed under budget
      expect(canChallenge({}, { maxTotalSpawns: 5 })).toBe(true);
      // the one-shot cap still wins even with budget remaining
      expect(canChallenge({ challengeCount: 1, totalSpawnCount: 0 }, { maxTotalSpawns: 5 })).toBe(false);
    });
  });

  describe('buildChallengePatch', () => {
    it('records the case, increments the count, and clears any prior resolution', () => {
      const patch = buildChallengePatch(
        { challengeResolution: { outcome: 'escalated' } },
        { reason: '  the reviewer misread the diff  ', evidence: '  see line 42  ', reviewer: 'ollama', now: NOW }
      );
      expect(patch.challengeCount).toBe(1);
      expect(patch.challenge.reason).toBe('the reviewer misread the diff');
      expect(patch.challenge.evidence).toBe('see line 42');
      expect(patch.challenge.reviewer).toBe('ollama');
      expect(patch.challenge.challengedAt).toBe(new Date(NOW).toISOString());
      // undefined so updateTask's undefined-strip drops the stale resolution
      expect(patch.challengeResolution).toBeUndefined();
      expect('challengeResolution' in patch).toBe(true);
    });

    it('increments from an existing count (string form)', () => {
      const patch = buildChallengePatch({ challengeCount: '1' }, { reason: 'x', now: NOW });
      expect(patch.challengeCount).toBe(2);
    });

    it('omits optional evidence/reviewer when blank', () => {
      const patch = buildChallengePatch({}, { reason: 'x', evidence: '   ', reviewer: '', now: NOW });
      expect('evidence' in patch.challenge).toBe(false);
      expect('reviewer' in patch.challenge).toBe(false);
    });
  });

  describe('buildChallengeResolutionPatch', () => {
    it('returns null for an unknown outcome', () => {
      expect(buildChallengeResolutionPatch({ outcome: 'bogus', now: NOW })).toBeNull();
      expect(buildChallengeResolutionPatch({ now: NOW })).toBeNull();
    });

    it('records outcome + timestamp and trims optional note/resolvedBy', () => {
      const patch = buildChallengeResolutionPatch({ outcome: 'upheld', note: '  agreed  ', resolvedBy: '  user  ', now: NOW });
      expect(patch.challengeResolution.outcome).toBe('upheld');
      expect(patch.challengeResolution.resolvedAt).toBe(new Date(NOW).toISOString());
      expect(patch.challengeResolution.note).toBe('agreed');
      expect(patch.challengeResolution.resolvedBy).toBe('user');
    });

    it('accepts every CHALLENGE_OUTCOMES value', () => {
      for (const outcome of CHALLENGE_OUTCOMES) {
        expect(buildChallengeResolutionPatch({ outcome, now: NOW })).not.toBeNull();
      }
    });
  });

  describe('classifyRecheckOutcome', () => {
    it('overturns (upheld) on the clean sentinel', () => {
      expect(classifyRecheckOutcome('No findings.')).toBe('upheld');
      expect(classifyRecheckOutcome('  no findings  ')).toBe('upheld');
    });

    it('escalates when a blocking section survives', () => {
      expect(classifyRecheckOutcome('## Blocking\n- foo.js:1 leaks a handle')).toBe('escalated');
      expect(classifyRecheckOutcome('Some preamble\n### Blocking\n- bar')).toBe('escalated');
    });

    it('overturns when only non-blocking (recommended/nits) findings remain', () => {
      expect(classifyRecheckOutcome('## Recommended\n- rename x\n\n## Nits\n- spacing')).toBe('upheld');
    });

    it('does NOT escalate on a bare/empty Blocking header (noisy-reviewer guard)', () => {
      expect(classifyRecheckOutcome('## Blocking\n\n## Recommended\n- x')).toBe('upheld');
      expect(classifyRecheckOutcome('## Blocking')).toBe('upheld');
    });

    it('returns null for unusable input so the caller treats it as a failed re-check', () => {
      expect(classifyRecheckOutcome('')).toBeNull();
      expect(classifyRecheckOutcome('   ')).toBeNull();
      expect(classifyRecheckOutcome(null)).toBeNull();
      expect(classifyRecheckOutcome(undefined)).toBeNull();
    });
  });

  describe('resolveChallengeSchema recheck branch (#2471)', () => {
    it('accepts a recheck object without an explicit outcome', () => {
      expect(resolveChallengeSchema.safeParse({ recheck: { backend: 'ollama', diff: 'd' } }).success).toBe(true);
    });

    it('rejects supplying both outcome and recheck', () => {
      expect(resolveChallengeSchema.safeParse({ outcome: 'upheld', recheck: { backend: 'ollama', diff: 'd' } }).success).toBe(false);
    });

    it('rejects supplying neither outcome nor recheck', () => {
      expect(resolveChallengeSchema.safeParse({ note: 'nothing' }).success).toBe(false);
    });

    it('rejects a non-local reviewer backend in recheck', () => {
      expect(resolveChallengeSchema.safeParse({ recheck: { backend: 'codex', diff: 'd' } }).success).toBe(false);
    });
  });

  describe('constants / parity', () => {
    it('caps disputes at exactly one per task', () => {
      expect(MAX_CHALLENGES_PER_TASK).toBe(1);
    });

    it('owns exactly the three challenge metadata keys', () => {
      expect([...CHALLENGE_METADATA_KEYS].sort()).toEqual(['challenge', 'challengeCount', 'challengeResolution']);
    });

    it('the route resolution enum stays in lockstep with CHALLENGE_OUTCOMES', () => {
      // Drift guard: the Zod enum in cosValidation.js is a mirror; both must match.
      for (const outcome of CHALLENGE_OUTCOMES) {
        expect(resolveChallengeSchema.safeParse({ outcome }).success).toBe(true);
      }
      // An outcome NOT in the list must be rejected by the schema.
      expect(resolveChallengeSchema.safeParse({ outcome: 'overruled' }).success).toBe(false);
    });
  });
});