Skip to content

Commit a8dca32

Browse files
committed
TF-4378 Add unit test to cover the replace of placeholder
1 parent 28d5036 commit a8dca32

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

scribe/lib/scribe/ai/domain/model/prompt_data.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ factory PromptData.fromJson(Map<String, dynamic> json) {
2121
}
2222

2323
class Prompt {
24+
static final _inputPlaceholder = RegExp(r'\{\{\s*input\s*\}\}');
25+
static final _taskPlaceholder = RegExp(r'\{\{\s*task\s*\}\}');
26+
2427
final String name;
2528
final List<AIMessage> messages;
2629

@@ -60,8 +63,8 @@ class Prompt {
6063
}
6164

6265
String _replacePlaceholders(String content, String inputText, String? task) {
63-
var result = content.replaceAll(RegExp(r'\{\{\s*input\s*\}\}'), inputText);
64-
result = result.replaceAll(RegExp(r'\{\{\s*task\s*\}\}'), task ?? '');
66+
var result = content.replaceAll(_inputPlaceholder, inputText);
67+
result = result.replaceAll(_taskPlaceholder, task ?? '');
6568
return result;
6669
}
6770
}

scribe/test/scribe/ai/domain/model/prompt_data_test.dart

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void main() {
129129
expect(result.last.content, 'Task: test task value, Input: test input value');
130130
});
131131

132-
test('buildPrompt should not replace task placeholder when not provided', () {
132+
test('buildPrompt should replace task placeholder with empty string when not provided', () {
133133
// Arrange
134134
final messages = [
135135
const AIMessage(role: AIRole.system, content: 'System message'),
@@ -145,7 +145,52 @@ void main() {
145145
expect(result.first.role, AIRole.system);
146146
expect(result.first.content, 'System message');
147147
expect(result.last.role, AIRole.user);
148-
expect(result.last.content, 'Task: {{task}}, Input: test input value');
148+
expect(result.last.content, 'Task: , Input: test input value');
149+
});
150+
151+
test('buildPrompt should replace spaced {{ task }} placeholder (remote template variant)', () {
152+
// Arrange — remote prompts.json may use {{ task }} with spaces
153+
final messages = [
154+
const AIMessage(role: AIRole.system, content: 'System message'),
155+
const AIMessage(role: AIRole.user, content: 'Task: {{ task }}, Input: {{ input }}')
156+
];
157+
final prompt = Prompt(name: 'test-prompt', messages: messages);
158+
159+
// Act
160+
final result = prompt.buildPrompt('email body', task: 'write a follow-up');
161+
162+
// Assert
163+
expect(result.last.content, 'Task: write a follow-up, Input: email body');
164+
});
165+
166+
test('buildPrompt should replace spaced {{ task }} with empty string when task is null', () {
167+
// Arrange
168+
final messages = [
169+
const AIMessage(role: AIRole.system, content: 'System message'),
170+
const AIMessage(role: AIRole.user, content: 'Task: {{ task }}, Input: {{ input }}')
171+
];
172+
final prompt = Prompt(name: 'test-prompt', messages: messages);
173+
174+
// Act
175+
final result = prompt.buildPrompt('email body');
176+
177+
// Assert
178+
expect(result.last.content, 'Task: , Input: email body');
179+
});
180+
181+
test('buildPrompt should handle mixed spacing in placeholders', () {
182+
// Arrange — one with spaces, one without
183+
final messages = [
184+
const AIMessage(role: AIRole.system, content: 'System message'),
185+
const AIMessage(role: AIRole.user, content: '{{ task }} / {{input}}')
186+
];
187+
final prompt = Prompt(name: 'test-prompt', messages: messages);
188+
189+
// Act
190+
final result = prompt.buildPrompt('body text', task: 'my task');
191+
192+
// Assert
193+
expect(result.last.content, 'my task / body text');
149194
});
150195

151196
test('buildPrompt should handle messages without placeholders', () {

0 commit comments

Comments
 (0)