Skip to content

Commit

Permalink
refactor: cleanup types (#255)
Browse files Browse the repository at this point in the history
## Motivation

removes all instances of lazy typing (`any`)
  • Loading branch information
pixelass authored May 1, 2024
1 parent 84f43d9 commit 5d01fe8
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 104 deletions.
13 changes: 10 additions & 3 deletions jest.setup.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import "@testing-library/jest-dom";
// Define an interface for the mock if TypeScript doesn't include CSS or is missing properties/methods you need
interface CSSMock {
escape: (input: string) => string;
}

// Assigning the mock to global.CSS with proper typing
if (typeof CSS === "undefined") {
global.CSS = {
const mockCSS: CSSMock = {
escape: (string_: string) => string_.replaceAll(/([()\\{}])/g, "\\$1"),
} as any; // Cast to 'any' to bypass TypeScript's type checking for the mock.
};

// Extend the existing global interface (if necessary) and assign the mock
global.CSS = mockCSS as typeof CSS;
}
120 changes: 120 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions src/client/apps/story/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import Box from "@mui/joy/Box";
import Button from "@mui/joy/Button";
import Sheet from "@mui/joy/Sheet";
import { useAtom } from "jotai/index";
import type { CSSProperties } from "react";
import type { CSSProperties, RefCallback } from "react";
import { useMemo } from "react";
import { useState } from "react";
import { forwardRef, type LegacyRef, type ReactNode, useCallback, useRef } from "react";
import type { ScrollbarProps } from "react-custom-scrollbars";
import Scrollbars from "react-custom-scrollbars";
import AutoSizer from "react-virtualized-auto-sizer";
import { FixedSizeGrid } from "react-window";
Expand Down Expand Up @@ -172,13 +173,13 @@ export function LegacyCustomScrollbars({
style,
children,
}: {
onScroll?: any;
forwardedRef?: any;
style?: any;
children?: any;
onScroll?: ScrollbarProps["onScroll"];
forwardedRef?: RefCallback<HTMLDivElement>;
style?: CSSProperties;
children?: ReactNode;
}) {
const referenceSetter: LegacyRef<any> = useCallback(
(scrollbarsReference: { view: any }) => {
const referenceSetter: LegacyRef<unknown> = useCallback(
(scrollbarsReference: { view: HTMLDivElement }) => {
if (forwardedRef) {
if (scrollbarsReference) {
forwardedRef(scrollbarsReference.view);
Expand Down Expand Up @@ -214,10 +215,14 @@ export function LegacyCustomScrollbars({
);
}

export const CustomScrollbarsVirtualList = forwardRef<
HTMLDivElement,
{ onScroll: any; forwardedRef: any; style: any; children: any }
>((properties, reference) => <LegacyCustomScrollbars {...properties} forwardedRef={reference} />);
export const CustomScrollbarsVirtualList = forwardRef<HTMLDivElement | undefined>(
(properties, reference) => (
<LegacyCustomScrollbars
{...properties}
forwardedRef={reference as RefCallback<HTMLDivElement>}
/>
)
);

CustomScrollbarsVirtualList.displayName = "CustomScrollbarsVirtualList";

Expand Down
12 changes: 9 additions & 3 deletions src/client/atoms/icons/dynamic.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { SvgIconProps } from "@mui/joy/SvgIcon";
import type { DefaultComponentProps } from "@mui/material/OverridableComponent";
import type { SvgIconProps } from "@mui/material/SvgIcon";
import type { SvgIconTypeMap } from "@mui/material/SvgIcon";
import dynamic from "next/dynamic";
import type { ComponentType } from "react";
import { createElement } from "react";

const Brush = dynamic(() => import("@mui/icons-material/Brush"));
Expand All @@ -17,7 +20,10 @@ const ShoppingBag = dynamic(() => import("@mui/icons-material/ShoppingBag"));
const Stream = dynamic(() => import("@mui/icons-material/Stream"));
const QuestionMark = dynamic(() => import("@mui/icons-material/QuestionMark"));

const iconCache: Record<string, any> = {
const iconCache: Record<
string,
ComponentType<DefaultComponentProps<SvgIconTypeMap<object, "svg">>>
> = {
Brush,
DarkMode,
Dashboard,
Expand All @@ -39,5 +45,5 @@ const iconCache: Record<string, any> = {

export function DynamicIcon({ icon, ...rest }: { icon: string } & SvgIconProps) {
const component = iconCache[icon] ?? null;
return component && createElement(component, rest);
return component && createElement(component, rest as SvgIconProps);
}
6 changes: 2 additions & 4 deletions src/client/ions/handlers/__tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VectorStoreResponse } from "@captn/utils/types";
import type { IPCHandlers, VectorStoreResponse } from "@captn/utils/types";

import { handleCaptainAction } from "../action";

Expand All @@ -10,11 +10,9 @@ jest.mock("#/build-key", () => ({
}));

describe("handleCaptainAction", () => {
// Mocking window.ipc.send
const mockSend = jest.fn();
beforeAll(() => {
// Ensure window.ipc exists
global.window.ipc = { send: mockSend } as any;
global.window.ipc = { send: mockSend } as unknown as IPCHandlers;
});

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/electron/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const handlers = {
send(channel: string, value?: unknown) {
ipcRenderer.send(channel, value);
},
on(channel: string, callback: (...arguments_: any[]) => void) {
function subscription(_event: IpcRendererEvent, ...arguments_: any[]) {
on(channel: string, callback: (...arguments_: unknown[]) => void) {
function subscription(_event: IpcRendererEvent, ...arguments_: unknown[]) {
return callback(...arguments_);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from "node:path";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { env } from "@xenova/transformers";

import { CustomHuggingFaceTransformersEmbeddings } from "../custom-hugging-face-transformers-embeddings";

const originalImplementation = Array.isArray;
// @ts-expect-error we just want to mock this
Array.isArray = jest.fn(type => {
Expand All @@ -16,10 +20,8 @@ Array.isArray = jest.fn(type => {
return originalImplementation(type);
});

import { CustomHuggingFaceTransformersEmbeddings } from "../custom-hugging-face-transformers-embeddings";

describe("CustomHuggingFaceEmbeddings", () => {
let embeddings: any;
let embeddings: CustomHuggingFaceTransformersEmbeddings;

beforeAll(() => {
env.localModelPath = path.join(process.cwd(), "models");
Expand Down Expand Up @@ -48,14 +50,14 @@ describe("CustomHuggingFaceEmbeddings", () => {
expect(results.length).toEqual(texts.length);

// Check each result to ensure it's an array and not empty
for (const [index, embedding] of results.entries()) {
for (const [, embedding] of results.entries()) {
expect(Array.isArray(embedding)).toBeTruthy();
expect(embedding.length).toBeGreaterThan(0);
}
});

describe("without maxTokens defined", () => {
let defaultEmbeddings: any;
let defaultEmbeddings: CustomHuggingFaceTransformersEmbeddings;

beforeAll(() => {
defaultEmbeddings = new CustomHuggingFaceTransformersEmbeddings({
Expand All @@ -79,7 +81,7 @@ describe("CustomHuggingFaceEmbeddings", () => {
expect(Array.isArray(results)).toBeTruthy();
expect(results.length).toEqual(texts.length);

for (const [index, embedding] of results.entries()) {
for (const [, embedding] of results.entries()) {
expect(Array.isArray(embedding)).toBeTruthy();
expect(embedding.length).toBeGreaterThan(0);
}
Expand Down
15 changes: 12 additions & 3 deletions src/electron/helpers/services/__tests__/download-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { unpack } from "@/utils/unpack";

const testDownloadFile = "https://example.com/test.jpg";

interface DownloadEventArguments {
action: DownloadEvent;
}

jest.mock("electron", () => {
const originalModule = jest.requireActual("electron");

Expand Down Expand Up @@ -377,11 +381,16 @@ describe("DownloadManager", () => {
});

expect(
spySend.mock.calls.some((call: any) => call[1].action === DownloadEvent.PROGRESS)
spySend.mock.calls.some(
(call: [string, unknown]) =>
(call[1] as DownloadEventArguments).action === DownloadEvent.PROGRESS
)
).toBe(true);
expect(
spySend.mock.calls.filter((call: any) => call[1].action === DownloadEvent.PROGRESS)
.length
spySend.mock.calls.filter(
(call: [string, unknown]) =>
(call[1] as DownloadEventArguments).action === DownloadEvent.PROGRESS
).length
).toBeGreaterThan(1);
});

Expand Down
Loading

0 comments on commit 5d01fe8

Please sign in to comment.