import { ShoppingPolicyController } from './shopping-policy.controller';

describe('ShoppingPolicyController', () => {
  const cfg = {
    shoppingTermsVersion: 'v1',
    shoppingTermsText:
      'Prices and stock are checked at checkout. <script>alert(1)</script>',
    shoppingPrivacyText: 'We use order details for fulfilment.',
  };
  const prisma: any = {
    businessIntegration: {
      findFirst: jest.fn().mockResolvedValue({
        business: { name: 'Store <test>' },
        credentialsEncrypted: 'encrypted',
      }),
    },
  };
  const controller = new ShoppingPolicyController(prisma, {
    decryptJson: () => cfg,
  } as any);
  it('serves only escaped policy text without exposing integration credentials', async () => {
    const res: any = {
      setHeader: jest.fn(),
      type: jest.fn().mockReturnThis(),
      send: jest.fn(),
    };
    await controller.page('business', 'v1', 'terms', res);
    const html = res.send.mock.calls[0][0];
    expect(html).toContain('Prices and stock are checked');
    expect(html).toContain('&lt;script&gt;');
    expect(html).not.toContain('<script>');
    expect(html).not.toContain('encrypted');
    expect(res.setHeader).toHaveBeenCalledWith('Cache-Control', 'no-store');
  });
  it('does not serve missing versions or arbitrary credential keys', async () => {
    await expect(
      controller.page('business', 'v0', 'terms', {} as any),
    ).rejects.toThrow();
    await expect(
      controller.page('business', 'v1', 'accessToken', {} as any),
    ).rejects.toThrow();
  });
});
