_template_old.ts 2.58 KB
Newer Older
1
import { titleGenerationTemplate } from '$lib/utils/index';
2
3
import { expect, test } from 'vitest';

4
test('titleGenerationTemplate correctly replaces {{prompt}} placeholder', () => {
5
6
7
	const template = 'Hello {{prompt}}!';
	const prompt = 'world';
	const expected = 'Hello world!';
8
	const actual = titleGenerationTemplate(template, prompt);
9
10
11
	expect(actual).toBe(expected);
});

12
test('titleGenerationTemplate correctly replaces {{prompt:start:<length>}} placeholder', () => {
13
14
15
	const template = 'Hello {{prompt:start:3}}!';
	const prompt = 'world';
	const expected = 'Hello wor!';
16
	const actual = titleGenerationTemplate(template, prompt);
17
18
19
	expect(actual).toBe(expected);
});

20
test('titleGenerationTemplate correctly replaces {{prompt:end:<length>}} placeholder', () => {
21
22
23
	const template = 'Hello {{prompt:end:3}}!';
	const prompt = 'world';
	const expected = 'Hello rld!';
24
	const actual = titleGenerationTemplate(template, prompt);
25
26
27
	expect(actual).toBe(expected);
});

28
test('titleGenerationTemplate correctly replaces {{prompt:middletruncate:<length>}} placeholder when prompt length is greater than length', () => {
29
30
31
	const template = 'Hello {{prompt:middletruncate:4}}!';
	const prompt = 'world';
	const expected = 'Hello wo...ld!';
32
	const actual = titleGenerationTemplate(template, prompt);
33
34
35
	expect(actual).toBe(expected);
});

36
test('titleGenerationTemplate correctly replaces {{prompt:middletruncate:<length>}} placeholder when prompt length is less than or equal to length', () => {
37
38
39
	const template = 'Hello {{prompt:middletruncate:5}}!';
	const prompt = 'world';
	const expected = 'Hello world!';
40
	const actual = titleGenerationTemplate(template, prompt);
41
42
43
	expect(actual).toBe(expected);
});

44
test('titleGenerationTemplate returns original template when no placeholders are present', () => {
45
46
47
	const template = 'Hello world!';
	const prompt = 'world';
	const expected = 'Hello world!';
48
	const actual = titleGenerationTemplate(template, prompt);
49
50
51
	expect(actual).toBe(expected);
});

52
test('titleGenerationTemplate does not replace placeholders inside of replaced placeholders', () => {
53
54
55
	const template = 'Hello {{prompt}}!';
	const prompt = 'World, {{prompt}} injection';
	const expected = 'Hello World, {{prompt}} injection!';
56
	const actual = titleGenerationTemplate(template, prompt);
57
58
59
	expect(actual).toBe(expected);
});

60
test('titleGenerationTemplate correctly replaces multiple placeholders', () => {
61
62
63
	const template = 'Hello {{prompt}}! This is {{prompt:start:3}}!';
	const prompt = 'world';
	const expected = 'Hello world! This is wor!';
64
	const actual = titleGenerationTemplate(template, prompt);
65
66
	expect(actual).toBe(expected);
});