/**
 * Self-check: clientCache TTL + peek.
 * Run: npx --yes tsx src/lib/clientCache.check.ts
 */
import { CLIENT_TTL, clientCacheGet, clientCachePeek, clientCacheInvalidate } from './clientCache.ts';

let passed = 0;
function assert(cond: boolean, msg: string) {
  if (!cond) throw new Error(msg);
  passed += 1;
}

async function main() {
  clientCacheInvalidate();
  assert(CLIENT_TTL.publicConfig === 45_000, 'publicConfig TTL matches BE');

  let loads = 0;
  const a = await clientCacheGet('t', 100, async () => {
    loads += 1;
    return 7;
  });
  const b = await clientCacheGet('t', 100, async () => {
    loads += 1;
    return 9;
  });
  assert(a === 7 && b === 7 && loads === 1, 'dedupe hit');
  assert(clientCachePeek('t', 100) === 7, 'peek');

  await new Promise((r) => setTimeout(r, 120));
  assert(clientCachePeek('t', 100) === null, 'peek expired');

  console.log(`clientCache.check: ${passed} asserts ok`);
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});
