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:
Philipp committed 2026-05-16 21:48:56 +02:00
1 parent 5fbc4606d3
commit 05fe8a3dcd
3 files changed
+196 -60

No files matched your search

+149 -28
View File
@@ -11,25 +11,30 @@ vi.mock("@actions/core", () => ({
endGroup: vi.fn(),
}));
// ── mock @actions/artifact so tests never attempt real uploads ──────────────
import * as artifact from "@actions/artifact";
vi.mock("@actions/artifact", () => ({
DefaultArtifactClient: vi.fn().mockImplementation(() => ({
uploadArtifact: vi.fn().mockResolvedValue({}),
})),
DefaultArtifactClient: class {
uploadArtifact() {
return Promise.resolve({});
}
},
}));
import * as core from "@actions/core";
import fs from "fs";
import os from "os";
import path from "path";
import { DefaultArtifactClient } from "@actions/artifact";
import {
isOfficialServer,
buildSubscribePath,
detectTraceback,
detectWarning,
outputMultiline,
buildProgressMessage,
writeLogFile,
uploadLogArtifacts,
handleConsoleEvent,
monitorConsole,
} 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", () => {
it("formats correctly at 0 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
// ────────────────────────────────────────────────────────────────────────────
describe("handleConsoleEvent", () => {
let state;
let stdoutBuffer;
let shardBuffers;
beforeEach(() => {
vi.clearAllMocks();
state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false };
stdoutBuffer = [];
shardBuffers = {};
});
const makeEvent = (log = [], results = [], error = null) => ({
data: { messages: { log, results }, error },
});
it("calls core.info for each stdout line when logToFile=false", () => {
const event = makeEvent(["line1", "line2"]);
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("line1");
expect(core.info).toHaveBeenCalledWith("line2");
expect(stdoutBuffer).toHaveLength(0);
it("calls core.info for each stdout line with shard prefix when logToFile=false", () => {
const event = {
data: {
shard: "shard0",
messages: { log: ["line1"], results: ["line2"] },
},
};
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", () => {
const event = makeEvent(["line1", "line2"]);
handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, state);
const event = {
data: { shard: "shard0", messages: { log: ["line1", "line2"] } },
};
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
expect(core.info).not.toHaveBeenCalled();
expect(stdoutBuffer).toEqual(["line1", "line2"]);
expect(shardBuffers["shard0"]).toEqual(["line1", "line2"]);
});
it("includes results lines in output", () => {
const event = makeEvent([], ["result1"]);
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("result1");
});
it("calls core.error when error field is non-empty", () => {
const event = makeEvent([], [], "Script crashed");
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(core.error).toHaveBeenCalledWith("Script crashed");
expect(state.sawErrorLog).toBe(true);
});
@@ -190,58 +260,73 @@ describe("handleConsoleEvent", () => {
it("sets state.sawTraceback when error contains stack frames", () => {
const error = "TypeError: boom\n at Object.<anonymous> (main:1:1)";
const event = makeEvent([], [], error);
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(state.sawTraceback).toBe(true);
expect(state.sawErrorLog).toBe(true);
});
it("does not set sawTraceback for plain error without stack frames", () => {
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.sawErrorLog).toBe(true);
});
it("sets state.sawWarningLog and calls core.warning for warn lines", () => {
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(core.warning).toHaveBeenCalled();
});
it("calls core.warning regardless of logToFile setting", () => {
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();
});
it("handles missing messages gracefully (no crash on empty event)", () => {
const event = { data: {} };
expect(() =>
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state),
).not.toThrow();
});
it("handles completely empty event gracefully", () => {
const event = {};
expect(() =>
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state),
).not.toThrow();
});
it("filters messages by shard when targetShard is provided", () => {
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();
handleConsoleEvent(event, { shard: "shard0" }, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("msg0");
handleConsoleEvent(event, { shard: "shard0" }, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("[shard0] msg0");
});
it("does not filter when targetShard is undefined", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, {}, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("msg0");
handleConsoleEvent(event, {}, shardBuffers, state);
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.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
@@ -97,4 +97,4 @@ var re=z(20181);var Pe=re.Buffer;function copyProps(i,_){for(var z in i){_[z]=i[
* @license [MIT]{@link https://github.com/archiverjs/node-zip-stream/blob/master/LICENSE}
* @copyright (c) 2014 Chris Talkington, contributors.
*/
var re=z(39023).inherits;var Pe=z(47544).ZipArchiveOutputStream;var $t=z(47544).ZipArchiveEntry;var er=z(53296);var tr=i.exports=function(i){if(!(this instanceof tr)){return new tr(i)}i=this.options=i||{};i.zlib=i.zlib||{};Pe.call(this,i);if(typeof i.level==="number"&&i.level>=0){i.zlib.level=i.level;delete i.level}if(!i.forceZip64&&typeof i.zlib.level==="number"&&i.zlib.level===0){i.store=true}i.namePrependSlash=i.namePrependSlash||false;if(i.comment&&i.comment.length>0){this.setComment(i.comment)}};re(tr,Pe);tr.prototype._normalizeFileData=function(i){i=er.defaults(i,{type:"file",name:null,namePrependSlash:this.options.namePrependSlash,linkname:null,date:null,mode:null,store:this.options.store,comment:""});var _=i.type==="directory";var z=i.type==="symlink";if(i.name){i.name=er.sanitizePath(i.name);if(!z&&i.name.slice(-1)==="/"){_=true;i.type="directory"}else if(_){i.name+="/"}}if(_||z){i.store=true}i.date=er.dateify(i.date);return i};tr.prototype.entry=function(i,_,z){if(typeof z!=="function"){z=this._emitErrorCallback.bind(this)}_=this._normalizeFileData(_);if(_.type!=="file"&&_.type!=="directory"&&_.type!=="symlink"){z(new Error(_.type+" entries not currently supported"));return}if(typeof _.name!=="string"||_.name.length===0){z(new Error("entry name must be a non-empty string value"));return}if(_.type==="symlink"&&typeof _.linkname!=="string"){z(new Error("entry linkname must be a non-empty string value when type equals symlink"));return}var re=new $t(_.name);re.setTime(_.date,this.options.forceLocalTime);if(_.namePrependSlash){re.setName(_.name,true)}if(_.store){re.setMethod(0)}if(_.comment.length>0){re.setComment(_.comment)}if(_.type==="symlink"&&typeof _.mode!=="number"){_.mode=40960}if(typeof _.mode==="number"){if(_.type==="symlink"){_.mode|=40960}re.setUnixMode(_.mode)}if(_.type==="symlink"&&typeof _.linkname==="string"){i=Buffer.from(_.linkname)}return Pe.prototype.entry.call(this,re,i,z)};tr.prototype.finalize=function(){this.finish()}},42078:module=>{module.exports=eval("require")("encoding")},42613:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("assert")},90290:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("async_hooks")},20181:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("buffer")},77969:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("bufferutil")},35317:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("child_process")},64236:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("console")},49140:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("constants")},76982:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("crypto")},31637:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("diagnostics_channel")},24434:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("events")},79896:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("fs")},91943:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("fs/promises")},58611:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("http")},85675:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("http2")},65692:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("https")},69278:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("net")},34589:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:assert")},16698:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:async_hooks")},4573:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:buffer")},37540:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:console")},77598:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:crypto")},53053:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:diagnostics_channel")},40610:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:dns")},78474:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:events")},73024:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs")},51455:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs/promises")},37067:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:http")},32467:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:http2")},44708:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:https")},77030:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:net")},48161:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:os")},76760:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:path")},643:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:perf_hooks")},1708:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:process")},41792:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:queryLine truncated
var re=z(39023).inherits;var Pe=z(47544).ZipArchiveOutputStream;var $t=z(47544).ZipArchiveEntry;var er=z(53296);var tr=i.exports=function(i){if(!(this instanceof tr)){return new tr(i)}i=this.options=i||{};i.zlib=i.zlib||{};Pe.call(this,i);if(typeof i.level==="number"&&i.level>=0){i.zlib.level=i.level;delete i.level}if(!i.forceZip64&&typeof i.zlib.level==="number"&&i.zlib.level===0){i.store=true}i.namePrependSlash=i.namePrependSlash||false;if(i.comment&&i.comment.length>0){this.setComment(i.comment)}};re(tr,Pe);tr.prototype._normalizeFileData=function(i){i=er.defaults(i,{type:"file",name:null,namePrependSlash:this.options.namePrependSlash,linkname:null,date:null,mode:null,store:this.options.store,comment:""});var _=i.type==="directory";var z=i.type==="symlink";if(i.name){i.name=er.sanitizePath(i.name);if(!z&&i.name.slice(-1)==="/"){_=true;i.type="directory"}else if(_){i.name+="/"}}if(_||z){i.store=true}i.date=er.dateify(i.date);return i};tr.prototype.entry=function(i,_,z){if(typeof z!=="function"){z=this._emitErrorCallback.bind(this)}_=this._normalizeFileData(_);if(_.type!=="file"&&_.type!=="directory"&&_.type!=="symlink"){z(new Error(_.type+" entries not currently supported"));return}if(typeof _.name!=="string"||_.name.length===0){z(new Error("entry name must be a non-empty string value"));return}if(_.type==="symlink"&&typeof _.linkname!=="string"){z(new Error("entry linkname must be a non-empty string value when type equals symlink"));return}var re=new $t(_.name);re.setTime(_.date,this.options.forceLocalTime);if(_.namePrependSlash){re.setName(_.name,true)}if(_.store){re.setMethod(0)}if(_.comment.length>0){re.setComment(_.comment)}if(_.type==="symlink"&&typeof _.mode!=="number"){_.mode=40960}if(typeof _.mode==="number"){if(_.type==="symlink"){_.mode|=40960}re.setUnixMode(_.mode)}if(_.type==="symlink"&&typeof _.linkname==="string"){i=Buffer.from(_.linkname)}return Pe.prototype.entry.call(this,re,i,z)};tr.prototype.finalize=function(){this.finish()}},42078:module=>{module.exports=eval("require")("encoding")},42613:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("assert")},90290:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("async_hooks")},20181:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("buffer")},77969:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("bufferutil")},35317:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("child_process")},64236:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("console")},49140:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("constants")},76982:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("crypto")},31637:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("diagnostics_channel")},24434:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("events")},79896:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("fs")},91943:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("fs/promises")},58611:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("http")},85675:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("http2")},65692:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("https")},69278:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("net")},34589:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:assert")},16698:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:async_hooks")},4573:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:buffer")},37540:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:console")},77598:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:crypto")},53053:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:diagnostics_channel")},40610:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:dns")},78474:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:events")},73024:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs")},51455:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs/promises")},37067:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:http")},32467:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:http2")},44708:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:https")},77030:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:net")},48161:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:os")},76760:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:path")},643:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:perf_hooks")},1708:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:process")},41792:i=>{i.exports=__WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:queryLine truncated
+46 -31
View File
@@ -88,17 +88,21 @@ export function safeDecode(text) {
/**
* Outputs text to the action log, splitting by newline to ensure proper display.
* Optionally prepends a [shard] prefix.
*
* @param {string} text
* @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;
const prefix = shard ? `[${shard}] ` : "";
const lines = text.split(/\r?\n/);
lines.forEach((line) => {
if (level === "error") core.error(line);
else if (level === "warning") core.warning(line);
else core.info(line);
const formattedLine = `${prefix}${line}`;
if (level === "error") core.error(formattedLine);
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
* 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} [artifactName="screeps-console-log"] - Artifact display name
* @param {string[]} filePaths - Absolute paths of the files to upload
* @param {string} [artifactName="screeps-console-log"] - Artifact display name
* @returns {Promise<void>}
*/
export async function uploadLogArtifact(
filePath,
export async function uploadLogArtifacts(
filePaths,
artifactName = "screeps-console-log",
) {
if (!filePaths || filePaths.length === 0) return;
try {
const client = new DefaultArtifactClient();
await client.uploadArtifact(
artifactName,
[filePath],
path.dirname(filePath),
);
core.info(`[Monitor] Console log uploaded as artifact '${artifactName}'.`);
const rootDir = path.dirname(filePaths[0]);
await client.uploadArtifact(artifactName, filePaths, rootDir);
core.info(`[Monitor] Console logs uploaded as artifact '${artifactName}'.`);
} catch (err) {
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,
* pushed to stdoutBuffer when logToFile=true.
*
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string } }} event
* @param {{ logToFile: boolean }} opts
* @param {string[]} stdoutBuffer - Mutable buffer used in logToFile mode
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string, shard?: string } }} event
* @param {{ logToFile: boolean, shard?: string }} opts
* @param {Record<string, string[]>} shardBuffers - Mutable buffer Map used in logToFile mode
* @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 data = event?.data ?? {};
@@ -198,7 +200,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines
.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)
@@ -211,9 +213,11 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) {
if (logToFile) {
stdoutBuffer.push(...allStdout);
const shard = data.shard || "default";
if (!shardBuffers[shard]) shardBuffers[shard] = [];
shardBuffers[shard].push(...allStdout);
} 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) {
state.sawErrorLog = true;
const decodedError = safeDecode(errorText);
outputMultiline(decodedError, "error");
outputMultiline(decodedError, "error", data.shard);
if (detectTraceback(decodedError)) {
state.sawTraceback = true;
}
@@ -322,7 +326,7 @@ export async function monitorConsole(api, opts) {
const subscribePath = buildSubscribePath(hostname, providedShard);
// Shared mutable state — updated by handleConsoleEvent via event listener
const stdoutBuffer = [];
const shardBuffers = {}; // { [shardName]: string[] }
const state = {
sawTraceback: false,
sawErrorLog: false,
@@ -336,7 +340,7 @@ export async function monitorConsole(api, opts) {
// Step 2: connect socket + subscribe
await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, stdoutBuffer, state);
handleConsoleEvent(event, opts, shardBuffers, state);
});
core.info(
@@ -378,13 +382,24 @@ export async function monitorConsole(api, opts) {
}
// 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(
path.join(os.tmpdir(), "screeps-monitor-"),
);
const tmpFile = path.join(tmpDir, "screeps_console_log.txt");
await writeLogFile(stdoutBuffer, tmpFile);
await uploadLogArtifact(tmpFile);
const filePaths = [];
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) {
core.info(
"[Monitor] No stdout lines were collected; skipping artifact upload.",