forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.test.ts
198 lines (165 loc) · 5.88 KB
/
context.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { describe, expect, it } from "vitest";
import { composeContext } from "../context";
import handlebars from "handlebars";
import { State } from "../types.ts";
describe("composeContext", () => {
const baseState: State = {
actors: "",
recentMessages: "",
recentMessagesData: [],
roomId: "-----",
bio: "",
lore: "",
messageDirections: "",
postDirections: "",
userName: "",
};
// Test simple string replacement
describe("simple string replacement (default)", () => {
it("should replace placeholders with corresponding state values", () => {
const state: State = {
...baseState,
userName: "Alice",
userAge: 30,
};
const template =
"Hello, {{userName}}! You are {{userAge}} years old.";
const result = composeContext({ state, template });
expect(result).toBe("Hello, Alice! You are 30 years old.");
});
it("should replace missing state values with empty string", () => {
const state: State = {
...baseState,
userName: "Alice",
};
const template =
"Hello, {{userName}}! You are {{userAge}} years old.";
const result = composeContext({ state, template });
expect(result).toBe("Hello, Alice! You are years old.");
});
it("should handle templates with no placeholders", () => {
const state: State = {
...baseState,
userName: "Alice",
};
const template = "Hello, world!";
const result = composeContext({ state, template });
expect(result).toBe("Hello, world!");
});
it("should handle empty template", () => {
const state: State = {
...baseState,
userName: "Alice",
};
const template = "";
const result = composeContext({ state, template });
expect(result).toBe("");
});
});
// Test Handlebars templating
describe("handlebars templating", () => {
it("should process basic handlebars template", () => {
const state: State = {
...baseState,
userName: "Alice",
userAge: 30,
};
const template =
"Hello, {{userName}}! You are {{userAge}} years old.";
const result = composeContext({
state,
template,
templatingEngine: "handlebars",
});
expect(result).toBe("Hello, Alice! You are 30 years old.");
});
it("should handle handlebars conditionals", () => {
const state: State = {
...baseState,
userName: "Alice",
userAge: 30,
};
const template =
"{{#if userAge}}Age: {{userAge}}{{else}}Age unknown{{/if}}";
const result = composeContext({
state,
template,
templatingEngine: "handlebars",
});
expect(result).toBe("Age: 30");
});
it("should handle handlebars loops", () => {
const state: State = {
...baseState,
colors: ["red", "blue", "green"],
};
const template =
"{{#each colors}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}";
const result = composeContext({
state,
template,
templatingEngine: "handlebars",
});
expect(result).toBe("red, blue, green");
});
it("should handle complex handlebars template", () => {
// Register the 'gt' helper before running tests
handlebars.registerHelper("gt", function (a, b) {
return a > b;
});
const state = {
...baseState,
userName: "Alice",
userAge: 30,
favoriteColors: ["blue", "green", "red"],
};
const template = `
{{#if userAge}}
Hello, {{userName}}! {{#if (gt userAge 18)}}You are an adult.{{else}}You are a minor.{{/if}}
{{else}}
Hello! We don't know your age.
{{/if}}
{{#each favoriteColors}}
- {{this}}
{{/each}}`;
const result = composeContext({
state,
template,
templatingEngine: "handlebars",
});
expect(result.trim()).toMatch(/Hello, Alice! You are an adult./);
expect(result).toContain("- blue");
expect(result).toContain("- green");
expect(result).toContain("- red");
});
it("should handle missing values in handlebars template", () => {
const state = {...baseState}
const template = "Hello, {{userName}}!";
const result = composeContext({
state,
template,
templatingEngine: "handlebars",
});
expect(result).toBe("Hello, !");
});
});
describe("error handling", () => {
it("should handle undefined state", () => {
const template = "Hello, {{userName}}!";
expect(() => {
// @ts-expect-error testing undefined state
composeContext({ template });
}).toThrow();
});
it("should handle undefined template", () => {
const state = {
...baseState,
userName: "Alice",
};
expect(() => {
// @ts-expect-error testing undefined template
composeContext({ state });
}).toThrow();
});
});
});