mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-07 22:44:16 +00:00
test: speed up slow media and synology suites
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
|
||||
// Mock http and https modules before importing the client
|
||||
vi.mock("node:https", () => {
|
||||
@@ -15,6 +15,13 @@ vi.mock("node:http", () => {
|
||||
// Import after mocks are set up
|
||||
const { sendMessage, sendFileUrl } = await import("./client.js");
|
||||
const https = await import("node:https");
|
||||
let fakeNowMs = 1_700_000_000_000;
|
||||
|
||||
async function settleTimers<T>(promise: Promise<T>): Promise<T> {
|
||||
await Promise.resolve();
|
||||
await vi.runAllTimersAsync();
|
||||
return promise;
|
||||
}
|
||||
|
||||
function mockSuccessResponse() {
|
||||
const httpsRequest = vi.mocked(https.request);
|
||||
@@ -55,23 +62,30 @@ function mockFailureResponse(statusCode = 500) {
|
||||
describe("sendMessage", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.useFakeTimers();
|
||||
fakeNowMs += 10_000;
|
||||
vi.setSystemTime(fakeNowMs);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("returns true on successful send", async () => {
|
||||
mockSuccessResponse();
|
||||
const result = await sendMessage("https://nas.example.com/incoming", "Hello");
|
||||
const result = await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello"));
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false on server error after retries", async () => {
|
||||
mockFailureResponse(500);
|
||||
const result = await sendMessage("https://nas.example.com/incoming", "Hello");
|
||||
const result = await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello"));
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("includes user_ids when userId is numeric", async () => {
|
||||
mockSuccessResponse();
|
||||
await sendMessage("https://nas.example.com/incoming", "Hello", 42);
|
||||
await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello", 42));
|
||||
const httpsRequest = vi.mocked(https.request);
|
||||
expect(httpsRequest).toHaveBeenCalled();
|
||||
const callArgs = httpsRequest.mock.calls[0];
|
||||
@@ -82,22 +96,27 @@ describe("sendMessage", () => {
|
||||
describe("sendFileUrl", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.useFakeTimers();
|
||||
fakeNowMs += 10_000;
|
||||
vi.setSystemTime(fakeNowMs);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("returns true on success", async () => {
|
||||
mockSuccessResponse();
|
||||
const result = await sendFileUrl(
|
||||
"https://nas.example.com/incoming",
|
||||
"https://example.com/file.png",
|
||||
const result = await settleTimers(
|
||||
sendFileUrl("https://nas.example.com/incoming", "https://example.com/file.png"),
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false on failure", async () => {
|
||||
mockFailureResponse(500);
|
||||
const result = await sendFileUrl(
|
||||
"https://nas.example.com/incoming",
|
||||
"https://example.com/file.png",
|
||||
const result = await settleTimers(
|
||||
sendFileUrl("https://nas.example.com/incoming", "https://example.com/file.png"),
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
@@ -98,8 +98,8 @@ describe("web auto-reply", () => {
|
||||
},
|
||||
] as const;
|
||||
|
||||
const width = 1200;
|
||||
const height = 1200;
|
||||
const width = 1150;
|
||||
const height = 1150;
|
||||
const sharedRaw = crypto.randomBytes(width * height * 3);
|
||||
|
||||
for (const fmt of formats) {
|
||||
@@ -179,8 +179,8 @@ describe("web auto-reply", () => {
|
||||
|
||||
const bigPng = await sharp({
|
||||
create: {
|
||||
width: 1800,
|
||||
height: 1800,
|
||||
width: 1200,
|
||||
height: 1200,
|
||||
channels: 3,
|
||||
background: { r: 0, g: 0, b: 255 },
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user