1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace aiprovider_ollama;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Test Ollama provider methods.
|
|
|
21 |
*
|
|
|
22 |
* @package aiprovider_ollama
|
|
|
23 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
* @covers \aiprovider_ollama\provider
|
|
|
26 |
*/
|
|
|
27 |
final class provider_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
/** @var \core_ai\manager */
|
|
|
30 |
private $manager;
|
|
|
31 |
|
|
|
32 |
/** @var \core_ai\provider */
|
|
|
33 |
private $provider;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Overriding setUp() function to always reset after tests.
|
|
|
37 |
*/
|
|
|
38 |
public function setUp(): void {
|
|
|
39 |
parent::setUp();
|
|
|
40 |
$this->resetAfterTest();
|
|
|
41 |
|
|
|
42 |
// Create the provider instance.
|
|
|
43 |
$this->manager = \core\di::get(\core_ai\manager::class);
|
|
|
44 |
$config = ['data' => 'goeshere'];
|
|
|
45 |
$this->provider = $this->manager->create_provider_instance(
|
|
|
46 |
classname: '\aiprovider_ollama\provider',
|
|
|
47 |
name: 'dummy',
|
|
|
48 |
config: $config,
|
|
|
49 |
);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Test get_action_list
|
|
|
54 |
*/
|
|
|
55 |
public function test_get_action_list(): void {
|
|
|
56 |
$actionlist = $this->provider->get_action_list();
|
|
|
57 |
$this->assertIsArray($actionlist);
|
|
|
58 |
$this->assertCount(3, $actionlist);
|
|
|
59 |
$this->assertContains(\core_ai\aiactions\generate_text::class, $actionlist);
|
|
|
60 |
$this->assertContains(\core_ai\aiactions\summarise_text::class, $actionlist);
|
|
|
61 |
$this->assertContains(\core_ai\aiactions\explain_text::class, $actionlist);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Test is_request_allowed.
|
|
|
66 |
*/
|
|
|
67 |
public function test_is_request_allowed(): void {
|
|
|
68 |
// Create the provider instance.
|
|
|
69 |
$config = [
|
|
|
70 |
'enableuserratelimit' => true,
|
|
|
71 |
'userratelimit' => 3,
|
|
|
72 |
'enableglobalratelimit' => true,
|
|
|
73 |
'globalratelimit' => 5,
|
|
|
74 |
];
|
|
|
75 |
|
|
|
76 |
$provider = $this->manager->create_provider_instance(
|
|
|
77 |
classname: '\aiprovider_ollama\provider',
|
|
|
78 |
name: 'dummy',
|
|
|
79 |
config: $config,
|
|
|
80 |
);
|
|
|
81 |
|
|
|
82 |
$contextid = 1;
|
|
|
83 |
$userid = 1;
|
|
|
84 |
$prompttext = 'This is a test prompt';
|
|
|
85 |
$action = new \core_ai\aiactions\generate_text(
|
|
|
86 |
contextid: $contextid,
|
|
|
87 |
userid: $userid,
|
|
|
88 |
prompttext: $prompttext,
|
|
|
89 |
);
|
|
|
90 |
|
|
|
91 |
// Make 3 requests, all should be allowed.
|
|
|
92 |
for ($i = 0; $i < 3; $i++) {
|
|
|
93 |
$this->assertTrue($provider->is_request_allowed($action));
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// The 4th request for the same user should be denied.
|
|
|
97 |
$result = $provider->is_request_allowed($action);
|
|
|
98 |
$this->assertFalse($result['success']);
|
|
|
99 |
$this->assertEquals('User rate limit exceeded', $result['errormessage']);
|
|
|
100 |
|
|
|
101 |
// Change user id to make a request for a different user, should pass (4 requests for global rate).
|
|
|
102 |
$action = new \core_ai\aiactions\generate_text(
|
|
|
103 |
contextid: $contextid,
|
|
|
104 |
userid: 2,
|
|
|
105 |
prompttext: $prompttext,
|
|
|
106 |
);
|
|
|
107 |
$this->assertTrue($provider->is_request_allowed($action));
|
|
|
108 |
|
|
|
109 |
// Make a 5th request for the global rate limit, it should be allowed.
|
|
|
110 |
$this->assertTrue($provider->is_request_allowed($action));
|
|
|
111 |
|
|
|
112 |
// The 6th request should be denied.
|
|
|
113 |
$result = $provider->is_request_allowed($action);
|
|
|
114 |
$this->assertFalse($result['success']);
|
|
|
115 |
$this->assertEquals('Global rate limit exceeded', $result['errormessage']);
|
|
|
116 |
}
|
|
|
117 |
}
|