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_azureai;
|
|
|
18 |
|
|
|
19 |
use core_ai\manager;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Test Azure AI provider methods.
|
|
|
23 |
*
|
|
|
24 |
* @package aiprovider_azureai
|
|
|
25 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
* @covers \aiprovider_azureai\provider
|
|
|
28 |
*/
|
|
|
29 |
final class provider_test extends \advanced_testcase {
|
|
|
30 |
|
|
|
31 |
/** @var manager $manager */
|
|
|
32 |
private manager $manager;
|
|
|
33 |
|
|
|
34 |
/** @var provider $provider */
|
|
|
35 |
private provider $provider;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Overriding setUp() function to always reset after tests.
|
|
|
39 |
*/
|
|
|
40 |
public function setUp(): void {
|
|
|
41 |
parent::setUp();
|
|
|
42 |
$this->resetAfterTest();
|
|
|
43 |
|
|
|
44 |
// Create the provider instance.
|
|
|
45 |
$this->manager = \core\di::get(\core_ai\manager::class);
|
|
|
46 |
$config = ['data' => 'goeshere'];
|
|
|
47 |
$this->provider = $this->manager->create_provider_instance(
|
|
|
48 |
classname: '\aiprovider_azureai\provider',
|
|
|
49 |
name: 'dummy',
|
|
|
50 |
config: $config,
|
|
|
51 |
);
|
|
|
52 |
}
|
|
|
53 |
/**
|
|
|
54 |
* Test get_action_list
|
|
|
55 |
*/
|
|
|
56 |
public function test_get_action_list(): void {
|
|
|
57 |
$actionlist = $this->provider->get_action_list();
|
|
|
58 |
$this->assertIsArray($actionlist);
|
|
|
59 |
$this->assertEquals(4, count($actionlist));
|
|
|
60 |
$this->assertContains(\core_ai\aiactions\generate_text::class, $actionlist);
|
|
|
61 |
$this->assertContains(\core_ai\aiactions\generate_image::class, $actionlist);
|
|
|
62 |
$this->assertContains(\core_ai\aiactions\summarise_text::class, $actionlist);
|
|
|
63 |
$this->assertContains(\core_ai\aiactions\explain_text::class, $actionlist);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Test generate_userid.
|
|
|
68 |
*/
|
|
|
69 |
public function test_generate_userid(): void {
|
|
|
70 |
$userid = $this->provider->generate_userid(1);
|
|
|
71 |
|
|
|
72 |
// Assert that the generated userid is a string of proper length.
|
|
|
73 |
$this->assertIsString($userid);
|
|
|
74 |
$this->assertEquals(64, strlen($userid));
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Test is_request_allowed.
|
|
|
79 |
*/
|
|
|
80 |
public function test_is_request_allowed(): void {
|
|
|
81 |
// Create the provider instance.
|
|
|
82 |
$config = [
|
|
|
83 |
'enableuserratelimit' => true,
|
|
|
84 |
'userratelimit' => 3,
|
|
|
85 |
'enableglobalratelimit' => true,
|
|
|
86 |
'globalratelimit' => 5,
|
|
|
87 |
];
|
|
|
88 |
|
|
|
89 |
/** @var provider $provider */
|
|
|
90 |
$provider = $this->manager->create_provider_instance(
|
|
|
91 |
classname: provider::class,
|
|
|
92 |
name: 'dummy',
|
|
|
93 |
config: $config,
|
|
|
94 |
);
|
|
|
95 |
|
|
|
96 |
$contextid = 1;
|
|
|
97 |
$userid = 1;
|
|
|
98 |
$prompttext = 'This is a test prompt';
|
|
|
99 |
$aspectratio = 'square';
|
|
|
100 |
$quality = 'hd';
|
|
|
101 |
$numimages = 1;
|
|
|
102 |
$style = 'vivid';
|
|
|
103 |
$action = new \core_ai\aiactions\generate_image(
|
|
|
104 |
contextid: $contextid,
|
|
|
105 |
userid: $userid,
|
|
|
106 |
prompttext: $prompttext,
|
|
|
107 |
quality: $quality,
|
|
|
108 |
aspectratio: $aspectratio,
|
|
|
109 |
numimages: $numimages,
|
|
|
110 |
style: $style
|
|
|
111 |
);
|
|
|
112 |
|
|
|
113 |
// Make 3 requests, all should be allowed.
|
|
|
114 |
for ($i = 0; $i < 3; $i++) {
|
|
|
115 |
$this->assertTrue($provider->is_request_allowed($action));
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// The 4th request for the same user should be denied.
|
|
|
119 |
$result = $provider->is_request_allowed($action);
|
|
|
120 |
$this->assertFalse($result['success']);
|
|
|
121 |
$this->assertEquals('User rate limit exceeded', $result['errormessage']);
|
|
|
122 |
|
|
|
123 |
// Change user id to make a request for a different user, should pass (4 requests for global rate).
|
|
|
124 |
$action = new \core_ai\aiactions\generate_image(
|
|
|
125 |
contextid: $contextid,
|
|
|
126 |
userid: 2,
|
|
|
127 |
prompttext: $prompttext,
|
|
|
128 |
quality: $quality,
|
|
|
129 |
aspectratio: $aspectratio,
|
|
|
130 |
numimages: $numimages,
|
|
|
131 |
style: $style);
|
|
|
132 |
$this->assertTrue($provider->is_request_allowed($action));
|
|
|
133 |
|
|
|
134 |
// Make a 5th request for the global rate limit, it should be allowed.
|
|
|
135 |
$this->assertTrue($provider->is_request_allowed($action));
|
|
|
136 |
|
|
|
137 |
// The 6th request should be denied.
|
|
|
138 |
$result = $provider->is_request_allowed($action);
|
|
|
139 |
$this->assertFalse($result['success']);
|
|
|
140 |
$this->assertEquals('Global rate limit exceeded', $result['errormessage']);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Test is_provider_configured.
|
|
|
145 |
*/
|
|
|
146 |
public function test_is_provider_configured(): void {
|
|
|
147 |
|
|
|
148 |
// No configured values.
|
|
|
149 |
$this->assertFalse($this->provider->is_provider_configured());
|
|
|
150 |
|
|
|
151 |
// Partially configured values.
|
|
|
152 |
$updatedprovider = $this->manager->update_provider_instance(
|
|
|
153 |
provider: $this->provider,
|
|
|
154 |
config: ['apikey' => '123'],
|
|
|
155 |
);
|
|
|
156 |
$this->assertFalse($updatedprovider->is_provider_configured());
|
|
|
157 |
|
|
|
158 |
// Properly configured values.
|
|
|
159 |
$updatedprovider = $this->manager->update_provider_instance(
|
|
|
160 |
provider: $this->provider,
|
|
|
161 |
config: [
|
|
|
162 |
'apikey' => '123',
|
|
|
163 |
'endpoint' => 'abc',
|
|
|
164 |
],
|
|
|
165 |
);
|
|
|
166 |
$this->assertTrue($updatedprovider->is_provider_configured());
|
|
|
167 |
}
|
|
|
168 |
}
|