2024-05-31 18:11:28 +00:00
import { titleGenerationTemplate } from '$lib/utils/index' ;
2024-05-10 12:31:17 +00:00
import { expect , test } from 'vitest' ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate correctly replaces {{prompt}} placeholder' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt}}!' ;
const prompt = 'world' ;
const expected = 'Hello world!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate correctly replaces {{prompt:start:<length>}} placeholder' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt:start:3}}!' ;
const prompt = 'world' ;
const expected = 'Hello wor!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate correctly replaces {{prompt:end:<length>}} placeholder' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt:end:3}}!' ;
const prompt = 'world' ;
const expected = 'Hello rld!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate correctly replaces {{prompt:middletruncate:<length>}} placeholder when prompt length is greater than length' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt:middletruncate:4}}!' ;
const prompt = 'world' ;
const expected = 'Hello wo...ld!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate correctly replaces {{prompt:middletruncate:<length>}} placeholder when prompt length is less than or equal to length' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt:middletruncate:5}}!' ;
const prompt = 'world' ;
const expected = 'Hello world!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate returns original template when no placeholders are present' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello world!' ;
const prompt = 'world' ;
const expected = 'Hello world!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate does not replace placeholders inside of replaced placeholders' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt}}!' ;
const prompt = 'World, {{prompt}} injection' ;
const expected = 'Hello World, {{prompt}} injection!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;
2024-05-31 18:11:28 +00:00
test ( 'titleGenerationTemplate correctly replaces multiple placeholders' , ( ) = > {
2024-05-10 12:31:17 +00:00
const template = 'Hello {{prompt}}! This is {{prompt:start:3}}!' ;
const prompt = 'world' ;
const expected = 'Hello world! This is wor!' ;
2024-05-31 18:11:28 +00:00
const actual = titleGenerationTemplate ( template , prompt ) ;
2024-05-10 12:31:17 +00:00
expect ( actual ) . toBe ( expected ) ;
} ) ;