|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { definePrompt } from "./prompt.js"; |
| 3 | + |
| 4 | +describe("local prompt resolution", () => { |
| 5 | + // Catches: values inserted inside conditional sections being evaluated again as placeholders. |
| 6 | + it("keeps substituted template syntax literal inside and outside conditionals", async () => { |
| 7 | + const prompt = definePrompt({ |
| 8 | + id: "literal-prompt-values", |
| 9 | + content: "{{#show}}Hello {{name}} / {{ name }}!{{/show}} Outside: {{name}}", |
| 10 | + }); |
| 11 | + |
| 12 | + const result = await prompt.resolve({ |
| 13 | + show: true, |
| 14 | + name: "{{systemPrompt}}", |
| 15 | + systemPrompt: "private instructions", |
| 16 | + }); |
| 17 | + |
| 18 | + expect(result.text).toBe( |
| 19 | + "Hello {{systemPrompt}} / {{systemPrompt}}! Outside: {{systemPrompt}}" |
| 20 | + ); |
| 21 | + }); |
| 22 | + |
| 23 | + // Catches: inherited Object.prototype properties resolving as values or enabling conditionals. |
| 24 | + it.each(["constructor", "__proto__", "toString"])( |
| 25 | + "does not resolve inherited %s properties", |
| 26 | + async (key) => { |
| 27 | + const prompt = definePrompt({ |
| 28 | + id: `inherited-prompt-${key}`, |
| 29 | + content: `Before{{${key}}}{{#${key}}}hidden{{/${key}}}After`, |
| 30 | + }); |
| 31 | + |
| 32 | + expect((await prompt.resolve({})).text).toBe("BeforeAfter"); |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + // Catches: custom prototype values being substituted, including inside enabled sections. |
| 37 | + it("ignores custom inherited properties and supports shadowed hasOwnProperty", async () => { |
| 38 | + const variables = Object.assign(Object.create({ inherited: "private" }), { |
| 39 | + show: true, |
| 40 | + hasOwnProperty: "own value", |
| 41 | + }); |
| 42 | + const prompt = definePrompt({ |
| 43 | + id: "custom-prototype-prompt", |
| 44 | + content: |
| 45 | + "{{inherited}}{{#inherited}}hidden{{/inherited}}{{#show}}{{inherited}}{{hasOwnProperty}}{{/show}}", |
| 46 | + }); |
| 47 | + |
| 48 | + expect((await prompt.resolve(variables)).text).toBe("own value"); |
| 49 | + }); |
| 50 | + |
| 51 | + // Catches: own properties with prototype-related names being incorrectly rejected. |
| 52 | + it("resolves explicitly supplied prototype-related names on a null-prototype object", async () => { |
| 53 | + const variables = Object.assign(Object.create(null), { |
| 54 | + constructor: "own constructor", |
| 55 | + ["__proto__"]: "own proto", |
| 56 | + }); |
| 57 | + const prompt = definePrompt({ |
| 58 | + id: "own-prototype-names-prompt", |
| 59 | + content: "{{constructor}} / {{#__proto__}}{{ __proto__ }}{{/__proto__}}", |
| 60 | + }); |
| 61 | + |
| 62 | + expect((await prompt.resolve(variables)).text).toBe("own constructor / own proto"); |
| 63 | + }); |
| 64 | + |
| 65 | + // Catches: conditional selection or ordinary substitution changing during compiler refactoring. |
| 66 | + it("preserves conditionals, whitespace, missing values, and falsy substitutions", async () => { |
| 67 | + const prompt = definePrompt({ |
| 68 | + id: "ordinary-prompt-substitutions", |
| 69 | + content: |
| 70 | + "{{#show}}Hello {{ name }}: {{count}} / {{enabled}} / {{nil}} / {{missing}}{{/show}}" + |
| 71 | + "{{#disabled}}hidden{{/disabled}}{{#missing}}hidden{{/missing}} Outside: {{name}}", |
| 72 | + }); |
| 73 | + |
| 74 | + const result = await prompt.resolve({ |
| 75 | + show: true, |
| 76 | + name: "Ada", |
| 77 | + count: 0, |
| 78 | + enabled: false, |
| 79 | + nil: null, |
| 80 | + disabled: false, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.text).toBe("Hello Ada: 0 / false / / Outside: Ada"); |
| 84 | + }); |
| 85 | +}); |
0 commit comments