Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 74 additions & 56 deletions apps/cyberstorm-remix/public/cyberstorm-static/scripts/beta-switch.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
const legacyProd = {
protocol: "https://",
hostname: "thunderstore.io",
port: "",
tld: "io",
};
const betaProd = {
protocol: "https://",
hostname: "new.thunderstore.io",
port: "",
tld: "io",
};
const legacyQA = {
protocol: "https://",
hostname: "thunderstore.dev",
port: "",
tld: "dev",
};
const betaQA = {
protocol: "https://",
hostname: "new.thunderstore.dev",
port: "",
tld: "dev",
};
const legacyDev = {
protocol: "http://",
hostname: "thunderstore.temp",
port: "",
tld: "temp",
};
const betaDev = {
protocol: "http://",
hostname: "new.thunderstore.temp",
port: "",
tld: "temp",
};
async function checkBetaRedirect(legacy, beta, goToBetaRoR2) {
const legacyOnlyPages = [
"/settings",
Expand Down Expand Up @@ -124,25 +88,79 @@ async function insertSwitchButton(legacy, beta) {
}
}
}
const legacy = window.location.hostname.endsWith(legacyProd.tld)
? legacyProd
: window.location.hostname.endsWith(legacyQA.tld)
? legacyQA
: legacyDev;
const beta = window.location.hostname.endsWith(betaProd.tld)
? betaProd
: window.location.hostname.endsWith(betaQA.tld)
? betaQA
: betaDev;
async function insertSwitchButtonListener() {
insertSwitchButton(legacy, beta);
document.removeEventListener("DOMContentLoaded", insertSwitchButtonListener);
function hasBrowserGlobals() {
return typeof window !== "undefined" && typeof document !== "undefined";
}
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
insertSwitchButton(legacy, beta);
} else {
document.addEventListener("DOMContentLoaded", insertSwitchButtonListener);
export function initBetaSwitch() {
if (!hasBrowserGlobals()) {
return;
}
const globalWindow = window;
const initFlag = "__thunderstore_beta_switch_initialized__";
if (globalWindow[initFlag]) {
return;
}
globalWindow[initFlag] = true;
const legacyProd = {
protocol: "https://",
hostname: "thunderstore.io",
port: "",
tld: "io",
};
const betaProd = {
protocol: "https://",
hostname: "new.thunderstore.io",
port: "",
tld: "io",
};
const legacyQA = {
protocol: "https://",
hostname: "thunderstore.dev",
port: "",
tld: "dev",
};
const betaQA = {
protocol: "https://",
hostname: "new.thunderstore.dev",
port: "",
tld: "dev",
};
const legacyDev = {
protocol: "http://",
hostname: "thunderstore.localhost",
port: "",
tld: "localhost",
};
const betaDev = {
protocol: "http://",
hostname: "new.thunderstore.localhost",
port: "",
tld: "localhost",
};
const legacy = window.location.hostname.endsWith(legacyProd.tld)
? legacyProd
: window.location.hostname.endsWith(legacyQA.tld)
? legacyQA
: legacyDev;
const beta = window.location.hostname.endsWith(betaProd.tld)
? betaProd
: window.location.hostname.endsWith(betaQA.tld)
? betaQA
: betaDev;
async function insertSwitchButtonListener() {
insertSwitchButton(legacy, beta);
document.removeEventListener(
"DOMContentLoaded",
insertSwitchButtonListener
);
}
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
insertSwitchButton(legacy, beta);
} else {
document.addEventListener("DOMContentLoaded", insertSwitchButtonListener);
}
}
initBetaSwitch();
95 changes: 95 additions & 0 deletions packages/beta-switch/src/__tests__/beta-switch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { describe, expect, it, vi } from "vitest";

type TestWindow = {
location: {
hostname: string;
pathname: string;
assign: (url: string) => void;
};
};

type TestElement = {
tag: string;
attributes: Map<string, string>;
setAttribute: (key: string, value: string) => void;
onclick?: (() => void) | null;
innerHTML: string;
cloneNode: (deep?: boolean) => TestElement;
};

type TestDocument = {
readyState: "complete" | "interactive" | "loading";
createElement: (tag: string) => TestElement;
addEventListener: (type: string, listener: () => void) => void;
removeEventListener: (type: string, listener: () => void) => void;
querySelector: (
selector: string
) => { appendChild: (child: TestElement) => void } | null;
};

