MISC: Make TIX access independent from WSE account (#2342)

This commit is contained in:
catloversg
2025-10-12 07:28:45 +07:00
committed by GitHub
parent bd4e34fae0
commit d421d4fcf9
17 changed files with 278 additions and 67 deletions

View File

@@ -29,8 +29,7 @@ import { installAugmentations } from "../../../src/Augmentation/AugmentationHelp
import { AddToAllServers } from "../../../src/Server/AllServers";
import { Server } from "../../../src/Server/Server";
import { initSourceFiles } from "../../../src/SourceFile/SourceFiles";
import type { NetscriptContext } from "../../../src/Netscript/APIWrapper";
import type { WorkerScript } from "../../../src/Netscript/WorkerScript";
import { getMockedNetscriptContext } from "../Utilities";
jest.mock("../../../src/Faction/Factions", () => ({
Factions: {},
@@ -43,18 +42,9 @@ jest.mock("../../../src/ui/GameRoot", () => ({
},
}));
const mockLogger: (s: string) => void = jest.fn();
const mockCtx: NetscriptContext = {
function: "",
functionPath: "",
workerScript: {
log: (_: string, text: () => string) => {
mockLogger(text());
},
scriptRef: {
dependencies: [],
},
} as unknown as WorkerScript,
};
const mockCtx = getMockedNetscriptContext((_: string, txt: () => string) => {
mockLogger(txt());
});
setPlayer(new PlayerObject());
AddToAllServers(new Server({ hostname: "home" }));

View File

@@ -0,0 +1,133 @@
import { Player } from "@player";
import { getMockedNetscriptContext, getNS, initGameEnvironment, setupBasicTestingEnvironment } from "../Utilities";
import { deleteStockMarket, getDefaultEmptyStockMarket, StockMarket } from "../../../src/StockMarket/StockMarket";
import { Stock } from "../../../src/StockMarket/Stock";
import { buyStock } from "../../../src/StockMarket/BuyingAndSelling";
function expectUninitializedStockMarket(): void {
expect(StockMarket).toStrictEqual(getDefaultEmptyStockMarket());
}
function expectInitializedStockMarket(): void {
const symbols = Object.keys(StockMarket.Orders);
expect(symbols.length).toBeGreaterThan(0);
expect(symbols.includes("ECP")).toStrictEqual(true);
expect(StockMarket["ECorp"] instanceof Stock).toStrictEqual(true);
expect(StockMarket.lastUpdate).toBeGreaterThan(0);
}
function buyShareOfECP(): void {
const eCorpStock = StockMarket["ECorp"];
expect(eCorpStock.playerShares).toStrictEqual(0);
expect(buyStock(eCorpStock, 1, getMockedNetscriptContext(), {})).toStrictEqual(true);
expect(eCorpStock.playerShares).toStrictEqual(1);
}
beforeAll(() => {
initGameEnvironment();
});
beforeEach(() => {
setupBasicTestingEnvironment();
Player.money = 1e100;
deleteStockMarket();
expectUninitializedStockMarket();
});
describe("WSE account and TIX API access", () => {
test("purchaseWseAccount then purchaseTixApi", () => {
const ns = getNS();
// Check if purchaseWseAccount works
expect(Player.hasWseAccount).toStrictEqual(false);
expect(ns.stock.purchaseWseAccount()).toStrictEqual(true);
expect(Player.hasWseAccount).toStrictEqual(true);
expectInitializedStockMarket();
buyShareOfECP();
// Check if purchaseTixApi works
expect(Player.hasTixApiAccess).toStrictEqual(false);
expect(ns.stock.purchaseTixApi()).toStrictEqual(true);
expect(Player.hasTixApiAccess).toStrictEqual(true);
// Check if stock market data is reset
expect(StockMarket["ECorp"].playerShares).toStrictEqual(1);
});
test("purchaseTixApi then purchaseWseAccount", () => {
const ns = getNS();
// Check if purchaseTixApi works
expect(Player.hasTixApiAccess).toStrictEqual(false);
expect(ns.stock.purchaseTixApi()).toStrictEqual(true);
expect(Player.hasTixApiAccess).toStrictEqual(true);
expectInitializedStockMarket();
buyShareOfECP();
// Check if purchaseWseAccount works
expect(Player.hasWseAccount).toStrictEqual(false);
expect(ns.stock.purchaseWseAccount()).toStrictEqual(true);
expect(Player.hasWseAccount).toStrictEqual(true);
// Check if stock market data is reset
expect(StockMarket["ECorp"].playerShares).toStrictEqual(1);
});
});
describe("4S Market Data", () => {
test("purchase4SMarketData", () => {
const ns = getNS();
expect(ns.stock.purchase4SMarketData()).toStrictEqual(false);
ns.stock.purchaseWseAccount();
expect(ns.stock.purchase4SMarketData()).toStrictEqual(true);
expect(Player.has4SData).toStrictEqual(true);
});
test("purchase4SMarketDataTixApi", () => {
const ns = getNS();
expect(() => ns.stock.purchase4SMarketDataTixApi()).toThrow("You don't have TIX API Access");
ns.stock.purchaseTixApi();
expect(ns.stock.purchase4SMarketDataTixApi()).toStrictEqual(true);
expect(Player.has4SDataTixApi).toStrictEqual(true);
});
});
describe("Prestige", () => {
test("soft reset without initializing stock market", () => {
const ns = getNS();
ns.singularity.softReset();
expectUninitializedStockMarket();
});
test("soft reset after initializing stock market", () => {
const ns = getNS();
ns.stock.purchaseTixApi();
expectInitializedStockMarket();
buyShareOfECP();
expect(StockMarket["ECorp"].playerShares).toStrictEqual(1);
ns.singularity.softReset();
expect(StockMarket["ECorp"].playerShares).toStrictEqual(0);
});
test("b1tflum3", () => {
const ns = getNS();
ns.stock.purchaseTixApi();
expectInitializedStockMarket();
buyShareOfECP();
ns.singularity.b1tflum3(1);
expectUninitializedStockMarket();
});
test("b1tflum3 with SF8", () => {
const ns = getNS();
ns.stock.purchaseTixApi();
expectInitializedStockMarket();
buyShareOfECP();
Player.sourceFiles.set(8, 1);
ns.singularity.b1tflum3(1);
expectInitializedStockMarket();
expect(StockMarket["ECorp"].playerShares).toStrictEqual(0);
});
});

View File

@@ -382,6 +382,7 @@ describe("Stock Market Tests", function () {
const stocks: string[] = [];
beforeEach(function () {
expect(deleteStockMarket).not.toThrow();
expect(initStockMarket).not.toThrow();
expect(initSymbolToStockMap).not.toThrow();
});
@@ -413,7 +414,7 @@ describe("Stock Market Tests", function () {
expect(StockMarket).toHaveProperty("storedCycles");
expect(StockMarket["storedCycles"]).toEqual(0);
expect(StockMarket).toHaveProperty("lastUpdate");
expect(StockMarket["lastUpdate"]).toEqual(0);
expect(StockMarket["lastUpdate"]).toBeGreaterThan(0);
expect(StockMarket).toHaveProperty("ticksUntilCycle");
expect(typeof StockMarket["ticksUntilCycle"]).toBe("number");
});
@@ -1069,6 +1070,7 @@ describe("Stock Market Tests", function () {
describe("Order Placing & Processing", function () {
beforeEach(function () {
expect(deleteStockMarket).not.toThrow();
expect(initStockMarket).not.toThrow();
expect(initSymbolToStockMap).not.toThrow();
@@ -1155,6 +1157,7 @@ describe("Stock Market Tests", function () {
let processOrdersRefs: IProcessOrderRefs;
beforeEach(function () {
expect(deleteStockMarket).not.toThrow();
expect(initStockMarket).not.toThrow();
expect(initSymbolToStockMap).not.toThrow();
@@ -1289,6 +1292,7 @@ describe("Stock Market Tests", function () {
});
beforeEach(function () {
expect(deleteStockMarket).not.toThrow();
expect(initStockMarket).not.toThrow();
expect(initSymbolToStockMap).not.toThrow();

View File

@@ -10,6 +10,7 @@ import { initSourceFiles } from "../../src/SourceFile/SourceFiles";
import { FormatsNeedToChange } from "../../src/ui/formatNumber";
import { Router } from "../../src/ui/GameRoot";
import { config } from "../../src/NetscriptJSEvaluator";
import type { NetscriptContext } from "../../src/Netscript/APIWrapper";
declare const importActual: (typeof config)["doImport"];
@@ -71,3 +72,18 @@ export function getNS(): NSFull {
}
return ns;
}
export function getMockedNetscriptContext(
workerScriptLogFunction: (func: string, txt: () => string) => void = () => {},
): NetscriptContext {
return {
function: "",
functionPath: "",
workerScript: {
log: workerScriptLogFunction,
scriptRef: {
dependencies: [],
},
} as unknown as WorkerScript,
};
}