From 430750e3d4b223f8b09a60b84c8d797664200067 Mon Sep 17 00:00:00 2001 From: vutukuri15 Date: Sun, 9 Mar 2025 20:54:47 -0700 Subject: [PATCH 1/2] Fixed issue with extra colors of image input --- components/ChipDetection.tsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/components/ChipDetection.tsx b/components/ChipDetection.tsx index bcbe689..eace4cc 100644 --- a/components/ChipDetection.tsx +++ b/components/ChipDetection.tsx @@ -16,6 +16,8 @@ const ChipDetection = ({ Record >({}); + const chipColors = ["white", "red", "green", "blue", "black"]; + const requestCameraPermissions = async () => { const cameraPermission = await ImagePicker.requestCameraPermissionsAsync(); return cameraPermission.granted; @@ -99,9 +101,19 @@ const ChipDetection = ({ const cleanJSON = rawContent.replace(/```json|```/g, "").trim(); const parsedData: Record = JSON.parse(cleanJSON); - const filteredData = Object.fromEntries( - Object.entries(parsedData).filter(([_, count]) => count > 0) - ); + const filteredData = Object.entries(parsedData) + .filter(([color]) => chipColors.includes(color)) + .sort( + ([colorA], [colorB]) => + chipColors.indexOf(colorA) - chipColors.indexOf(colorB) + ) + .reduce( + (acc, [color, count]) => { + acc[color] = count; + return acc; + }, + {} as Record + ); setLastDetectedChips(filteredData); updateChipCount(filteredData); From 63ecde6c9994695e9c7e83040a3fd5afd5e8b56a Mon Sep 17 00:00:00 2001 From: vutukuri15 Date: Mon, 10 Mar 2025 12:01:36 -0700 Subject: [PATCH 2/2] Fixed test code --- components/__tests__/ChipDetection.test.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/__tests__/ChipDetection.test.tsx b/components/__tests__/ChipDetection.test.tsx index 2dac26c..543b17d 100644 --- a/components/__tests__/ChipDetection.test.tsx +++ b/components/__tests__/ChipDetection.test.tsx @@ -40,7 +40,7 @@ describe("ChipDetection", () => { }); afterEach(() => { - jest.restoreAllMocks(); // Reset all mocks to prevent test contamination + jest.restoreAllMocks(); }); it("renders correctly", () => { @@ -83,7 +83,11 @@ describe("ChipDetection", () => { fireEvent.press(getByText(/take a photo/i)); await waitFor(() => - expect(mockUpdateChipCount).toHaveBeenCalledWith({ red: 5, green: 3 }) + expect(mockUpdateChipCount).toHaveBeenCalledWith({ + red: 5, + green: 3, + blue: 0, + }) ); }); @@ -144,7 +148,7 @@ describe("ChipDetection", () => { choices: [ { message: { - content: JSON.stringify({ red: 5, green: 3 }), + content: JSON.stringify({ red: 5, green: 3, blue: 0 }), }, }, ], @@ -163,6 +167,7 @@ describe("ChipDetection", () => { expect(mockUpdateChipCount).toHaveBeenCalledWith({ red: 5, green: 3, + blue: 0, }) ); });