describe("@thunderstore/beta-switch", () => {
it("can be imported without window/document (SSR-safe)", async () => {
// Ensure SSR-like environment
const g = globalThis as unknown as { window?: unknown; document?: unknown };
delete g.window;
delete g.document;

await expect(import("../index")).resolves.toBeTruthy();
});

it("inserts switch button into #nimbusBeta when container exists", async () => {
const appended: TestElement[] = [];

const desktopContainer = {
appendChild: (child: TestElement) => appended.push(child),
};

const createElement = (tag: string) => {
const element: TestElement = {
tag,
attributes: new Map<string, string>(),
setAttribute: (k: string, v: string) => element.attributes.set(k, v),
onclick: undefined,
innerHTML: "",
cloneNode: () => ({
...element,
attributes: new Map(element.attributes),
}),
};
return element;
};

const addEventListener = vi.fn();
const removeEventListener = vi.fn();

const g = globalThis as unknown as { window?: unknown; document?: unknown };

const w: TestWindow = {
location: {
hostname: "thunderstore.temp",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test uses outdated hostname. The implementation changed from thunderstore.temp to thunderstore.localhost (beta-switch.js line 130, index.ts line 185), but the test still uses the old value. While the test may still pass due to fallback logic, it's not properly testing the dev environment detection.

hostname: "thunderstore.localhost",
Suggested change
hostname: "thunderstore.temp",
hostname: "thunderstore.localhost",

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

pathname: "/",
assign: vi.fn(),
},
};

const d: TestDocument = {
readyState: "complete",
createElement,
addEventListener:
addEventListener as unknown as TestDocument["addEventListener"],
removeEventListener:
removeEventListener as unknown as TestDocument["removeEventListener"],
querySelector: (selector: string) =>
selector === "#nimbusBeta" ? desktopContainer : null,
};

g.window = w;
g.document = d;

const mod: typeof import("../index") = await import("../index");
mod.initBetaSwitch();

expect(appended.length).toBe(1);
expect(appended[0].tag).toBe("button");
});
});
150 changes: 86 additions & 64 deletions packages/beta-switch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,6 @@ type UrlStructure = {
tld: string;
};

const legacyProd: UrlStructure = {
protocol: "https://",
hostname: "thunderstore.io",
port: "",
tld: "io",
};

const betaProd: UrlStructure = {
protocol: "https://",
hostname: "new.thunderstore.io",
port: "",
tld: "io",
};

const legacyQA: UrlStructure = {
protocol: "https://",
hostname: "thunderstore.dev",
port: "",
tld: "dev",
};

const betaQA: UrlStructure = {
protocol: "https://",
hostname: "new.thunderstore.dev",
port: "",
tld: "dev",
};

const legacyDev: UrlStructure = {
protocol: "http://",
hostname: "thunderstore.temp",
port: "",
tld: "temp",
};

const betaDev: UrlStructure = {
protocol: "http://",
hostname: "new.thunderstore.temp",
port: "",
tld: "temp",
};

async function checkBetaRedirect(
legacy: UrlStructure,
beta: UrlStructure,
Expand Down Expand Up @@ -178,28 +136,92 @@ async function insertSwitchButton(legacy: UrlStructure, beta: UrlStructure) {
}
}

const legacy = window.location.hostname.endsWith(legacyProd.tld)
? legacyProd
: window.location.hostname.endsWith(legacyQA.tld)
? legacyQA
: legacyDev;
const beta = window.location.hostname.endsWith(betaProd.tld)
? betaProd
: window.location.hostname.endsWith(betaQA.tld)
? betaQA
: betaDev;

async function insertSwitchButtonListener() {
insertSwitchButton(legacy, beta);
document.removeEventListener("DOMContentLoaded", insertSwitchButtonListener);
function hasBrowserGlobals(): boolean {
return typeof window !== "undefined" && typeof document !== "undefined";
}

// Run above code
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
insertSwitchButton(legacy, beta);
} else {
document.addEventListener("DOMContentLoaded", insertSwitchButtonListener);
export function initBetaSwitch() {
if (!hasBrowserGlobals()) {
return;
}

const globalWindow = window as unknown as Record<string, unknown>;
const initFlag = "__thunderstore_beta_switch_initialized__";
if (globalWindow[initFlag]) {
return;
}
globalWindow[initFlag] = true;

const legacyProd: UrlStructure = {
protocol: "https://",
hostname: "thunderstore.io",
port: "",
tld: "io",
};

const betaProd: UrlStructure = {
protocol: "https://",
hostname: "new.thunderstore.io",
port: "",
tld: "io",
};

const legacyQA: UrlStructure = {
protocol: "https://",
hostname: "thunderstore.dev",
port: "",
tld: "dev",
};

const betaQA: UrlStructure = {
protocol: "https://",
hostname: "new.thunderstore.dev",
port: "",
tld: "dev",
};

const legacyDev: UrlStructure = {
protocol: "http://",
hostname: "thunderstore.localhost",
port: "",
tld: "localhost",
};

const betaDev: UrlStructure = {
protocol: "http://",
hostname: "new.thunderstore.localhost",
port: "",
tld: "localhost",
};

const legacy = window.location.hostname.endsWith(legacyProd.tld)
? legacyProd
: window.location.hostname.endsWith(legacyQA.tld)
? legacyQA
: legacyDev;
const beta = window.location.hostname.endsWith(betaProd.tld)
? betaProd
: window.location.hostname.endsWith(betaQA.tld)
? betaQA
: betaDev;

async function insertSwitchButtonListener() {
insertSwitchButton(legacy, beta);
document.removeEventListener(
"DOMContentLoaded",
insertSwitchButtonListener
);
}

if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
insertSwitchButton(legacy, beta);
} else {
document.addEventListener("DOMContentLoaded", insertSwitchButtonListener);
}
}

// Backwards compatible: importing the module in the browser initializes it.
initBetaSwitch();
Loading
Loading