Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_openai;
18
 
19
/**
20
 * Test OpenAI provider methods.
21
 *
22
 * @package    aiprovider_openai
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_openai\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
        $this->provider = $this->manager->create_provider_instance(
45
            classname: '\aiprovider_openai\provider',
46
            name: 'dummy',
47
        );
48
    }
49
 
50
    /**
51
     * Test get_action_list
52
     */
53
    public function test_get_action_list(): void {
54
        $actionlist = $this->provider->get_action_list();
55
        $this->assertIsArray($actionlist);
56
        $this->assertCount(4, $actionlist);
57
        $this->assertContains(\core_ai\aiactions\generate_text::class, $actionlist);
58
        $this->assertContains(\core_ai\aiactions\generate_image::class, $actionlist);
59
        $this->assertContains(\core_ai\aiactions\summarise_text::class, $actionlist);
60
        $this->assertContains(\core_ai\aiactions\explain_text::class, $actionlist);
61
    }
62
 
63
    /**
64
     * Test generate_userid.
65
     */
66
    public function test_generate_userid(): void {
67
        $userid = $this->provider->generate_userid(1);
68
 
69
        // Assert that the generated userid is a string of proper length.
70
        $this->assertIsString($userid);
71
        $this->assertEquals(64, strlen($userid));
72
    }
73
 
74
    /**
75
     * Test is_request_allowed.
76
     */
77
    public function test_is_request_allowed(): void {
78
        // Create the provider instance.
79
        $config = [
80
            'enableuserratelimit' => true,
81
            'userratelimit' => 3,
82
            'enableglobalratelimit' => true,
83
            'globalratelimit' => 5,
84
        ];
85
        $provider = $this->manager->create_provider_instance(
86
            classname: '\aiprovider_openai\provider',
87
            name: 'dummy',
88
            config: $config,
89
        );
90
 
91
        $contextid = 1;
92
        $userid = 1;
93
        $prompttext = 'This is a test prompt';
94
        $aspectratio = 'square';
95
        $quality = 'hd';
96
        $numimages = 1;
97
        $style = 'vivid';
98
        $action = new \core_ai\aiactions\generate_image(
99
            contextid: $contextid,
100
            userid: $userid,
101
            prompttext: $prompttext,
102
            quality: $quality,
103
            aspectratio: $aspectratio,
104
            numimages: $numimages,
105
            style: $style,
106
        );
107
 
108
        // Make 3 requests, all should be allowed.
109
        for ($i = 0; $i < 3; $i++) {
110
            $this->assertTrue($provider->is_request_allowed($action));
111
        }
112
 
113
        // The 4th request for the same user should be denied.
114
        $result = $provider->is_request_allowed($action);
115
        $this->assertFalse($result['success']);
116
        $this->assertEquals('User rate limit exceeded', $result['errormessage']);
117
 
118
        // Change user id to make a request for a different user, should pass (4 requests for global rate).
119
        $action = new \core_ai\aiactions\generate_image(
120
            contextid: $contextid,
121
            userid: 2,
122
            prompttext: $prompttext,
123
            quality: $quality,
124
            aspectratio: $aspectratio,
125
            numimages: $numimages,
126
            style: $style,
127
        );
128
        $this->assertTrue($provider->is_request_allowed($action));
129
 
130
        // Make a 5th request for the global rate limit, it should be allowed.
131
        $this->assertTrue($provider->is_request_allowed($action));
132
 
133
        // The 6th request should be denied.
134
        $result = $provider->is_request_allowed($action);
135
        $this->assertFalse($result['success']);
136
        $this->assertEquals('Global rate limit exceeded', $result['errormessage']);
137
    }
138
 
139
    /**
140
     * Test is_provider_configured.
141
     */
142
    public function test_is_provider_configured(): void {
143
 
144
        // No configured values.
145
        $this->assertFalse($this->provider->is_provider_configured());
146
 
147
        // Properly configured values.
148
        $updatedprovider = $this->manager->update_provider_instance(
149
            provider: $this->provider,
150
            config: ['apikey' => '123'],
151
        );
152
        $this->assertTrue($updatedprovider->is_provider_configured());
153
    }
154
}