import { describe, expect, it } from 'vitest';
import { resolveSerial, type FindingGroup } from '../src/core/resolver';

describe('resolveSerial monotonicity', () => {
  it('finding counts are non-increasing across iterations', async () => {
    let bag = ['a', 'a', 'b', 'b', 'c'];
    const outcome = await resolveSerial<string>({
      measure: () => [...bag],
      group: (errs) => {
        const m = new Map<string, FindingGroup<string>>();
        for (const e of errs) {
          if (!m.has(e)) {
            m.set(e, {
              classKey: { key: e, bugClass: e, target: `src/${e}.ts` },
              findings: [],
            });
          }
          m.get(e)!.findings.push(e);
        }
        return [...m.values()];
      },
      depthOf: () => 0,
      applyLessons: () => false,
      repairFresh: async (g) => {
        // Clear this class key entirely
        bag = bag.filter(x => x !== g.classKey.key);
      },
      scopedRemaining: async (g) => bag.filter(x => x === g.classKey.key),
      onLesson: () => {},
      escalateFeature: () => {},
      owningFeature: (t) => t,
      maxIterations: 20,
    });
    expect(outcome.errors).toEqual([]);
    for (let i = 1; i < outcome.history.length; i++) {
      expect(outcome.history[i]).toBeLessThanOrEqual(outcome.history[i - 1]);
    }
  });
});
