### ----------------------------------------------------
### -- LLM::RetrievalAugmentedGeneration
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/01-ingest-vector-database.rakutest
### ----------------------------------------------------
use v6.d;
use Test;

#use lib <. lib>;
use LLM::RetrievalAugmentedGeneration;
use LLM::RetrievalAugmentedGeneration::VectorDatabase;
use Math::DistanceFunctions::Native;
use NativeCall;

my $fileName = 'SemSe-49a66108-8357-4614-82df-dbc6abb51df2.json';
my $file = LLM::RetrievalAugmentedGeneration::resources{$fileName}.IO;
my $vdbObj = LLM::RetrievalAugmentedGeneration::VectorDatabase.new();

## 1
is $file.f, True, "Vector database file exists";

## 2
ok $vdbObj.import($file), "Importing with a method is ok";

## 3
isa-ok
        create-vector-database(:$file, id => 'MY_NEW_VDB'),
        LLM::RetrievalAugmentedGeneration::VectorDatabase,
        "Importing with top-level function is ok";

## 4
isa-ok
        $vdbObj.Hash,
        Map:D,
        "Method Hash() works";

## 5
my @expectedAttributeNames = <name id vectors items item-count document-count version location>;
is
        ($vdbObj.Hash.keys (&) @expectedAttributeNames).elems,
        @expectedAttributeNames.elems,
        "Expected attributes";

## 6
is
        $vdbObj.vectors.elems && ($vdbObj.vectors.values.all ~~ CArray:D),
        True,
        "All vectors are CArray's by default";

## 7
my $vec5 = $vdbObj.vectors.head.value;
ok
        $vdbObj.nearest($vec5, 2, prop => <distance label>),
        "Method nearest() works";

## 8
my $vec6 = $vdbObj.vectors.head.value.Array;
ok
        $vdbObj.nearest($vec6, 2, prop => <distance label>),
        "Method nearest() works with Array argument";

## 9
my $res6 = $vdbObj.nearest($vec5, 2, prop => <distance label>);
is
        ($res6 ~~ Positional:D) && ($res6.all ~~ Positional:D),
        True,
        "Expected nearest data shape";

## 10
nok
        $vdbObj.nearest($vec6, 2, distance-function => { sum($^a <<*>> $^b) }),
        "Expected failure for non-CArray distance function";

done-testing;


