-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.test.js
179 lines (162 loc) · 5.72 KB
/
helpers.test.js
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
const dotenv = require("dotenv");
dotenv.config();
const {
destructureArgs,
getHexagram,
countTokens,
countMessageTokens,
removeMentionFromMessage,
isBreakingMessageChain,
processChunks,
assembleMessagePreamble,
addCurrentDateTime,
addHexagramPrompt,
// addSystemPrompt,
addCapabilityPromptIntro,
addCapabilityManifestMessage,
addTodosToMessages,
addUserMessages,
addUserMemories,
addRelevantMemories,
addGeneralMemories,
} = require("./helpers.js");
// Mocking the dependent functions
// jest.mock("./helpers.js", () => ({
// ...jest.requireActual("./helpers.js"),
// addCurrentDateTime: jest.fn(),
// addHexagramPrompt: jest.fn(),
// addSystemPrompt: jest.fn(),
// addCapabilityPromptIntro: jest.fn(),
// addCapabilityManifestMessage: jest.fn(),
// addTodosToMessages: jest.fn(),
// addUserMessages: jest.fn(),
// addUserMemories: jest.fn(),
// addRelevantMemories: jest.fn(),
// addGeneralMemories: jest.fn()
// }));
describe("Helpers", () => {
describe("destructureArgs", () => {
it("should destructure arguments correctly", () => {
const args = "arg1, arg2, arg3";
const result = destructureArgs(args);
expect(result).toEqual(["arg1", "arg2", "arg3"]);
});
});
describe("getHexagram", () => {
it("should return a valid hexagram", () => {
const hexagram = getHexagram();
expect(hexagram).toMatch(/^[1-9][0-9]?.\s.+/);
});
});
describe("countTokens", () => {
it("should return correct token count", () => {
const str = "Hello, world!";
const result = countTokens(str);
expect(result).toBe(4);
});
});
describe("countMessageTokens", () => {
it("should return correct token count for messages", () => {
const messages = [{ role: "user", content: "Hello, world!" }];
const result = countMessageTokens(messages);
expect(result).toBe(12);
});
});
describe("removeMentionFromMessage", () => {
it("should remove mention from message", () => {
const message = "<@1234567890> Hello, world!";
const mention = "<@1234567890>";
const result = removeMentionFromMessage(message, mention);
expect(result).toBe("Hello, world!");
});
});
describe("countTokens", () => {
it("should return correct token count", () => {
const str = "Hello, world!";
const result = countTokens(str);
expect(result).toBe(4);
});
});
describe("countMessageTokens", () => {
it("should return correct token count for messages", () => {
const messages = [{ role: "user", content: "Hello, world!" }];
const result = countMessageTokens(messages);
expect(result).toBe(12);
});
});
describe("removeMentionFromMessage", () => {
it("should remove mention from message", () => {
const message = "<@1234567890> Hello, world!";
const mention = "<@1234567890>";
const result = removeMentionFromMessage(message, mention);
expect(result).toBe("Hello, world!");
});
});
describe("isBreakingMessageChain", () => {
it("should return true if there is no capability match and the last message role is not user or system", () => {
const capabilityMatch = false;
const lastMessage = { role: "bot" };
const result = isBreakingMessageChain(capabilityMatch, lastMessage);
expect(result).toBe(true);
});
it("should return false if there is a capability match", () => {
const capabilityMatch = true;
const lastMessage = { role: "bot" };
const result = isBreakingMessageChain(capabilityMatch, lastMessage);
expect(result).toBe(false);
});
it("should return false if the last message role is user or system", () => {
const capabilityMatch = false;
const lastMessageUser = { role: "user" };
const lastMessageSystem = { role: "system" };
const resultUser = isBreakingMessageChain(
capabilityMatch,
lastMessageUser
);
const resultSystem = isBreakingMessageChain(
capabilityMatch,
lastMessageSystem
);
expect(resultUser).toBe(false);
expect(resultSystem).toBe(false);
});
describe("processChunks", () => {
it("should process chunks of data asynchronously", async () => {
const chunks = [["data1"], ["data2"], ["data3"]];
const processFunction = jest.fn().mockResolvedValue("processed");
const results = await processChunks(chunks, processFunction, 2);
expect(results).toEqual(["processed", "processed", "processed"]);
expect(processFunction).toHaveBeenCalledTimes(chunks.length);
});
});
describe("addCurrentDateTime", () => {
it("should add the current date and time to the messages array", () => {
const messages = [];
addCurrentDateTime(messages);
expect(messages).toHaveLength(1);
expect(messages[0].role).toEqual("system");
// Adjusted the regex to match the received string format including AM/PM
expect(messages[0].content).toMatch(
/Today is \d{1,2}\/\d{1,2}\/\d{4} at \d{1,2}:\d{2}:\d{2} [AP]M/
);
});
});
describe("addSystemPrompt", () => {
it("should add a system prompt to the messages array", async () => {
const { addSystemPrompt } = require("./helpers.js");
const messages = [];
await addSystemPrompt(messages);
expect(messages).toHaveLength(1);
expect(messages[0].role).toEqual("user");
});
});
// make sure assembleMessagePreamble works as expected
// describe("assembleMessagePreamble", () => {
// it("should assemble a message preamble", () => {
// const messages = [];
// assembleMessagePreamble(messages);
// expect(messages).toHaveLength(1);
// });
// });
});
});