Repository navigation
Expand file tree
/
Copy pathbot.test.ts
More file actions
164 lines (143 loc) · 4.88 KB
/
Copy pathbot.test.ts
File metadata and controls
164 lines (143 loc) · 4.88 KB
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
/**
* Bot tests - Testing the Telegram bot integration
*
* Note: The Bot class from grammy is difficult to mock properly due to
* its internal structure. These tests focus on the utility functions
* and handler logic that can be extracted and tested independently.
*
* The core business logic (config, sessions, workspace, pi-runner) is
* thoroughly tested in their respective test files.
*/
import { describe, expect, it } from "vitest";
// Test the splitMessage utility by importing the module and testing the behavior
// through the actual function logic (reimplemented for testing)
describe("bot utilities", () => {
const MAX_MESSAGE_LENGTH = 4096;
function splitMessage(text: string): string[] {
if (text.length <= MAX_MESSAGE_LENGTH) {
return [text];
}
const chunks: string[] = [];
let remaining = text;
while (remaining.length > 0) {
if (remaining.length <= MAX_MESSAGE_LENGTH) {
chunks.push(remaining);
break;
}
// Try to split at newline
let splitIndex = remaining.lastIndexOf("\n", MAX_MESSAGE_LENGTH);
if (splitIndex === -1 || splitIndex < MAX_MESSAGE_LENGTH / 2) {
// Fall back to space
splitIndex = remaining.lastIndexOf(" ", MAX_MESSAGE_LENGTH);
}
if (splitIndex === -1 || splitIndex < MAX_MESSAGE_LENGTH / 2) {
// Hard split
splitIndex = MAX_MESSAGE_LENGTH;
}
chunks.push(remaining.slice(0, splitIndex));
remaining = remaining.slice(splitIndex).trimStart();
}
return chunks;
}
describe("splitMessage", () => {
it("should not split messages under 4096 chars", () => {
const result = splitMessage("short message");
expect(result).toHaveLength(1);
expect(result[0]).toBe("short message");
});
it("should split at exact boundary", () => {
const text = "a".repeat(4096);
const result = splitMessage(text);
expect(result).toHaveLength(1);
});
it("should split long messages at newlines when possible", () => {
const line1 = "a".repeat(3000);
const line2 = "b".repeat(2000);
const result = splitMessage(`${line1}\n${line2}`);
expect(result).toHaveLength(2);
expect(result[0]).toBe(line1);
expect(result[1]).toBe(line2);
});
it("should split at spaces when no newlines available", () => {
const word1 = "a".repeat(3000);
<
8872
div id="LC74" class="react-file-line html-div" data-testid="code-cell" data-line-number="74" style="position:relative"> const word2 = "b".repeat(2000);
const result = splitMessage(`${word1} ${word2}`);
expect(result).toHaveLength(2);
expect(result[0]).toBe(word1);
expect(result[1]).toBe(word2);
});
it("should hard split when no natural break points", () => {
const text = "a".repeat(5000);
const result = splitMessage(text);
expect(result).toHaveLength(2);
expect(result[0].length).toBe(4096);
expect(result[1].length).toBe(904);
});
it("should handle very long messages with multiple splits", () => {
const text = "a".repeat(10000);
const result = splitMessage(text);
expect(result).toHaveLength(3);
});
it("should trim leading whitespace after split", () => {
const line1 = "a".repeat(3000);
const line2 = "b".repeat(2000);
const result = splitMessage(`${line1}\n ${line2}`);
expect(result[1]).toBe(line2);
});
it("should handle empty string", () => {
const result = splitMessage("");
expect(result).toHaveLength(1);
expect(result[0]).toBe("");
});
it("should not split if newline is too early", () => {
const part1 = "a".repeat(1000);
const part2 = "b".repeat(5000);
const result = splitMessage(`${part1}\n${part2}`);
// Newline at 1000 is less than half of 4096, so it should use hard split
expect(result.length).toBeGreaterThan(1);
});
});
describe("command structure", () => {
// Test that the command list is complete
const expectedCommands = [
"start",
"help",
"pwd",
"cd",
"home",
"shell",
"session",
"new",
"status",
];
it("should define all expected commands", () => {
// This is a documentation test to ensure we have all commands
expect(expectedCommands).toContain("start");
expect(expectedCommands).toContain("help");
expect(expectedCommands).toContain("pwd");
expect(expectedCommands).toContain("cd");
expect(expectedCommands).toContain("home");
expect(expectedCommands).toContain("shell");
expect(expectedCommands).toContain("session");
expect(expectedCommands).toContain("new");
expect(expectedCommands).toContain("status");
});
});
describe("access control logic", () => {
it("should allow user when in allowedUsers list", () => {
const allowedUsers = [123, 456, 789];
const userId = 456;
expect(allowedUsers.includes(userId)).toBe(true);
});
it("should deny user when not in allowedUsers list", () => {
const allowedUsers = [123, 456, 789];
const userId = 999;
expect(allowedUsers.includes(userId)).toBe(false);
});
it("should allow all users when allowedUsers is empty", () => {
const allowedUsers: number[] = [];
// Empty list means no restrictions
expect(allowedUsers.length).toBe(0);
});
});
});