forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
198 lines (159 loc) · 5.8 KB
/
Copy pathutils.test.ts
File metadata and controls
198 lines (159 loc) · 5.8 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
import {
getModalityFromMimeType,
formatFileSize,
exceedsMaxSize,
matchesAcceptFilter,
} from "../utils";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function mockFile(size: number): File {
return { size } as File;
}
function mockFileWithType(type: string, name = "test"): File {
return { type, name } as File;
}
// ---------------------------------------------------------------------------
// getModalityFromMimeType
// ---------------------------------------------------------------------------
describe("getModalityFromMimeType", () => {
it('returns "image" for image/png', () => {
expect(getModalityFromMimeType("image/png")).toBe("image");
});
it('returns "image" for image/jpeg', () => {
expect(getModalityFromMimeType("image/jpeg")).toBe("image");
});
it('returns "audio" for audio/mp3', () => {
expect(getModalityFromMimeType("audio/mp3")).toBe("audio");
});
it('returns "audio" for audio/wav', () => {
expect(getModalityFromMimeType("audio/wav")).toBe("audio");
});
it('returns "video" for video/mp4', () => {
expect(getModalityFromMimeType("video/mp4")).toBe("video");
});
it('returns "video" for video/webm', () => {
expect(getModalityFromMimeType("video/webm")).toBe("video");
});
it('returns "document" for application/pdf', () => {
expect(getModalityFromMimeType("application/pdf")).toBe("document");
});
it('returns "document" for text/plain', () => {
expect(getModalityFromMimeType("text/plain")).toBe("document");
});
it('returns "document" for empty string (fallback)', () => {
expect(getModalityFromMimeType("")).toBe("document");
});
});
// ---------------------------------------------------------------------------
// formatFileSize
// ---------------------------------------------------------------------------
describe("formatFileSize", () => {
it('formats 0 bytes as "0 B"', () => {
expect(formatFileSize(0)).toBe("0 B");
});
it('formats 512 bytes as "512 B"', () => {
expect(formatFileSize(512)).toBe("512 B");
});
it('formats 1023 bytes as "1023 B"', () => {
expect(formatFileSize(1023)).toBe("1023 B");
});
it('formats 1024 bytes as "1.0 KB"', () => {
expect(formatFileSize(1024)).toBe("1.0 KB");
});
it('formats 1536 bytes as "1.5 KB"', () => {
expect(formatFileSize(1536)).toBe("1.5 KB");
});
it('formats 1048575 bytes as "1024.0 KB" (one byte under 1 MB)', () => {
expect(formatFileSize(1048575)).toBe("1024.0 KB");
});
it('formats 1048576 bytes as "1.0 MB"', () => {
expect(formatFileSize(1048576)).toBe("1.0 MB");
});
it('formats 10485760 bytes as "10.0 MB"', () => {
expect(formatFileSize(10485760)).toBe("10.0 MB");
});
});
// ---------------------------------------------------------------------------
// exceedsMaxSize
// ---------------------------------------------------------------------------
const MB_20 = 20 * 1024 * 1024;
describe("exceedsMaxSize", () => {
it("returns false for a file exactly at the 20 MB default limit", () => {
expect(exceedsMaxSize(mockFile(MB_20))).toBe(false);
});
it("returns true for a file one byte over the 20 MB default limit", () => {
expect(exceedsMaxSize(mockFile(MB_20 + 1))).toBe(true);
});
it("returns false for a file well under the default limit", () => {
expect(exceedsMaxSize(mockFile(1024))).toBe(false);
});
it("returns true when file exceeds a custom maxSize", () => {
const MB_5 = 5 * 1024 * 1024;
const MB_10 = 10 * 1024 * 1024;
expect(exceedsMaxSize(mockFile(MB_10), MB_5)).toBe(true);
});
});
// ---------------------------------------------------------------------------
// matchesAcceptFilter
// ---------------------------------------------------------------------------
describe("matchesAcceptFilter", () => {
it('returns true for accept "*/*" regardless of file type', () => {
expect(matchesAcceptFilter(mockFileWithType("image/png"), "*/*")).toBe(
true,
);
});
it("returns true for empty accept string (accept all)", () => {
expect(matchesAcceptFilter(mockFileWithType("image/png"), "")).toBe(true);
});
it('returns true when accept "image/*" matches "image/png"', () => {
expect(matchesAcceptFilter(mockFileWithType("image/png"), "image/*")).toBe(
true,
);
});
it('returns false when accept "image/*" rejects "audio/mp3"', () => {
expect(matchesAcceptFilter(mockFileWithType("audio/mp3"), "image/*")).toBe(
false,
);
});
it('returns true for exact match accept "application/pdf"', () => {
expect(
matchesAcceptFilter(
mockFileWithType("application/pdf"),
"application/pdf",
),
).toBe(true);
});
it('returns false when exact accept "application/pdf" rejects "image/png"', () => {
expect(
matchesAcceptFilter(mockFileWithType("image/png"), "application/pdf"),
).toBe(false);
});
it('returns true for comma-separated accept "image/*,application/pdf" with "image/jpeg"', () => {
expect(
matchesAcceptFilter(
mockFileWithType("image/jpeg"),
"image/*,application/pdf",
),
).toBe(true);
});
it('returns true for comma-separated accept "image/*,application/pdf" with "application/pdf"', () => {
expect(
matchesAcceptFilter(
mockFileWithType("application/pdf"),
"image/*,application/pdf",
),
).toBe(true);
});
it("handles whitespace in comma-separated accept values", () => {
expect(
matchesAcceptFilter(
mockFileWithType("application/pdf"),
"image/* , application/pdf",
),
).toBe(true);
});
it("returns false for empty file type against a specific filter", () => {
expect(matchesAcceptFilter(mockFileWithType(""), "image/*")).toBe(false);
});
});