import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
import {
  clearDiagnosticsCursorRegistry,
  deleteDiagnosticsCursorsFor,
  getDiagnosticsCursors,
  resetDiagnosticsCursorsFor,
} from './diagnostics-cursors';

// GENC-1351 §5.8/§5.12 (#9): the forward-capture cursors, and the crucial difference
// between the Clear reset (keep the event high-water) and the in-place-switch delete
// (fresh cursors, because the meta-event buffer is torn down and its indices restart).

const KEY = 'session-1';

const Suite = createLogicSuite('diagnostics-cursors');

Suite.before.each(() => {
  clearDiagnosticsCursorRegistry();
});

Suite('resetDiagnosticsCursorsFor KEEPS lastEventIndex (Clear: meta buffer keeps climbing)', () => {
  const c = getDiagnosticsCursors(KEY);
  c.lastEventIndex = 40;
  c.emittedMsgKeys.add('m');
  c.restoredMsgKeys.add('r');
  resetDiagnosticsCursorsFor(KEY);
  const after = getDiagnosticsCursors(KEY);
  assert.is(
    after.lastEventIndex,
    40,
    'event high-water kept — Clear does not reset the meta buffer',
  );
  assert.is(after.emittedMsgKeys.size, 0, 'per-conversation message cursors restarted');
  assert.is(after.restoredMsgKeys.size, 0, 'restore watermark restarted');
});

Suite(
  'deleteDiagnosticsCursorsFor RESETS lastEventIndex (switch: meta buffer restarts at 0)',
  () => {
    const c = getDiagnosticsCursors(KEY);
    c.lastEventIndex = 40;
    deleteDiagnosticsCursorsFor(KEY);
    // A later return re-creates fresh cursors on first access.
    const after = getDiagnosticsCursors(KEY);
    assert.is(
      after.lastEventIndex,
      -1,
      'fresh cursor — a returning session whose events restart at 0 is NOT skipped',
    );
  },
);

Suite.run();
