mirror of https://github.com/openclaw/openclaw.git
test: refine telegram token coverage
This commit is contained in:
parent
431463dec2
commit
3e8d9bc6ea
|
|
@ -7,50 +7,75 @@ import { withStateDirEnv } from "../test-helpers/state-dir-env.js";
|
||||||
import { resolveTelegramToken } from "./token.js";
|
import { resolveTelegramToken } from "./token.js";
|
||||||
import { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.js";
|
import { readTelegramUpdateOffset, writeTelegramUpdateOffset } from "./update-offset-store.js";
|
||||||
|
|
||||||
function withTempDir(): string {
|
|
||||||
return fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-token-"));
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("resolveTelegramToken", () => {
|
describe("resolveTelegramToken", () => {
|
||||||
|
const tempDirs: string[] = [];
|
||||||
|
|
||||||
|
function createTempDir(): string {
|
||||||
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-token-"));
|
||||||
|
tempDirs.push(dir);
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTokenFile(fileName: string, contents = "file-token\n"): string {
|
||||||
|
const dir = createTempDir();
|
||||||
|
const tokenFile = path.join(dir, fileName);
|
||||||
|
fs.writeFileSync(tokenFile, contents, "utf-8");
|
||||||
|
return tokenFile;
|
||||||
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.unstubAllEnvs();
|
vi.unstubAllEnvs();
|
||||||
|
for (const dir of tempDirs.splice(0)) {
|
||||||
|
fs.rmSync(dir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("prefers config token over env", () => {
|
it.each([
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", "env-token");
|
{
|
||||||
const cfg = {
|
name: "prefers config token over env",
|
||||||
channels: { telegram: { botToken: "cfg-token" } },
|
envToken: "env-token",
|
||||||
} as OpenClawConfig;
|
cfg: {
|
||||||
const res = resolveTelegramToken(cfg);
|
channels: { telegram: { botToken: "cfg-token" } },
|
||||||
expect(res.token).toBe("cfg-token");
|
} as OpenClawConfig,
|
||||||
expect(res.source).toBe("config");
|
expected: { token: "cfg-token", source: "config" },
|
||||||
});
|
},
|
||||||
|
{
|
||||||
it("uses env token when config is missing", () => {
|
name: "uses env token when config is missing",
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", "env-token");
|
envToken: "env-token",
|
||||||
const cfg = {
|
cfg: {
|
||||||
channels: { telegram: {} },
|
channels: { telegram: {} },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig,
|
||||||
const res = resolveTelegramToken(cfg);
|
expected: { token: "env-token", source: "env" },
|
||||||
expect(res.token).toBe("env-token");
|
},
|
||||||
expect(res.source).toBe("env");
|
{
|
||||||
});
|
name: "uses tokenFile when configured",
|
||||||
|
envToken: "",
|
||||||
it("uses tokenFile when configured", () => {
|
cfg: {
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
|
channels: { telegram: { tokenFile: "" } },
|
||||||
const dir = withTempDir();
|
} as OpenClawConfig,
|
||||||
const tokenFile = path.join(dir, "token.txt");
|
resolveCfg: () =>
|
||||||
fs.writeFileSync(tokenFile, "file-token\n", "utf-8");
|
({
|
||||||
const cfg = { channels: { telegram: { tokenFile } } } as OpenClawConfig;
|
channels: { telegram: { tokenFile: createTokenFile("token.txt") } },
|
||||||
const res = resolveTelegramToken(cfg);
|
}) as OpenClawConfig,
|
||||||
expect(res.token).toBe("file-token");
|
expected: { token: "file-token", source: "tokenFile" },
|
||||||
expect(res.source).toBe("tokenFile");
|
},
|
||||||
fs.rmSync(dir, { recursive: true, force: true });
|
{
|
||||||
|
name: "falls back to config token when no env or tokenFile",
|
||||||
|
envToken: "",
|
||||||
|
cfg: {
|
||||||
|
channels: { telegram: { botToken: "cfg-token" } },
|
||||||
|
} as OpenClawConfig,
|
||||||
|
expected: { token: "cfg-token", source: "config" },
|
||||||
|
},
|
||||||
|
])("$name", ({ envToken, cfg, resolveCfg, expected }) => {
|
||||||
|
vi.stubEnv("TELEGRAM_BOT_TOKEN", envToken);
|
||||||
|
const res = resolveTelegramToken(resolveCfg ? resolveCfg() : cfg);
|
||||||
|
expect(res).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.runIf(process.platform !== "win32")("rejects symlinked tokenFile paths", () => {
|
it.runIf(process.platform !== "win32")("rejects symlinked tokenFile paths", () => {
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
|
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
|
||||||
const dir = withTempDir();
|
const dir = createTempDir();
|
||||||
const tokenFile = path.join(dir, "token.txt");
|
const tokenFile = path.join(dir, "token.txt");
|
||||||
const tokenLink = path.join(dir, "token-link.txt");
|
const tokenLink = path.join(dir, "token-link.txt");
|
||||||
fs.writeFileSync(tokenFile, "file-token\n", "utf-8");
|
fs.writeFileSync(tokenFile, "file-token\n", "utf-8");
|
||||||
|
|
@ -60,22 +85,11 @@ describe("resolveTelegramToken", () => {
|
||||||
const res = resolveTelegramToken(cfg);
|
const res = resolveTelegramToken(cfg);
|
||||||
expect(res.token).toBe("");
|
expect(res.token).toBe("");
|
||||||
expect(res.source).toBe("none");
|
expect(res.source).toBe("none");
|
||||||
fs.rmSync(dir, { recursive: true, force: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("falls back to config token when no env or tokenFile", () => {
|
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
|
|
||||||
const cfg = {
|
|
||||||
channels: { telegram: { botToken: "cfg-token" } },
|
|
||||||
} as OpenClawConfig;
|
|
||||||
const res = resolveTelegramToken(cfg);
|
|
||||||
expect(res.token).toBe("cfg-token");
|
|
||||||
expect(res.source).toBe("config");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not fall back to config when tokenFile is missing", () => {
|
it("does not fall back to config when tokenFile is missing", () => {
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
|
vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
|
||||||
const dir = withTempDir();
|
const dir = createTempDir();
|
||||||
const tokenFile = path.join(dir, "missing-token.txt");
|
const tokenFile = path.join(dir, "missing-token.txt");
|
||||||
const cfg = {
|
const cfg = {
|
||||||
channels: { telegram: { tokenFile, botToken: "cfg-token" } },
|
channels: { telegram: { tokenFile, botToken: "cfg-token" } },
|
||||||
|
|
@ -83,7 +97,6 @@ describe("resolveTelegramToken", () => {
|
||||||
const res = resolveTelegramToken(cfg);
|
const res = resolveTelegramToken(cfg);
|
||||||
expect(res.token).toBe("");
|
expect(res.token).toBe("");
|
||||||
expect(res.source).toBe("none");
|
expect(res.source).toBe("none");
|
||||||
fs.rmSync(dir, { recursive: true, force: true });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("resolves per-account tokens when the config account key casing doesn't match routing normalization", () => {
|
it("resolves per-account tokens when the config account key casing doesn't match routing normalization", () => {
|
||||||
|
|
@ -121,14 +134,31 @@ describe("resolveTelegramToken", () => {
|
||||||
expect(res.source).toBe("config");
|
expect(res.source).toBe("config");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to top-level tokenFile for non-default accounts", () => {
|
it("uses account-level tokenFile before top-level fallbacks", () => {
|
||||||
const dir = withTempDir();
|
|
||||||
const tokenFile = path.join(dir, "token.txt");
|
|
||||||
fs.writeFileSync(tokenFile, "file-token\n", "utf-8");
|
|
||||||
const cfg = {
|
const cfg = {
|
||||||
channels: {
|
channels: {
|
||||||
telegram: {
|
telegram: {
|
||||||
tokenFile,
|
botToken: "top-level-token",
|
||||||
|
tokenFile: createTokenFile("top-level-token.txt", "top-level-file-token\n"),
|
||||||
|
accounts: {
|
||||||
|
work: {
|
||||||
|
tokenFile: createTokenFile("account-token.txt", "account-file-token\n"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as OpenClawConfig;
|
||||||
|
|
||||||
|
const res = resolveTelegramToken(cfg, { accountId: "work" });
|
||||||
|
expect(res.token).toBe("account-file-token");
|
||||||
|
expect(res.source).toBe("tokenFile");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to top-level tokenFile for non-default accounts", () => {
|
||||||
|
const cfg = {
|
||||||
|
channels: {
|
||||||
|
telegram: {
|
||||||
|
tokenFile: createTokenFile("token.txt"),
|
||||||
accounts: {
|
accounts: {
|
||||||
work: {},
|
work: {},
|
||||||
},
|
},
|
||||||
|
|
@ -139,7 +169,23 @@ describe("resolveTelegramToken", () => {
|
||||||
const res = resolveTelegramToken(cfg, { accountId: "work" });
|
const res = resolveTelegramToken(cfg, { accountId: "work" });
|
||||||
expect(res.token).toBe("file-token");
|
expect(res.token).toBe("file-token");
|
||||||
expect(res.source).toBe("tokenFile");
|
expect(res.source).toBe("tokenFile");
|
||||||
fs.rmSync(dir, { recursive: true, force: true });
|
});
|
||||||
|
|
||||||
|
it("does not use env token for non-default accounts", () => {
|
||||||
|
vi.stubEnv("TELEGRAM_BOT_TOKEN", "env-token");
|
||||||
|
const cfg = {
|
||||||
|
channels: {
|
||||||
|
telegram: {
|
||||||
|
accounts: {
|
||||||
|
work: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as OpenClawConfig;
|
||||||
|
|
||||||
|
const res = resolveTelegramToken(cfg, { accountId: "work" });
|
||||||
|
expect(res.token).toBe("");
|
||||||
|
expect(res.source).toBe("none");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws when botToken is an unresolved SecretRef object", () => {
|
it("throws when botToken is an unresolved SecretRef object", () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue