test: Fix monitor tests artifact mock and timeouts
Lint / pre-commit Linting (push) Successful in 45s
Test / Run Tests (push) Successful in 1m4s

This commit is contained in:
2026-05-16 21:48:56 +02:00
parent 5fbc4606d3
commit 05fe8a3dcd
3 changed files with 196 additions and 60 deletions
+149 -28
View File
@@ -11,25 +11,30 @@ vi.mock("@actions/core", () => ({
endGroup: vi.fn(), endGroup: vi.fn(),
})); }));
// ── mock @actions/artifact so tests never attempt real uploads ────────────── import * as artifact from "@actions/artifact";
vi.mock("@actions/artifact", () => ({ vi.mock("@actions/artifact", () => ({
DefaultArtifactClient: vi.fn().mockImplementation(() => ({ DefaultArtifactClient: class {
uploadArtifact: vi.fn().mockResolvedValue({}), uploadArtifact() {
})), return Promise.resolve({});
}
},
})); }));
import * as core from "@actions/core"; import * as core from "@actions/core";
import fs from "fs"; import fs from "fs";
import os from "os"; import os from "os";
import path from "path"; import path from "path";
import { DefaultArtifactClient } from "@actions/artifact";
import { import {
isOfficialServer, isOfficialServer,
buildSubscribePath, buildSubscribePath,
detectTraceback, detectTraceback,
detectWarning, detectWarning,
outputMultiline,
buildProgressMessage, buildProgressMessage,
writeLogFile, writeLogFile,
uploadLogArtifacts,
handleConsoleEvent, handleConsoleEvent,
monitorConsole, monitorConsole,
} from "../monitor.js"; } from "../monitor.js";
@@ -123,6 +128,33 @@ describe("detectWarning", () => {
}); });
}); });
describe("outputMultiline", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("prefixes lines with shard when provided", () => {
outputMultiline("line1\nline2", "info", "shard0");
expect(core.info).toHaveBeenCalledWith("[shard0] line1");
expect(core.info).toHaveBeenCalledWith("[shard0] line2");
});
it("does not prefix when shard is missing", () => {
outputMultiline("line1", "info");
expect(core.info).toHaveBeenCalledWith("line1");
});
it("uses core.warning for level=warning", () => {
outputMultiline("warn", "warning", "s0");
expect(core.warning).toHaveBeenCalledWith("[s0] warn");
});
it("uses core.error for level=error", () => {
outputMultiline("err", "error", "s0");
expect(core.error).toHaveBeenCalledWith("[s0] err");
});
});
describe("buildProgressMessage", () => { describe("buildProgressMessage", () => {
it("formats correctly at 0 elapsed", () => { it("formats correctly at 0 elapsed", () => {
expect(buildProgressMessage(0, 50)).toBe("[Monitor] 0/50 ticks elapsed..."); expect(buildProgressMessage(0, 50)).toBe("[Monitor] 0/50 ticks elapsed...");
@@ -141,48 +173,86 @@ describe("buildProgressMessage", () => {
}); });
}); });
describe("uploadLogArtifacts", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("instantiates DefaultArtifactClient and calls uploadArtifact", async () => {
const spy = vi.spyOn(
artifact.DefaultArtifactClient.prototype,
"uploadArtifact",
);
await uploadLogArtifacts(
["/path/to/shard0_console_log.txt"],
"custom-name",
);
expect(spy).toHaveBeenCalledWith(
"custom-name",
["/path/to/shard0_console_log.txt"],
"/path/to",
);
});
it("does nothing if filePaths is empty", async () => {
const spy = vi.spyOn(
artifact.DefaultArtifactClient.prototype,
"uploadArtifact",
);
await uploadLogArtifacts([]);
expect(spy).not.toHaveBeenCalled();
});
});
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
// handleConsoleEvent // handleConsoleEvent
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
describe("handleConsoleEvent", () => { describe("handleConsoleEvent", () => {
let state; let state;
let stdoutBuffer; let shardBuffers;
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false }; state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false };
stdoutBuffer = []; shardBuffers = {};
}); });
const makeEvent = (log = [], results = [], error = null) => ({ const makeEvent = (log = [], results = [], error = null) => ({
data: { messages: { log, results }, error }, data: { messages: { log, results }, error },
}); });
it("calls core.info for each stdout line when logToFile=false", () => { it("calls core.info for each stdout line with shard prefix when logToFile=false", () => {
const event = makeEvent(["line1", "line2"]); const event = {
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state); data: {
expect(core.info).toHaveBeenCalledWith("line1"); shard: "shard0",
expect(core.info).toHaveBeenCalledWith("line2"); messages: { log: ["line1"], results: ["line2"] },
expect(stdoutBuffer).toHaveLength(0); },
};
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("[shard0] line1");
expect(core.info).toHaveBeenCalledWith("[shard0] line2");
expect(Object.keys(shardBuffers)).toHaveLength(0);
}); });
it("buffers stdout when logToFile=true; does not call core.info", () => { it("buffers stdout when logToFile=true; does not call core.info", () => {
const event = makeEvent(["line1", "line2"]); const event = {
handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, state); data: { shard: "shard0", messages: { log: ["line1", "line2"] } },
};
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
expect(core.info).not.toHaveBeenCalled(); expect(core.info).not.toHaveBeenCalled();
expect(stdoutBuffer).toEqual(["line1", "line2"]); expect(shardBuffers["shard0"]).toEqual(["line1", "line2"]);
}); });
it("includes results lines in output", () => { it("includes results lines in output", () => {
const event = makeEvent([], ["result1"]); const event = makeEvent([], ["result1"]);
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state); handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("result1"); expect(core.info).toHaveBeenCalledWith("result1");
}); });
it("calls core.error when error field is non-empty", () => { it("calls core.error when error field is non-empty", () => {
const event = makeEvent([], [], "Script crashed"); const event = makeEvent([], [], "Script crashed");
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state); handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(core.error).toHaveBeenCalledWith("Script crashed"); expect(core.error).toHaveBeenCalledWith("Script crashed");
expect(state.sawErrorLog).toBe(true); expect(state.sawErrorLog).toBe(true);
}); });
@@ -190,58 +260,73 @@ describe("handleConsoleEvent", () => {
it("sets state.sawTraceback when error contains stack frames", () => { it("sets state.sawTraceback when error contains stack frames", () => {
const error = "TypeError: boom\n at Object.<anonymous> (main:1:1)"; const error = "TypeError: boom\n at Object.<anonymous> (main:1:1)";
const event = makeEvent([], [], error); const event = makeEvent([], [], error);
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state); handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(state.sawTraceback).toBe(true); expect(state.sawTraceback).toBe(true);
expect(state.sawErrorLog).toBe(true); expect(state.sawErrorLog).toBe(true);
}); });
it("does not set sawTraceback for plain error without stack frames", () => { it("does not set sawTraceback for plain error without stack frames", () => {
const event = makeEvent([], [], "Script error: low CPU"); const event = makeEvent([], [], "Script error: low CPU");
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state); handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(state.sawTraceback).toBe(false); expect(state.sawTraceback).toBe(false);
expect(state.sawErrorLog).toBe(true); expect(state.sawErrorLog).toBe(true);
}); });
it("sets state.sawWarningLog and calls core.warning for warn lines", () => { it("sets state.sawWarningLog and calls core.warning for warn lines", () => {
const event = makeEvent(["<font color='orange'>low energy</font>"]); const event = makeEvent(["<font color='orange'>low energy</font>"]);
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state); handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(state.sawWarningLog).toBe(true); expect(state.sawWarningLog).toBe(true);
expect(core.warning).toHaveBeenCalled(); expect(core.warning).toHaveBeenCalled();
}); });
it("calls core.warning regardless of logToFile setting", () => { it("calls core.warning regardless of logToFile setting", () => {
const event = makeEvent(["<font color='orange'>warn line</font>"]); const event = makeEvent(["<font color='orange'>warn line</font>"]);
handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, state); handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
expect(core.warning).toHaveBeenCalled(); expect(core.warning).toHaveBeenCalled();
}); });
it("handles missing messages gracefully (no crash on empty event)", () => { it("handles missing messages gracefully (no crash on empty event)", () => {
const event = { data: {} }; const event = { data: {} };
expect(() => expect(() =>
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state), handleConsoleEvent(event, { logToFile: false }, shardBuffers, state),
).not.toThrow(); ).not.toThrow();
}); });
it("handles completely empty event gracefully", () => { it("handles completely empty event gracefully", () => {
const event = {}; const event = {};
expect(() => expect(() =>
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state), handleConsoleEvent(event, { logToFile: false }, shardBuffers, state),
).not.toThrow(); ).not.toThrow();
}); });
it("filters messages by shard when targetShard is provided", () => { it("filters messages by shard when targetShard is provided", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } }; const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, { shard: "shard1" }, stdoutBuffer, state); handleConsoleEvent(event, { shard: "shard1" }, shardBuffers, state);
expect(core.info).not.toHaveBeenCalled(); expect(core.info).not.toHaveBeenCalled();
handleConsoleEvent(event, { shard: "shard0" }, stdoutBuffer, state); handleConsoleEvent(event, { shard: "shard0" }, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("msg0"); expect(core.info).toHaveBeenCalledWith("[shard0] msg0");
}); });
it("does not filter when targetShard is undefined", () => { it("does not filter when targetShard is undefined", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } }; const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, {}, stdoutBuffer, state); handleConsoleEvent(event, {}, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("msg0"); expect(core.info).toHaveBeenCalledWith("[shard0] msg0");
});
it("buffers messages separately for different shards", () => {
const event0 = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
const event1 = { data: { shard: "shard1", messages: { log: ["msg1"] } } };
handleConsoleEvent(event0, { logToFile: true }, shardBuffers, state);
handleConsoleEvent(event1, { logToFile: true }, shardBuffers, state);
expect(shardBuffers["shard0"]).toEqual(["msg0"]);
expect(shardBuffers["shard1"]).toEqual(["msg1"]);
});
it("uses 'default' key when shard is missing in event", () => {
const event = { data: { messages: { log: ["msg"] } } };
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
expect(shardBuffers["default"]).toEqual(["msg"]);
}); });
}); });
@@ -519,4 +604,40 @@ describe("monitorConsole", () => {
expect(result.sawTraceback).toBe(true); expect(result.sawTraceback).toBe(true);
expect(result.sawErrorLog).toBe(true); expect(result.sawErrorLog).toBe(true);
}); });
it("creates separate log files for different shards when logToFile=true", async () => {
const api = buildMockApi({ ticks: [100, 101, 102, 103] });
setTimeout(() => {
api._fireConsole({
shard: "shard0",
messages: { log: ["msg0"], results: [] },
error: null,
});
api._fireConsole({
shard: "shard1",
messages: { log: ["msg1"], results: [] },
error: null,
});
}, 50);
// Verify uploadArtifact was called with two files
const spy = vi.spyOn(
artifact.DefaultArtifactClient.prototype,
"uploadArtifact",
);
await monitorConsole(api, {
...BASE_OPTS,
logToFile: true,
});
expect(spy).toHaveBeenCalledWith(
"screeps-console-log",
expect.arrayContaining([
expect.stringContaining("shard0_console_log.txt"),
expect.stringContaining("shard1_console_log.txt"),
]),
expect.any(String),
);
});
}); });
+1 -1
View File
File diff suppressed because one or more lines are too long
+45 -30
View File
@@ -88,17 +88,21 @@ export function safeDecode(text) {
/** /**
* Outputs text to the action log, splitting by newline to ensure proper display. * Outputs text to the action log, splitting by newline to ensure proper display.
* Optionally prepends a [shard] prefix.
* *
* @param {string} text * @param {string} text
* @param {"info"|"warning"|"error"} level * @param {"info"|"warning"|"error"} level
* @param {string} [shard]
*/ */
export function outputMultiline(text, level = "info") { export function outputMultiline(text, level = "info", shard = null) {
if (!text) return; if (!text) return;
const prefix = shard ? `[${shard}] ` : "";
const lines = text.split(/\r?\n/); const lines = text.split(/\r?\n/);
lines.forEach((line) => { lines.forEach((line) => {
if (level === "error") core.error(line); const formattedLine = `${prefix}${line}`;
else if (level === "warning") core.warning(line); if (level === "error") core.error(formattedLine);
else core.info(line); else if (level === "warning") core.warning(formattedLine);
else core.info(formattedLine);
}); });
} }
@@ -125,29 +129,27 @@ export async function writeLogFile(lines, filePath) {
} }
/** /**
* Uploads a file as a named workflow artifact using @actions/artifact. * Uploads one or more files as a named workflow artifact using @actions/artifact.
* Degrades gracefully to core.warning() if the runner does not support the * Degrades gracefully to core.warning() if the runner does not support the
* artifact service (e.g. a bare self-hosted runner without the service configured). * artifact service.
* *
* @param {string} filePath - Absolute path of the file to upload * @param {string[]} filePaths - Absolute paths of the files to upload
* @param {string} [artifactName="screeps-console-log"] - Artifact display name * @param {string} [artifactName="screeps-console-log"] - Artifact display name
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
export async function uploadLogArtifact( export async function uploadLogArtifacts(
filePath, filePaths,
artifactName = "screeps-console-log", artifactName = "screeps-console-log",
) { ) {
if (!filePaths || filePaths.length === 0) return;
try { try {
const client = new DefaultArtifactClient(); const client = new DefaultArtifactClient();
await client.uploadArtifact( const rootDir = path.dirname(filePaths[0]);
artifactName, await client.uploadArtifact(artifactName, filePaths, rootDir);
[filePath], core.info(`[Monitor] Console logs uploaded as artifact '${artifactName}'.`);
path.dirname(filePath),
);
core.info(`[Monitor] Console log uploaded as artifact '${artifactName}'.`);
} catch (err) { } catch (err) {
core.warning( core.warning(
`[Monitor] Could not upload console log as artifact: ${err.message}`, `[Monitor] Could not upload console logs as artifact: ${err.message}`,
); );
} }
} }
@@ -173,12 +175,12 @@ export async function uploadLogArtifact(
* - All stdout lines → core.info() when logToFile=false, * - All stdout lines → core.info() when logToFile=false,
* pushed to stdoutBuffer when logToFile=true. * pushed to stdoutBuffer when logToFile=true.
* *
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string } }} event * @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string, shard?: string } }} event
* @param {{ logToFile: boolean }} opts * @param {{ logToFile: boolean, shard?: string }} opts
* @param {string[]} stdoutBuffer - Mutable buffer used in logToFile mode * @param {Record<string, string[]>} shardBuffers - Mutable buffer Map used in logToFile mode
* @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state * @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state
*/ */
export function handleConsoleEvent(event, opts, stdoutBuffer, state) { export function handleConsoleEvent(event, opts, shardBuffers, state) {
const { logToFile, shard: targetShard } = opts; const { logToFile, shard: targetShard } = opts;
const data = event?.data ?? {}; const data = event?.data ?? {};
@@ -198,7 +200,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i; const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines logLines
.filter((l) => warnPattern.test(l)) .filter((l) => warnPattern.test(l))
.forEach((l) => outputMultiline(safeDecode(l), "warning")); .forEach((l) => outputMultiline(safeDecode(l), "warning", data.shard));
} }
// Traceback detection in log lines (Screeps sometimes sends errors here) // Traceback detection in log lines (Screeps sometimes sends errors here)
@@ -211,9 +213,11 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const allStdout = [...logLines, ...results].map(safeDecode); const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) { if (allStdout.length > 0) {
if (logToFile) { if (logToFile) {
stdoutBuffer.push(...allStdout); const shard = data.shard || "default";
if (!shardBuffers[shard]) shardBuffers[shard] = [];
shardBuffers[shard].push(...allStdout);
} else { } else {
allStdout.forEach((l) => outputMultiline(l, "info")); allStdout.forEach((l) => outputMultiline(l, "info", data.shard));
} }
} }
@@ -221,7 +225,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
if (errorText) { if (errorText) {
state.sawErrorLog = true; state.sawErrorLog = true;
const decodedError = safeDecode(errorText); const decodedError = safeDecode(errorText);
outputMultiline(decodedError, "error"); outputMultiline(decodedError, "error", data.shard);
if (detectTraceback(decodedError)) { if (detectTraceback(decodedError)) {
state.sawTraceback = true; state.sawTraceback = true;
} }
@@ -322,7 +326,7 @@ export async function monitorConsole(api, opts) {
const subscribePath = buildSubscribePath(hostname, providedShard); const subscribePath = buildSubscribePath(hostname, providedShard);
// Shared mutable state — updated by handleConsoleEvent via event listener // Shared mutable state — updated by handleConsoleEvent via event listener
const stdoutBuffer = []; const shardBuffers = {}; // { [shardName]: string[] }
const state = { const state = {
sawTraceback: false, sawTraceback: false,
sawErrorLog: false, sawErrorLog: false,
@@ -336,7 +340,7 @@ export async function monitorConsole(api, opts) {
// Step 2: connect socket + subscribe // Step 2: connect socket + subscribe
await api.socket.connect(); await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => { await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, stdoutBuffer, state); handleConsoleEvent(event, opts, shardBuffers, state);
}); });
core.info( core.info(
@@ -378,13 +382,24 @@ export async function monitorConsole(api, opts) {
} }
// Step 6: artifact upload // Step 6: artifact upload
if (logToFile && stdoutBuffer.length > 0) { const shardKeys = Object.keys(shardBuffers);
if (logToFile && shardKeys.length > 0) {
const tmpDir = await fs.promises.mkdtemp( const tmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), "screeps-monitor-"), path.join(os.tmpdir(), "screeps-monitor-"),
); );
const tmpFile = path.join(tmpDir, "screeps_console_log.txt"); const filePaths = [];
await writeLogFile(stdoutBuffer, tmpFile);
await uploadLogArtifact(tmpFile); for (const sName of shardKeys) {
const fileName =
sName === "default"
? "screeps_console_log.txt"
: `${sName}_console_log.txt`;
const tmpFile = path.join(tmpDir, fileName);
await writeLogFile(shardBuffers[sName], tmpFile);
filePaths.push(tmpFile);
}
await uploadLogArtifacts(filePaths);
} else if (logToFile) { } else if (logToFile) {
core.info( core.info(
"[Monitor] No stdout lines were collected; skipping artifact upload.", "[Monitor] No stdout lines were collected; skipping artifact upload.",