test: collapse telegram context and transport suites

This commit is contained in:
Peter Steinberger 2026-03-25 11:16:23 +00:00
parent fc49258c12
commit 3de04bdd6d
4 changed files with 76 additions and 78 deletions

View File

@ -1,6 +1,31 @@
import { describe, expect, it, vi } from "vitest";
import { createRequire } from "node:module";
import { afterEach, describe, expect, it, vi } from "vitest";
import { fetchTelegramChatId } from "./api-fetch.js";
const require = createRequire(import.meta.url);
const EnvHttpProxyAgent = require("undici/lib/dispatcher/env-http-proxy-agent.js") as {
new (opts?: Record<string, unknown>): Record<PropertyKey, unknown>;
};
const { kHttpsProxyAgent, kNoProxyAgent } = require("undici/lib/core/symbols.js") as {
kHttpsProxyAgent: symbol;
kNoProxyAgent: symbol;
};
function getOwnSymbolValue(
target: Record<PropertyKey, unknown>,
description: string,
): Record<string, unknown> | undefined {
const symbol = Object.getOwnPropertySymbols(target).find(
(entry) => entry.description === description,
);
const value = symbol ? target[symbol] : undefined;
return value && typeof value === "object" ? (value as Record<string, unknown>) : undefined;
}
afterEach(() => {
vi.unstubAllEnvs();
});
describe("fetchTelegramChatId", () => {
const cases = [
{
@ -79,3 +104,35 @@ describe("fetchTelegramChatId", () => {
);
});
});
describe("undici env proxy semantics", () => {
it("uses proxyTls rather than connect for proxied HTTPS transport settings", () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
const connect = {
family: 4,
autoSelectFamily: false,
};
const withoutProxyTls = new EnvHttpProxyAgent({ connect });
const noProxyAgent = withoutProxyTls[kNoProxyAgent] as Record<PropertyKey, unknown>;
const httpsProxyAgent = withoutProxyTls[kHttpsProxyAgent] as Record<PropertyKey, unknown>;
expect(getOwnSymbolValue(noProxyAgent, "options")?.connect).toEqual(
expect.objectContaining(connect),
);
expect(getOwnSymbolValue(httpsProxyAgent, "proxy tls settings")).toBeUndefined();
const withProxyTls = new EnvHttpProxyAgent({
connect,
proxyTls: connect,
});
const httpsProxyAgentWithProxyTls = withProxyTls[kHttpsProxyAgent] as Record<
PropertyKey,
unknown
>;
expect(getOwnSymbolValue(httpsProxyAgentWithProxyTls, "proxy tls settings")).toEqual(
expect.objectContaining(connect),
);
});
});

View File

@ -1,5 +1,23 @@
import { describe, expect, it } from "vitest";
import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js";
import {
isTelegramForumServiceMessage,
TELEGRAM_FORUM_SERVICE_FIELDS,
} from "./forum-service-message.js";
describe("isTelegramForumServiceMessage", () => {
it("returns true for any Telegram forum service field", () => {
for (const field of TELEGRAM_FORUM_SERVICE_FIELDS) {
expect(isTelegramForumServiceMessage({ [field]: {} })).toBe(true);
}
});
it("returns false for normal messages and non-objects", () => {
expect(isTelegramForumServiceMessage({ text: "hello" })).toBe(false);
expect(isTelegramForumServiceMessage(null)).toBe(false);
expect(isTelegramForumServiceMessage("topic created")).toBe(false);
});
});
describe("buildTelegramMessageContext sender prefix", () => {
async function buildCtx(params: { messageId: number; options?: Record<string, unknown> }) {

View File

@ -1,58 +0,0 @@
import { createRequire } from "node:module";
import { afterEach, describe, expect, it, vi } from "vitest";
const require = createRequire(import.meta.url);
const EnvHttpProxyAgent = require("undici/lib/dispatcher/env-http-proxy-agent.js") as {
new (opts?: Record<string, unknown>): Record<PropertyKey, unknown>;
};
const { kHttpsProxyAgent, kNoProxyAgent } = require("undici/lib/core/symbols.js") as {
kHttpsProxyAgent: symbol;
kNoProxyAgent: symbol;
};
function getOwnSymbolValue(
target: Record<PropertyKey, unknown>,
description: string,
): Record<string, unknown> | undefined {
const symbol = Object.getOwnPropertySymbols(target).find(
(entry) => entry.description === description,
);
const value = symbol ? target[symbol] : undefined;
return value && typeof value === "object" ? (value as Record<string, unknown>) : undefined;
}
afterEach(() => {
vi.unstubAllEnvs();
});
describe("undici env proxy semantics", () => {
it("uses proxyTls rather than connect for proxied HTTPS transport settings", () => {
vi.stubEnv("HTTPS_PROXY", "http://127.0.0.1:7890");
const connect = {
family: 4,
autoSelectFamily: false,
};
const withoutProxyTls = new EnvHttpProxyAgent({ connect });
const noProxyAgent = withoutProxyTls[kNoProxyAgent] as Record<PropertyKey, unknown>;
const httpsProxyAgent = withoutProxyTls[kHttpsProxyAgent] as Record<PropertyKey, unknown>;
expect(getOwnSymbolValue(noProxyAgent, "options")?.connect).toEqual(
expect.objectContaining(connect),
);
expect(getOwnSymbolValue(httpsProxyAgent, "proxy tls settings")).toBeUndefined();
const withProxyTls = new EnvHttpProxyAgent({
connect,
proxyTls: connect,
});
const httpsProxyAgentWithProxyTls = withProxyTls[kHttpsProxyAgent] as Record<
PropertyKey,
unknown
>;
expect(getOwnSymbolValue(httpsProxyAgentWithProxyTls, "proxy tls settings")).toEqual(
expect.objectContaining(connect),
);
});
});

View File

@ -1,19 +0,0 @@
import { describe, expect, it } from "vitest";
import {
isTelegramForumServiceMessage,
TELEGRAM_FORUM_SERVICE_FIELDS,
} from "./forum-service-message.js";
describe("isTelegramForumServiceMessage", () => {
it("returns true for any Telegram forum service field", () => {
for (const field of TELEGRAM_FORUM_SERVICE_FIELDS) {
expect(isTelegramForumServiceMessage({ [field]: {} })).toBe(true);
}
});
it("returns false for normal messages and non-objects", () => {
expect(isTelegramForumServiceMessage({ text: "hello" })).toBe(false);
expect(isTelegramForumServiceMessage(null)).toBe(false);
expect(isTelegramForumServiceMessage("topic created")).toBe(false);
});
});