7
7
splitChunks ,
8
8
trimTokens ,
9
9
} from "../generation" ;
10
- import type { TiktokenModel } from "js-tiktoken" ;
11
10
12
11
// Mock the elizaLogger
13
12
vi . mock ( "../index.ts" , ( ) => ( {
@@ -130,30 +129,28 @@ describe("Generation", () => {
130
129
} ) ;
131
130
132
131
describe ( "trimTokens" , ( ) => {
133
- const model = "gpt-4" as TiktokenModel ;
134
-
135
- it ( "should return empty string for empty input" , ( ) => {
136
- const result = trimTokens ( "" , 100 , model ) ;
132
+ it ( "should return empty string for empty input" , async ( ) => {
133
+ const result = await trimTokens ( "" , 100 , mockRuntime ) ;
137
134
expect ( result ) . toBe ( "" ) ;
138
135
} ) ;
139
136
140
- it ( "should throw error for negative maxTokens" , ( ) => {
141
- expect ( ( ) => trimTokens ( "test" , - 1 , model ) ) . toThrow (
137
+ it ( "should throw error for negative maxTokens" , async ( ) => {
138
+ await expect ( trimTokens ( "test" , - 1 , mockRuntime ) ) . rejects . toThrow (
142
139
"maxTokens must be positive"
143
140
) ;
144
141
} ) ;
145
142
146
- it ( "should return unchanged text if within token limit" , ( ) => {
143
+ it ( "should return unchanged text if within token limit" , async ( ) => {
147
144
const shortText = "This is a short text" ;
148
- const result = trimTokens ( shortText , 10 , model ) ;
145
+ const result = await trimTokens ( shortText , 10 , mockRuntime ) ;
149
146
expect ( result ) . toBe ( shortText ) ;
150
147
} ) ;
151
148
152
- it ( "should truncate text to specified token limit" , ( ) => {
149
+ it ( "should truncate text to specified token limit" , async ( ) => {
153
150
// Using a longer text that we know will exceed the token limit
154
151
const longText =
155
152
"This is a much longer text that will definitely exceed our very small token limit and need to be truncated to fit within the specified constraints." ;
156
- const result = trimTokens ( longText , 5 , model ) ;
153
+ const result = await trimTokens ( longText , 5 , mockRuntime ) ;
157
154
158
155
// The exact result will depend on the tokenizer, but we can verify:
159
156
// 1. Result is shorter than original
@@ -164,19 +161,19 @@ describe("Generation", () => {
164
161
expect ( longText . includes ( result ) ) . toBe ( true ) ;
165
162
} ) ;
166
163
167
- it ( "should handle non-ASCII characters" , ( ) => {
164
+ it ( "should handle non-ASCII characters" , async ( ) => {
168
165
const unicodeText = "Hello 👋 World 🌍" ;
169
- const result = trimTokens ( unicodeText , 5 , model ) ;
166
+ const result = await trimTokens ( unicodeText , 5 , mockRuntime ) ;
170
167
expect ( result . length ) . toBeGreaterThan ( 0 ) ;
171
168
} ) ;
172
169
173
- it ( "should handle multiline text" , ( ) => {
170
+ it ( "should handle multiline text" , async ( ) => {
174
171
const multilineText = `Line 1
175
172
Line 2
176
173
Line 3
177
174
Line 4
178
175
Line 5` ;
179
- const result = trimTokens ( multilineText , 5 , model ) ;
176
+ const result = await trimTokens ( multilineText , 5 , mockRuntime ) ;
180
177
expect ( result . length ) . toBeGreaterThan ( 0 ) ;
181
178
expect ( result . length ) . toBeLessThan ( multilineText . length ) ;
182
179
} ) ;
0 commit comments