Create test of text direction (luatex)

You need to expand the \directlua before the test. E.g.

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Script=Arabic]{Amiri}

\ExplSyntaxOn
\cs_generate_variant:Nn\tl_if_eq:nnTF {xn}
\def\foo{\tl_if_eq:xnTF{\directlua{tex.print(tex.textdir)}}{TLT}{TLT}{TRT}}
\ExplSyntaxOff

\begin{document}

%

\foo ; \directlua{tex.print(tex.textdir)}

\textdir TRT
\foo ; \directlua{tex.print(tex.textdir)}

\end{document}

enter image description here


\ifx only looks at the first level expansion when comparing two control sequences and cannot compare strings.

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Script=Arabic]{Amiri}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\ifdirTLT}{mm}
 {
  \str_if_eq:eeTF { TLT } { \lua_now:n { tex.print(tex.textdir) } }
   { #1 }
   { #2 }
 }
\ExplSyntaxOff

\begin{document}

\textdir TLT

\ifdirTLT{TLT}{TRT}; \directlua{tex.print(tex.textdir)}

\textdir TRT

\ifdirTLT{TLT}{TRT}; \directlua{tex.print(tex.textdir)}

\end{document}

enter image description here