index.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { promptTemplate } from '$lib/utils/index';
  2. import { expect, test } from 'vitest';
  3. test('promptTemplate correctly replaces {{prompt}} placeholder', () => {
  4. const template = 'Hello {{prompt}}!';
  5. const prompt = 'world';
  6. const expected = 'Hello world!';
  7. const actual = promptTemplate(template, prompt);
  8. expect(actual).toBe(expected);
  9. });
  10. test('promptTemplate correctly replaces {{prompt:start:<length>}} placeholder', () => {
  11. const template = 'Hello {{prompt:start:3}}!';
  12. const prompt = 'world';
  13. const expected = 'Hello wor!';
  14. const actual = promptTemplate(template, prompt);
  15. expect(actual).toBe(expected);
  16. });
  17. test('promptTemplate correctly replaces {{prompt:end:<length>}} placeholder', () => {
  18. const template = 'Hello {{prompt:end:3}}!';
  19. const prompt = 'world';
  20. const expected = 'Hello rld!';
  21. const actual = promptTemplate(template, prompt);
  22. expect(actual).toBe(expected);
  23. });
  24. test('promptTemplate correctly replaces {{prompt:middletruncate:<length>}} placeholder when prompt length is greater than length', () => {
  25. const template = 'Hello {{prompt:middletruncate:4}}!';
  26. const prompt = 'world';
  27. const expected = 'Hello wo...ld!';
  28. const actual = promptTemplate(template, prompt);
  29. expect(actual).toBe(expected);
  30. });
  31. test('promptTemplate correctly replaces {{prompt:middletruncate:<length>}} placeholder when prompt length is less than or equal to length', () => {
  32. const template = 'Hello {{prompt:middletruncate:5}}!';
  33. const prompt = 'world';
  34. const expected = 'Hello world!';
  35. const actual = promptTemplate(template, prompt);
  36. expect(actual).toBe(expected);
  37. });
  38. test('promptTemplate returns original template when no placeholders are present', () => {
  39. const template = 'Hello world!';
  40. const prompt = 'world';
  41. const expected = 'Hello world!';
  42. const actual = promptTemplate(template, prompt);
  43. expect(actual).toBe(expected);
  44. });
  45. test('promptTemplate does not replace placeholders inside of replaced placeholders', () => {
  46. const template = 'Hello {{prompt}}!';
  47. const prompt = 'World, {{prompt}} injection';
  48. const expected = 'Hello World, {{prompt}} injection!';
  49. const actual = promptTemplate(template, prompt);
  50. expect(actual).toBe(expected);
  51. });
  52. test('promptTemplate correctly replaces multiple placeholders', () => {
  53. const template = 'Hello {{prompt}}! This is {{prompt:start:3}}!';
  54. const prompt = 'world';
  55. const expected = 'Hello world! This is wor!';
  56. const actual = promptTemplate(template, prompt);
  57. expect(actual).toBe(expected);
  58. });