|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { composeContext, addHeader } from "../context"; |
| 3 | +import { State } from "../types"; |
| 4 | + |
| 5 | +describe("composeContext", () => { |
| 6 | + it("should replace placeholders with state values", () => { |
| 7 | + const state: State = { |
| 8 | + userName: "Alice", |
| 9 | + userAge: 30, |
| 10 | + bio: "", |
| 11 | + lore: "", |
| 12 | + messageDirections: "", |
| 13 | + postDirections: "", |
| 14 | + roomId: "aaaa-bbbb-cccc-dddd-eeee", |
| 15 | + actors: "", |
| 16 | + recentMessages: "", |
| 17 | + recentMessagesData: [], |
| 18 | + }; |
| 19 | + const template = "Hello, {{userName}}! You are {{userAge}} years old."; |
| 20 | + |
| 21 | + const result = composeContext({ state, template }); |
| 22 | + |
| 23 | + expect(result).toBe("Hello, Alice! You are 30 years old."); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should replace unknown placeholders with empty string", () => { |
| 27 | + const state: State = { |
| 28 | + userName: "Bob", |
| 29 | + bio: "", |
| 30 | + lore: "", |
| 31 | + messageDirections: "", |
| 32 | + postDirections: "", |
| 33 | + roomId: "aaaa-bbbb-cccc-dddd-eeee", |
| 34 | + actors: "", |
| 35 | + recentMessages: "", |
| 36 | + recentMessagesData: [], |
| 37 | + }; |
| 38 | + const template = "Hello, {{userName}}! Your age is {{userAge}}."; |
| 39 | + |
| 40 | + const result = composeContext({ state, template }); |
| 41 | + |
| 42 | + expect(result).toBe("Hello, Bob! Your age is ."); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should handle empty template", () => { |
| 46 | + const state: State = { |
| 47 | + userName: "Charlie", |
| 48 | + bio: "", |
| 49 | + lore: "", |
| 50 | + messageDirections: "", |
| 51 | + postDirections: "", |
| 52 | + roomId: "aaaa-bbbb-cccc-dddd-eeee", |
| 53 | + actors: "", |
| 54 | + recentMessages: "", |
| 55 | + recentMessagesData: [], |
| 56 | + }; |
| 57 | + const template = ""; |
| 58 | + |
| 59 | + const result = composeContext({ state, template }); |
| 60 | + |
| 61 | + expect(result).toBe(""); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("addHeader", () => { |
| 66 | + it("should combine header and body with newline", () => { |
| 67 | + const header = "Title"; |
| 68 | + const body = "Content"; |
| 69 | + |
| 70 | + const result = addHeader(header, body); |
| 71 | + |
| 72 | + expect(result).toBe("Title\nContent\n"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should return empty string if body is empty", () => { |
| 76 | + const header = "Title"; |
| 77 | + const body = ""; |
| 78 | + |
| 79 | + const result = addHeader(header, body); |
| 80 | + |
| 81 | + expect(result).toBe(""); |
| 82 | + }); |
| 83 | +}); |
0 commit comments