import { test } from 'node:test';
import assert from 'node:assert/strict';
import { CryptoApi } from 'matrix-js-sdk/lib/crypto-api';
import { verifiedDevice } from './matrix-crypto';

// `verifiedDevice` only touches `api.getDeviceVerificationStatus`, so a tiny
// stub standing in for the real CryptoApi is enough to exercise the pure logic.
// Anything requiring an actual crypto backend (key import, cross-signing setup,
// etc.) is out of scope here and intentionally not covered.
const cryptoApi = (status: unknown): CryptoApi =>
  ({
    getDeviceVerificationStatus: async () => status,
  }) as unknown as CryptoApi;

test('verifiedDevice returns null when there is no verification status', async () => {
  assert.equal(await verifiedDevice(cryptoApi(null), '@a:b', 'DEV'), null);
  assert.equal(await verifiedDevice(cryptoApi(undefined), '@a:b', 'DEV'), null);
});

test('verifiedDevice surfaces crossSigningVerified when status exists', async () => {
  assert.equal(
    await verifiedDevice(cryptoApi({ crossSigningVerified: true }), '@a:b', 'DEV'),
    true,
  );
  assert.equal(
    await verifiedDevice(cryptoApi({ crossSigningVerified: false }), '@a:b', 'DEV'),
    false,
  );
});

test('verifiedDevice forwards userId and deviceId to the crypto api', async () => {
  let received: [string, string] | undefined;
  const api = {
    getDeviceVerificationStatus: async (userId: string, deviceId: string) => {
      received = [userId, deviceId];
      return { crossSigningVerified: true };
    },
  } as unknown as CryptoApi;

  await verifiedDevice(api, '@alice:example.org', 'ABCDEF');
  assert.deepEqual(received, ['@alice:example.org', 'ABCDEF']);
});
