import test from "node:test";
import assert from "node:assert/strict";
import { loadConversationScroll, saveConversationScroll } from "./conversation-scroll-persistence.js";

test("stores a bounded scroll offset per conversation", () => {
  const storage = memoryStorage();

  saveConversationScroll(storage, "session-a", 428.7);

  assert.equal(loadConversationScroll(storage, "session-a"), 429);
  assert.equal(loadConversationScroll(storage, "session-b"), null);
});

test("ignores invalid conversation scroll data", () => {
  const storage = memoryStorage();
  storage.setItem("codexRemote.conversationScroll:session-a", "not-a-number");

  assert.equal(loadConversationScroll(storage, "session-a"), null);
});

function memoryStorage() {
  const values = new Map();
  return {
    getItem: (key) => values.get(key) ?? null,
    setItem: (key, value) => values.set(key, String(value))
  };
}
