import { ShoppingFlowController } from './shopping-flow.controller';

describe('ShoppingFlowController', () => {
  const saved = { ...process.env };
  afterEach(() => {
    process.env = { ...saved };
  });
  it('does not accept unsigned requests or run shopping logic', async () => {
    process.env.WHATSAPP_APP_SECRET = 'secret';
    process.env.WHATSAPP_FLOWS_PRIVATE_KEY_PATH = '/configured/private-key.pem';
    const flows = { exchange: jest.fn() };
    const controller = new ShoppingFlowController(flows as any);
    const res: any = { status: jest.fn().mockReturnThis(), send: jest.fn() };
    await controller.exchange(
      { rawBody: Buffer.from('{}'), get: () => undefined } as any,
      res,
    );
    expect(res.status).toHaveBeenCalledWith(432);
    expect(flows.exchange).not.toHaveBeenCalled();
  });
  it('fails closed when server secrets are unconfigured', async () => {
    delete process.env.WHATSAPP_APP_SECRET;
    const res: any = { status: jest.fn().mockReturnThis(), send: jest.fn() };
    await new ShoppingFlowController({} as any).exchange({} as any, res);
    expect(res.status).toHaveBeenCalledWith(503);
  });
});
