refactor: migrate to screeps-api v2 and update actions to node 22
This commit is contained in:
+19
-9
@@ -24,16 +24,23 @@ vi.mock("../monitor.js", () => ({
|
||||
vi.mock("screeps-api", () => {
|
||||
const mockApi = {
|
||||
auth: vi.fn().mockResolvedValue(),
|
||||
authSignin: vi.fn().mockResolvedValue({ ok: 1, token: "test_token" }),
|
||||
userCodeGet: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ ok: 1, modules: { main: "old_code" } }),
|
||||
userCodeSet: vi.fn().mockResolvedValue({ ok: 1 }),
|
||||
code: {
|
||||
get: vi.fn().mockResolvedValue({ ok: 1, modules: { main: "old_code" } }),
|
||||
set: vi.fn().mockResolvedValue({ ok: 1 }),
|
||||
},
|
||||
};
|
||||
// Use a regular function so it can be called with `new`
|
||||
const MockClient = vi.fn(function () {
|
||||
return mockApi;
|
||||
});
|
||||
return {
|
||||
ScreepsAPI: vi.fn(function () {
|
||||
return mockApi;
|
||||
}),
|
||||
ScreepsHttpClient: MockClient,
|
||||
ScreepsAPI: MockClient,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -48,7 +55,7 @@ import {
|
||||
applyOnAction,
|
||||
postCode,
|
||||
} from "../index.js";
|
||||
import { ScreepsAPI } from "screeps-api";
|
||||
import { ScreepsHttpClient } from "screeps-api";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
@@ -330,14 +337,17 @@ describe("postCode — monitor wiring", () => {
|
||||
await postCode();
|
||||
|
||||
// Verify rollback was performed
|
||||
const mockApiInstance = new ScreepsAPI();
|
||||
const mockApiInstance = new ScreepsHttpClient();
|
||||
|
||||
// `code.set` should be called twice:
|
||||
// `userCodeSet` should be called twice:
|
||||
// 1st time: uploading the new files
|
||||
// 2nd time: rolling back to oldCode
|
||||
expect(mockApiInstance.code.set).toHaveBeenCalledTimes(2);
|
||||
expect(mockApiInstance.code.set).toHaveBeenNthCalledWith(2, "default", {
|
||||
main: "old_code",
|
||||
expect(mockApiInstance.userCodeSet).toHaveBeenCalledTimes(2);
|
||||
expect(mockApiInstance.userCodeSet).toHaveBeenNthCalledWith(2, {
|
||||
branch: "default",
|
||||
modules: {
|
||||
main: "old_code",
|
||||
},
|
||||
});
|
||||
|
||||
// Verify it called core.setFailed due to traceback
|
||||
|
||||
@@ -382,13 +382,21 @@ function buildMockApi({
|
||||
disconnect: vi.fn(),
|
||||
};
|
||||
|
||||
const getTime = vi.fn().mockImplementation(() => {
|
||||
const t = ticks[Math.min(tickIndex, ticks.length - 1)];
|
||||
tickIndex++;
|
||||
return Promise.resolve({ time: t });
|
||||
});
|
||||
|
||||
const api = {
|
||||
opts: { hostname },
|
||||
time: vi.fn().mockImplementation(() => {
|
||||
const t = ticks[Math.min(tickIndex, ticks.length - 1)];
|
||||
tickIndex++;
|
||||
return Promise.resolve({ time: t });
|
||||
}),
|
||||
time: getTime,
|
||||
get gameTime() {
|
||||
return this.time;
|
||||
},
|
||||
set gameTime(fn) {
|
||||
this.time = fn;
|
||||
},
|
||||
socket,
|
||||
// Expose so tests can fire console events
|
||||
_fireConsole: (eventData) => {
|
||||
|
||||
Reference in New Issue
Block a user