Skip to content

Commit

Permalink
Merge pull request #1539 from qdraw/feature/202404_desktop_codesmells_9
Browse files Browse the repository at this point in the history
code smells desktop 9
  • Loading branch information
qdraw authored Oct 7, 2024
2 parents ae6a387 + 3bf045c commit c7b1dba
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 37 deletions.
2 changes: 1 addition & 1 deletion starskydesktop/src/app/child-process/spawn-clean-mac-os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function SpawnCleanMacOs(appStarskyPath: string, processPlatform: string)
.then(() => {
resolve(true);
})
.catch((err) => {
.catch((err: Error) => {
reject(err);
});
});
Expand Down
34 changes: 34 additions & 0 deletions starskydesktop/src/app/config/url-regex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ipRegex } from "./url-regex";

describe("ipRegex", () => {
test("Should match valid URLs with IP addresses", () => {
expect(ipRegex.test("https://255.255.255.255")).toBe(true);
});

test("Should not match invalid URLs with IP addresses", () => {
expect(ipRegex.test("http://192.168.0.1")).toBe(false);
expect(ipRegex.test("http://123.45.67.89:8080")).toBe(true);
expect(ipRegex.test("http://0.0.0.0:12345/path")).toBe(false);

expect(ipRegex.test("http://300.168.0.1")).toBe(false); // Out of range octet
expect(ipRegex.test("https://192.168.1")).toBe(false); // Incomplete IP address
expect(ipRegex.test("http://192.168.0.1.")).toBe(false); // Extra dot at the end
expect(ipRegex.test("http://192.168.0.1:")).toBe(true); // Colon without port number
expect(ipRegex.test("http://192.168.0.1/path?query")).toBe(false); // Path and query string
});

test("Should match valid URLs with domain names", () => {
expect(ipRegex.test("http://example.com")).toBe(false);
expect(ipRegex.test("https://sub.domain.com")).toBe(false);
expect(ipRegex.test("http://www.example123.net:8080")).toBe(false);
expect(ipRegex.test("http://www.example.com/path")).toBe(false);
});

test("Should not match invalid URLs with domain names", () => {
expect(ipRegex.test("http://example")).toBe(false); // No top-level domain
expect(ipRegex.test("https://.example.com")).toBe(false); // Empty subdomain
expect(ipRegex.test("http://example..com")).toBe(false); // Double dot in domain name
expect(ipRegex.test("http://example.com:")).toBe(false); // Colon without port number
expect(ipRegex.test("http://example.com/path?query")).toBe(false); // Path and query string
});
});
28 changes: 11 additions & 17 deletions starskydesktop/src/app/edit-file/download-binary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,12 @@ jest.mock("electron-settings", () => {

describe("downloadBinary", () => {
it("should download and fail", async () => {
jest
.spyOn(GetParentDiskPath, "GetParentDiskPath")
.mockImplementationOnce(() => {
return Promise.resolve("test");
});
jest
.spyOn(downloadNetRequest, "downloadNetRequest")
.mockImplementationOnce(() => {
return Promise.resolve("test");
});
jest.spyOn(GetParentDiskPath, "GetParentDiskPath").mockImplementationOnce(() => {
return Promise.resolve("test");
});
jest.spyOn(downloadNetRequest, "downloadNetRequest").mockImplementationOnce(() => {
return Promise.resolve("test");
});

const result = await downloadBinary(
{
Expand All @@ -57,19 +53,17 @@ describe("downloadBinary", () => {
});

it("should download and fail 2", async () => {
jest
.spyOn(GetParentDiskPath, "GetParentDiskPath")
.mockImplementationOnce(() => {
return Promise.resolve("test");
});
jest.spyOn(GetParentDiskPath, "GetParentDiskPath").mockImplementationOnce(() => {
return Promise.resolve("test");
});
const downloadSpy = jest
.spyOn(downloadNetRequest, "downloadNetRequest")
.mockClear()
.mockImplementationOnce(() => {
return Promise.reject("test");
return Promise.reject(new Error("test"));
})
.mockImplementationOnce(() => {
return Promise.resolve("test");
return Promise.resolve(new Error("test"));
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Expand Down
5 changes: 3 additions & 2 deletions starskydesktop/src/app/edit-file/edit-file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BrowserWindow } from "electron";
import { IFileIndexItem } from "src/shared/IFileindexItem";
import { IDetailView } from "../../shared/IDetailView";
import { IFileIndexItem } from "../../shared/IFileindexItem";
import { GetBaseUrlFromSettings } from "../config/get-base-url-from-settings";
import UrlQuery from "../config/url-query";
import { createErrorWindow } from "../error-window/create-error-window";
Expand Down Expand Up @@ -48,7 +49,7 @@ export async function EditFile(fromMainWindow: BrowserWindow) {
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-explicit-any
const fileIndexItem = (result.data as any).fileIndexItem as IFileIndexItem;
const fileIndexItem = (result.data as unknown as IDetailView).fileIndexItem as IFileIndexItem;

await createParentFolders(fileIndexItem.parentDirectory);

Expand Down
66 changes: 66 additions & 0 deletions starskydesktop/src/app/edit-file/is-detail-view-result.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { IGetNetRequestResponse } from "../net-request/get-net-request";
import { IsDetailViewResult } from "./is-detail-view-result";

describe("IsDetailViewResult", () => {
it("should return true for valid result", () => {
const validResult: IGetNetRequestResponse = {
statusCode: 200,
data: {
fileIndexItem: {
status: "Ok",
collectionPaths: [],
sidecarExtensionsList: [],
},
},
};
expect(IsDetailViewResult(validResult)).toBe(true);
});

it("should return false if statusCode is not 200", () => {
const invalidStatusCode: IGetNetRequestResponse = {
statusCode: 404,
data: {
fileIndexItem: {
status: "Ok",
collectionPaths: [],
sidecarExtensionsList: [],
},
},
};
expect(IsDetailViewResult(invalidStatusCode)).toBe(false);
});

it("should return false if any required property is missing", () => {
const missingData: IGetNetRequestResponse = {
statusCode: 200,
data: undefined,
};
const missingFileIndexItem: IGetNetRequestResponse = {
statusCode: 200,
data: {},
};
const missingStatus: IGetNetRequestResponse = {
statusCode: 200,
data: {
fileIndexItem: {},
},
};
expect(IsDetailViewResult(missingData)).toBeUndefined();
expect(IsDetailViewResult(missingFileIndexItem)).toBeUndefined();
expect(IsDetailViewResult(missingStatus)).toBeUndefined();
});

it('should return false if status is not "Ok", "OkAndSame", or "Default"', () => {
const invalidStatus: IGetNetRequestResponse = {
statusCode: 200,
data: {
fileIndexItem: {
status: "InvalidStatus",
collectionPaths: [],
sidecarExtensionsList: [],
},
},
};
expect(IsDetailViewResult(invalidStatus)).toBe(false);
});
});
15 changes: 6 additions & 9 deletions starskydesktop/src/app/edit-file/is-detail-view-result.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { IGetNetRequestResponse } from "../net-request/get-net-request";

export function IsDetailViewResult(result: IGetNetRequestResponse) {
return (
result.statusCode === 200
&& result.data
&& result.data.fileIndexItem
&& result.data.fileIndexItem.status
&& result.data.fileIndexItem.collectionPaths
&& result.data.fileIndexItem.sidecarExtensionsList
&& (result.data.fileIndexItem.status === "Ok"
|| result.data.fileIndexItem.status === "OkAndSame"
|| result.data.fileIndexItem.status === "Default")
&& result.data?.fileIndexItem?.status
&& result.data?.fileIndexItem?.collectionPaths
&& result.data?.fileIndexItem?.sidecarExtensionsList
&& ["Ok", "OkAndSame", "Default"].includes(result.data.fileIndexItem.status)
);
}
2 changes: 1 addition & 1 deletion starskydesktop/src/app/ipc-bridge/ipc-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function LocationIsRemoteCallback(event: Electron.IpcMainEvent, arg
}

export function AppVersionCallback(event: Electron.IpcMainEvent) {
const appVersion = app.getVersion().match(/^[0-9]+\.[0-9]+/gi);
const appVersion = app.getVersion().match(/^\d+\.\d+/gi);

event.reply(AppVersionIpcKey, appVersion);
}
Expand Down
14 changes: 7 additions & 7 deletions starskydesktop/src/app/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ const winstonLogger = winston.createLogger({
defaultMeta: { service: "app" },
transports: [
new winston.transports.Console({
level: "info"
level: "info",
}),
new winston.transports.Console({
level: "warn"
level: "warn",
}),
new winston.transports.File({
dirname: path.join(electronCacheLocation(), "logs"),
filename: `${today}_app_combined.log`
})
]
filename: `${today}_app_combined.log`,
}),
],
});

// eslint-disable-next-line @typescript-eslint/naming-convention
class logger {
class Logger {
static info(message: unknown, ...meta: unknown[]) {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
Expand All @@ -46,4 +46,4 @@ class logger {
}
}

export default logger;
export default Logger;
27 changes: 27 additions & 0 deletions starskydesktop/src/shared/IArchive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { IRelativeObjects, PageType } from "./IDetailView";
import { IFileIndexItem } from "./IFileindexItem";

export interface IArchive {
breadcrumb: Array<string>;
pageType: PageType;
fileIndexItems: Array<IFileIndexItem>;
relativeObjects: IRelativeObjects;
subPath: string;
colorClassActiveList: Array<number>;
colorClassUsage: Array<number>;
collectionsCount: number;
isReadOnly: boolean;
searchQuery?: string;
collections?: boolean;
dateCache: number;
sort?: SortType;
}

export enum SortType {
fileName = "fileName",
imageFormat = "imageFormat",
}

export function newIArchive(): IArchive {
return {} as IArchive;
}
47 changes: 47 additions & 0 deletions starskydesktop/src/shared/IDetailView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { IFileIndexItem } from "./IFileindexItem";

export enum PageType {
Loading = "Loading",
Archive = "Archive",
DetailView = "DetailView",
Search = "Search",
ApplicationException = "ApplicationException",
NotFound = "NotFound",
Unauthorized = "Unauthorized",
Trash = "Trash",
}

export interface IRelativeObjects {
nextFilePath: string;
prevFilePath: string;
nextHash: string;
prevHash: string;
args: Array<string>;
}

export function newIRelativeObjects(): IRelativeObjects {
return {
nextFilePath: "",
prevFilePath: "",
nextHash: "",
prevHash: "",
args: new Array<string>(),
} as IRelativeObjects;
}

export interface IDetailView {
breadcrumb: [];
pageType: PageType;
fileIndexItem: IFileIndexItem;
relativeObjects: IRelativeObjects;
subPath: string;
colorClassActiveList: Array<number>;
lastUpdated?: Date;
isReadOnly: boolean;
collections?: boolean;
dateCache: number;
}

export function newDetailView(): IDetailView {
return { pageType: PageType.DetailView } as IDetailView;
}

0 comments on commit c7b1dba

Please sign in to comment.