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 aiplacement_editor\external;
18
 
19
/**
20
 * Test generate text external webservice calls.
21
 *
22
 * @package    aiplacement_editor
23
 * @copyright  2025 Matt Porritt <matt.porritt@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @covers     \aiplacement_editor\external\generate_text
26
 */
27
final class generate_text_test extends \advanced_testcase {
28
    /**
29
     * Test generate_text webservice.
30
     */
31
    public function test_execute(): void {
32
        $this->resetAfterTest();
33
        set_config('enabled', 1, 'aiplacement_editor');
34
        $this->setAdminUser();
35
 
36
        // Get system context.
37
        $context = \core\context\system::instance();
38
 
39
        // Mock the manager class call.
40
        $response = new \core_ai\aiactions\responses\response_generate_text(success: true);
41
        $response->set_response_data(
42
            [
43
                'generatedcontent' => 'This was a test',
44
                'finishreason' => 'max_-tokens', // Alphanumext.
45
            ]
46
        );
47
 
48
        $mockmanager = $this->createMock(\core_ai\manager::class);
49
        $mockmanager->method('process_action')->willReturn($response);
50
        $mockmanager->method('is_action_available')->willReturn(true);
51
        $mockmanager->method('is_action_enabled')->willReturn(true);
52
        \core\di::set(\core_ai\manager::class, function() use ($mockmanager) {
53
            return $mockmanager;
54
        });
55
 
56
        $_POST['sesskey'] = sesskey();
57
        $params = [
58
            'contextid' => $context->id,
59
            'prompttext' => 'This is a test',
60
        ];
61
 
62
        $result = \core_external\external_api::call_external_function(
63
            'aiplacement_editor_generate_text',
64
            $params,
65
        );
66
 
67
        $this->assertFalse($result['error']);
68
        $this->assertEquals('max_-tokens', $result['data']['finishreason']);
69
        $this->assertEquals('This was a test', $result['data']['generatedcontent']);
70
        $this->assertTrue($result['data']['success']);
71
    }
72
}