test(utils): await temp dir cleanup in async tests

This commit is contained in:
Peter Steinberger 2026-03-13 04:38:40 +00:00
parent 16ececf0a6
commit c38e7b0270
1 changed files with 13 additions and 10 deletions

View File

@ -18,10 +18,13 @@ import {
toWhatsappJid,
} from "./utils.js";
function withTempDirSync<T>(prefix: string, run: (dir: string) => T): T {
async function withTempDir<T>(
prefix: string,
run: (dir: string) => T | Promise<T>,
): Promise<Awaited<T>> {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
try {
return run(dir);
return await run(dir);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
@ -29,7 +32,7 @@ function withTempDirSync<T>(prefix: string, run: (dir: string) => T): T {
describe("ensureDir", () => {
it("creates nested directory", async () => {
await withTempDirSync("openclaw-test-", async (tmp) => {
await withTempDir("openclaw-test-", async (tmp) => {
const target = path.join(tmp, "nested", "dir");
await ensureDir(target);
expect(fs.existsSync(target)).toBe(true);
@ -84,16 +87,16 @@ describe("jidToE164", () => {
spy.mockRestore();
});
it("maps @lid from authDir mapping files", () => {
withTempDirSync("openclaw-auth-", (authDir) => {
it("maps @lid from authDir mapping files", async () => {
await withTempDir("openclaw-auth-", (authDir) => {
const mappingPath = path.join(authDir, "lid-mapping-456_reverse.json");
fs.writeFileSync(mappingPath, JSON.stringify("5559876"));
expect(jidToE164("456@lid", { authDir })).toBe("+5559876");
});
});
it("maps @hosted.lid from authDir mapping files", () => {
withTempDirSync("openclaw-auth-", (authDir) => {
it("maps @hosted.lid from authDir mapping files", async () => {
await withTempDir("openclaw-auth-", (authDir) => {
const mappingPath = path.join(authDir, "lid-mapping-789_reverse.json");
fs.writeFileSync(mappingPath, JSON.stringify(4440001));
expect(jidToE164("789@hosted.lid", { authDir })).toBe("+4440001");
@ -104,9 +107,9 @@ describe("jidToE164", () => {
expect(jidToE164("1555000:2@hosted")).toBe("+1555000");
});
it("falls back through lidMappingDirs in order", () => {
withTempDirSync("openclaw-lid-a-", (first) => {
withTempDirSync("openclaw-lid-b-", (second) => {
it("falls back through lidMappingDirs in order", async () => {
await withTempDir("openclaw-lid-a-", async (first) => {
await withTempDir("openclaw-lid-b-", (second) => {
const mappingPath = path.join(second, "lid-mapping-321_reverse.json");
fs.writeFileSync(mappingPath, JSON.stringify("123321"));
expect(jidToE164("321@lid", { lidMappingDirs: [first, second] })).toBe("+123321");