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 core_ai\aiactions;
|
|
|
18 |
|
|
|
19 |
use core_ai\aiactions\responses\response_generate_text;
|
|
|
20 |
use core_ai\aiactions\generate_text;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Test generate_text action methods.
|
|
|
24 |
*
|
|
|
25 |
* @package core_ai
|
|
|
26 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \core_ai\aiactions\base
|
|
|
29 |
*/
|
|
|
30 |
final class generate_text_test extends \advanced_testcase {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Test configure method.
|
|
|
34 |
*/
|
|
|
35 |
public function test_configure(): void {
|
|
|
36 |
$contextid = 1;
|
|
|
37 |
$userid = 1;
|
|
|
38 |
$prompttext = 'This is a test prompt';
|
|
|
39 |
|
|
|
40 |
$action = new generate_text(
|
|
|
41 |
contextid: $contextid,
|
|
|
42 |
userid: $userid,
|
|
|
43 |
prompttext: $prompttext
|
|
|
44 |
);
|
|
|
45 |
$this->assertEquals($userid, $action->get_configuration('userid'));
|
|
|
46 |
$this->assertEquals($prompttext, $action->get_configuration('prompttext'));
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Test store method.
|
|
|
51 |
*/
|
|
|
52 |
public function test_store(): void {
|
|
|
53 |
$this->resetAfterTest();
|
|
|
54 |
global $DB;
|
|
|
55 |
|
|
|
56 |
$contextid = 1;
|
|
|
57 |
$userid = 1;
|
|
|
58 |
$prompttext = 'This is a test prompt';
|
|
|
59 |
|
|
|
60 |
$action = new generate_text(
|
|
|
61 |
contextid: $contextid,
|
|
|
62 |
userid: $userid,
|
|
|
63 |
prompttext: $prompttext
|
|
|
64 |
);
|
|
|
65 |
|
|
|
66 |
$body = [
|
|
|
67 |
'id' => 'chatcmpl-123',
|
|
|
68 |
'fingerprint' => 'fp_44709d6fcb',
|
|
|
69 |
'generatedcontent' => 'This is the generated content',
|
|
|
70 |
'finishreason' => 'stop',
|
|
|
71 |
'prompttokens' => 9,
|
|
|
72 |
'completiontokens' => 12,
|
|
|
73 |
'model' => 'gpt-4o',
|
|
|
74 |
];
|
|
|
75 |
$actionresponse = new response_generate_text(
|
|
|
76 |
success: true,
|
|
|
77 |
);
|
|
|
78 |
$actionresponse->set_response_data($body);
|
|
|
79 |
|
|
|
80 |
$storeid = $action->store($actionresponse);
|
|
|
81 |
|
|
|
82 |
// Check the stored record.
|
|
|
83 |
$record = $DB->get_record('ai_action_generate_text', ['id' => $storeid]);
|
|
|
84 |
$this->assertEquals($prompttext, $record->prompt);
|
|
|
85 |
$this->assertEquals($body['id'], $record->responseid);
|
|
|
86 |
$this->assertEquals($body['fingerprint'], $record->fingerprint);
|
|
|
87 |
$this->assertEquals($body['generatedcontent'], $record->generatedcontent);
|
|
|
88 |
$this->assertEquals($body['finishreason'], $record->finishreason);
|
|
|
89 |
$this->assertEquals($body['prompttokens'], $record->prompttokens);
|
|
|
90 |
$this->assertEquals($body['completiontokens'], $record->completiontoken);
|
|
|
91 |
}
|
|
|
92 |
}
|