@autobe/ui 0.22.1 → 0.23.0
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/lib/components/AutoBeChatMain.d.ts +6 -4
- package/lib/components/AutoBeChatMain.js +204 -56
- package/lib/components/AutoBeChatMain.js.map +1 -1
- package/lib/components/AutoBeChatSidebar.d.ts +36 -0
- package/lib/components/AutoBeChatSidebar.js +227 -0
- package/lib/components/AutoBeChatSidebar.js.map +1 -0
- package/lib/components/AutoBeConfigButton.d.ts +15 -0
- package/lib/components/AutoBeConfigButton.js +33 -0
- package/lib/components/AutoBeConfigButton.js.map +1 -0
- package/lib/components/AutoBeConfigModal.d.ts +59 -0
- package/lib/components/AutoBeConfigModal.js +294 -0
- package/lib/components/AutoBeConfigModal.js.map +1 -0
- package/lib/components/AutoBeStatusButton.d.ts +12 -0
- package/lib/components/AutoBeStatusButton.js +29 -0
- package/lib/components/AutoBeStatusButton.js.map +1 -0
- package/lib/components/AutoBeStatusModal.js +35 -15
- package/lib/components/AutoBeStatusModal.js.map +1 -1
- package/lib/components/common/ActionButton.d.ts +16 -0
- package/lib/components/common/ActionButton.js +115 -0
- package/lib/components/common/ActionButton.js.map +1 -0
- package/lib/components/common/ActionButtonGroup.d.ts +13 -0
- package/lib/components/common/ActionButtonGroup.js +37 -0
- package/lib/components/common/ActionButtonGroup.js.map +1 -0
- package/lib/components/common/AutoBeConfigInput.d.ts +24 -0
- package/lib/components/common/AutoBeConfigInput.js +90 -0
- package/lib/components/common/AutoBeConfigInput.js.map +1 -0
- package/lib/components/common/CompactSessionIndicator.d.ts +16 -0
- package/lib/components/common/CompactSessionIndicator.js +46 -0
- package/lib/components/common/CompactSessionIndicator.js.map +1 -0
- package/lib/components/common/CompactSessionList.d.ts +22 -0
- package/lib/components/common/CompactSessionList.js +40 -0
- package/lib/components/common/CompactSessionList.js.map +1 -0
- package/lib/components/common/index.d.ts +6 -0
- package/lib/components/common/index.js +6 -0
- package/lib/components/common/index.js.map +1 -1
- package/lib/components/events/AutoBeEventGroupMovie.d.ts +6 -0
- package/lib/components/events/AutoBeEventGroupMovie.js +11 -0
- package/lib/components/events/AutoBeEventGroupMovie.js.map +1 -0
- package/lib/components/events/AutoBeEventMovie.js +5 -0
- package/lib/components/events/AutoBeEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeValidateEventMovie.js +1 -3
- package/lib/components/events/AutoBeValidateEventMovie.js.map +1 -1
- package/lib/components/events/common/CollapsibleEventGroup.d.ts +1 -1
- package/lib/components/events/groups/ValidateEventGroup.d.ts +1 -1
- package/lib/components/events/utils/eventGrouper.js.map +1 -1
- package/lib/components/index.d.ts +6 -0
- package/lib/components/index.js +7 -0
- package/lib/components/index.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadBox.js +75 -33
- package/lib/components/upload/AutoBeChatUploadBox.js.map +1 -1
- package/lib/context/AutoBeAgentContext.d.ts +22 -11
- package/lib/context/AutoBeAgentContext.js +127 -11
- package/lib/context/AutoBeAgentContext.js.map +1 -1
- package/lib/context/AutoBeAgentSessionList.d.ts +12 -0
- package/lib/context/AutoBeAgentSessionList.js +37 -0
- package/lib/context/AutoBeAgentSessionList.js.map +1 -0
- package/lib/context/SearchParamsContext.d.ts +10 -0
- package/lib/context/SearchParamsContext.js +29 -0
- package/lib/context/SearchParamsContext.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -1
- package/lib/structure/AutoBeListener.d.ts +6 -0
- package/lib/structure/AutoBeListener.js +21 -4
- package/lib/structure/AutoBeListener.js.map +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.d.ts +35 -0
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js +30 -0
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js.map +1 -0
- package/lib/structure/index.d.ts +1 -0
- package/lib/structure/index.js +1 -0
- package/lib/structure/index.js.map +1 -1
- package/lib/types/config.d.ts +26 -0
- package/lib/types/config.js +14 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +18 -0
- package/lib/types/index.js.map +1 -0
- package/lib/utils/__tests__/crypto.test.d.ts +1 -0
- package/lib/utils/__tests__/crypto.test.js +222 -0
- package/lib/utils/__tests__/crypto.test.js.map +1 -0
- package/lib/utils/__tests__/storage.test.d.ts +1 -0
- package/lib/utils/__tests__/storage.test.js +174 -0
- package/lib/utils/__tests__/storage.test.js.map +1 -0
- package/lib/utils/crypto.d.ts +18 -0
- package/lib/utils/crypto.js +84 -0
- package/lib/utils/crypto.js.map +1 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +2 -0
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/storage.d.ts +29 -0
- package/lib/utils/storage.js +93 -0
- package/lib/utils/storage.js.map +1 -0
- package/package.json +11 -3
- package/src/components/AutoBeChatMain.tsx +329 -131
- package/src/components/AutoBeChatSidebar.tsx +414 -0
- package/src/components/AutoBeConfigButton.tsx +83 -0
- package/src/components/AutoBeConfigModal.tsx +444 -0
- package/src/components/AutoBeStatusButton.tsx +75 -0
- package/src/components/AutoBeStatusModal.tsx +55 -54
- package/src/components/common/ActionButton.tsx +205 -0
- package/src/components/common/ActionButtonGroup.tsx +80 -0
- package/src/components/common/AutoBeConfigInput.tsx +185 -0
- package/src/components/common/CompactSessionIndicator.tsx +73 -0
- package/src/components/common/CompactSessionList.tsx +82 -0
- package/src/components/common/index.ts +6 -0
- package/src/components/events/AutoBeEventGroupMovie.tsx +18 -0
- package/src/components/events/AutoBeEventMovie.tsx +5 -0
- package/src/components/events/AutoBeValidateEventMovie.tsx +7 -9
- package/src/components/events/common/CollapsibleEventGroup.tsx +1 -1
- package/src/components/events/groups/ValidateEventGroup.tsx +1 -1
- package/src/components/events/utils/eventGrouper.tsx +2 -1
- package/src/components/index.ts +6 -0
- package/src/components/upload/AutoBeChatUploadBox.tsx +94 -44
- package/src/context/AutoBeAgentContext.tsx +201 -22
- package/src/context/AutoBeAgentSessionList.tsx +58 -0
- package/src/context/SearchParamsContext.tsx +49 -0
- package/src/index.ts +4 -0
- package/src/structure/AutoBeListener.ts +32 -6
- package/src/structure/IAutoBeAgentSessionStorageStrategy.ts +87 -0
- package/src/structure/index.ts +1 -0
- package/src/types/config.ts +44 -0
- package/src/types/index.ts +1 -0
- package/src/utils/__tests__/crypto.test.ts +286 -0
- package/src/utils/__tests__/storage.test.ts +229 -0
- package/src/utils/crypto.ts +95 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/storage.ts +96 -0
- package/vitest.config.ts +15 -0
|
@@ -5,20 +5,28 @@ import { AutoBeListenerState } from "./AutoBeListenerState";
|
|
|
5
5
|
import { IAutoBeEventGroup } from "./IAutoBeEventGroup";
|
|
6
6
|
|
|
7
7
|
export class AutoBeListener {
|
|
8
|
-
private callback_:
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
private callback_: Set<(eventGroups: IAutoBeEventGroup[]) => Promise<void>>;
|
|
9
|
+
private onEnableCallback_: Set<(value: boolean) => Promise<void>>;
|
|
10
|
+
|
|
11
11
|
private listener_: Required<IAutoBeRpcListener>;
|
|
12
12
|
private events_: List<IAutoBeEventGroup> = new List();
|
|
13
13
|
private dict_: Map<AutoBeEvent.Type, List.Iterator<IAutoBeEventGroup>> =
|
|
14
14
|
new Map();
|
|
15
|
+
private enable_: boolean = false;
|
|
15
16
|
private readonly state_: AutoBeListenerState;
|
|
16
17
|
|
|
17
18
|
public constructor() {
|
|
18
|
-
this.callback_ =
|
|
19
|
+
this.callback_ = new Set();
|
|
20
|
+
this.onEnableCallback_ = new Set();
|
|
19
21
|
|
|
20
22
|
this.state_ = new AutoBeListenerState();
|
|
21
23
|
this.listener_ = {
|
|
24
|
+
enable: async (value) => {
|
|
25
|
+
this.enable_ = value;
|
|
26
|
+
this.onEnableCallback_.forEach((callback) =>
|
|
27
|
+
callback(value).catch(() => {}),
|
|
28
|
+
);
|
|
29
|
+
},
|
|
22
30
|
assistantMessage: async (event) => {
|
|
23
31
|
this.insert(event);
|
|
24
32
|
},
|
|
@@ -222,7 +230,11 @@ export class AutoBeListener {
|
|
|
222
230
|
}
|
|
223
231
|
|
|
224
232
|
public on(callback: (eventGroups: IAutoBeEventGroup[]) => Promise<void>) {
|
|
225
|
-
this.callback_
|
|
233
|
+
this.callback_.add(callback);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
public off(callback: (eventGroups: IAutoBeEventGroup[]) => Promise<void>) {
|
|
237
|
+
this.callback_.delete(callback);
|
|
226
238
|
}
|
|
227
239
|
|
|
228
240
|
public getListener(): Required<IAutoBeRpcListener> {
|
|
@@ -233,6 +245,18 @@ export class AutoBeListener {
|
|
|
233
245
|
return this.state_;
|
|
234
246
|
}
|
|
235
247
|
|
|
248
|
+
public getEnable(): boolean {
|
|
249
|
+
return this.enable_;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
public onEnable(callback: (value: boolean) => Promise<void>) {
|
|
253
|
+
this.onEnableCallback_.add(callback);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public offEnable(callback: (value: boolean) => Promise<void>) {
|
|
257
|
+
this.onEnableCallback_.delete(callback);
|
|
258
|
+
}
|
|
259
|
+
|
|
236
260
|
private accumulate(event: AutoBeEvent) {
|
|
237
261
|
const it: List.Iterator<IAutoBeEventGroup> | undefined = this.dict_.get(
|
|
238
262
|
event.type,
|
|
@@ -258,6 +282,8 @@ export class AutoBeListener {
|
|
|
258
282
|
}
|
|
259
283
|
|
|
260
284
|
private dispatch() {
|
|
261
|
-
this.callback_
|
|
285
|
+
this.callback_.forEach((callback) =>
|
|
286
|
+
callback(this.events_.toJSON()).catch(() => {}),
|
|
287
|
+
);
|
|
262
288
|
}
|
|
263
289
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AutoBeEvent,
|
|
3
|
+
AutoBeHistory,
|
|
4
|
+
IAutoBeTokenUsageJson,
|
|
5
|
+
} from "@autobe/interface";
|
|
6
|
+
import { IAutoBeRpcService } from "@autobe/interface";
|
|
7
|
+
|
|
8
|
+
import { AutoBeListener } from "./AutoBeListener";
|
|
9
|
+
import { IAutoBeEventGroup } from "./IAutoBeEventGroup";
|
|
10
|
+
|
|
11
|
+
export interface IAutoBeAgentSession {
|
|
12
|
+
id: string;
|
|
13
|
+
title: string;
|
|
14
|
+
history: AutoBeHistory[];
|
|
15
|
+
tokenUsage: IAutoBeTokenUsageJson;
|
|
16
|
+
createdAt: Date;
|
|
17
|
+
updatedAt: Date;
|
|
18
|
+
events: IAutoBeEventGroup[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IAutoBeAgentSessionStorageStrategy {
|
|
22
|
+
appendHistory: (
|
|
23
|
+
props: Pick<IAutoBeAgentSession, "id" | "history">,
|
|
24
|
+
) => Promise<void>;
|
|
25
|
+
appendEvent: (
|
|
26
|
+
props: Pick<IAutoBeAgentSession, "id" | "events">,
|
|
27
|
+
) => Promise<void>;
|
|
28
|
+
setTokenUsage: (
|
|
29
|
+
props: Pick<IAutoBeAgentSession, "id" | "tokenUsage">,
|
|
30
|
+
) => Promise<void>;
|
|
31
|
+
getSession: (
|
|
32
|
+
props: Pick<IAutoBeAgentSession, "id">,
|
|
33
|
+
) => Promise<IAutoBeAgentSession>;
|
|
34
|
+
getSessionList: () => Promise<IAutoBeAgentSession[]>;
|
|
35
|
+
deleteSession: (props: Pick<IAutoBeAgentSession, "id">) => Promise<void>;
|
|
36
|
+
editSessionTitle: (
|
|
37
|
+
props: Pick<IAutoBeAgentSession, "id" | "title">,
|
|
38
|
+
) => Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const AutoBeAgentSession_INIT = {
|
|
42
|
+
title: "Untitled",
|
|
43
|
+
history: [],
|
|
44
|
+
events: [],
|
|
45
|
+
tokenUsage: [
|
|
46
|
+
"aggregate",
|
|
47
|
+
"facade",
|
|
48
|
+
"analyze",
|
|
49
|
+
"prisma",
|
|
50
|
+
"interface",
|
|
51
|
+
"test",
|
|
52
|
+
"realize",
|
|
53
|
+
].reduce(
|
|
54
|
+
(acc, cur) => ({
|
|
55
|
+
...acc,
|
|
56
|
+
[cur]: {
|
|
57
|
+
total: 0,
|
|
58
|
+
input: {
|
|
59
|
+
total: 0,
|
|
60
|
+
cached: 0,
|
|
61
|
+
},
|
|
62
|
+
output: {
|
|
63
|
+
total: 0,
|
|
64
|
+
reasoning: 0,
|
|
65
|
+
accepted_prediction: 0,
|
|
66
|
+
rejected_prediction: 0,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
{} as IAutoBeTokenUsageJson,
|
|
71
|
+
),
|
|
72
|
+
} satisfies {
|
|
73
|
+
title: string;
|
|
74
|
+
history: AutoBeHistory[];
|
|
75
|
+
events: AutoBeEvent[];
|
|
76
|
+
tokenUsage: IAutoBeTokenUsageJson;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export interface IGetAutoBeAgentSessionProps<
|
|
80
|
+
T extends Record<string, unknown>,
|
|
81
|
+
> {
|
|
82
|
+
listener: AutoBeListener;
|
|
83
|
+
connect: () => Promise<IAutoBeRpcService>;
|
|
84
|
+
sessionId?: string;
|
|
85
|
+
storageStrategy: IAutoBeAgentSessionStorageStrategy;
|
|
86
|
+
additional?: T;
|
|
87
|
+
}
|
package/src/structure/index.ts
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base configuration interface for AutoBE Contains all the standard
|
|
3
|
+
* configuration fields with proper types
|
|
4
|
+
*/
|
|
5
|
+
export interface IAutoBeBaseConfig {
|
|
6
|
+
/** AI model to use (e.g., gpt-4.1, claude-3-sonnet) */
|
|
7
|
+
aiModel: string;
|
|
8
|
+
|
|
9
|
+
/** Schema model for API generation */
|
|
10
|
+
schemaModel: string;
|
|
11
|
+
|
|
12
|
+
/** Locale for internationalization */
|
|
13
|
+
locale: string;
|
|
14
|
+
|
|
15
|
+
/** OpenAI API key for authentication */
|
|
16
|
+
openApiKey: string;
|
|
17
|
+
|
|
18
|
+
/** Custom base URL for AI service (optional override) */
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
|
|
21
|
+
/** Concurrency limit for requests */
|
|
22
|
+
semaphore: number;
|
|
23
|
+
|
|
24
|
+
/** Whether audio support is enabled */
|
|
25
|
+
supportAudioEnable: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Extended configuration type that includes base config plus additional fields */
|
|
29
|
+
export type IAutoBeConfig = IAutoBeBaseConfig & Record<string, unknown>;
|
|
30
|
+
|
|
31
|
+
/** Partial configuration for optional fields */
|
|
32
|
+
export type IAutoBePartialConfig = Partial<IAutoBeBaseConfig> &
|
|
33
|
+
Record<string, unknown>;
|
|
34
|
+
|
|
35
|
+
/** Default configuration values */
|
|
36
|
+
export const DEFAULT_CONFIG: IAutoBeBaseConfig = {
|
|
37
|
+
aiModel: "gpt-4.1",
|
|
38
|
+
schemaModel: "chatgpt",
|
|
39
|
+
locale: "en",
|
|
40
|
+
openApiKey: "",
|
|
41
|
+
baseUrl: "",
|
|
42
|
+
semaphore: 16,
|
|
43
|
+
supportAudioEnable: false,
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./config";
|
|
@@ -0,0 +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
|
+
});
|