Skip to content

Commit c6e0420

Browse files
add file processing state
1 parent a942149 commit c6e0420

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

src/__tests__/background.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ describe("Background Script", () => {
120120
"Your emojis have been uploaded - please allow 20 minutes for Teams to sync",
121121
error: undefined,
122122
});
123+
// Verify storage state was saved correctly
124+
expect(chrome.storage.local.set).toHaveBeenCalledWith({
125+
processingState: {
126+
status: "Your emojis have been uploaded - please allow 20 minutes for Teams to sync",
127+
type: "success"
128+
}
129+
});
123130
});
124131

125132
it("should handle missing tokens", async () => {
@@ -152,6 +159,13 @@ describe("Background Script", () => {
152159
success: false,
153160
error: "Could not find required tokens",
154161
});
162+
// Verify storage state was saved correctly
163+
expect(chrome.storage.local.set).toHaveBeenCalledWith({
164+
processingState: {
165+
status: "Could not find required tokens",
166+
type: "error"
167+
}
168+
});
155169
});
156170

157171
it("should handle upload errors", async () => {
@@ -193,6 +207,59 @@ describe("Background Script", () => {
193207
success: false,
194208
error: "Upload failed",
195209
});
210+
// Verify storage state was saved correctly
211+
expect(chrome.storage.local.set).toHaveBeenCalledWith({
212+
processingState: {
213+
status: "Upload failed",
214+
type: "error"
215+
}
216+
});
217+
});
218+
219+
// Test that setting initial processing state works
220+
it("should set initial processing state on file processing", async () => {
221+
// Reset the mocks
222+
jest.clearAllMocks();
223+
224+
// Create test data
225+
const mockFiles: FileDetails[] = [
226+
{
227+
name: "test.png",
228+
size: 1024,
229+
type: "image/png",
230+
base64: "dGVzdA==", // test in base64
231+
},
232+
];
233+
234+
const mockTokens = {
235+
chatsvcagg: "chat-token",
236+
ic3: "ic3-token",
237+
permissionsId: "permissions-id",
238+
};
239+
240+
// Setup a mock for uploadFiles
241+
const mockUploadFiles = jest.fn().mockResolvedValue({
242+
success: true,
243+
status: "Test status"
244+
});
245+
246+
MsTeamsClient.mockImplementation(() => ({
247+
uploadFiles: mockUploadFiles,
248+
}));
249+
250+
// Call handleFileProcessing
251+
await handleFileProcessing(mockFiles, mockTokens);
252+
253+
// Check if chrome.storage.local.set was called with the initial state
254+
// This verifies our state storage logic works properly
255+
expect(chrome.storage.local.set).toHaveBeenCalledWith(
256+
expect.objectContaining({
257+
processingState: expect.objectContaining({
258+
status: expect.any(String),
259+
type: expect.any(String),
260+
}),
261+
})
262+
);
196263
});
197264
});
198265
});

src/background.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import { ProcessResult, FileDetails } from "./types";
44
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
55
if (message.action === "processFiles") {
66
const files = JSON.parse(message.files) as FileDetails[];
7+
// Set initial processing state
8+
chrome.storage.local.set({ processingState: { status: "Processing...", type: "processing" } });
79
handleFileProcessing(files, message.tokens)
810
.then(sendResponse)
911
.catch((error) => {
1012
const errorMessage = formatErrorMessage(error);
13+
// Store error state
14+
chrome.storage.local.set({ processingState: { status: errorMessage, type: "error" } });
1115
sendResponse({ success: false, error: errorMessage });
1216
});
1317
return true;
@@ -54,7 +58,9 @@ export async function handleFileProcessing(
5458
): Promise<ProcessResult> {
5559
try {
5660
if (!tokens.chatsvcagg || !tokens.ic3 || !tokens.permissionsId) {
57-
throw new Error("Could not find required tokens");
61+
const errorMsg = "Could not find required tokens";
62+
chrome.storage.local.set({ processingState: { status: errorMsg, type: "error" } });
63+
throw new Error(errorMsg);
5864
}
5965
const teams = new MsTeamsClient(
6066
tokens.ic3,
@@ -63,6 +69,14 @@ export async function handleFileProcessing(
6369
);
6470
const result = await teams.uploadFiles(files);
6571

72+
// Store processing result state
73+
chrome.storage.local.set({
74+
processingState: {
75+
status: result.status || "",
76+
type: result.success ? "success" : "error"
77+
}
78+
});
79+
6680
chrome.runtime.sendMessage({
6781
type: "processUpdate",
6882
success: result.success,
@@ -75,6 +89,9 @@ export async function handleFileProcessing(
7589
const errorMessage = formatErrorMessage(error);
7690
const errorResult = { success: false, error: errorMessage };
7791

92+
// Store error state
93+
chrome.storage.local.set({ processingState: { status: errorMessage, type: "error" } });
94+
7895
chrome.runtime.sendMessage({
7996
type: "processUpdate",
8097
...errorResult,

src/popup.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import { ProcessResult, FileDetails } from "./types";
33

44
document.addEventListener("DOMContentLoaded", async () => {
55
try {
6+
// Check for existing processing state
7+
const state = await chrome.storage.local.get("processingState");
8+
if (state.processingState) {
9+
updateStatus(
10+
state.processingState.status,
11+
state.processingState.type as "ready" | "success" | "error" | "processing"
12+
);
13+
}
14+
615
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
716
const tab = tabs[0];
817

@@ -176,10 +185,20 @@ document
176185
chrome.runtime.onMessage.addListener(
177186
(message: { type: string; error: any; status: any; success: any }) => {
178187
if (message.type === "processUpdate") {
179-
updateStatus(
180-
message.error || message.status,
181-
message.success ? "success" : "error",
182-
);
188+
const status = message.error || message.status;
189+
const type = message.success ? "success" : (message.error ? "error" : "processing");
190+
191+
// Update the UI
192+
updateStatus(status, type as "success" | "error" | "processing");
193+
194+
// Ensure we store the state in case it was sent directly from background.ts
195+
// Background should be storing the state, but this is a good fallback
196+
chrome.storage.local.set({
197+
processingState: {
198+
status: status,
199+
type: type
200+
}
201+
});
183202
}
184-
},
203+
}
185204
);

0 commit comments

Comments
 (0)