CODEBASE: Validate theme, editor theme, and styles (#1789)

This commit is contained in:
catloversg
2025-01-09 10:20:05 +07:00
committed by GitHub
parent 320c852386
commit 0f9144a059
27 changed files with 969 additions and 213 deletions
+7 -47
View File
@@ -1,25 +1,10 @@
import { installAugmentations } from "../../../src/Augmentation/AugmentationHelpers";
import { blackOpsArray } from "../../../src/Bladeburner/data/BlackOperations";
import { AugmentationName } from "../../../src/Enums";
import { WorkerScript } from "../../../src/Netscript/WorkerScript";
import { NetscriptFunctions, type NSFull } from "../../../src/NetscriptFunctions";
import type { ScriptFilePath } from "../../../src/Paths/ScriptFilePath";
import { PlayerObject } from "../../../src/PersonObjects/Player/PlayerObject";
import { Player, setPlayer } from "../../../src/Player";
import { RunningScript } from "../../../src/Script/RunningScript";
import { GetServerOrThrow, initForeignServers, prestigeAllServers } from "../../../src/Server/AllServers";
import { Player } from "../../../src/Player";
import { GetServerOrThrow } from "../../../src/Server/AllServers";
import { SpecialServers } from "../../../src/Server/data/SpecialServers";
import { initSourceFiles } from "../../../src/SourceFile/SourceFiles";
import { FormatsNeedToChange } from "../../../src/ui/formatNumber";
import { Router } from "../../../src/ui/GameRoot";
function setupBasicTestingEnvironment(): void {
prestigeAllServers();
setPlayer(new PlayerObject());
Player.init();
Player.sourceFiles.set(4, 3);
initForeignServers(Player.getHomeComputer());
}
import { getNS, initGameEnvironment, setupBasicTestingEnvironment } from "./Utilities";
function setNumBlackOpsComplete(value: number): void {
if (!Player.bladeburner) {
@@ -28,37 +13,12 @@ function setNumBlackOpsComplete(value: number): void {
Player.bladeburner.numBlackOpsComplete = value;
}
function getNS(): NSFull {
const home = GetServerOrThrow(SpecialServers.Home);
home.maxRam = 1024;
const filePath = "test.js" as ScriptFilePath;
home.writeToScriptFile(filePath, "");
const script = home.scripts.get(filePath);
if (!script) {
throw new Error("Invalid script");
}
const runningScript = new RunningScript(script, 1024);
const workerScript = new WorkerScript(runningScript, 1, NetscriptFunctions);
const ns = workerScript.env.vars;
if (!ns) {
throw new Error("Invalid NS instance");
}
return ns;
}
// We need to patch this function. Some APIs call it, but it only works properly after the main UI is loaded.
Router.toPage = () => {};
/**
* In src\ui\formatNumber.ts, there are some variables that need to be initialized before other functions can be
* called. We have to call FormatsNeedToChange.emit() to initialize those variables.
*/
FormatsNeedToChange.emit();
initSourceFiles();
const nextBN = 3;
beforeAll(() => {
initGameEnvironment();
});
describe("b1tflum3", () => {
beforeEach(() => {
setupBasicTestingEnvironment();
+144
View File
@@ -0,0 +1,144 @@
import { IStyleSettings, UserInterfaceTheme } from "../../../src/ScriptEditor/NetscriptDefinitions";
import { Settings } from "../../../src/Settings/Settings";
import { defaultStyles } from "../../../src/Themes/Styles";
import { defaultTheme } from "../../../src/Themes/Themes";
import { getNS, initGameEnvironment, setupBasicTestingEnvironment } from "./Utilities";
const themeHexColor = "#abc";
const fontFamily = "monospace";
beforeAll(() => {
initGameEnvironment();
});
describe("setTheme", () => {
beforeEach(() => {
setupBasicTestingEnvironment();
Settings.theme = { ...defaultTheme };
});
describe("Success", () => {
test("Full theme", () => {
const ns = getNS();
const newTheme = ns.ui.getTheme();
newTheme.primary = themeHexColor;
ns.ui.setTheme(newTheme);
const result = ns.ui.getTheme();
expect(result.primary).toStrictEqual(themeHexColor);
expect(result.secondary).toStrictEqual(defaultTheme.secondary);
});
test("Partial theme", () => {
const ns = getNS();
const newTheme = {
primary: themeHexColor,
};
ns.ui.setTheme(newTheme as unknown as UserInterfaceTheme);
const result = ns.ui.getTheme();
expect(result.primary).toStrictEqual(themeHexColor);
expect(result.secondary).toStrictEqual(defaultTheme.secondary);
});
test("Unknown property", () => {
const ns = getNS();
const newTheme = {
primary: themeHexColor,
unknownProperty: themeHexColor,
};
ns.ui.setTheme(newTheme as unknown as UserInterfaceTheme);
const result = ns.ui.getTheme();
expect(result.primary).toStrictEqual(themeHexColor);
expect(result.secondary).toStrictEqual(defaultTheme.secondary);
// "unknownProperty" of newTheme is not changed.
expect(newTheme.unknownProperty).toStrictEqual(themeHexColor);
// "unknownProperty" is ignored when being processed.
expect((result as unknown as { unknownProperty: unknown }).unknownProperty).toBeUndefined();
});
});
describe("Failure", () => {
test("Full theme", () => {
const ns = getNS();
const newTheme = ns.ui.getTheme();
newTheme.primary = "";
ns.ui.setTheme(newTheme);
const result = ns.ui.getTheme();
expect(result.primary).toStrictEqual(defaultTheme.primary);
});
test("Partial theme", () => {
const ns = getNS();
const newTheme = {
primary: "",
};
ns.ui.setTheme(newTheme as unknown as UserInterfaceTheme);
const result = ns.ui.getTheme();
expect(result.primary).toStrictEqual(defaultTheme.primary);
});
});
});
describe("setStyles", () => {
beforeEach(() => {
setupBasicTestingEnvironment();
Settings.styles = { ...defaultStyles };
});
describe("Success", () => {
test("Full styles", () => {
const ns = getNS();
const newStyles = ns.ui.getStyles();
newStyles.fontFamily = fontFamily;
ns.ui.setStyles(newStyles);
const result = ns.ui.getStyles();
expect(result.fontFamily).toStrictEqual(fontFamily);
expect(result.fontSize).toStrictEqual(defaultStyles.fontSize);
});
test("Partial styles", () => {
const ns = getNS();
const newStyles = {
fontFamily: fontFamily,
};
ns.ui.setStyles(newStyles as unknown as IStyleSettings);
const result = ns.ui.getStyles();
expect(result.fontFamily).toStrictEqual(fontFamily);
expect(result.fontSize).toStrictEqual(defaultStyles.fontSize);
});
test("Unknown property", () => {
const ns = getNS();
const newStyles = {
fontFamily: fontFamily,
unknownProperty: themeHexColor,
};
ns.ui.setStyles(newStyles as unknown as IStyleSettings);
const result = ns.ui.getStyles();
expect(result.fontFamily).toStrictEqual(fontFamily);
expect(result.fontSize).toStrictEqual(defaultStyles.fontSize);
// "unknownProperty" of newStyles is not changed.
expect(newStyles.unknownProperty).toStrictEqual(themeHexColor);
// "unknownProperty" is ignored when being processed.
expect((result as unknown as { unknownProperty: unknown }).unknownProperty).toBeUndefined();
});
});
describe("Failure", () => {
test("Full styles", () => {
const ns = getNS();
const newStyles = ns.ui.getStyles();
(newStyles.fontFamily as unknown) = 123;
ns.ui.setStyles(newStyles);
const result = ns.ui.getStyles();
expect(result.fontFamily).toStrictEqual(defaultStyles.fontFamily);
});
test("Partial styles", () => {
const ns = getNS();
const newStyles = {
fontFamily: 123,
};
ns.ui.setStyles(newStyles as unknown as IStyleSettings);
const result = ns.ui.getStyles();
expect(result.fontFamily).toStrictEqual(defaultStyles.fontFamily);
});
});
});
+50
View File
@@ -0,0 +1,50 @@
import { WorkerScript } from "../../../src/Netscript/WorkerScript";
import { NetscriptFunctions, type NSFull } from "../../../src/NetscriptFunctions";
import type { ScriptFilePath } from "../../../src/Paths/ScriptFilePath";
import { PlayerObject } from "../../../src/PersonObjects/Player/PlayerObject";
import { Player, setPlayer } from "../../../src/Player";
import { RunningScript } from "../../../src/Script/RunningScript";
import { GetServerOrThrow, initForeignServers, prestigeAllServers } from "../../../src/Server/AllServers";
import { SpecialServers } from "../../../src/Server/data/SpecialServers";
import { initSourceFiles } from "../../../src/SourceFile/SourceFiles";
import { FormatsNeedToChange } from "../../../src/ui/formatNumber";
import { Router } from "../../../src/ui/GameRoot";
export function initGameEnvironment() {
// We need to patch this function. Some APIs call it, but it only works properly after the main UI is loaded.
Router.toPage = () => {};
/**
* In src\ui\formatNumber.ts, there are some variables that need to be initialized before other functions can be
* called. We have to call FormatsNeedToChange.emit() to initialize those variables.
*/
FormatsNeedToChange.emit();
initSourceFiles();
}
export function setupBasicTestingEnvironment(): void {
prestigeAllServers();
setPlayer(new PlayerObject());
Player.init();
Player.sourceFiles.set(4, 3);
initForeignServers(Player.getHomeComputer());
}
export function getNS(): NSFull {
const home = GetServerOrThrow(SpecialServers.Home);
home.maxRam = 1024;
const filePath = "test.js" as ScriptFilePath;
home.writeToScriptFile(filePath, "");
const script = home.scripts.get(filePath);
if (!script) {
throw new Error("Invalid script");
}
const runningScript = new RunningScript(script, 1024);
const workerScript = new WorkerScript(runningScript, 1, NetscriptFunctions);
const ns = workerScript.env.vars;
if (!ns) {
throw new Error("Invalid NS instance");
}
return ns;
}