2022-11-29 13:32:08 +01:00
|
|
|
import { startWorkerScript } from "../../../src/NetscriptWorker";
|
2023-04-01 13:45:23 +02:00
|
|
|
import { config as EvaluatorConfig } from "../../../src/NetscriptJSEvaluator";
|
2022-11-29 13:32:08 +01:00
|
|
|
import { Server } from "../../../src/Server/Server";
|
|
|
|
import { RunningScript } from "../../../src/Script/RunningScript";
|
|
|
|
import { AddToAllServers, DeleteServer } from "../../../src/Server/AllServers";
|
|
|
|
import { WorkerScriptStartStopEventEmitter } from "../../../src/Netscript/WorkerScriptStartStopEventEmitter";
|
|
|
|
import { AlertEvents } from "../../../src/ui/React/AlertManager";
|
|
|
|
|
2023-04-01 13:45:23 +02:00
|
|
|
// Replace Blob/ObjectURL functions, because they don't work natively in Jest
|
|
|
|
global.Blob = class extends Blob {
|
|
|
|
code: string;
|
|
|
|
constructor(blobParts?: BlobPart[], options?: BlobPropertyBag) {
|
|
|
|
super();
|
|
|
|
this.code = (blobParts ?? [])[0] + "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
global.URL.revokeObjectURL = function () {};
|
|
|
|
// Critical: We have to overwrite this, otherwise we get Jest's hooked
|
|
|
|
// implementation, which will not work without passing special flags to Node,
|
|
|
|
// and tends to crash even if you do.
|
|
|
|
EvaluatorConfig.doImport = importActual;
|
|
|
|
|
2022-11-29 13:32:08 +01:00
|
|
|
test.each([
|
|
|
|
{
|
|
|
|
name: "NS1 test /w import",
|
|
|
|
expected: ["false home 8", "Script finished running"],
|
|
|
|
scripts: [
|
|
|
|
{
|
|
|
|
name: "import.script",
|
|
|
|
code: `
|
|
|
|
export function getInfo() {
|
|
|
|
return stock.has4SData();
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple_test.script",
|
|
|
|
code: `
|
|
|
|
import { getInfo } from "import.script";
|
|
|
|
|
|
|
|
var access = getInfo();
|
|
|
|
var server = getServer();
|
|
|
|
printf("%s %s %d", access, server.hostname, server.maxRam);
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-04-01 13:45:23 +02:00
|
|
|
{
|
|
|
|
name: "NS2 test /w import",
|
|
|
|
expected: ["false home 8", "Script finished running"],
|
2022-11-29 13:32:08 +01:00
|
|
|
scripts: [
|
2023-04-01 13:45:23 +02:00
|
|
|
{
|
|
|
|
name: "import.js",
|
|
|
|
code: `
|
|
|
|
export function getInfo(ns) {
|
|
|
|
return ns.stock.has4SData();
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple_test.js",
|
|
|
|
code: `
|
|
|
|
import { getInfo } from "./import.js";
|
|
|
|
|
2022-11-29 13:32:08 +01:00
|
|
|
export async function main(ns) {
|
2023-04-01 13:45:23 +02:00
|
|
|
var access = getInfo(ns);
|
2022-11-29 13:32:08 +01:00
|
|
|
var server = ns.getServer();
|
|
|
|
ns.printf("%s %s %d", access, server.hostname, server.maxRam);
|
|
|
|
}
|
2023-04-01 13:45:23 +02:00
|
|
|
`,
|
|
|
|
},
|
2022-11-29 13:32:08 +01:00
|
|
|
],
|
2023-04-01 13:45:23 +02:00
|
|
|
},
|
2022-11-29 13:32:08 +01:00
|
|
|
])("Netscript execution: $name", async function ({ expected: expectedLog, scripts }) {
|
2023-04-01 13:45:23 +02:00
|
|
|
global.URL.createObjectURL = function (blob) {
|
|
|
|
return "data:text/javascript," + encodeURIComponent(blob.code);
|
|
|
|
};
|
|
|
|
|
|
|
|
let server;
|
|
|
|
let eventDelete = () => {};
|
|
|
|
let alertDelete = () => {};
|
2022-11-29 13:32:08 +01:00
|
|
|
try {
|
|
|
|
const alerted = new Promise((resolve) => {
|
|
|
|
alertDelete = AlertEvents.subscribe((x) => resolve(x));
|
|
|
|
});
|
2023-03-06 04:39:42 +01:00
|
|
|
server = new Server({ hostname: "home", adminRights: true, maxRam: 8 });
|
2022-11-29 13:32:08 +01:00
|
|
|
AddToAllServers(server);
|
|
|
|
for (const s of scripts) {
|
|
|
|
expect(server.writeToScriptFile(s.name, s.code)).toEqual({ success: true, overwritten: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
const script = server.scripts[server.scripts.length - 1];
|
|
|
|
expect(script.filename).toEqual(scripts[scripts.length - 1].name);
|
|
|
|
|
2023-03-06 04:39:42 +01:00
|
|
|
const ramUsage = script.getRamUsage(server.scripts);
|
|
|
|
const runningScript = new RunningScript(script, ramUsage as number);
|
2022-11-29 13:32:08 +01:00
|
|
|
expect(startWorkerScript(runningScript, server)).toBeGreaterThan(0);
|
|
|
|
// We don't care about start, so subscribe after that. Await script death.
|
|
|
|
const result = await Promise.race([
|
|
|
|
alerted,
|
|
|
|
new Promise((resolve) => {
|
2023-04-08 03:08:39 +02:00
|
|
|
eventDelete = WorkerScriptStartStopEventEmitter.subscribe(() => {
|
|
|
|
if (!server.runningScripts.includes(runningScript)) {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2022-11-29 13:32:08 +01:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
// If an error alert was thrown, we catch it here.
|
|
|
|
expect(result).toBeNull();
|
|
|
|
expect(runningScript.logs).toEqual(expectedLog);
|
|
|
|
} finally {
|
2023-04-01 13:45:23 +02:00
|
|
|
eventDelete();
|
2022-11-29 13:32:08 +01:00
|
|
|
if (server) DeleteServer(server.hostname);
|
2023-04-01 13:45:23 +02:00
|
|
|
alertDelete();
|
2022-11-29 13:32:08 +01:00
|
|
|
}
|
|
|
|
});
|