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
+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.",