Skip to content

Commit

Permalink
Fixed Update-Issues and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealIndianBoi committed Dec 27, 2024
1 parent 33f50f5 commit 189f8e3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 63 deletions.
26 changes: 14 additions & 12 deletions src/components/header/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import { toastStore } from "$lib/stores/ToastStore";
const appWindow = getCurrentWebviewWindow();
let launcherVerison = null;
let launcherVersion: string | null = null;
onMount(async () => {
// Get current versions
launcherVerison = `v${await getVersion()}`;
launcherVersion = `v${await getVersion()}`;
$VersionStore.activeVersionType = await getActiveVersionFolder();
$VersionStore.activeVersionName = await getActiveVersion();
Expand All @@ -36,12 +36,12 @@
if (!isInDebugMode()) {
const updateResult = await check();
if (updateResult.shouldUpdate) {
if (updateResult) {
let changeLog = [];
try {
changeLog = JSON.parse(updateResult.manifest.body);
changeLog = JSON.parse(updateResult.body ?? "");
} catch (e) {
exceptionLog(
await exceptionLog(
`Could not parse changelog JSON from release metadata - ${JSON.stringify(
updateResult,
)}`,
Expand All @@ -50,19 +50,21 @@
}
$UpdateStore.launcher = {
updateAvailable: true,
versionNumber: updateResult.manifest.version,
date: updateResult.manifest.date,
versionNumber: updateResult.version,
date: updateResult.date ?? "",
changeLog: changeLog,
};
infoLog(`Launcher Update Available`);
await infoLog(`Launcher Update Available`);
} else {
$UpdateStore.launcher = {
updateAvailable: false,
versionNumber: null,
date: null,
versionNumber: "",
date: "",
changeLog: [],
};
infoLog(`Launcher is up to date - ${JSON.stringify(updateResult)}`);
await infoLog(
`Launcher is up to date - ${JSON.stringify(updateResult)}`,
);
}
}
Expand Down Expand Up @@ -125,7 +127,7 @@
class="flex flex-col text-neutral-300 mr-2 pointer-events-none max-w-[250px]"
>
<p class="font-mono text-sm truncate-text">
{launcherVerison === null ? "not set!" : launcherVerison}
{launcherVersion === null ? "not set!" : launcherVersion}
</p>
<p class="font-mono text-sm truncate-text">
{$VersionStore.activeVersionName === null
Expand Down
6 changes: 3 additions & 3 deletions src/lib/stores/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { writable } from "svelte/store";
export const UpdateStore = writable({
launcher: {
updateAvailable: false,
versionNumber: undefined,
versionNumber: "",
changeLog: [],
date: undefined,
date: "",
},
selectedTooling: {
updateAvailable: false,
versionNumber: undefined,
versionNumber: "",
},
});
50 changes: 25 additions & 25 deletions src/lib/utils/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
Expand All @@ -13,7 +12,8 @@ import { listOfficialReleases } from "./github";
import { init } from "svelte-i18n";
import { initLocales } from "$lib/i18n/i18n";

vi.mock("@tauri-apps/api/os");
vi.mock("@tauri-apps/plugin-os");

global.fetch = vi.fn();

function createFetchResponse(data: any) {
Expand All @@ -25,7 +25,7 @@ function createFetchResponse(data: any) {
};
}

function createFakeGithubReleaseAsset(assetName) {
function createFakeGithubReleaseAsset(assetName: string) {
return {
url: "https://api.github.com/repos/open-goal/jak-project/releases/assets/115111791",
id: 115111791,
Expand Down Expand Up @@ -148,8 +148,8 @@ describe("listOfficialReleases", () => {
});

it("should retrieve intel macOS releases properly", async () => {
vi.mocked(platform).mockResolvedValue("darwin");
vi.mocked(arch).mockResolvedValue("x86_64");
vi.mocked(platform).mockImplementation(() => "macos");
vi.mocked(arch).mockImplementation(() => "x86_64");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -162,13 +162,13 @@ describe("listOfficialReleases", () => {
const releases = await listOfficialReleases();
expect(releases.length).toBe(1);
expect(
releases[0].downloadUrl.endsWith("opengoal-macos-intel-v0.0.1.tar.gz"),
releases[0].downloadUrl?.endsWith("opengoal-macos-intel-v0.0.1.tar.gz"),
).toBeTruthy();
});

it("should not retrieve macOS ARM releases", async () => {
vi.mocked(platform).mockResolvedValue("darwin");
vi.mocked(arch).mockResolvedValue("arm");
vi.mocked(platform).mockImplementation(() => "macos");
vi.mocked(arch).mockImplementation(() => "arm");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -184,8 +184,8 @@ describe("listOfficialReleases", () => {
});

it("should retrieve windows releases properly", async () => {
vi.mocked(platform).mockResolvedValue("win32");
vi.mocked(arch).mockResolvedValue("x86_64");
vi.mocked(platform).mockImplementation(() => "windows");
vi.mocked(arch).mockImplementation(() => "x86_64");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -198,13 +198,13 @@ describe("listOfficialReleases", () => {
const releases = await listOfficialReleases();
expect(releases.length).toBe(1);
expect(
releases[0].downloadUrl.endsWith("opengoal-windows-v0.0.1.zip"),
releases[0].downloadUrl?.endsWith("opengoal-windows-v0.0.1.zip"),
).toBeTruthy();
});

it("should retrieve linux releases properly", async () => {
vi.mocked(platform).mockResolvedValue("linux");
vi.mocked(arch).mockResolvedValue("x86_64");
vi.mocked(platform).mockImplementation(() => "linux");
vi.mocked(arch).mockImplementation(() => "x86_64");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -217,7 +217,7 @@ describe("listOfficialReleases", () => {
const releases = await listOfficialReleases();
expect(releases.length).toBe(1);
expect(
releases[0].downloadUrl.endsWith("opengoal-linux-v0.0.1.tar.gz"),
releases[0].downloadUrl?.endsWith("opengoal-linux-v0.0.1.tar.gz"),
).toBeTruthy();
});
});
Expand All @@ -229,8 +229,8 @@ describe("getLatestOfficialRelease", () => {
});

it("should retrieve intel macOS releases properly", async () => {
vi.mocked(platform).mockResolvedValue("darwin");
vi.mocked(arch).mockResolvedValue("x86_64");
vi.mocked(platform).mockImplementation(() => "macos");
vi.mocked(arch).mockImplementation(() => "x86_64");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -243,13 +243,13 @@ describe("getLatestOfficialRelease", () => {
const releases = await listOfficialReleases();
expect(releases.length).toBe(1);
expect(
releases[0].downloadUrl.endsWith("opengoal-macos-intel-v0.0.1.tar.gz"),
releases[0].downloadUrl?.endsWith("opengoal-macos-intel-v0.0.1.tar.gz"),
).toBeTruthy();
});

it("should not retrieve macOS ARM releases", async () => {
vi.mocked(platform).mockResolvedValue("darwin");
vi.mocked(arch).mockResolvedValue("arm");
vi.mocked(platform).mockImplementation(() => "macos");
vi.mocked(arch).mockImplementation(() => "arm");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -265,8 +265,8 @@ describe("getLatestOfficialRelease", () => {
});

it("should retrieve windows releases properly", async () => {
vi.mocked(platform).mockResolvedValue("win32");
vi.mocked(arch).mockResolvedValue("x86_64");
vi.mocked(platform).mockImplementation(() => "windows");
vi.mocked(arch).mockImplementation(() => "x86_64");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -279,13 +279,13 @@ describe("getLatestOfficialRelease", () => {
const releases = await listOfficialReleases();
expect(releases.length).toBe(1);
expect(
releases[0].downloadUrl.endsWith("opengoal-windows-v0.0.1.zip"),
releases[0].downloadUrl?.endsWith("opengoal-windows-v0.0.1.zip"),
).toBeTruthy();
});

it("should retrieve linux releases properly", async () => {
vi.mocked(platform).mockResolvedValue("linux");
vi.mocked(arch).mockResolvedValue("x86_64");
vi.mocked(platform).mockImplementation(() => "linux");
vi.mocked(arch).mockImplementation(() => "x86_64");
(fetch as Mock).mockResolvedValue(
createFetchResponse([
createFakeGithubRelease([
Expand All @@ -298,7 +298,7 @@ describe("getLatestOfficialRelease", () => {
const releases = await listOfficialReleases();
expect(releases.length).toBe(1);
expect(
releases[0].downloadUrl.endsWith("opengoal-linux-v0.0.1.tar.gz"),
releases[0].downloadUrl?.endsWith("opengoal-linux-v0.0.1.tar.gz"),
).toBeTruthy();
});
});
43 changes: 26 additions & 17 deletions src/lib/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ function isIntelMacOsRelease(
architecture: string,
assetName: string,
): boolean {
console.log(platform, architecture, assetName);
return (
platform === "darwin" &&
platform === "macos" &&
architecture === "x86_64" &&
assetName.startsWith("opengoal-macos-intel-v")
);
Expand All @@ -33,22 +34,32 @@ function isWindowsRelease(
architecture: string,
assetName: string,
): boolean {
return platform === "win32" && assetName.startsWith("opengoal-windows-v");
return (
platform === "windows" &&
architecture === "x86_64" &&
assetName.startsWith("opengoal-windows-v")
);
}

function isLinuxRelease(
platform: string,
architecture: string,
assetName: string,
): boolean {
return platform === "linux" && assetName.startsWith("opengoal-linux-v");
//TODO: Why testing like this?
// Why not assetName.startsWith('opengoal-{platform}-{arch}-...')
return (
platform === "linux" &&
architecture === "x86_64" &&
assetName.startsWith("opengoal-linux-v")
);
}

async function getDownloadLinkForCurrentPlatform(
release: any,
): Promise<string | undefined> {
const platformName = await platform();
const archName = await arch();
const platformName = platform();
const archName = arch();
for (const asset of release.assets) {
if (isIntelMacOsRelease(platformName, archName, asset.name)) {
return asset.browser_download_url;
Expand Down Expand Up @@ -87,14 +98,13 @@ async function parseGithubRelease(githubRelease: any): Promise<ReleaseInfo> {
releaseInfo.invalidationReasons = ["Release invalid for unknown reasons"];
}
}

return releaseInfo;
}

export async function listOfficialReleases(): Promise<ReleaseInfo[]> {
const nextUrlPattern = /<([\S]+)>; rel="Next"/i;
let releases = [];
let urlToHit =
let urlToHit: string | undefined =
"https://api.github.com/repos/open-goal/jak-project/releases?per_page=100";

while (urlToHit !== undefined) {
Expand All @@ -112,18 +122,17 @@ export async function listOfficialReleases(): Promise<ReleaseInfo[]> {
releases.push(await parseGithubRelease(release));
}

if (
resp.headers.has("link") &&
resp.headers.get("link").includes(`rel=\"next\"`)
) {
// we must paginate!
urlToHit = resp.headers.get("link").match(nextUrlPattern)[1];
} else {
urlToHit = undefined;
}
urlToHit = resp.headers.get("link")?.includes(`rel="next"`)
? resp.headers.get("link")!.match(nextUrlPattern)?.[1]
: undefined;
}

return releases.sort((a, b) => b.date.localeCompare(a.date));
return releases.sort((a, b) => {
if (!a.date && !b.date) return 0;
if (!a.date) return 1;
if (!b.date) return -1;
return b.date.localeCompare(a.date);
});
}

export async function getLatestOfficialRelease(): Promise<
Expand Down
8 changes: 2 additions & 6 deletions src/routes/Update.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@
on:click={() => (showChanges = !showChanges)}
>{$_("update_button_viewChangelog")}</Button
>
<Toggle
checked={showDependencyChanges}
color="orange"
on:change={(evt) => {
showDependencyChanges = evt.target.checked;
}}>{$_("update_button_hideDependencyChanges")}</Toggle
<Toggle color="orange" bind:checked={showDependencyChanges}
>{$_("update_button_hideDependencyChanges")}</Toggle
>
</div>
{#if showChanges}
Expand Down

0 comments on commit 189f8e3

Please sign in to comment.