Rebasely Beta: data may be purged

examples/MCP-tools Public

TypeScript · 38 lines · 1.6 KiB · at 2090b14

import assert from "node:assert/strict";import { test } from "node:test";import { countCall, dayOf, readUsage } from "../src/usage.ts";import { fakeKv } from "./fakes.ts";const oct10 = new Date("2026-10-10T18:00:00Z");const oct9 = new Date("2026-10-09T23:59:59Z");test("counts calls and errors per tool per UTC day", async () => {  const kv = fakeKv();  await countCall(kv, "convert_units", true, oct10);  await countCall(kv, "convert_units", false, oct10);  await countCall(kv, "text_stats", true, oct10);  await countCall(kv, "convert_units", true, oct9);  assert.deepEqual(await readUsage(kv, 2, oct10), [    { date: "2026-10-10", tools: { convert_units: { calls: 2, errors: 1 }, text_stats: { calls: 1, errors: 0 } } },    { date: "2026-10-09", tools: { convert_units: { calls: 1, errors: 0 } } },  ]);});test("keeps the counts in metadata so a day is one list", async () => {  const kv = fakeKv();  await countCall(kv, "time_in_zone", true, oct10);  assert.deepEqual(kv.entries.get("calls:2026-10-10:time_in_zone")?.metadata, { calls: 1, errors: 0 });});test("reads every page of a busy day and reports empty days", async () => {  const kv = fakeKv(2);  for (const tool of ["a", "b", "c", "d", "e"]) await countCall(kv, tool, true, oct10);  const [today, yesterday, before] = await readUsage(kv, 3, oct10);  assert.deepEqual(Object.keys(today.tools), ["a", "b", "c", "d", "e"]);  assert.deepEqual(yesterday, { date: "2026-10-09", tools: {} });  assert.equal(before.date, "2026-10-08");});test("days are UTC", () => {  assert.equal(dayOf(new Date("2026-10-10T23:30:00-07:00")), "2026-10-11");});