Skip to content

Commit

Permalink
Make unit tests work with the key in config.php
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed Aug 20, 2024
1 parent 91e2d74 commit 9a80c93
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ jobs:
ini-values: max_input_vars=5000
# none to use phpdbg fallback. Specify pcov (Moodle 3.10 and up) or xdebug to use them instead.
coverage: none
- name: Install NVM and Node
run: |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 20
nvm use 20
- name: Deploy moodle-plugin-ci
run: |
Expand Down
2 changes: 1 addition & 1 deletion classes/ai/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private function make_request(array $data, string $apikey, $multipart = null): a
* @return string|array The generated completion or null if the model is empty.
* @throws moodle_exception If the model is empty.
*/
public function prompt_completion($prompttext) {
public function prompt_completion($prompttext) : array {
$data = $this->get_prompt_data($prompttext);
$result = $this->make_request($data, $this->apikey);

Expand Down
33 changes: 4 additions & 29 deletions tests/test_aiconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@
*/
class test_aiconnect extends \advanced_testcase {

/**
* Where most of the functionality lives
*
* @var ai $ai
*
*/

/**
* The class with most of the functionality
* @var $ai
Expand All @@ -53,29 +46,10 @@ public function setUp(): void {
if (defined('TEST_LLM_APIKEY')) {
set_config('apikey', TEST_LLM_APIKEY, 'tool_aiconnect');
$this->ai = new ai\ai();
} else {
exit('Test will only run if TEST_LLM_APIKEY is defined in config.php');
}
}
/**
* Work around the get_prompt_data method
* being private
*
* @return void
*/
public function test_get_prompt_data(): void {
$this->assertTrue(true);
$mockai = $this->getMockBuilder(ai\ai::class)->getMock();
$getpromptdata = new \ReflectionMethod(
ai\ai::class,
'get_prompt_data'
);
$getpromptdata->setAccessible(true);

$result = $getpromptdata->invokeArgs(
$mockai,
['myprompt']
);
$this->assertStringContainsString("You: myprompt", $result['messages'][0]['content']);
}

/**
* Ask the LLM to do some maths
Expand All @@ -93,14 +67,15 @@ public function test_prompt_completion(): void {
}
/**
* Confirm that an array of models are returned.
*
* This may not work as expected with ollama
* @return void
*/
public function test_get_models(): void {
$this->resetAfterTest();
if (!$this->ai) {
$this->markTestSkipped();
}

$result = $this->ai->get_models();
$this->assertIsArray($result->models);
}
Expand Down

0 comments on commit 9a80c93

Please sign in to comment.