import { RUBRO_HEADER, correrConRubro, rubroContextMiddleware, rubroDelRequest } from './rubro-context';

describe('rubro-context', () => {
  it('fuera de un request no hay rubro', () => {
    expect(rubroDelRequest()).toBeNull();
  });

  it('correrConRubro deja el rubro visible dentro del callback, incluso tras un await', async () => {
    const visto = await correrConRubro('r1', async () => {
      await Promise.resolve();
      return rubroDelRequest();
    });
    expect(visto).toBe('r1');
    expect(rubroDelRequest()).toBeNull();
  });

  it('el middleware toma el header si es un UUID y lo ignora si no', () => {
    const uuid = '3f2504e0-4f89-11d3-9a0c-0305e82c3301';
    let dentro: string | null = 'sin-correr';
    rubroContextMiddleware({ headers: { [RUBRO_HEADER]: uuid } } as any, {} as any, () => {
      dentro = rubroDelRequest();
    });
    expect(dentro).toBe(uuid);

    rubroContextMiddleware({ headers: { [RUBRO_HEADER]: 'todos' } } as any, {} as any, () => {
      dentro = rubroDelRequest();
    });
    expect(dentro).toBeNull();

    rubroContextMiddleware({ headers: {} } as any, {} as any, () => {
      dentro = rubroDelRequest();
    });
    expect(dentro).toBeNull();
  });
});
