import { test } from 'node:test';
import assert from 'node:assert/strict';
import { LoginFlow } from 'matrix-js-sdk/lib/@types/auth';
import {
  getOidcCompatibilityFlag,
  getSSOFlow,
  getPasswordFlow,
  getTokenFlow,
} from './useParsedLoginFlows';

const flows = (...f: Record<string, unknown>[]): LoginFlow[] => f as unknown as LoginFlow[];

test('flow getters pick the right flow', () => {
  const f = flows({ type: 'm.login.password' }, { type: 'm.login.sso' }, { type: 'm.login.token' });
  assert.equal(getPasswordFlow(f)?.type, 'm.login.password');
  assert.equal(getSSOFlow(f)?.type, 'm.login.sso');
  assert.equal(getTokenFlow(f)?.type, 'm.login.token');
  assert.equal(getSSOFlow(flows({ type: 'm.login.cas' }))?.type, 'm.login.cas');
});

test('getOidcCompatibilityFlag detects the stable flag name', () => {
  assert.equal(
    getOidcCompatibilityFlag(flows({ type: 'm.login.sso', oauth_aware_preferred: true })),
    true,
  );
});

test('getOidcCompatibilityFlag detects the msc3824 alt name', () => {
  assert.equal(
    getOidcCompatibilityFlag(
      flows({ type: 'm.login.sso', 'org.matrix.msc3824.delegated_oidc_compatibility': true }),
    ),
    true,
  );
});

test('getOidcCompatibilityFlag is false without sso or without the flag', () => {
  assert.equal(getOidcCompatibilityFlag(flows({ type: 'm.login.password' })), false);
  assert.equal(getOidcCompatibilityFlag(flows({ type: 'm.login.sso' })), false);
  // non-true values do not count
  assert.equal(
    getOidcCompatibilityFlag(flows({ type: 'm.login.sso', oauth_aware_preferred: 'yes' })),
    false,
  );
});
