mirror of https://github.com/openclaw/openclaw.git
refactor: share net and slack input helpers
This commit is contained in:
parent
b5eb329f94
commit
854df8352c
|
|
@ -128,12 +128,16 @@ function normalizeIpv4MappedAddress(address: ParsedIpAddress): ParsedIpAddress {
|
||||||
return address.toIPv4Address();
|
return address.toIPv4Address();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseCanonicalIpAddress(raw: string | undefined): ParsedIpAddress | undefined {
|
function normalizeIpParseInput(raw: string | undefined): string | undefined {
|
||||||
const trimmed = raw?.trim();
|
const trimmed = raw?.trim();
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const normalized = stripIpv6Brackets(trimmed);
|
return stripIpv6Brackets(trimmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseCanonicalIpAddress(raw: string | undefined): ParsedIpAddress | undefined {
|
||||||
|
const normalized = normalizeIpParseInput(raw);
|
||||||
if (!normalized) {
|
if (!normalized) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
@ -150,11 +154,7 @@ export function parseCanonicalIpAddress(raw: string | undefined): ParsedIpAddres
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseLooseIpAddress(raw: string | undefined): ParsedIpAddress | undefined {
|
export function parseLooseIpAddress(raw: string | undefined): ParsedIpAddress | undefined {
|
||||||
const trimmed = raw?.trim();
|
const normalized = normalizeIpParseInput(raw);
|
||||||
if (!trimmed) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const normalized = stripIpv6Brackets(trimmed);
|
|
||||||
if (!normalized) {
|
if (!normalized) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,20 @@ function createHandlerWithTracker(overrides?: {
|
||||||
return { handler, trackEvent };
|
return { handler, trackEvent };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleDirectMessage(
|
||||||
|
handler: ReturnType<typeof createHandlerWithTracker>["handler"],
|
||||||
|
) {
|
||||||
|
await handler(
|
||||||
|
{
|
||||||
|
type: "message",
|
||||||
|
channel: "D1",
|
||||||
|
ts: "123.456",
|
||||||
|
text: "hello",
|
||||||
|
} as never,
|
||||||
|
{ source: "message" },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
describe("createSlackMessageHandler", () => {
|
describe("createSlackMessageHandler", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
enqueueMock.mockClear();
|
enqueueMock.mockClear();
|
||||||
|
|
@ -82,15 +96,7 @@ describe("createSlackMessageHandler", () => {
|
||||||
it("does not track duplicate messages that are already seen", async () => {
|
it("does not track duplicate messages that are already seen", async () => {
|
||||||
const { handler, trackEvent } = createHandlerWithTracker({ markMessageSeen: () => true });
|
const { handler, trackEvent } = createHandlerWithTracker({ markMessageSeen: () => true });
|
||||||
|
|
||||||
await handler(
|
await handleDirectMessage(handler);
|
||||||
{
|
|
||||||
type: "message",
|
|
||||||
channel: "D1",
|
|
||||||
ts: "123.456",
|
|
||||||
text: "hello",
|
|
||||||
} as never,
|
|
||||||
{ source: "message" },
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(trackEvent).not.toHaveBeenCalled();
|
expect(trackEvent).not.toHaveBeenCalled();
|
||||||
expect(resolveThreadTsMock).not.toHaveBeenCalled();
|
expect(resolveThreadTsMock).not.toHaveBeenCalled();
|
||||||
|
|
@ -100,15 +106,7 @@ describe("createSlackMessageHandler", () => {
|
||||||
it("tracks accepted non-duplicate messages", async () => {
|
it("tracks accepted non-duplicate messages", async () => {
|
||||||
const { handler, trackEvent } = createHandlerWithTracker();
|
const { handler, trackEvent } = createHandlerWithTracker();
|
||||||
|
|
||||||
await handler(
|
await handleDirectMessage(handler);
|
||||||
{
|
|
||||||
type: "message",
|
|
||||||
channel: "D1",
|
|
||||||
ts: "123.456",
|
|
||||||
text: "hello",
|
|
||||||
} as never,
|
|
||||||
{ source: "message" },
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(trackEvent).toHaveBeenCalledTimes(1);
|
expect(trackEvent).toHaveBeenCalledTimes(1);
|
||||||
expect(resolveThreadTsMock).toHaveBeenCalledTimes(1);
|
expect(resolveThreadTsMock).toHaveBeenCalledTimes(1);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue