mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-17 23:08:36 +02:00
JEST: Add Jest test to test migration from v2 to v3 (#2305)
This commit is contained in:
@@ -14,5 +14,17 @@ export default class FixJSDOMEnvironment extends JSDOMEnvironment {
|
|||||||
// Wrap the construction of the function in eval, so that transpilers
|
// Wrap the construction of the function in eval, so that transpilers
|
||||||
// don't touch the import() call.
|
// don't touch the import() call.
|
||||||
this.global.importActual = eval("url => import(url)");
|
this.global.importActual = eval("url => import(url)");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* By default, Jest only passes a limited number of global objects to the test environment. We need these global
|
||||||
|
* objects when testing code that loads save data.
|
||||||
|
*/
|
||||||
|
this.global.Uint8Array = Uint8Array;
|
||||||
|
this.global.Blob = Blob;
|
||||||
|
this.global.CompressionStream = CompressionStream;
|
||||||
|
this.global.DecompressionStream = DecompressionStream;
|
||||||
|
this.global.TextDecoderStream = TextDecoderStream;
|
||||||
|
this.global.URL = URL;
|
||||||
|
this.global.Response = Response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
test/jest/Migration/Migration.test.ts
Normal file
40
test/jest/Migration/Migration.test.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { Player } from "@player";
|
||||||
|
import fs from "node:fs";
|
||||||
|
import type { ScriptFilePath } from "../../../src/Paths/ScriptFilePath";
|
||||||
|
import { loadGame } from "../../../src/SaveObject";
|
||||||
|
import * as db from "../../../src/db";
|
||||||
|
import * as FileUtils from "../../../src/utils/FileUtils";
|
||||||
|
|
||||||
|
describe("v3", () => {
|
||||||
|
test("v2.8.1 to v3.0.0", async () => {
|
||||||
|
const saveData = new Uint8Array(fs.readFileSync("test/jest/Migration/save-files/v2.8.1.gz"));
|
||||||
|
|
||||||
|
// Simulate loading the data in IndexedDB
|
||||||
|
const mockedLoad = jest.spyOn(db, "load");
|
||||||
|
/**
|
||||||
|
* We must use structuredClone(saveData) instead of saveData; otherwise, the check of mockedDownload won't catch
|
||||||
|
* wrong changes in evaluateVersionCompatibility (e.g., unexpectedly mutating saveData before passing it to
|
||||||
|
* downloadContentAsFile).
|
||||||
|
*/
|
||||||
|
mockedLoad.mockReturnValue(Promise.resolve(structuredClone(saveData)));
|
||||||
|
|
||||||
|
const mockedDownload = jest.spyOn(FileUtils, "downloadContentAsFile");
|
||||||
|
|
||||||
|
await loadGame(await db.load());
|
||||||
|
|
||||||
|
// Check if auto-migration works
|
||||||
|
expect(
|
||||||
|
Player.getHomeComputer()
|
||||||
|
.scripts.get("a.js" as ScriptFilePath)
|
||||||
|
?.code.includes("ns.ui.openTail()"),
|
||||||
|
).toStrictEqual(true);
|
||||||
|
if (!Player.corporation) {
|
||||||
|
throw new Error("The save file does not have corporation data");
|
||||||
|
}
|
||||||
|
expect(Object.keys(Player.corporation.upgrades).includes("DreamSense")).toStrictEqual(false);
|
||||||
|
expect(Player.corporation.funds).toStrictEqual(110e9);
|
||||||
|
|
||||||
|
// Check if evaluateVersionCompatibility correctly loads the data in IndexedDB and passes it to downloadContentAsFile
|
||||||
|
expect(mockedDownload).toHaveBeenCalledWith(saveData, "bitburnerSave_backup_2.8.1_1756913326.json.gz");
|
||||||
|
});
|
||||||
|
});
|
||||||
BIN
test/jest/Migration/save-files/v2.8.1.gz
Normal file
BIN
test/jest/Migration/save-files/v2.8.1.gz
Normal file
Binary file not shown.
Reference in New Issue
Block a user