@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
22
33import { processRule } from "../hubLoader.js" ;
44
5- import { processAndCombinePrompts } from "./promptProcessor.js" ;
5+ import { prependPrompt , processAndCombinePrompts } from "./promptProcessor.js" ;
66
77// Mock the args module
88vi . mock ( "../hubLoader.js" , ( ) => ( {
@@ -125,3 +125,179 @@ describe("promptProcessor", () => {
125125 } ) ;
126126 } ) ;
127127} ) ;
128+
129+ describe ( "prependPrompt" , ( ) => {
130+ describe ( "normal cases" , ( ) => {
131+ it ( "should combine two non-empty strings with double newlines" , ( ) => {
132+ const result = prependPrompt ( "Hello" , "World" ) ;
133+ expect ( result ) . toBe ( "Hello\n\nWorld" ) ;
134+ } ) ;
135+
136+ it ( "should combine multi-line strings correctly" , ( ) => {
137+ const prepend = "Line 1\nLine 2" ;
138+ const original = "Line 3\nLine 4" ;
139+ const result = prependPrompt ( prepend , original ) ;
140+ expect ( result ) . toBe ( "Line 1\nLine 2\n\nLine 3\nLine 4" ) ;
141+ } ) ;
142+
143+ it ( "should handle strings with special characters" , ( ) => {
144+ const prepend = "Special: @#$%^&*()" ;
145+ const original = "Unicode: 🚀 ∑ ∆" ;
146+ const result = prependPrompt ( prepend , original ) ;
147+ expect ( result ) . toBe ( "Special: @#$%^&*()\n\nUnicode: 🚀 ∑ ∆" ) ;
148+ } ) ;
149+ } ) ;
150+
151+ describe ( "undefined parameter cases" , ( ) => {
152+ it ( "should handle undefined prepend with defined original" , ( ) => {
153+ const result = prependPrompt ( undefined , "original" ) ;
154+ expect ( result ) . toBe ( "original" ) ;
155+ } ) ;
156+
157+ it ( "should handle defined prepend with undefined original" , ( ) => {
158+ const result = prependPrompt ( "prepend" , undefined ) ;
159+ expect ( result ) . toBe ( "prepend" ) ;
160+ } ) ;
161+
162+ it ( "should return undefined when both parameters are undefined" , ( ) => {
163+ const result = prependPrompt ( undefined , undefined ) ;
164+ expect ( result ) . toBeUndefined ( ) ;
165+ } ) ;
166+ } ) ;
167+
168+ describe ( "empty string cases" , ( ) => {
169+ it ( "should handle empty prepend with non-empty original" , ( ) => {
170+ const result = prependPrompt ( "" , "original" ) ;
171+ expect ( result ) . toBe ( "original" ) ;
172+ } ) ;
173+
174+ it ( "should handle non-empty prepend with empty original" , ( ) => {
175+ const result = prependPrompt ( "prepend" , "" ) ;
176+ expect ( result ) . toBe ( "prepend" ) ;
177+ } ) ;
178+
179+ it ( "should return undefined when both parameters are empty strings" , ( ) => {
180+ const result = prependPrompt ( "" , "" ) ;
181+ expect ( result ) . toBeUndefined ( ) ;
182+ } ) ;
183+
184+ it ( "should handle mixed undefined and empty string" , ( ) => {
185+ const result1 = prependPrompt ( undefined , "" ) ;
186+ const result2 = prependPrompt ( "" , undefined ) ;
187+ expect ( result1 ) . toBeUndefined ( ) ;
188+ expect ( result2 ) . toBeUndefined ( ) ;
189+ } ) ;
190+ } ) ;
191+
192+ describe ( "whitespace cases" , ( ) => {
193+ it ( "should trim leading and trailing whitespace" , ( ) => {
194+ const result = prependPrompt ( " hello " , " world " ) ;
195+ expect ( result ) . toBe ( "hello\n\nworld" ) ;
196+ } ) ;
197+
198+ it ( "should return undefined when result is only whitespace" , ( ) => {
199+ const result = prependPrompt ( " " , " " ) ;
200+ expect ( result ) . toBeUndefined ( ) ;
201+ } ) ;
202+
203+ it ( "should handle newlines in parameters" , ( ) => {
204+ const prepend = "\nhello\n" ;
205+ const original = "\nworld\n" ;
206+ const result = prependPrompt ( prepend , original ) ;
207+ expect ( result ) . toBe ( "hello\n\nworld" ) ;
208+ } ) ;
209+
210+ it ( "should handle mixed whitespace characters" , ( ) => {
211+ const prepend = "\t hello \t" ;
212+ const original = "\r\n world \r\n" ;
213+ const result = prependPrompt ( prepend , original ) ;
214+ expect ( result ) . toBe ( "hello\n\nworld" ) ;
215+ } ) ;
216+
217+ it ( "should preserve internal whitespace and newlines" , ( ) => {
218+ const prepend = "hello\n world" ;
219+ const original = "foo\t\tbar" ;
220+ const result = prependPrompt ( prepend , original ) ;
221+ expect ( result ) . toBe ( "hello\n world\n\nfoo\t\tbar" ) ;
222+ } ) ;
223+ } ) ;
224+
225+ describe ( "edge cases" , ( ) => {
226+ it ( "should handle very long strings" , ( ) => {
227+ const longString = "a" . repeat ( 10000 ) ;
228+ const result = prependPrompt ( longString , "short" ) ;
229+ expect ( result ) . toBe ( `${ longString } \n\nshort` ) ;
230+ expect ( result ?. length ) . toBe ( 10000 + 2 + 5 ) ; // long + \n\n + short
231+ } ) ;
232+
233+ it ( "should handle strings with only newlines" , ( ) => {
234+ const result = prependPrompt ( "\n\n\n" , "\n\n" ) ;
235+ expect ( result ) . toBeUndefined ( ) ;
236+ } ) ;
237+
238+ it ( "should handle one parameter with content and other with only whitespace" , ( ) => {
239+ const result1 = prependPrompt ( "content" , " \n " ) ;
240+ const result2 = prependPrompt ( " \t " , "content" ) ;
241+ expect ( result1 ) . toBe ( "content" ) ;
242+ expect ( result2 ) . toBe ( "content" ) ;
243+ } ) ;
244+ } ) ;
245+
246+ describe ( "boundary conditions" , ( ) => {
247+ it ( "should return undefined for whitespace-only result after trim" , ( ) => {
248+ const cases = [
249+ [ "" , "" ] ,
250+ [ " " , "" ] ,
251+ [ "" , " " ] ,
252+ [ " " , " " ] ,
253+ [ "\n" , "\n" ] ,
254+ [ "\t" , "\r" ] ,
255+ [ undefined , "" ] ,
256+ [ "" , undefined ] ,
257+ [ undefined , undefined ] ,
258+ [ " \n " , " \n " ] ,
259+ ] as const ;
260+
261+ cases . forEach ( ( [ prepend , original ] ) => {
262+ const result = prependPrompt ( prepend , original ) ;
263+ expect ( result ) . toBeUndefined ( ) ;
264+ } ) ;
265+ } ) ;
266+
267+ it ( "should return trimmed string for non-empty result" , ( ) => {
268+ const cases = [
269+ [ "a" , "" , "a" ] ,
270+ [ "" , "b" , "b" ] ,
271+ [ "a" , "b" , "a\n\nb" ] ,
272+ [ " a " , "" , "a" ] ,
273+ [ "" , " b " , "b" ] ,
274+ [ " a " , " b " , "a\n\nb" ] ,
275+ [ undefined , "content" , "content" ] ,
276+ [ "content" , undefined , "content" ] ,
277+ ] as const ;
278+
279+ cases . forEach ( ( [ prepend , original , expected ] ) => {
280+ const result = prependPrompt ( prepend , original ) ;
281+ expect ( result ) . toBe ( expected ) ;
282+ } ) ;
283+ } ) ;
284+ } ) ;
285+
286+ describe ( "return type consistency" , ( ) => {
287+ it ( "should always return string or undefined" , ( ) => {
288+ const testCases = [
289+ [ "hello" , "world" ] ,
290+ [ undefined , "world" ] ,
291+ [ "hello" , undefined ] ,
292+ [ undefined , undefined ] ,
293+ [ "" , "" ] ,
294+ [ " " , " " ] ,
295+ ] as const ;
296+
297+ testCases . forEach ( ( [ prepend , original ] ) => {
298+ const result = prependPrompt ( prepend , original ) ;
299+ expect ( typeof result === "string" || result === undefined ) . toBe ( true ) ;
300+ } ) ;
301+ } ) ;
302+ } ) ;
303+ } ) ;
0 commit comments