import { describe, expect, it } from 'vitest';
import { ManualClock, PreviewStore } from '../src/index.ts';

const pair = (n: number) => ({ plain: Buffer.from([n]), grid: Buffer.from([n, n]) });

describe('PreviewStore (спека хостинга 7.3)', () => {
  it('id — 16 байт base64url; отдаёт обе картинки', () => {
    const s = new PreviewStore({ now: () => 0 });
    const id = s.put(pair(1));
    expect(id).toMatch(/^[A-Za-z0-9_-]{22}$/);
    expect([s.get(id, 'plain'), s.get(id, 'grid')]).toEqual([Buffer.from([1]), Buffer.from([1, 1])]);
    expect(s.get('нет', 'plain')).toBeNull();
  });

  it('21-я пара вытесняет первую', () => {
    const s = new PreviewStore({ now: () => 0 });
    const ids = Array.from({ length: 21 }, (_, i) => s.put(pair(i)));
    expect(s.get(ids[0]!, 'plain')).toBeNull();
    expect(s.get(ids[1]!, 'plain')).toEqual(Buffer.from([1]));
  });

  it('через 15 минут пара забыта', () => {
    const clock = new ManualClock();
    const s = new PreviewStore({ now: () => clock.now() });
    const id = s.put(pair(1));
    clock.advance(15 * 60_000 - 1);
    expect(s.get(id, 'grid')).not.toBeNull();
    clock.advance(1);
    expect(s.get(id, 'grid')).toBeNull();
  });
});
