import { describe, expect, it } from 'vitest';

import { Client, ServerSideSortingRequestControl } from '../src/index.js';

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';

const LDAP_URI = 'ldap://localhost:389';
const BASE_DN = 'dc=ldap,dc=local';
const BIND_DN = `cn=admin,${BASE_DN}`;
const BIND_PW = '1234';

describe('Controls', () => {
  describe('ServerSideSortingRequestControl', () => {
    it('should sort values', async () => {
      const client = new Client({
        url: LDAP_URI,
      });

      await client.bind(BIND_DN, BIND_PW);
      const res = await client.search(
        BASE_DN,
        {
          filter: 'uid=*',
          attributes: ['uid'],
        },
        new ServerSideSortingRequestControl({
          value: { reverseOrder: false, attributeType: 'uid', orderingRule: 'caseIgnoreOrderingMatch' },
        }),
      );
      const results = res.searchEntries.map((entry) => entry['uid']) as [string, ...string[]];

      expect(results[0]).toBe(results.sort()[0]);
    });

    it('should sort values (descending)', async () => {
      const client = new Client({
        url: LDAP_URI,
      });

      await client.bind(BIND_DN, BIND_PW);
      const res = await client.search(
        BASE_DN,
        {
          filter: 'uid=*',
          attributes: ['uid'],
        },
        new ServerSideSortingRequestControl({
          value: { reverseOrder: true, attributeType: 'uid', orderingRule: 'caseIgnoreOrderingMatch' },
        }),
      );
      const results = res.searchEntries.map((entry) => entry['uid']) as [string, ...string[]];

      expect(results[0]).toBe(results.sort().reverse()[0]);
    });
  });
});
