WIP on NLPCRAFT-385.
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
index 78a6899..f232a7c 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
@@ -80,11 +80,14 @@
     }
 
     object TestDesc {
+        def apply(truth: String): TestDesc =
+            new TestDesc(truth = truth, idlCtx = mkIdlContext())
+
         def apply(truth: String, token: NCToken, idlCtx: NCIdlContext): TestDesc =
-            TestDesc(truth = truth, token = Some(token), idlCtx = idlCtx)
+            new TestDesc(truth = truth, token = Some(token), idlCtx = idlCtx)
 
         def apply(truth: String, token: NCToken): TestDesc =
-            TestDesc(truth = truth, token = Some(token), idlCtx = mkIdlContext(toks = Seq(token)))
+            new TestDesc(truth = truth, token = Some(token), idlCtx = mkIdlContext(toks = Seq(token)))
     }
 
     private def t2s(t: NCToken): String = {
@@ -144,7 +147,6 @@
         map.putAll(meta.asJava)
 
         map.put("nlpcraft:nlp:origtext", txt)
-        map.put("nlpcraft:nlp:origtext", txt)
 
         new NCToken {
             override def getModel: NCModelView = MODEL
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
index a8946d2..99892b3 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
@@ -45,7 +45,7 @@
     def testErrors(): Unit = {
         def test(truth: String*): Unit =
             for (t <- truth)
-                expectError(TestDesc(truth = t, isCustom = true))
+                expectError(TestDesc(truth = t, idlCtx = mkIdlContext(), isCustom = true))
 
         test(
             "invalid",
@@ -61,26 +61,34 @@
     }
 
     @Test
-    def test(): Unit =
+    def test(): Unit = {
+        val tok123 = mkToken(txt = "123")
+        val tok456 = mkToken(txt = "456")
+        val tokAny = mkToken(txt = "any")
+
         test(
             TestDesc(
                 truth = s"$C#trueOn123",
                 isCustom = true,
-                token = Some(mkToken(txt = "123")),
+                token = Some(tok123),
+                idlCtx = mkIdlContext(toks = Seq(tok123)),
                 tokensUsed = Some(1)
             ),
             TestDesc(
                 truth = s"$C#trueOn123",
                 isCustom = true,
-                token = Some(mkToken(txt = "456")),
+                token = Some(tok456),
+                idlCtx = mkIdlContext(toks = Seq(tok456)),
                 expectedRes = false,
                 tokensUsed = Some(1)
             ),
             TestDesc(
-                // Method defined in model.
+                // Method defined in the model.
                 truth = s"#trueAlwaysCustomToken",
                 isCustom = true,
-                token = Some(mkToken(txt = "any"))
+                token = Some(tokAny),
+                idlCtx = mkIdlContext(toks = Seq(tokAny)),
             )
         )
+    }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala
index b0d5824..347f63a 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala
@@ -106,8 +106,26 @@
                 truth = "tok_end_idx() == 123",
                 token = mkToken(end = 123)
             ),
-            TestDesc(truth = "tok_this() == tok_this()"),
-            TestDesc(truth = "tok_is_first() == true"),
-            TestDesc(truth = "tok_is_last() == true")
+            TestDesc(truth = "tok_this() == tok_this()", idlCtx = mkIdlContext())
         )
+
+    @Test
+    def testTokenOrder(): Unit = {
+        val tok = mkToken(id = "a")
+
+        tok.getMetadata.put("nlpcraft:nlp:index", 0)
+
+        test(
+            TestDesc(
+                truth = "tok_is_first() == true",
+                token = tok,
+                idlCtx = mkIdlContext(toks = Seq(tok))
+            ),
+            TestDesc(
+                truth = "tok_is_last() == true",
+                token = tok,
+                idlCtx = mkIdlContext(toks = Seq(tok))
+            )
+        )
+    }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsTokensUsed.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsTokensUsed.scala
index ea0c1cb..624c34b 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsTokensUsed.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsTokensUsed.scala
@@ -24,31 +24,39 @@
   */
 class NCIdlFunctionsTokensUsed extends NCIdlFunctions {
     @Test
-    def test(): Unit =
+    def test(): Unit = {
+        val tokIdA = mkToken(id = "a")
+        val tokAb = mkToken(id = "a", parentId = "b")
         test(
             TestDesc(
                 truth = "1 == 1",
+                idlCtx = mkIdlContext(),
                 tokensUsed = Some(0)
             ),
             TestDesc(
                 truth = "tok_id() == 'a'",
-                token = Some(mkToken(id = "a")),
+                token = Some(tokIdA),
+                idlCtx = mkIdlContext(Seq(tokIdA)),
                 tokensUsed = Some(1)
             ),
             TestDesc(
                 truth = "tok_id() == 'a' && tok_id() == 'a'",
-                token = Some(mkToken(id = "a")),
+                token = Some(tokIdA),
+                idlCtx = mkIdlContext(Seq(tokIdA)),
                 tokensUsed = Some(2)
             ),
             TestDesc(
                 truth = "tok_id() == 'a' && tok_parent() == 'b'",
-                token = Some(mkToken(id = "a", parentId = "b")),
+                token = Some(tokAb),
+                idlCtx = mkIdlContext(Seq(tokAb)),
                 tokensUsed = Some(2)
             ),
             TestDesc(
                 truth = "tok_id() == 'a' && tok_id() == 'a' && tok_parent() == 'b'",
-                token = Some(mkToken(id = "a", parentId = "b")),
+                token = Some(tokAb),
+                idlCtx = mkIdlContext(Seq(tokAb)),
                 tokensUsed = Some(3)
             )
         )
+    }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModel.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/NCStmTestModel.scala
similarity index 98%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModel.scala
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/NCStmTestModel.scala
index d51ec12..7201388 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModel.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/NCStmTestModel.scala
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.models.stm
+package org.apache.nlpcraft.model.stm
 
 import org.apache.nlpcraft.model.{NCIntentMatch, _}
 
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/NCStmTestModelSpec.scala
similarity index 97%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/NCStmTestModelSpec.scala
index bdff1cf..de74383 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/NCStmTestModelSpec.scala
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.models.stm
+package org.apache.nlpcraft.model.stm
 
 import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
 import org.junit.jupiter.api.Test
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCLimitSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCLimitSpec.scala
similarity index 97%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCLimitSpec.scala
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCLimitSpec.scala
index 9e1cb8f..a10594a 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCLimitSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCLimitSpec.scala
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.models.stm.indexes
+package org.apache.nlpcraft.model.stm.indexes
 
 import org.apache.nlpcraft.model.{NCIntent, NCIntentMatch, NCResult, _}
-import org.apache.nlpcraft.models.stm.indexes.NCSpecModelAdapter.mapper
+import NCSpecModelAdapter.mapper
 import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
 import org.junit.jupiter.api.Test
 
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCRelationSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCRelationSpec.scala
similarity index 97%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCRelationSpec.scala
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCRelationSpec.scala
index 4198c77..d4f5c37 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCRelationSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCRelationSpec.scala
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.models.stm.indexes
+package org.apache.nlpcraft.model.stm.indexes
 
 import org.apache.nlpcraft.model.{NCIntent, NCIntentMatch, NCResult, _}
-import org.apache.nlpcraft.models.stm.indexes.NCSpecModelAdapter.mapper
+import NCSpecModelAdapter.mapper
 import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
 import org.junit.jupiter.api.Test
 
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCSortSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCSortSpec.scala
similarity index 98%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCSortSpec.scala
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCSortSpec.scala
index 4658df6..6b19bba 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCSortSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCSortSpec.scala
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.models.stm.indexes
+package org.apache.nlpcraft.model.stm.indexes
 
 import org.apache.nlpcraft.model.{NCIntent, NCIntentMatch, NCResult, _}
-import org.apache.nlpcraft.models.stm.indexes.NCSpecModelAdapter.mapper
+import NCSpecModelAdapter.mapper
 import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
 import org.junit.jupiter.api.Test
 
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCSpecModelAdapter.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCSpecModelAdapter.scala
similarity index 97%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCSpecModelAdapter.scala
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCSpecModelAdapter.scala
index 81e5563..c0a8ac4 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/indexes/NCSpecModelAdapter.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/stm/indexes/NCSpecModelAdapter.scala
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.models.stm.indexes
+package org.apache.nlpcraft.model.stm.indexes
 
 import com.fasterxml.jackson.databind.ObjectMapper
 import com.fasterxml.jackson.module.scala.DefaultScalaModule