This repository was archived by the owner on Jun 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemPromptLoaderTest.php
More file actions
125 lines (109 loc) · 3.58 KB
/
Copy pathSystemPromptLoaderTest.php
File metadata and controls
125 lines (109 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Drupal\Tests\dkan_ai_query\Unit\Service;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\dkan_ai_query\Service\SystemPromptLoader;
use PHPUnit\Framework\TestCase;
/**
* Tests SystemPromptLoader file loading and version handling.
*
* @group dkan_ai_query
*/
class SystemPromptLoaderTest extends TestCase {
/**
* Throwaway temp directory simulating the module path.
*
* @var string
*/
protected string $tmpDir;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->tmpDir = sys_get_temp_dir() . '/prompts_' . bin2hex(random_bytes(4));
mkdir($this->tmpDir);
mkdir($this->tmpDir . '/prompts');
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void {
foreach (glob($this->tmpDir . '/prompts/*') as $f) {
@unlink($f);
}
@rmdir($this->tmpDir . '/prompts');
@rmdir($this->tmpDir);
}
/**
* Build a loader whose ExtensionPathResolver points at a tmp dir we control.
*/
protected function makeLoader(): SystemPromptLoader {
$resolver = $this->createMock(ExtensionPathResolver::class);
$resolver->method('getPath')->willReturn($this->tmpDir);
return new SystemPromptLoader($resolver);
}
/**
* Loads a prompt from its versioned Markdown file.
*/
public function testLoadsPromptFromMarkdownFile(): void {
file_put_contents($this->tmpDir . '/prompts/query_system_prompt.v1.md', "Hello prompt v1\n");
$loader = $this->makeLoader();
$this->assertSame('Hello prompt v1', $loader->load('v1'));
}
/**
* Load() returns NULL when the prompt file is missing.
*/
public function testReturnsNullWhenFileMissing(): void {
$this->assertNull($this->makeLoader()->load('v99'));
}
/**
* Prompt content is cached after the first load.
*/
public function testCachesAfterFirstLoad(): void {
file_put_contents($this->tmpDir . '/prompts/query_system_prompt.v1.md', 'first');
$loader = $this->makeLoader();
$this->assertSame('first', $loader->load('v1'));
file_put_contents($this->tmpDir . '/prompts/query_system_prompt.v1.md', 'second');
// Cached.
$this->assertSame('first', $loader->load('v1'));
}
/**
* ActiveVersion() defaults to DEFAULT_VERSION.
*/
public function testActiveVersionDefault(): void {
$this->assertSame('v10', $this->makeLoader()->activeVersion());
}
/**
* SetOverride() changes and resets the active version.
*/
public function testActiveVersionOverride(): void {
$loader = $this->makeLoader();
$loader->setOverride('v2');
$this->assertSame('v2', $loader->activeVersion());
$loader->setOverride('1');
$this->assertSame('v1', $loader->activeVersion());
$loader->setOverride(NULL);
$this->assertSame('v10', $loader->activeVersion());
$loader->setOverride('');
$this->assertSame('v10', $loader->activeVersion());
}
/**
* Loaded prompt content is trimmed.
*/
public function testStripsTrailingWhitespace(): void {
file_put_contents($this->tmpDir . '/prompts/query_system_prompt.v1.md', " body\n\n\n");
$this->assertSame('body', $this->makeLoader()->load('v1'));
}
/**
* Versions resolve with or without the 'v' prefix.
*/
public function testVersionPrefixHandling(): void {
file_put_contents($this->tmpDir . '/prompts/query_system_prompt.v2.md', 'two');
$loader = $this->makeLoader();
// Either form should resolve.
$this->assertSame('two', $loader->load('v2'));
// New instance to avoid the per-version cache hit.
$loader2 = $this->makeLoader();
$this->assertSame('two', $loader2->load('2'));
}
}