Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
fe7c14540e
|
|||
|
b6cef04a9c
|
|||
|
55a9fc027d
|
|||
|
242b03bb29
|
|||
| 84224f3678 | |||
|
172204508b
|
+35
-161
@@ -11,15 +11,12 @@ vi.mock("@actions/core", () => ({
|
|||||||
endGroup: vi.fn(),
|
endGroup: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
import * as artifact from "@actions/artifact";
|
// ── mock @actions/artifact so tests never attempt real uploads ──────────────
|
||||||
vi.mock("@actions/artifact", () => {
|
vi.mock("@actions/artifact", () => ({
|
||||||
const mockClient = {
|
DefaultArtifactClient: vi.fn().mockImplementation(() => ({
|
||||||
uploadArtifact: vi.fn().mockResolvedValue({}),
|
uploadArtifact: vi.fn().mockResolvedValue({}),
|
||||||
};
|
})),
|
||||||
return {
|
}));
|
||||||
create: vi.fn(() => mockClient),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
@@ -31,10 +28,8 @@ import {
|
|||||||
buildSubscribePath,
|
buildSubscribePath,
|
||||||
detectTraceback,
|
detectTraceback,
|
||||||
detectWarning,
|
detectWarning,
|
||||||
outputMultiline,
|
|
||||||
buildProgressMessage,
|
buildProgressMessage,
|
||||||
writeLogFile,
|
writeLogFile,
|
||||||
uploadLogArtifacts,
|
|
||||||
handleConsoleEvent,
|
handleConsoleEvent,
|
||||||
monitorConsole,
|
monitorConsole,
|
||||||
} from "../monitor.js";
|
} from "../monitor.js";
|
||||||
@@ -58,20 +53,20 @@ describe("isOfficialServer", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("buildSubscribePath", () => {
|
describe("buildSubscribePath", () => {
|
||||||
it("returns console for official server (no shard provided)", () => {
|
it("returns shard0/console for official server (no shard provided)", () => {
|
||||||
expect(buildSubscribePath("screeps.com")).toBe("console");
|
expect(buildSubscribePath("screeps.com")).toBe("shard0/console");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns console for private server (no shard provided)", () => {
|
it("returns console for private server (no shard provided)", () => {
|
||||||
expect(buildSubscribePath("builder64")).toBe("console");
|
expect(buildSubscribePath("builder64")).toBe("console");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns console when shard is provided (official)", () => {
|
it("returns <shard>/console when shard is provided (official)", () => {
|
||||||
expect(buildSubscribePath("screeps.com", "shard3")).toBe("console");
|
expect(buildSubscribePath("screeps.com", "shard3")).toBe("shard3/console");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns console when shard is provided (private)", () => {
|
it("returns <shard>/console when shard is provided (private)", () => {
|
||||||
expect(buildSubscribePath("builder64", "myshard")).toBe("console");
|
expect(buildSubscribePath("builder64", "myshard")).toBe("myshard/console");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -128,33 +123,6 @@ 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...");
|
||||||
@@ -173,79 +141,48 @@ describe("buildProgressMessage", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("uploadLogArtifacts", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.clearAllMocks();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("instantiates client and calls uploadArtifact", async () => {
|
|
||||||
await uploadLogArtifacts(
|
|
||||||
["/path/to/shard0_console_log.txt"],
|
|
||||||
"custom-name",
|
|
||||||
);
|
|
||||||
expect(artifact.create().uploadArtifact).toHaveBeenCalledWith(
|
|
||||||
"custom-name",
|
|
||||||
["/path/to/shard0_console_log.txt"],
|
|
||||||
"/path/to",
|
|
||||||
{ continueOnError: true },
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does nothing if filePaths is empty", async () => {
|
|
||||||
await uploadLogArtifacts([]);
|
|
||||||
expect(artifact.create().uploadArtifact).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ────────────────────────────────────────────────────────────────────────────
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
// handleConsoleEvent
|
// handleConsoleEvent
|
||||||
// ────────────────────────────────────────────────────────────────────────────
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
describe("handleConsoleEvent", () => {
|
describe("handleConsoleEvent", () => {
|
||||||
let state;
|
let state;
|
||||||
let shardBuffers;
|
let stdoutBuffer;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false };
|
state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false };
|
||||||
shardBuffers = {};
|
stdoutBuffer = [];
|
||||||
});
|
});
|
||||||
|
|
||||||
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 with shard prefix when logToFile=false", () => {
|
it("calls core.info for each stdout line when logToFile=false", () => {
|
||||||
const event = {
|
const event = makeEvent(["line1", "line2"]);
|
||||||
data: {
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
|
||||||
shard: "shard0",
|
expect(core.info).toHaveBeenCalledWith("line1");
|
||||||
messages: { log: ["line1"], results: ["line2"] },
|
expect(core.info).toHaveBeenCalledWith("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 = {
|
const event = makeEvent(["line1", "line2"]);
|
||||||
data: { shard: "shard0", messages: { log: ["line1", "line2"] } },
|
handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, state);
|
||||||
};
|
|
||||||
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
|
|
||||||
expect(core.info).not.toHaveBeenCalled();
|
expect(core.info).not.toHaveBeenCalled();
|
||||||
expect(shardBuffers["shard0"]).toEqual(["line1", "line2"]);
|
expect(stdoutBuffer).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 }, shardBuffers, state);
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, 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 }, shardBuffers, state);
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
|
||||||
expect(core.error).toHaveBeenCalledWith("Script crashed");
|
expect(core.error).toHaveBeenCalledWith("Script crashed");
|
||||||
expect(state.sawErrorLog).toBe(true);
|
expect(state.sawErrorLog).toBe(true);
|
||||||
});
|
});
|
||||||
@@ -253,74 +190,44 @@ 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 }, shardBuffers, state);
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, 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 }, shardBuffers, state);
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, 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 }, shardBuffers, state);
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, 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 }, shardBuffers, state);
|
handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, 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 }, shardBuffers, state),
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, 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 }, shardBuffers, state),
|
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
|
||||||
).not.toThrow();
|
).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("filters messages by shard when targetShard is provided", () => {
|
|
||||||
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
|
|
||||||
handleConsoleEvent(event, { shard: "shard1" }, shardBuffers, state);
|
|
||||||
expect(core.info).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
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, {}, 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"]);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// ────────────────────────────────────────────────────────────────────────────
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
@@ -450,19 +357,19 @@ describe("monitorConsole", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("subscribes to 'console' for the official server (default)", async () => {
|
it("subscribes to 'shard0/console' for the official server (default)", async () => {
|
||||||
const api = buildMockApi({
|
const api = buildMockApi({
|
||||||
hostname: "screeps.com",
|
hostname: "screeps.com",
|
||||||
ticks: [100, 101, 102, 103],
|
ticks: [100, 101, 102, 103],
|
||||||
});
|
});
|
||||||
await monitorConsole(api, { ...BASE_OPTS, hostname: "screeps.com" });
|
await monitorConsole(api, { ...BASE_OPTS, hostname: "screeps.com" });
|
||||||
expect(api.socket.subscribe).toHaveBeenCalledWith(
|
expect(api.socket.subscribe).toHaveBeenCalledWith(
|
||||||
"console",
|
"shard0/console",
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("subscribes to 'console' even when custom shard is provided", async () => {
|
it("subscribes to custom shard when provided", async () => {
|
||||||
const api = buildMockApi({
|
const api = buildMockApi({
|
||||||
hostname: "screeps.com",
|
hostname: "screeps.com",
|
||||||
ticks: [100, 101, 102, 103],
|
ticks: [100, 101, 102, 103],
|
||||||
@@ -473,10 +380,10 @@ describe("monitorConsole", () => {
|
|||||||
shard: "shard3",
|
shard: "shard3",
|
||||||
});
|
});
|
||||||
expect(api.socket.subscribe).toHaveBeenCalledWith(
|
expect(api.socket.subscribe).toHaveBeenCalledWith(
|
||||||
"console",
|
"shard3/console",
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
);
|
);
|
||||||
// Verify polling still uses shard3 for timing
|
// Verify polling also uses shard3
|
||||||
expect(api.time).toHaveBeenCalledWith("shard3");
|
expect(api.time).toHaveBeenCalledWith("shard3");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -597,37 +504,4 @@ 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
|
|
||||||
await monitorConsole(api, {
|
|
||||||
...BASE_OPTS,
|
|
||||||
logToFile: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(artifact.create().uploadArtifact).toHaveBeenCalledWith(
|
|
||||||
"screeps-console-log",
|
|
||||||
expect.arrayContaining([
|
|
||||||
expect.stringContaining("shard0_console_log.txt"),
|
|
||||||
expect.stringContaining("shard1_console_log.txt"),
|
|
||||||
]),
|
|
||||||
expect.any(String),
|
|
||||||
{ continueOnError: true },
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Vendored
+85
-12
File diff suppressed because one or more lines are too long
@@ -197,7 +197,7 @@ export async function postCode() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Console monitoring (optional)
|
// ── Console monitoring (optional) ────────────────────────────────────────
|
||||||
const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
|
const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
|
||||||
if (monitorTicks > 0) {
|
if (monitorTicks > 0) {
|
||||||
const result = await monitorConsole(api, {
|
const result = await monitorConsole(api, {
|
||||||
|
|||||||
+73
-98
@@ -1,9 +1,13 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import { create } from "@actions/artifact";
|
import { DefaultArtifactClient } from "@actions/artifact";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Shard / subscribe-path helpers
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the hostname is the official Screeps server.
|
* Returns true if the hostname is the official Screeps server.
|
||||||
* Used to decide whether to prefix the subscribe path with a shard name.
|
* Used to decide whether to prefix the subscribe path with a shard name.
|
||||||
@@ -29,11 +33,14 @@ export function isOfficialServer(hostname) {
|
|||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function buildSubscribePath(hostname, shard) {
|
export function buildSubscribePath(hostname, shard) {
|
||||||
// The console channel on Screeps official and most private servers is 'console'.
|
if (shard) return `${shard}/console`;
|
||||||
// We subscribe to the aggregate feed and filter by shard in handleConsoleEvent.
|
return isOfficialServer(hostname) ? "shard0/console" : "console";
|
||||||
return "console";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Detection helpers
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true when errorText contains JavaScript stack-frame lines.
|
* Returns true when errorText contains JavaScript stack-frame lines.
|
||||||
* Screeps places runtime errors (including stack traces) in event.data.error.
|
* Screeps places runtime errors (including stack traces) in event.data.error.
|
||||||
@@ -69,42 +76,17 @@ export function detectWarning(logLines) {
|
|||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function safeDecode(text) {
|
export function safeDecode(text) {
|
||||||
if (typeof text !== "string") return text;
|
if (typeof text !== "string" || !text.includes("%")) return text;
|
||||||
let result = text;
|
try {
|
||||||
if (result.includes("%")) {
|
return decodeURIComponent(text);
|
||||||
try {
|
} catch (err) {
|
||||||
result = decodeURIComponent(result);
|
return text;
|
||||||
} catch (err) {
|
|
||||||
// Ignore decoding errors
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Screeps console often contains HTML entities
|
|
||||||
return result
|
|
||||||
.replace(/</g, "<")
|
|
||||||
.replace(/>/g, ">")
|
|
||||||
.replace(/"/g, '"')
|
|
||||||
.replace(/&/g, "&");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
* Outputs text to the action log, splitting by newline to ensure proper display.
|
// Output helpers
|
||||||
* Optionally prepends a [shard] prefix.
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
*
|
|
||||||
* @param {string} text
|
|
||||||
* @param {"info"|"warning"|"error"} level
|
|
||||||
* @param {string} [shard]
|
|
||||||
*/
|
|
||||||
export function outputMultiline(text, level = "info", shard = null) {
|
|
||||||
if (!text) return;
|
|
||||||
const prefix = shard ? `[${shard}] ` : "";
|
|
||||||
const lines = text.split(/\r?\n/);
|
|
||||||
lines.forEach((line) => {
|
|
||||||
const formattedLine = `${prefix}${line}`;
|
|
||||||
if (level === "error") core.error(formattedLine);
|
|
||||||
else if (level === "warning") core.warning(formattedLine);
|
|
||||||
else core.info(formattedLine);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a CI-friendly progress string for core.info().
|
* Builds a CI-friendly progress string for core.info().
|
||||||
@@ -117,6 +99,10 @@ export function buildProgressMessage(elapsed, total) {
|
|||||||
return `[Monitor] ${elapsed}/${total} ticks elapsed...`;
|
return `[Monitor] ${elapsed}/${total} ticks elapsed...`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
// File / artifact helpers
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes an array of log lines to a UTF-8 text file, one line per entry.
|
* Writes an array of log lines to a UTF-8 text file, one line per entry.
|
||||||
*
|
*
|
||||||
@@ -129,33 +115,37 @@ export async function writeLogFile(lines, filePath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uploads one or more files as a named workflow artifact using @actions/artifact.
|
* Uploads a file 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.
|
* artifact service (e.g. a bare self-hosted runner without the service configured).
|
||||||
*
|
*
|
||||||
* @param {string[]} filePaths - Absolute paths of the files to upload
|
* @param {string} filePath - Absolute path of the file 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 uploadLogArtifacts(
|
export async function uploadLogArtifact(
|
||||||
filePaths,
|
filePath,
|
||||||
artifactName = "screeps-console-log",
|
artifactName = "screeps-console-log",
|
||||||
) {
|
) {
|
||||||
if (!filePaths || filePaths.length === 0) return;
|
|
||||||
try {
|
try {
|
||||||
const client = create();
|
const client = new DefaultArtifactClient();
|
||||||
const rootDir = path.dirname(filePaths[0]);
|
await client.uploadArtifact(
|
||||||
await client.uploadArtifact(artifactName, filePaths, rootDir, {
|
artifactName,
|
||||||
continueOnError: true,
|
[filePath],
|
||||||
});
|
path.dirname(filePath),
|
||||||
core.info(`[Monitor] Console logs uploaded as artifact '${artifactName}'.`);
|
);
|
||||||
|
core.info(`[Monitor] Console log uploaded as artifact '${artifactName}'.`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
core.warning(
|
core.warning(
|
||||||
`[Monitor] Could not upload console logs as artifact: ${err.message}`,
|
`[Monitor] Could not upload console log as artifact: ${err.message}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Console event handler
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes a single 'console' WebSocket event from the Screeps socket.
|
* Processes a single 'console' WebSocket event from the Screeps socket.
|
||||||
* Mutates `state` and `stdoutBuffer` in place; never throws.
|
* Mutates `state` and `stdoutBuffer` in place; never throws.
|
||||||
@@ -177,63 +167,58 @@ export async function uploadLogArtifacts(
|
|||||||
* - 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, shard?: string } }} event
|
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string } }} event
|
||||||
* @param {{ logToFile: boolean, shard?: string }} opts
|
* @param {{ logToFile: boolean }} opts
|
||||||
* @param {Record<string, string[]>} shardBuffers - Mutable buffer Map used in logToFile mode
|
* @param {string[]} stdoutBuffer - Mutable buffer 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, shardBuffers, state) {
|
export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
|
||||||
const { logToFile, shard: targetShard } = opts;
|
const { logToFile } = opts;
|
||||||
const data = event?.data ?? {};
|
const data = event?.data ?? {};
|
||||||
|
|
||||||
// Shard filtering: If a shard is specified in opts, only process messages from that shard.
|
|
||||||
// Official server events include a 'shard' property in event.data.
|
|
||||||
if (targetShard && data.shard && data.shard !== targetShard) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const logLines = data?.messages?.log ?? [];
|
const logLines = data?.messages?.log ?? [];
|
||||||
const results = data?.messages?.results ?? [];
|
const results = data?.messages?.results ?? [];
|
||||||
const errorText = data?.error ?? null;
|
const errorText = data?.error ?? null;
|
||||||
|
|
||||||
// Warn detection (always live regardless of logToFile)
|
// ── Warn detection (always live regardless of logToFile) ─────────────────
|
||||||
if (detectWarning(logLines)) {
|
if (detectWarning(logLines)) {
|
||||||
state.sawWarningLog = true;
|
state.sawWarningLog = true;
|
||||||
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", data.shard));
|
.forEach((l) => core.warning(safeDecode(l)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traceback detection in log lines (Screeps sometimes sends errors here)
|
// ── Traceback detection in log lines (Screeps sometimes sends errors here) ─
|
||||||
if (logLines.some((l) => detectTraceback(l))) {
|
if (logLines.some((l) => detectTraceback(l))) {
|
||||||
state.sawTraceback = true;
|
state.sawTraceback = true;
|
||||||
state.sawErrorLog = true;
|
state.sawErrorLog = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stdout lines
|
// ── Stdout lines ──────────────────────────────────────────────────────────
|
||||||
const allStdout = [...logLines, ...results].map(safeDecode);
|
const allStdout = [...logLines, ...results].map(safeDecode);
|
||||||
if (allStdout.length > 0) {
|
if (allStdout.length > 0) {
|
||||||
if (logToFile) {
|
if (logToFile) {
|
||||||
const shard = data.shard || "default";
|
stdoutBuffer.push(...allStdout);
|
||||||
if (!shardBuffers[shard]) shardBuffers[shard] = [];
|
|
||||||
shardBuffers[shard].push(...allStdout);
|
|
||||||
} else {
|
} else {
|
||||||
allStdout.forEach((l) => outputMultiline(l, "info", data.shard));
|
allStdout.forEach((l) => core.info(l));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error field (always live)
|
// ── Error field (always live) ─────────────────────────────────────────────
|
||||||
if (errorText) {
|
if (errorText) {
|
||||||
state.sawErrorLog = true;
|
state.sawErrorLog = true;
|
||||||
const decodedError = safeDecode(errorText);
|
const decodedError = safeDecode(errorText);
|
||||||
outputMultiline(decodedError, "error", data.shard);
|
core.error(decodedError);
|
||||||
if (detectTraceback(decodedError)) {
|
if (detectTraceback(decodedError)) {
|
||||||
state.sawTraceback = true;
|
state.sawTraceback = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Tick polling
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sleeps for the given number of milliseconds.
|
* Sleeps for the given number of milliseconds.
|
||||||
*
|
*
|
||||||
@@ -277,6 +262,10 @@ export async function pollUntilDone(
|
|||||||
return elapsed;
|
return elapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Main orchestrator
|
||||||
|
// ────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} MonitorOptions
|
* @typedef {Object} MonitorOptions
|
||||||
* @property {number} monitor - Number of game ticks to collect.
|
* @property {number} monitor - Number of game ticks to collect.
|
||||||
@@ -328,7 +317,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 shardBuffers = {}; // { [shardName]: string[] }
|
const stdoutBuffer = [];
|
||||||
const state = {
|
const state = {
|
||||||
sawTraceback: false,
|
sawTraceback: false,
|
||||||
sawErrorLog: false,
|
sawErrorLog: false,
|
||||||
@@ -336,13 +325,13 @@ export async function monitorConsole(api, opts) {
|
|||||||
};
|
};
|
||||||
let lastProgressTick = 0;
|
let lastProgressTick = 0;
|
||||||
|
|
||||||
// Step 1: record starting tick
|
// ── Step 1: record starting tick ─────────────────────────────────────────
|
||||||
const { time: startTick } = await api.time(shard);
|
const { time: startTick } = await api.time(shard);
|
||||||
|
|
||||||
// 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, shardBuffers, state);
|
handleConsoleEvent(event, opts, stdoutBuffer, state);
|
||||||
});
|
});
|
||||||
|
|
||||||
core.info(
|
core.info(
|
||||||
@@ -351,7 +340,7 @@ export async function monitorConsole(api, opts) {
|
|||||||
"...",
|
"...",
|
||||||
);
|
);
|
||||||
|
|
||||||
// Step 3 & 4: tick-poll loop
|
// ── Step 3 & 4: tick-poll loop ───────────────────────────────────────────
|
||||||
try {
|
try {
|
||||||
await pollUntilDone(
|
await pollUntilDone(
|
||||||
api,
|
api,
|
||||||
@@ -379,29 +368,15 @@ export async function monitorConsole(api, opts) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
// Step 5: always disconnect cleanly
|
// ── Step 5: always disconnect cleanly ────────────────────────────────
|
||||||
api.socket.disconnect();
|
api.socket.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 6: artifact upload
|
// ── Step 6: artifact upload ───────────────────────────────────────────────
|
||||||
const shardKeys = Object.keys(shardBuffers);
|
if (logToFile && stdoutBuffer.length > 0) {
|
||||||
if (logToFile && shardKeys.length > 0) {
|
const tmpFile = path.join(os.tmpdir(), "screeps_console_log.txt");
|
||||||
const tmpDir = await fs.promises.mkdtemp(
|
await writeLogFile(stdoutBuffer, tmpFile);
|
||||||
path.join(os.tmpdir(), "screeps-monitor-"),
|
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) {
|
} 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.",
|
||||||
|
|||||||
Generated
+1816
-28
File diff suppressed because it is too large
Load Diff
+2
-3
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "screeps-deploy-action",
|
"name": "screeps-deploy-action",
|
||||||
"version": "0.2.1",
|
"version": "0.2.0",
|
||||||
"description": "Deploys screeps code to the official game or a private server.",
|
"description": "Deploys screeps code to the official game or an pirvate server.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
"build": "ncc build index.js -o dist -m --external utf-8-validate --external bufferutil"
|
"build": "ncc build index.js -o dist -m --external utf-8-validate --external bufferutil"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/artifact": "^1.1.2",
|
|
||||||
"@actions/core": "^3.0.0",
|
"@actions/core": "^3.0.0",
|
||||||
"glob": "^13.0.0",
|
"glob": "^13.0.0",
|
||||||
"screeps-api": "^1.7.2"
|
"screeps-api": "^1.7.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user