mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 14:24:59 +00:00
fix(doctor): resolve telegram allowFrom usernames
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { withTempHome } from "../../test/helpers/temp-home.js";
|
||||
import { loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow.js";
|
||||
|
||||
@@ -64,4 +64,84 @@ describe("doctor config flow", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves Telegram @username allowFrom entries to numeric IDs on repair", async () => {
|
||||
const fetchSpy = vi.fn(async (url: string) => {
|
||||
const u = String(url);
|
||||
const chatId = new URL(u).searchParams.get("chat_id") ?? "";
|
||||
const id =
|
||||
chatId.toLowerCase() === "@testuser"
|
||||
? 111
|
||||
: chatId.toLowerCase() === "@groupuser"
|
||||
? 222
|
||||
: chatId.toLowerCase() === "@topicuser"
|
||||
? 333
|
||||
: chatId.toLowerCase() === "@accountuser"
|
||||
? 444
|
||||
: null;
|
||||
return {
|
||||
ok: id != null,
|
||||
json: async () => (id != null ? { ok: true, result: { id } } : { ok: false }),
|
||||
} as unknown as Response;
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
try {
|
||||
await withTempHome(async (home) => {
|
||||
const configDir = path.join(home, ".openclaw");
|
||||
await fs.mkdir(configDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(configDir, "openclaw.json"),
|
||||
JSON.stringify(
|
||||
{
|
||||
channels: {
|
||||
telegram: {
|
||||
botToken: "123:abc",
|
||||
allowFrom: ["@testuser"],
|
||||
groupAllowFrom: ["groupUser"],
|
||||
groups: {
|
||||
"-100123": {
|
||||
allowFrom: ["tg:@topicUser"],
|
||||
topics: { "99": { allowFrom: ["@accountUser"] } },
|
||||
},
|
||||
},
|
||||
accounts: {
|
||||
alerts: { botToken: "456:def", allowFrom: ["@accountUser"] },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
const result = await loadAndMaybeMigrateDoctorConfig({
|
||||
options: { nonInteractive: true, repair: true },
|
||||
confirm: async () => false,
|
||||
});
|
||||
|
||||
const cfg = result.cfg as unknown as {
|
||||
channels: {
|
||||
telegram: {
|
||||
allowFrom: string[];
|
||||
groupAllowFrom: string[];
|
||||
groups: Record<
|
||||
string,
|
||||
{ allowFrom: string[]; topics: Record<string, { allowFrom: string[] }> }
|
||||
>;
|
||||
accounts: Record<string, { allowFrom: string[] }>;
|
||||
};
|
||||
};
|
||||
};
|
||||
expect(cfg.channels.telegram.allowFrom).toEqual(["111"]);
|
||||
expect(cfg.channels.telegram.groupAllowFrom).toEqual(["222"]);
|
||||
expect(cfg.channels.telegram.groups["-100123"].allowFrom).toEqual(["333"]);
|
||||
expect(cfg.channels.telegram.groups["-100123"].topics["99"].allowFrom).toEqual(["444"]);
|
||||
expect(cfg.channels.telegram.accounts.alerts.allowFrom).toEqual(["444"]);
|
||||
});
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user