@autobe/ui 0.29.2 → 0.30.0-dev.20260315
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -661
- package/README.md +261 -0
- package/lib/components/AutoBeChatMain.js +5 -5
- package/lib/components/AutoBeChatMain.js.map +1 -1
- package/lib/components/AutoBeConfigModal.js +9 -9
- package/lib/components/AutoBeStatusModal.js +4 -4
- package/lib/components/AutoBeStatusModal.js.map +1 -1
- package/lib/components/AutoBeUserMessageMovie.d.ts +2 -2
- package/lib/components/common/ChatBubble.d.ts +2 -2
- package/lib/components/common/openai/OpenAIContent.d.ts +2 -2
- package/lib/components/common/openai/OpenAIContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserAudioContent.js +1 -1
- package/lib/components/common/openai/OpenAIUserAudioContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserFileContent.js +1 -1
- package/lib/components/common/openai/OpenAIUserFileContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserImageContent.d.ts +2 -2
- package/lib/components/events/AutoBeCompleteEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeCompleteEventMovie.js +5 -5
- package/lib/components/events/AutoBeCompleteEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeCorrectEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeCorrectEventMovie.js +4 -4
- package/lib/components/events/AutoBeCorrectEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeEventMovie.js +38 -17
- package/lib/components/events/AutoBeEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeProgressEventMovie.js +73 -13
- package/lib/components/events/AutoBeProgressEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeScenarioEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeScenarioEventMovie.js +18 -5
- package/lib/components/events/AutoBeScenarioEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeStartEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeStartEventMovie.js +2 -2
- package/lib/components/events/AutoBeStartEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeValidateEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeValidateEventMovie.js +3 -11
- package/lib/components/events/AutoBeValidateEventMovie.js.map +1 -1
- package/lib/components/events/groups/CorrectEventGroup.d.ts +2 -2
- package/lib/components/events/groups/CorrectEventGroup.js +1 -1
- package/lib/components/events/groups/CorrectEventGroup.js.map +1 -1
- package/lib/components/events/groups/ValidateEventGroup.d.ts +2 -2
- package/lib/components/events/groups/ValidateEventGroup.js +1 -2
- package/lib/components/events/groups/ValidateEventGroup.js.map +1 -1
- package/lib/components/events/utils/eventGrouper.js +1 -2
- package/lib/components/events/utils/eventGrouper.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadBox.d.ts +3 -4
- package/lib/components/upload/AutoBeChatUploadBox.js +2 -1
- package/lib/components/upload/AutoBeChatUploadBox.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadSendButton.js +1 -1
- package/lib/components/upload/AutoBeChatUploadSendButton.js.map +1 -1
- package/lib/context/AutoBeAgentContext.d.ts +1 -3
- package/lib/context/AutoBeAgentContext.js +0 -4
- package/lib/context/AutoBeAgentContext.js.map +1 -1
- package/lib/hooks/useSessionStorage.d.ts +4 -0
- package/lib/hooks/useSessionStorage.js +16 -0
- package/lib/hooks/useSessionStorage.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.d.ts +10 -0
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.js +117 -0
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.js.map +1 -0
- package/lib/structure/AutoBeListener.js +91 -23
- package/lib/structure/AutoBeListener.js.map +1 -1
- package/lib/structure/AutoBeListenerState.d.ts +3 -3
- package/lib/structure/AutoBeListenerState.js +4 -4
- package/lib/structure/AutoBeListenerState.js.map +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js.map +1 -1
- package/lib/utils/AutoBeFileUploader.d.ts +2 -2
- package/lib/utils/AutoBeFileUploader.js.map +1 -1
- package/package.json +3 -4
- package/src/components/AutoBeAssistantMessageMovie.tsx +22 -22
- package/src/components/AutoBeChatMain.tsx +376 -376
- package/src/components/AutoBeChatSidebar.tsx +414 -414
- package/src/components/AutoBeConfigButton.tsx +83 -83
- package/src/components/AutoBeConfigModal.tsx +443 -443
- package/src/components/AutoBeStatusButton.tsx +75 -75
- package/src/components/AutoBeStatusModal.tsx +486 -484
- package/src/components/AutoBeUserMessageMovie.tsx +27 -27
- package/src/components/common/ActionButton.tsx +205 -205
- package/src/components/common/ActionButtonGroup.tsx +80 -80
- package/src/components/common/AutoBeConfigInput.tsx +185 -185
- package/src/components/common/ChatBubble.tsx +119 -119
- package/src/components/common/Collapsible.tsx +95 -95
- package/src/components/common/CompactSessionIndicator.tsx +73 -73
- package/src/components/common/CompactSessionList.tsx +82 -82
- package/src/components/common/index.ts +8 -8
- package/src/components/common/openai/OpenAIContent.tsx +53 -53
- package/src/components/common/openai/OpenAIUserAudioContent.tsx +70 -70
- package/src/components/common/openai/OpenAIUserFileContent.tsx +76 -76
- package/src/components/common/openai/OpenAIUserImageContent.tsx +34 -34
- package/src/components/common/openai/OpenAIUserTextContent.tsx +15 -15
- package/src/components/common/openai/index.ts +5 -5
- package/src/components/events/AutoBeCompleteEventMovie.tsx +402 -402
- package/src/components/events/AutoBeCorrectEventMovie.tsx +354 -368
- package/src/components/events/AutoBeEventGroupMovie.tsx +18 -18
- package/src/components/events/AutoBeEventMovie.tsx +158 -139
- package/src/components/events/AutoBeProgressEventMovie.tsx +217 -157
- package/src/components/events/AutoBeScenarioEventMovie.tsx +135 -95
- package/src/components/events/AutoBeStartEventMovie.tsx +82 -82
- package/src/components/events/AutoBeValidateEventMovie.tsx +249 -286
- package/src/components/events/README.md +300 -300
- package/src/components/events/common/CollapsibleEventGroup.tsx +211 -211
- package/src/components/events/common/EventCard.tsx +61 -61
- package/src/components/events/common/EventContent.tsx +31 -31
- package/src/components/events/common/EventHeader.tsx +85 -85
- package/src/components/events/common/EventIcon.tsx +82 -82
- package/src/components/events/common/ProgressBar.tsx +64 -64
- package/src/components/events/common/index.ts +13 -13
- package/src/components/events/groups/CorrectEventGroup.tsx +183 -183
- package/src/components/events/groups/ValidateEventGroup.tsx +143 -146
- package/src/components/events/groups/index.ts +8 -8
- package/src/components/events/index.ts +16 -16
- package/src/components/events/utils/eventGrouper.tsx +116 -117
- package/src/components/events/utils/index.ts +1 -1
- package/src/components/index.ts +13 -13
- package/src/components/upload/AutoBeChatUploadBox.tsx +425 -424
- package/src/components/upload/AutoBeChatUploadSendButton.tsx +66 -66
- package/src/components/upload/AutoBeFileUploadBox.tsx +123 -123
- package/src/components/upload/AutoBeUploadConfig.ts +5 -5
- package/src/components/upload/AutoBeVoiceRecoderButton.tsx +100 -100
- package/src/components/upload/index.ts +5 -5
- package/src/constant/color.ts +28 -28
- package/src/context/AutoBeAgentContext.tsx +245 -258
- package/src/context/AutoBeAgentSessionList.tsx +58 -58
- package/src/context/SearchParamsContext.tsx +49 -49
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useEscapeKey.ts +24 -24
- package/src/hooks/useIsomorphicLayoutEffect.ts +8 -8
- package/src/hooks/useMediaQuery.ts +73 -73
- package/src/hooks/useSessionStorage.ts +10 -0
- package/src/icons/Receipt.tsx +74 -74
- package/src/index.ts +9 -8
- package/src/strategy/AutoBeAgentSessionStorageStrategy.ts +127 -0
- package/src/structure/AutoBeListener.ts +373 -304
- package/src/structure/AutoBeListenerState.ts +53 -53
- package/src/structure/IAutoBeAgentSessionStorageStrategy.ts +87 -87
- package/src/structure/IAutoBeEventGroup.ts +6 -6
- package/src/structure/index.ts +4 -4
- package/src/types/config.ts +44 -44
- package/src/types/index.ts +1 -1
- package/src/utils/AutoBeFileUploader.ts +279 -279
- package/src/utils/AutoBeVoiceRecorder.ts +95 -95
- package/src/utils/__tests__/crypto.test.ts +286 -286
- package/src/utils/__tests__/storage.test.ts +229 -229
- package/src/utils/crypto.ts +95 -95
- package/src/utils/index.ts +6 -6
- package/src/utils/number.ts +17 -17
- package/src/utils/storage.ts +96 -96
- package/src/utils/time.ts +14 -14
- package/tsconfig.json +9 -9
- package/vitest.config.ts +15 -15
|
@@ -1,286 +1,286 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { decrypt, encrypt } from "../crypto";
|
|
4
|
-
import {
|
|
5
|
-
getEncryptedSessionStorage,
|
|
6
|
-
removeEncryptedSessionStorage,
|
|
7
|
-
setEncryptedSessionStorage,
|
|
8
|
-
} from "../storage";
|
|
9
|
-
|
|
10
|
-
// Mock sessionStorage for testing
|
|
11
|
-
const mockSessionStorage = {
|
|
12
|
-
store: new Map<string, string>(),
|
|
13
|
-
getItem: vi.fn((key: string) => mockSessionStorage.store.get(key) || null),
|
|
14
|
-
setItem: vi.fn((key: string, value: string) => {
|
|
15
|
-
mockSessionStorage.store.set(key, value);
|
|
16
|
-
}),
|
|
17
|
-
removeItem: vi.fn((key: string) => {
|
|
18
|
-
mockSessionStorage.store.delete(key);
|
|
19
|
-
}),
|
|
20
|
-
clear: vi.fn(() => {
|
|
21
|
-
mockSessionStorage.store.clear();
|
|
22
|
-
}),
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// Mock global objects
|
|
26
|
-
Object.defineProperty(global, "sessionStorage", {
|
|
27
|
-
value: mockSessionStorage,
|
|
28
|
-
writable: true,
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
Object.defineProperty(global, "window", {
|
|
32
|
-
value: { sessionStorage: mockSessionStorage },
|
|
33
|
-
writable: true,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("Crypto Utils", () => {
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
mockSessionStorage.store.clear();
|
|
39
|
-
vi.clearAllMocks();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
afterEach(() => {
|
|
43
|
-
mockSessionStorage.store.clear();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("encrypt/decrypt", () => {
|
|
47
|
-
const testData = [
|
|
48
|
-
"sk-1234567890abcdef",
|
|
49
|
-
"test-api-key-12345",
|
|
50
|
-
"hello-world",
|
|
51
|
-
"한글테스트123",
|
|
52
|
-
"sk-proj-1234567890abcdefghijklmnop",
|
|
53
|
-
"special-chars!@#$%^&*()",
|
|
54
|
-
"🚀✨🔐💎", // Emoji test
|
|
55
|
-
"multi\nline\ntext",
|
|
56
|
-
"tab\tseparated\tvalues",
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
test.each(testData)(
|
|
60
|
-
'should encrypt and decrypt "%s" correctly',
|
|
61
|
-
(original) => {
|
|
62
|
-
const encrypted = encrypt(original);
|
|
63
|
-
const decrypted = decrypt(encrypted);
|
|
64
|
-
|
|
65
|
-
expect(decrypted).toBe(original);
|
|
66
|
-
expect(encrypted).not.toBe(original); // Should be different from original
|
|
67
|
-
expect(encrypted.length).toBeGreaterThan(0); // Should not be empty
|
|
68
|
-
},
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
test("should handle empty string", () => {
|
|
72
|
-
const original = "";
|
|
73
|
-
const encrypted = encrypt(original);
|
|
74
|
-
const decrypted = decrypt(encrypted);
|
|
75
|
-
|
|
76
|
-
expect(encrypted).toBe("");
|
|
77
|
-
expect(decrypted).toBe("");
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("should produce different encrypted values for same input (salt)", () => {
|
|
81
|
-
const original = "sk-test123";
|
|
82
|
-
const encrypted1 = encrypt(original);
|
|
83
|
-
const encrypted2 = encrypt(original);
|
|
84
|
-
|
|
85
|
-
expect(encrypted1).not.toBe(encrypted2); // Should be different due to salt
|
|
86
|
-
expect(decrypt(encrypted1)).toBe(original);
|
|
87
|
-
expect(decrypt(encrypted2)).toBe(original);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("should handle encryption errors gracefully", () => {
|
|
91
|
-
// Mock btoa to throw an error
|
|
92
|
-
const originalBtoa = global.btoa;
|
|
93
|
-
global.btoa = vi.fn(() => {
|
|
94
|
-
throw new Error("Base64 encoding failed");
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// Should throw error with meaningful message
|
|
98
|
-
expect(() => {
|
|
99
|
-
encrypt("test");
|
|
100
|
-
}).toThrow("Encryption failed: Base64 encoding failed");
|
|
101
|
-
|
|
102
|
-
global.btoa = originalBtoa; // Restore original
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test("should handle decryption errors gracefully", () => {
|
|
106
|
-
// Should throw error on invalid data
|
|
107
|
-
expect(() => {
|
|
108
|
-
decrypt("invalid-base64-data");
|
|
109
|
-
}).toThrow("Decryption failed:");
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("should handle malformed encrypted data", () => {
|
|
113
|
-
const invalidData = "VGVzdA=="; // Valid base64 but wrong format
|
|
114
|
-
// Should throw error on wrong format
|
|
115
|
-
expect(() => {
|
|
116
|
-
decrypt(invalidData);
|
|
117
|
-
}).toThrow("Invalid encrypted format: expected salt:data format");
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
describe("sessionStorage encryption", () => {
|
|
122
|
-
test("should store and retrieve encrypted data", () => {
|
|
123
|
-
const key = "test_encrypted_key";
|
|
124
|
-
const value = "sk-test123456789";
|
|
125
|
-
|
|
126
|
-
setEncryptedSessionStorage(key, value);
|
|
127
|
-
const retrieved = getEncryptedSessionStorage(key);
|
|
128
|
-
|
|
129
|
-
expect(retrieved).toBe(value);
|
|
130
|
-
expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
|
|
131
|
-
key,
|
|
132
|
-
expect.any(String),
|
|
133
|
-
);
|
|
134
|
-
expect(mockSessionStorage.getItem).toHaveBeenCalledWith(key);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test("should store encrypted value, not plain text", () => {
|
|
138
|
-
const key = "test_key";
|
|
139
|
-
const value = "secret-api-key";
|
|
140
|
-
|
|
141
|
-
setEncryptedSessionStorage(key, value);
|
|
142
|
-
const storedValue = mockSessionStorage.store.get(key);
|
|
143
|
-
|
|
144
|
-
expect(storedValue).not.toBe(value); // Should not store plain text
|
|
145
|
-
expect(storedValue).toBeTruthy(); // Should store something
|
|
146
|
-
expect(storedValue!.length).toBeGreaterThan(value.length); // Encrypted should be longer
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
test("should handle empty values", () => {
|
|
150
|
-
const key = "test_empty";
|
|
151
|
-
const value = "";
|
|
152
|
-
|
|
153
|
-
setEncryptedSessionStorage(key, value);
|
|
154
|
-
const retrieved = getEncryptedSessionStorage(key);
|
|
155
|
-
|
|
156
|
-
expect(retrieved).toBe("");
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test("should handle non-existent keys", () => {
|
|
160
|
-
const result = getEncryptedSessionStorage("non_existent_key");
|
|
161
|
-
expect(result).toBe("");
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
test("should remove encrypted data", () => {
|
|
165
|
-
const key = "test_remove";
|
|
166
|
-
const value = "test-value";
|
|
167
|
-
|
|
168
|
-
setEncryptedSessionStorage(key, value);
|
|
169
|
-
expect(getEncryptedSessionStorage(key)).toBe(value);
|
|
170
|
-
|
|
171
|
-
removeEncryptedSessionStorage(key);
|
|
172
|
-
expect(getEncryptedSessionStorage(key)).toBe("");
|
|
173
|
-
expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(key);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
test("should handle storage errors gracefully", () => {
|
|
177
|
-
const originalSetItem = mockSessionStorage.setItem;
|
|
178
|
-
mockSessionStorage.setItem = vi.fn(() => {
|
|
179
|
-
throw new Error("Storage quota exceeded");
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// Should throw error with meaningful message
|
|
183
|
-
expect(() => {
|
|
184
|
-
setEncryptedSessionStorage("test", "value");
|
|
185
|
-
}).toThrow(
|
|
186
|
-
'Failed to store encrypted data for key "test": Storage quota exceeded',
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
mockSessionStorage.setItem = originalSetItem; // Restore
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe("Unicode and special characters", () => {
|
|
194
|
-
const unicodeTestCases = [
|
|
195
|
-
"안녕하세요", // Korean
|
|
196
|
-
"こんにちは", // Japanese
|
|
197
|
-
"你好", // Chinese
|
|
198
|
-
"🚀🔐💎✨", // Emojis
|
|
199
|
-
"Café naïve résumé", // Accented characters
|
|
200
|
-
"𝕳𝖊𝖑𝖑𝖔", // Mathematical symbols
|
|
201
|
-
"‰‱‽⁇⁉", // Special punctuation
|
|
202
|
-
"\u0000\u001F\u007F", // Control characters
|
|
203
|
-
];
|
|
204
|
-
|
|
205
|
-
test.each(unicodeTestCases)(
|
|
206
|
-
'should handle Unicode text "%s" correctly',
|
|
207
|
-
(original) => {
|
|
208
|
-
const encrypted = encrypt(original);
|
|
209
|
-
const decrypted = decrypt(encrypted);
|
|
210
|
-
|
|
211
|
-
expect(decrypted).toBe(original);
|
|
212
|
-
expect(encrypted).not.toBe(original);
|
|
213
|
-
},
|
|
214
|
-
);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
describe("Large data handling", () => {
|
|
218
|
-
test("should handle long strings", () => {
|
|
219
|
-
const longString = "a".repeat(10000); // 10KB string
|
|
220
|
-
const encrypted = encrypt(longString);
|
|
221
|
-
const decrypted = decrypt(encrypted);
|
|
222
|
-
|
|
223
|
-
expect(decrypted).toBe(longString);
|
|
224
|
-
expect(encrypted.length).toBeGreaterThan(longString.length);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test("should handle API key formats", () => {
|
|
228
|
-
const apiKeyFormats = [
|
|
229
|
-
"fdsfgdsfgdsfgsfgdasdfasfdasfdasdfs",
|
|
230
|
-
"sk-asfdsadfasdfasdfasdfasfdasfdasasdfasdfas",
|
|
231
|
-
"afdssadfsafd-1234567890123-abcdefghijklmnopqrstuvwx",
|
|
232
|
-
"afdasfdasfdfdasdfsqwrewq55e4r65gh4g65hj4g32h4ty",
|
|
233
|
-
];
|
|
234
|
-
|
|
235
|
-
apiKeyFormats.forEach((apiKey) => {
|
|
236
|
-
const encrypted = encrypt(apiKey);
|
|
237
|
-
const decrypted = decrypt(encrypted);
|
|
238
|
-
|
|
239
|
-
expect(decrypted).toBe(apiKey);
|
|
240
|
-
expect(encrypted).not.toContain(apiKey.substring(10, 30)); // Should not contain readable parts
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
describe("Security features", () => {
|
|
246
|
-
test("should use different salts for same input", () => {
|
|
247
|
-
const input = "sk-test123";
|
|
248
|
-
const encrypted1 = encrypt(input);
|
|
249
|
-
const encrypted2 = encrypt(input);
|
|
250
|
-
|
|
251
|
-
expect(encrypted1).not.toBe(encrypted2);
|
|
252
|
-
|
|
253
|
-
// Both should decrypt to same original
|
|
254
|
-
expect(decrypt(encrypted1)).toBe(input);
|
|
255
|
-
expect(decrypt(encrypted2)).toBe(input);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
test("should produce base64-safe output", () => {
|
|
259
|
-
const testInputs = ["test", "sk-123", "한글", "🚀"];
|
|
260
|
-
|
|
261
|
-
testInputs.forEach((input) => {
|
|
262
|
-
const encrypted = encrypt(input);
|
|
263
|
-
// Should be valid base64 (only contains valid base64 characters)
|
|
264
|
-
expect(encrypted).toMatch(/^[A-Za-z0-9+/]*={0,2}$/);
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
test("encrypted data should not contain original text patterns", () => {
|
|
269
|
-
const sensitiveData = [
|
|
270
|
-
"sk-1234567890abcdef",
|
|
271
|
-
"password123",
|
|
272
|
-
"secret-api-key",
|
|
273
|
-
];
|
|
274
|
-
|
|
275
|
-
sensitiveData.forEach((data) => {
|
|
276
|
-
const encrypted = encrypt(data);
|
|
277
|
-
|
|
278
|
-
// Encrypted should not contain recognizable patterns from original
|
|
279
|
-
expect(encrypted.toLowerCase()).not.toContain(
|
|
280
|
-
data.substring(3, 10).toLowerCase(),
|
|
281
|
-
);
|
|
282
|
-
expect(encrypted).not.toContain(data.substring(0, 5));
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
});
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { decrypt, encrypt } from "../crypto";
|
|
4
|
+
import {
|
|
5
|
+
getEncryptedSessionStorage,
|
|
6
|
+
removeEncryptedSessionStorage,
|
|
7
|
+
setEncryptedSessionStorage,
|
|
8
|
+
} from "../storage";
|
|
9
|
+
|
|
10
|
+
// Mock sessionStorage for testing
|
|
11
|
+
const mockSessionStorage = {
|
|
12
|
+
store: new Map<string, string>(),
|
|
13
|
+
getItem: vi.fn((key: string) => mockSessionStorage.store.get(key) || null),
|
|
14
|
+
setItem: vi.fn((key: string, value: string) => {
|
|
15
|
+
mockSessionStorage.store.set(key, value);
|
|
16
|
+
}),
|
|
17
|
+
removeItem: vi.fn((key: string) => {
|
|
18
|
+
mockSessionStorage.store.delete(key);
|
|
19
|
+
}),
|
|
20
|
+
clear: vi.fn(() => {
|
|
21
|
+
mockSessionStorage.store.clear();
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Mock global objects
|
|
26
|
+
Object.defineProperty(global, "sessionStorage", {
|
|
27
|
+
value: mockSessionStorage,
|
|
28
|
+
writable: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
Object.defineProperty(global, "window", {
|
|
32
|
+
value: { sessionStorage: mockSessionStorage },
|
|
33
|
+
writable: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("Crypto Utils", () => {
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
mockSessionStorage.store.clear();
|
|
39
|
+
vi.clearAllMocks();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
mockSessionStorage.store.clear();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("encrypt/decrypt", () => {
|
|
47
|
+
const testData = [
|
|
48
|
+
"sk-1234567890abcdef",
|
|
49
|
+
"test-api-key-12345",
|
|
50
|
+
"hello-world",
|
|
51
|
+
"한글테스트123",
|
|
52
|
+
"sk-proj-1234567890abcdefghijklmnop",
|
|
53
|
+
"special-chars!@#$%^&*()",
|
|
54
|
+
"🚀✨🔐💎", // Emoji test
|
|
55
|
+
"multi\nline\ntext",
|
|
56
|
+
"tab\tseparated\tvalues",
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
test.each(testData)(
|
|
60
|
+
'should encrypt and decrypt "%s" correctly',
|
|
61
|
+
(original) => {
|
|
62
|
+
const encrypted = encrypt(original);
|
|
63
|
+
const decrypted = decrypt(encrypted);
|
|
64
|
+
|
|
65
|
+
expect(decrypted).toBe(original);
|
|
66
|
+
expect(encrypted).not.toBe(original); // Should be different from original
|
|
67
|
+
expect(encrypted.length).toBeGreaterThan(0); // Should not be empty
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
test("should handle empty string", () => {
|
|
72
|
+
const original = "";
|
|
73
|
+
const encrypted = encrypt(original);
|
|
74
|
+
const decrypted = decrypt(encrypted);
|
|
75
|
+
|
|
76
|
+
expect(encrypted).toBe("");
|
|
77
|
+
expect(decrypted).toBe("");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("should produce different encrypted values for same input (salt)", () => {
|
|
81
|
+
const original = "sk-test123";
|
|
82
|
+
const encrypted1 = encrypt(original);
|
|
83
|
+
const encrypted2 = encrypt(original);
|
|
84
|
+
|
|
85
|
+
expect(encrypted1).not.toBe(encrypted2); // Should be different due to salt
|
|
86
|
+
expect(decrypt(encrypted1)).toBe(original);
|
|
87
|
+
expect(decrypt(encrypted2)).toBe(original);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("should handle encryption errors gracefully", () => {
|
|
91
|
+
// Mock btoa to throw an error
|
|
92
|
+
const originalBtoa = global.btoa;
|
|
93
|
+
global.btoa = vi.fn(() => {
|
|
94
|
+
throw new Error("Base64 encoding failed");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Should throw error with meaningful message
|
|
98
|
+
expect(() => {
|
|
99
|
+
encrypt("test");
|
|
100
|
+
}).toThrow("Encryption failed: Base64 encoding failed");
|
|
101
|
+
|
|
102
|
+
global.btoa = originalBtoa; // Restore original
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("should handle decryption errors gracefully", () => {
|
|
106
|
+
// Should throw error on invalid data
|
|
107
|
+
expect(() => {
|
|
108
|
+
decrypt("invalid-base64-data");
|
|
109
|
+
}).toThrow("Decryption failed:");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("should handle malformed encrypted data", () => {
|
|
113
|
+
const invalidData = "VGVzdA=="; // Valid base64 but wrong format
|
|
114
|
+
// Should throw error on wrong format
|
|
115
|
+
expect(() => {
|
|
116
|
+
decrypt(invalidData);
|
|
117
|
+
}).toThrow("Invalid encrypted format: expected salt:data format");
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("sessionStorage encryption", () => {
|
|
122
|
+
test("should store and retrieve encrypted data", () => {
|
|
123
|
+
const key = "test_encrypted_key";
|
|
124
|
+
const value = "sk-test123456789";
|
|
125
|
+
|
|
126
|
+
setEncryptedSessionStorage(key, value);
|
|
127
|
+
const retrieved = getEncryptedSessionStorage(key);
|
|
128
|
+
|
|
129
|
+
expect(retrieved).toBe(value);
|
|
130
|
+
expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
|
|
131
|
+
key,
|
|
132
|
+
expect.any(String),
|
|
133
|
+
);
|
|
134
|
+
expect(mockSessionStorage.getItem).toHaveBeenCalledWith(key);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("should store encrypted value, not plain text", () => {
|
|
138
|
+
const key = "test_key";
|
|
139
|
+
const value = "secret-api-key";
|
|
140
|
+
|
|
141
|
+
setEncryptedSessionStorage(key, value);
|
|
142
|
+
const storedValue = mockSessionStorage.store.get(key);
|
|
143
|
+
|
|
144
|
+
expect(storedValue).not.toBe(value); // Should not store plain text
|
|
145
|
+
expect(storedValue).toBeTruthy(); // Should store something
|
|
146
|
+
expect(storedValue!.length).toBeGreaterThan(value.length); // Encrypted should be longer
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("should handle empty values", () => {
|
|
150
|
+
const key = "test_empty";
|
|
151
|
+
const value = "";
|
|
152
|
+
|
|
153
|
+
setEncryptedSessionStorage(key, value);
|
|
154
|
+
const retrieved = getEncryptedSessionStorage(key);
|
|
155
|
+
|
|
156
|
+
expect(retrieved).toBe("");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("should handle non-existent keys", () => {
|
|
160
|
+
const result = getEncryptedSessionStorage("non_existent_key");
|
|
161
|
+
expect(result).toBe("");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("should remove encrypted data", () => {
|
|
165
|
+
const key = "test_remove";
|
|
166
|
+
const value = "test-value";
|
|
167
|
+
|
|
168
|
+
setEncryptedSessionStorage(key, value);
|
|
169
|
+
expect(getEncryptedSessionStorage(key)).toBe(value);
|
|
170
|
+
|
|
171
|
+
removeEncryptedSessionStorage(key);
|
|
172
|
+
expect(getEncryptedSessionStorage(key)).toBe("");
|
|
173
|
+
expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(key);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("should handle storage errors gracefully", () => {
|
|
177
|
+
const originalSetItem = mockSessionStorage.setItem;
|
|
178
|
+
mockSessionStorage.setItem = vi.fn(() => {
|
|
179
|
+
throw new Error("Storage quota exceeded");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Should throw error with meaningful message
|
|
183
|
+
expect(() => {
|
|
184
|
+
setEncryptedSessionStorage("test", "value");
|
|
185
|
+
}).toThrow(
|
|
186
|
+
'Failed to store encrypted data for key "test": Storage quota exceeded',
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
mockSessionStorage.setItem = originalSetItem; // Restore
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("Unicode and special characters", () => {
|
|
194
|
+
const unicodeTestCases = [
|
|
195
|
+
"안녕하세요", // Korean
|
|
196
|
+
"こんにちは", // Japanese
|
|
197
|
+
"你好", // Chinese
|
|
198
|
+
"🚀🔐💎✨", // Emojis
|
|
199
|
+
"Café naïve résumé", // Accented characters
|
|
200
|
+
"𝕳𝖊𝖑𝖑𝖔", // Mathematical symbols
|
|
201
|
+
"‰‱‽⁇⁉", // Special punctuation
|
|
202
|
+
"\u0000\u001F\u007F", // Control characters
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
test.each(unicodeTestCases)(
|
|
206
|
+
'should handle Unicode text "%s" correctly',
|
|
207
|
+
(original) => {
|
|
208
|
+
const encrypted = encrypt(original);
|
|
209
|
+
const decrypted = decrypt(encrypted);
|
|
210
|
+
|
|
211
|
+
expect(decrypted).toBe(original);
|
|
212
|
+
expect(encrypted).not.toBe(original);
|
|
213
|
+
},
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("Large data handling", () => {
|
|
218
|
+
test("should handle long strings", () => {
|
|
219
|
+
const longString = "a".repeat(10000); // 10KB string
|
|
220
|
+
const encrypted = encrypt(longString);
|
|
221
|
+
const decrypted = decrypt(encrypted);
|
|
222
|
+
|
|
223
|
+
expect(decrypted).toBe(longString);
|
|
224
|
+
expect(encrypted.length).toBeGreaterThan(longString.length);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("should handle API key formats", () => {
|
|
228
|
+
const apiKeyFormats = [
|
|
229
|
+
"fdsfgdsfgdsfgsfgdasdfasfdasfdasdfs",
|
|
230
|
+
"sk-asfdsadfasdfasdfasdfasfdasfdasasdfasdfas",
|
|
231
|
+
"afdssadfsafd-1234567890123-abcdefghijklmnopqrstuvwx",
|
|
232
|
+
"afdasfdasfdfdasdfsqwrewq55e4r65gh4g65hj4g32h4ty",
|
|
233
|
+
];
|
|
234
|
+
|
|
235
|
+
apiKeyFormats.forEach((apiKey) => {
|
|
236
|
+
const encrypted = encrypt(apiKey);
|
|
237
|
+
const decrypted = decrypt(encrypted);
|
|
238
|
+
|
|
239
|
+
expect(decrypted).toBe(apiKey);
|
|
240
|
+
expect(encrypted).not.toContain(apiKey.substring(10, 30)); // Should not contain readable parts
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe("Security features", () => {
|
|
246
|
+
test("should use different salts for same input", () => {
|
|
247
|
+
const input = "sk-test123";
|
|
248
|
+
const encrypted1 = encrypt(input);
|
|
249
|
+
const encrypted2 = encrypt(input);
|
|
250
|
+
|
|
251
|
+
expect(encrypted1).not.toBe(encrypted2);
|
|
252
|
+
|
|
253
|
+
// Both should decrypt to same original
|
|
254
|
+
expect(decrypt(encrypted1)).toBe(input);
|
|
255
|
+
expect(decrypt(encrypted2)).toBe(input);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("should produce base64-safe output", () => {
|
|
259
|
+
const testInputs = ["test", "sk-123", "한글", "🚀"];
|
|
260
|
+
|
|
261
|
+
testInputs.forEach((input) => {
|
|
262
|
+
const encrypted = encrypt(input);
|
|
263
|
+
// Should be valid base64 (only contains valid base64 characters)
|
|
264
|
+
expect(encrypted).toMatch(/^[A-Za-z0-9+/]*={0,2}$/);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("encrypted data should not contain original text patterns", () => {
|
|
269
|
+
const sensitiveData = [
|
|
270
|
+
"sk-1234567890abcdef",
|
|
271
|
+
"password123",
|
|
272
|
+
"secret-api-key",
|
|
273
|
+
];
|
|
274
|
+
|
|
275
|
+
sensitiveData.forEach((data) => {
|
|
276
|
+
const encrypted = encrypt(data);
|
|
277
|
+
|
|
278
|
+
// Encrypted should not contain recognizable patterns from original
|
|
279
|
+
expect(encrypted.toLowerCase()).not.toContain(
|
|
280
|
+
data.substring(3, 10).toLowerCase(),
|
|
281
|
+
);
|
|
282
|
+
expect(encrypted).not.toContain(data.substring(0, 5));
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
});
|