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