iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / src / agent-kind-view / chat-history.test.tsmain at d68d7e1

import { expect, test } from "vitest";
import { withChatHistory } from "./chat-history.ts";

/** A log with an event at every tenth offset. */
const eventsIn = (after: number, through: number) => {
  const events: { offset: number }[] = [];
  for (let offset = Math.ceil((after + 1) / 10) * 10; offset <= through; offset += 10)
    events.push({ offset });
  return events;
};
const logOf = (asked: string[]) => ({
  other: () => "kept",
  readEvents: async (after = 0, limit = 1) => {
    asked.push(`plain ${after} ${limit}`);
    const events = eventsIn(after, after + limit * 10);
    return { events, atHead: false, scannedThroughOffset: events.at(-1)?.offset ?? after };
  },
});
const mindOf = (asked: string[]) => async (after: number, limit: number, through?: number) => {
  asked.push(`chat ${after} ${through ?? `x${limit}`}`);
  const events =
    through === undefined ? eventsIn(after, after + limit * 10) : eventsIn(after, through);
  return {
    events,
    atHead: false,
    scannedThroughOffset: through ?? events.at(-1)?.offset ?? after,
    version: 3,
  };
};

test("the first page is the context's own; older pages are answered from one block, and the next block is read ahead", async () => {
  const asked: string[] = [];
  const held = withChatHistory(logOf(asked), mindOf(asked));
  expect(held.other()).toBe("kept");
  await held.readEvents(Number.MAX_SAFE_INTEGER, 1); // the head's probe
  await held.readEvents(50_000, 1000); // the first page
  await held.readEvents(50_400, 600); // on, over it
  expect(asked).toEqual(["plain 9007199254740991 1", "plain 50000 1000", "plain 50400 600"]);
  asked.length = 0;

  // the first older window: a block of 8,000 offsets that ends where the window ends
  const first = await held.readEvents(49_000, 1000);
  expect(first.events).toHaveLength(100);
  expect(first).toMatchObject({ scannedThroughOffset: 50_000, atHead: false, version: 3 });

  // the reader is in the lower half of that block: the whole block below was read ahead, once
  expect(asked).toEqual(["chat 48000 50000", "chat 40000 48000"]);

  // the next windows come from what is held: no call
  const second = await held.readEvents(47_000, 1000);
  expect(second.events).toHaveLength(300);
  const third = await held.readEvents(45_000, 250);
  expect(third.events).toHaveLength(250);
  expect(third.scannedThroughOffset).toBe(47_500);
  expect(asked).toHaveLength(2);
  // in the lower half again: the next whole block
  await held.readEvents(43_000, 1000);
  await held.readEvents(41_000, 1000);
  expect(asked).toEqual(["chat 48000 50000", "chat 40000 48000", "chat 32000 40000"]);
});

test("a mind with no such read: one refusal, then the context's own read", async () => {
  const asked: string[] = [];
  const held = withChatHistory(logOf(asked), async (after) => {
    asked.push(`chat ${after}`);
    throw new Error("no such method");
  });
  await held.readEvents(9000, 1000);
  await held.readEvents(8000, 100);
  await held.readEvents(7000, 100);
  expect(asked).toEqual(["plain 9000 1000", "chat 8000", "plain 8000 100", "plain 7000 100"]);
});

test("an answer that is not the whole block: the window is read as a page", async () => {
  const asked: string[] = [];
  const held = withChatHistory(logOf(asked), async (after, limit, through) => {
    asked.push(`chat ${after} ${through ?? `x${limit}`}`);
    const events = eventsIn(after, after + 50);
    return { events, atHead: false, scannedThroughOffset: after + 50 };
  });
  await held.readEvents(9000, 1000);
  const page = await held.readEvents(8000, 100);
  expect(asked).toEqual(["plain 9000 1000", "chat 8000 8100", "chat 8000 x100"]);
  expect(page.events).toHaveLength(5);
});