import assert from "node:assert/strict";
import { test } from "bun:test";

import { traceEventsAutoReadDecision, workbenchSessionDetailReadKey, workbenchSessionMessagesReadKey, workbenchTraceEventsReadKey } from "./workbench-session-messages-read-budget";

test("session messages singleflight key separates limit and force class", () => {
  const limit8 = workbenchSessionMessagesReadKey({ sessionId: "ses_key", limit: 8, force: false });
  const limit20 = workbenchSessionMessagesReadKey({ sessionId: "ses_key", limit: 20, force: false });
  const force20 = workbenchSessionMessagesReadKey({ sessionId: "ses_key", limit: 20, force: true });

  assert.notEqual(limit8, limit20);
  assert.notEqual(limit20, force20);
  assert.equal(limit20, workbenchSessionMessagesReadKey({ sessionId: "ses_key", limit: 20, force: false }));
});
test("session detail singleflight key separates force freshness class", () => {
  const fresh = workbenchSessionDetailReadKey({ sessionId: "ses_key", force: false });
  const forced = workbenchSessionDetailReadKey({ sessionId: "ses_key", force: true });

  assert.notEqual(fresh, forced);
  assert.equal(fresh, workbenchSessionDetailReadKey({ sessionId: "ses_key" }));
});
test("trace events auto read skips initial and completed equivalent ranges", () => {
  const key = workbenchTraceEventsReadKey({ traceId: "trc_range", afterProjectedSeq: 12, limit: 50 });

  assert.equal(traceEventsAutoReadDecision({ traceId: "trc_range", afterProjectedSeq: 0, limit: 50 }).read, false);
  assert.equal(traceEventsAutoReadDecision({ traceId: "trc_range", afterProjectedSeq: 12, limit: 50, cachedRange: { traceId: "trc_range", afterProjectedSeq: 12, limit: 50, nextProjectedSeq: 12, hasMore: false } }).read, false);
  assert.equal(traceEventsAutoReadDecision({ traceId: "trc_range", afterProjectedSeq: 12, limit: 50, cachedRange: { traceId: "trc_range", afterProjectedSeq: 12, limit: 50, nextProjectedSeq: 40, hasMore: true } }).read, true);
  assert.equal(key, workbenchTraceEventsReadKey({ traceId: "trc_range", afterProjectedSeq: 12, limit: 50 }));
});
