-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlogger-json-attachment.test.ts
More file actions
287 lines (243 loc) · 8.22 KB
/
Copy pathlogger-json-attachment.test.ts
File metadata and controls
287 lines (243 loc) · 8.22 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {
JSONAttachment,
_exportsForTestingOnly,
BaseAttachment,
} from "./logger";
import { describe, expect, it } from "vitest";
const { extractAttachments } = _exportsForTestingOnly;
describe("JSONAttachment", () => {
it("should create an attachment from JSON data", async () => {
const testData = {
foo: "bar",
nested: {
array: [1, 2, 3],
bool: true,
},
};
const attachment = new JSONAttachment(testData);
expect(attachment.reference.type).toBe("braintrust_attachment");
expect(attachment.reference.filename).toBe("data.json");
expect(attachment.reference.content_type).toBe("application/json");
expect(attachment.reference.key).toBeDefined();
// Verify the data can be retrieved
const blob = await attachment.data();
const text = await blob.text();
const parsed = JSON.parse(text);
expect(parsed).toEqual(testData);
});
it("should handle custom filename", () => {
const attachment = new JSONAttachment(
{ test: "data" },
{ filename: "custom.json" },
);
expect(attachment.reference.filename).toBe("custom.json");
});
it("should defer to the dataset pipeline hook when installed", () => {
const globalWithHook = globalThis as typeof globalThis & {
__BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__?: (
data: unknown,
options?: { filename?: string; pretty?: boolean },
) => object;
};
const previous =
globalWithHook.__BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__;
try {
globalWithHook.__BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__ = (
data,
options,
) => {
const reference = {
type: "braintrust_deferred_attachment",
kind: "json",
filename: options?.filename,
content_type: "application/json",
pretty: options?.pretty,
data,
};
return { reference };
};
const attachment = new JSONAttachment(
{ test: "data" },
{ filename: "trace.json", pretty: true },
);
expect(attachment.reference).toEqual({
type: "braintrust_deferred_attachment",
kind: "json",
filename: "trace.json",
content_type: "application/json",
pretty: true,
data: { test: "data" },
});
} finally {
globalWithHook.__BT_DATASET_PIPELINE_DEFER_JSON_ATTACHMENT__ = previous;
}
});
it("should pretty print when requested", async () => {
const testData = { a: 1, b: 2 };
const attachment = new JSONAttachment(testData, { pretty: true });
const blob = await attachment.data();
const text = await blob.text();
expect(text).toBe('{\n "a": 1,\n "b": 2\n}');
});
it("should handle large transcript scenario", () => {
const largeTranscript = Array.from({ length: 1000 }, (_, i) => ({
role: i % 2 === 0 ? "user" : "assistant",
content: `Message ${i}`,
timestamp: Date.now() + i,
}));
const attachment = new JSONAttachment(largeTranscript, {
filename: "transcript.json",
});
expect(attachment.reference.filename).toBe("transcript.json");
expect(attachment.reference.content_type).toBe("application/json");
});
it("should handle arrays and primitives", async () => {
const arrayData = [1, 2, 3, 4, 5];
const attachment = new JSONAttachment(arrayData);
const blob = await attachment.data();
const text = await blob.text();
expect(JSON.parse(text)).toEqual(arrayData);
});
it("should integrate with logger patterns", () => {
// Example of the intended usage pattern
const logData = {
input: {
type: "nameOfPrompt",
transcript: new JSONAttachment([
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there!" },
]),
configValue1: 123,
configValue2: true,
},
output: [{ type: "text", value: "Generated response" }],
metadata: {
sessionId: "123",
userId: "456",
renderedPrompt: new JSONAttachment(
"This is a very long prompt template...",
{ filename: "prompt.json" },
),
},
};
// Verify the structure contains JSONAttachment instances
expect(logData.input.transcript).toBeInstanceOf(JSONAttachment);
expect(logData.metadata.renderedPrompt).toBeInstanceOf(JSONAttachment);
});
it("should handle null and undefined values", async () => {
const testData = {
nullValue: null,
undefinedValue: undefined,
nested: {
alsoNull: null,
},
};
const attachment = new JSONAttachment(testData);
const blob = await attachment.data();
const text = await blob.text();
const parsed = JSON.parse(text);
// undefined should be stripped by JSON.stringify
expect(parsed).toEqual({
nullValue: null,
nested: {
alsoNull: null,
},
});
});
it("should handle special characters and unicode", async () => {
const testData = {
emoji: "🚀 Hello World 🌍",
special: "quotes \"and\" 'apostrophes'",
unicode: "Iñtërnâtiônàlizætiøn",
newlines: "line1\nline2\ttabbed",
};
const attachment = new JSONAttachment(testData);
const blob = await attachment.data();
const text = await blob.text();
const parsed = JSON.parse(text);
expect(parsed).toEqual(testData);
});
it("should work with extractAttachments", () => {
const jsonAttachment = new JSONAttachment(
{ foo: "bar" },
{ filename: "test.json" },
);
const event = {
input: {
data: jsonAttachment,
},
};
const attachments: BaseAttachment[] = [];
extractAttachments(event, attachments);
expect(attachments).toHaveLength(1);
expect(attachments[0]).toBe(jsonAttachment);
expect(event.input.data).toEqual(jsonAttachment.reference);
});
it("should handle nested JSONAttachments in arrays", () => {
const attachment1 = new JSONAttachment({ id: 1 });
const attachment2 = new JSONAttachment({ id: 2 });
const event = {
messages: [attachment1, "text", attachment2],
};
const attachments: BaseAttachment[] = [];
extractAttachments(event, attachments);
expect(attachments).toHaveLength(2);
expect(attachments[0]).toBe(attachment1);
expect(attachments[1]).toBe(attachment2);
expect(event.messages[0]).toEqual(attachment1.reference);
expect(event.messages[1]).toBe("text");
expect(event.messages[2]).toEqual(attachment2.reference);
});
it("should create unique keys for each attachment", () => {
const attachment1 = new JSONAttachment({ test: 1 });
const attachment2 = new JSONAttachment({ test: 2 });
expect(attachment1.reference.key).toBeDefined();
expect(attachment2.reference.key).toBeDefined();
expect(attachment1.reference.key).not.toBe(attachment2.reference.key);
});
it("should handle empty objects and arrays", async () => {
const emptyObject = new JSONAttachment({});
const emptyArray = new JSONAttachment([]);
const objectBlob = await emptyObject.data();
const objectText = await objectBlob.text();
expect(JSON.parse(objectText)).toEqual({});
const arrayBlob = await emptyArray.data();
const arrayText = await arrayBlob.text();
expect(JSON.parse(arrayText)).toEqual([]);
});
it("should handle deeply nested structures", async () => {
const deepData = {
level1: {
level2: {
level3: {
level4: {
level5: {
value: "deep",
},
},
},
},
},
};
const attachment = new JSONAttachment(deepData);
const blob = await attachment.data();
const text = await blob.text();
const parsed = JSON.parse(text);
expect(parsed).toEqual(deepData);
});
it("should handle numbers including special values", async () => {
const testData = {
integer: 42,
float: 3.14159,
negative: -100,
zero: 0,
// Note: Infinity, -Infinity, and NaN are not valid JSON
// and will be converted to null by JSON.stringify
};
const attachment = new JSONAttachment(testData);
const blob = await attachment.data();
const text = await blob.text();
const parsed = JSON.parse(text);
expect(parsed).toEqual(testData);
});
});