Fixed issue with extra colors of image input # 57 #58
@ -16,6 +16,8 @@ const ChipDetection = ({
|
|||||||
Record<string, number>
|
Record<string, number>
|
||||||
>({});
|
>({});
|
||||||
|
|
||||||
|
const chipColors = ["white", "red", "green", "blue", "black"];
|
||||||
|
|
||||||
const requestCameraPermissions = async () => {
|
const requestCameraPermissions = async () => {
|
||||||
const cameraPermission = await ImagePicker.requestCameraPermissionsAsync();
|
const cameraPermission = await ImagePicker.requestCameraPermissionsAsync();
|
||||||
return cameraPermission.granted;
|
return cameraPermission.granted;
|
||||||
@ -99,9 +101,19 @@ const ChipDetection = ({
|
|||||||
const cleanJSON = rawContent.replace(/```json|```/g, "").trim();
|
const cleanJSON = rawContent.replace(/```json|```/g, "").trim();
|
||||||
const parsedData: Record<string, number> = JSON.parse(cleanJSON);
|
const parsedData: Record<string, number> = JSON.parse(cleanJSON);
|
||||||
|
|
||||||
const filteredData = Object.fromEntries(
|
const filteredData = Object.entries(parsedData)
|
||||||
Object.entries(parsedData).filter(([_, count]) => count > 0)
|
.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<string, number>
|
||||||
|
);
|
||||||
|
|
||||||
setLastDetectedChips(filteredData);
|
setLastDetectedChips(filteredData);
|
||||||
updateChipCount(filteredData);
|
updateChipCount(filteredData);
|
||||||
|
@ -40,7 +40,7 @@ describe("ChipDetection", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.restoreAllMocks(); // Reset all mocks to prevent test contamination
|
jest.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders correctly", () => {
|
it("renders correctly", () => {
|
||||||
@ -83,7 +83,11 @@ describe("ChipDetection", () => {
|
|||||||
fireEvent.press(getByText(/take a photo/i));
|
fireEvent.press(getByText(/take a photo/i));
|
||||||
|
|
||||||
await waitFor(() =>
|
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: [
|
choices: [
|
||||||
{
|
{
|
||||||
message: {
|
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({
|
expect(mockUpdateChipCount).toHaveBeenCalledWith({
|
||||||
red: 5,
|
red: 5,
|
||||||
green: 3,
|
green: 3,
|
||||||
|
blue: 0,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user