fix: stabilize CI path assumptions
This commit is contained in:
@@ -331,7 +331,11 @@ actor MacNodeBridgeSession {
|
|||||||
let now = self.clock.now
|
let now = self.clock.now
|
||||||
if now > last.advanced(by: timeout) {
|
if now > last.advanced(by: timeout) {
|
||||||
let age = last.duration(to: now)
|
let age = last.duration(to: now)
|
||||||
self.logger.warning("Node bridge heartbeat timed out; disconnecting (age: \(String(describing: age), privacy: .public)).")
|
let ageDescription = String(describing: age)
|
||||||
|
let message =
|
||||||
|
"Node bridge heartbeat timed out; disconnecting " +
|
||||||
|
"(age: \(ageDescription, privacy: .public))."
|
||||||
|
self.logger.warning(message)
|
||||||
await self.disconnect()
|
await self.disconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -341,7 +345,11 @@ actor MacNodeBridgeSession {
|
|||||||
do {
|
do {
|
||||||
try await self.send(BridgePing(type: "ping", id: id))
|
try await self.send(BridgePing(type: "ping", id: id))
|
||||||
} catch {
|
} catch {
|
||||||
self.logger.warning("Node bridge ping send failed; disconnecting (error: \(String(describing: error), privacy: .public)).")
|
let errorDescription = String(describing: error)
|
||||||
|
let message =
|
||||||
|
"Node bridge ping send failed; disconnecting " +
|
||||||
|
"(error: \(errorDescription, privacy: .public))."
|
||||||
|
self.logger.warning(message)
|
||||||
await self.disconnect()
|
await self.disconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -356,7 +364,11 @@ actor MacNodeBridgeSession {
|
|||||||
private func handleConnectionState(_ state: NWConnection.State) async {
|
private func handleConnectionState(_ state: NWConnection.State) async {
|
||||||
switch state {
|
switch state {
|
||||||
case let .failed(error):
|
case let .failed(error):
|
||||||
self.logger.warning("Node bridge connection failed; disconnecting (error: \(String(describing: error), privacy: .public)).")
|
let errorDescription = String(describing: error)
|
||||||
|
let message =
|
||||||
|
"Node bridge connection failed; disconnecting " +
|
||||||
|
"(error: \(errorDescription, privacy: .public))."
|
||||||
|
self.logger.warning(message)
|
||||||
await self.disconnect()
|
await self.disconnect()
|
||||||
case .cancelled:
|
case .cancelled:
|
||||||
self.logger.warning("Node bridge connection cancelled; disconnecting.")
|
self.logger.warning("Node bridge connection cancelled; disconnecting.")
|
||||||
|
|||||||
@@ -2,39 +2,29 @@ import fs from "node:fs/promises";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import JSZip from "jszip";
|
import JSZip from "jszip";
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const realOs = await vi.importActual<typeof import("node:os")>("node:os");
|
import { withTempHome } from "../../test/helpers/temp-home.js";
|
||||||
const HOME = path.join(realOs.tmpdir(), "clawdbot-home-test");
|
|
||||||
|
|
||||||
vi.mock("node:os", () => ({
|
|
||||||
default: { homedir: () => HOME, tmpdir: () => realOs.tmpdir() },
|
|
||||||
homedir: () => HOME,
|
|
||||||
tmpdir: () => realOs.tmpdir(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const store = await import("./store.js");
|
|
||||||
|
|
||||||
describe("media store", () => {
|
describe("media store", () => {
|
||||||
beforeAll(async () => {
|
|
||||||
await fs.rm(HOME, { recursive: true, force: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
await fs.rm(HOME, { recursive: true, force: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("creates and returns media directory", async () => {
|
it("creates and returns media directory", async () => {
|
||||||
|
await withTempHome(async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
const store = await import("./store.js");
|
||||||
|
|
||||||
const dir = await store.ensureMediaDir();
|
const dir = await store.ensureMediaDir();
|
||||||
const normalized = path.normalize(dir);
|
const normalized = path.normalize(dir);
|
||||||
expect(normalized).toContain(
|
expect(normalized).toContain(`${path.sep}.clawdbot${path.sep}media`);
|
||||||
`${path.sep}.clawdbot${path.sep}media`,
|
|
||||||
);
|
|
||||||
const stat = await fs.stat(dir);
|
const stat = await fs.stat(dir);
|
||||||
expect(stat.isDirectory()).toBe(true);
|
expect(stat.isDirectory()).toBe(true);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("saves buffers and enforces size limit", async () => {
|
it("saves buffers and enforces size limit", async () => {
|
||||||
|
await withTempHome(async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
const store = await import("./store.js");
|
||||||
|
|
||||||
const buf = Buffer.from("hello");
|
const buf = Buffer.from("hello");
|
||||||
const saved = await store.saveMediaBuffer(buf, "text/plain");
|
const saved = await store.saveMediaBuffer(buf, "text/plain");
|
||||||
const savedStat = await fs.stat(saved.path);
|
const savedStat = await fs.stat(saved.path);
|
||||||
@@ -56,10 +46,15 @@ describe("media store", () => {
|
|||||||
"Media exceeds 5MB limit",
|
"Media exceeds 5MB limit",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("copies local files and cleans old media", async () => {
|
it("copies local files and cleans old media", async () => {
|
||||||
const srcFile = path.join(HOME, "tmp-src.txt");
|
await withTempHome(async (home) => {
|
||||||
await fs.mkdir(HOME, { recursive: true });
|
vi.resetModules();
|
||||||
|
const store = await import("./store.js");
|
||||||
|
|
||||||
|
const srcFile = path.join(home, "tmp-src.txt");
|
||||||
|
await fs.mkdir(home, { recursive: true });
|
||||||
await fs.writeFile(srcFile, "local file");
|
await fs.writeFile(srcFile, "local file");
|
||||||
const saved = await store.saveMediaSource(srcFile);
|
const saved = await store.saveMediaSource(srcFile);
|
||||||
expect(saved.size).toBe(10);
|
expect(saved.size).toBe(10);
|
||||||
@@ -73,10 +68,15 @@ describe("media store", () => {
|
|||||||
await store.cleanOldMedia(1);
|
await store.cleanOldMedia(1);
|
||||||
await expect(fs.stat(saved.path)).rejects.toThrow();
|
await expect(fs.stat(saved.path)).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("sets correct mime for xlsx by extension", async () => {
|
it("sets correct mime for xlsx by extension", async () => {
|
||||||
const xlsxPath = path.join(HOME, "sheet.xlsx");
|
await withTempHome(async (home) => {
|
||||||
await fs.mkdir(HOME, { recursive: true });
|
vi.resetModules();
|
||||||
|
const store = await import("./store.js");
|
||||||
|
|
||||||
|
const xlsxPath = path.join(home, "sheet.xlsx");
|
||||||
|
await fs.mkdir(home, { recursive: true });
|
||||||
await fs.writeFile(xlsxPath, "not really an xlsx");
|
await fs.writeFile(xlsxPath, "not really an xlsx");
|
||||||
|
|
||||||
const saved = await store.saveMediaSource(xlsxPath);
|
const saved = await store.saveMediaSource(xlsxPath);
|
||||||
@@ -85,14 +85,19 @@ describe("media store", () => {
|
|||||||
);
|
);
|
||||||
expect(path.extname(saved.path)).toBe(".xlsx");
|
expect(path.extname(saved.path)).toBe(".xlsx");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("renames media based on detected mime even when extension is wrong", async () => {
|
it("renames media based on detected mime even when extension is wrong", async () => {
|
||||||
|
await withTempHome(async (home) => {
|
||||||
|
vi.resetModules();
|
||||||
|
const store = await import("./store.js");
|
||||||
|
|
||||||
const pngBytes = await sharp({
|
const pngBytes = await sharp({
|
||||||
create: { width: 2, height: 2, channels: 3, background: "#00ff00" },
|
create: { width: 2, height: 2, channels: 3, background: "#00ff00" },
|
||||||
})
|
})
|
||||||
.png()
|
.png()
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
const bogusExt = path.join(HOME, "image-wrong.bin");
|
const bogusExt = path.join(home, "image-wrong.bin");
|
||||||
await fs.writeFile(bogusExt, pngBytes);
|
await fs.writeFile(bogusExt, pngBytes);
|
||||||
|
|
||||||
const saved = await store.saveMediaSource(bogusExt);
|
const saved = await store.saveMediaSource(bogusExt);
|
||||||
@@ -102,8 +107,13 @@ describe("media store", () => {
|
|||||||
const buf = await fs.readFile(saved.path);
|
const buf = await fs.readFile(saved.path);
|
||||||
expect(buf.equals(pngBytes)).toBe(true);
|
expect(buf.equals(pngBytes)).toBe(true);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("sniffs xlsx mime for zip buffers and renames extension", async () => {
|
it("sniffs xlsx mime for zip buffers and renames extension", async () => {
|
||||||
|
await withTempHome(async (home) => {
|
||||||
|
vi.resetModules();
|
||||||
|
const store = await import("./store.js");
|
||||||
|
|
||||||
const zip = new JSZip();
|
const zip = new JSZip();
|
||||||
zip.file(
|
zip.file(
|
||||||
"[Content_Types].xml",
|
"[Content_Types].xml",
|
||||||
@@ -111,7 +121,7 @@ describe("media store", () => {
|
|||||||
);
|
);
|
||||||
zip.file("xl/workbook.xml", "<workbook/>");
|
zip.file("xl/workbook.xml", "<workbook/>");
|
||||||
const fakeXlsx = await zip.generateAsync({ type: "nodebuffer" });
|
const fakeXlsx = await zip.generateAsync({ type: "nodebuffer" });
|
||||||
const bogusExt = path.join(HOME, "sheet.bin");
|
const bogusExt = path.join(home, "sheet.bin");
|
||||||
await fs.writeFile(bogusExt, fakeXlsx);
|
await fs.writeFile(bogusExt, fakeXlsx);
|
||||||
|
|
||||||
const saved = await store.saveMediaSource(bogusExt);
|
const saved = await store.saveMediaSource(bogusExt);
|
||||||
@@ -121,3 +131,4 @@ describe("media store", () => {
|
|||||||
expect(path.extname(saved.path)).toBe(".xlsx");
|
expect(path.extname(saved.path)).toBe(".xlsx");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import fsPromises from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { withTempHome } from "../../test/helpers/temp-home.js";
|
||||||
|
|
||||||
const runtime = {
|
const runtime = {
|
||||||
log: vi.fn(),
|
log: vi.fn(),
|
||||||
error: vi.fn(),
|
error: vi.fn(),
|
||||||
@@ -12,71 +12,58 @@ const runtime = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe("web logout", () => {
|
describe("web logout", () => {
|
||||||
const origHomedir = os.homedir;
|
|
||||||
let tmpDir: string;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-logout-"));
|
|
||||||
vi.spyOn(os, "homedir").mockReturnValue(tmpDir);
|
|
||||||
vi.resetModules();
|
|
||||||
vi.doMock("../utils.js", async () => {
|
|
||||||
const actual =
|
|
||||||
await vi.importActual<typeof import("../utils.js")>("../utils.js");
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
CONFIG_DIR: path.join(tmpDir, ".clawdbot"),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(() => {
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
vi.doUnmock("../utils.js");
|
|
||||||
await fsPromises
|
|
||||||
.rm(tmpDir, { recursive: true, force: true })
|
|
||||||
.catch(() => {});
|
|
||||||
// restore for safety
|
|
||||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
||||||
(os.homedir as unknown as typeof origHomedir) = origHomedir;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it(
|
it(
|
||||||
"deletes cached credentials when present",
|
"deletes cached credentials when present",
|
||||||
{ timeout: 15_000 },
|
{ timeout: 15_000 },
|
||||||
async () => {
|
async () => {
|
||||||
|
await withTempHome(async (home) => {
|
||||||
|
vi.resetModules();
|
||||||
const { logoutWeb, WA_WEB_AUTH_DIR } = await import("./session.js");
|
const { logoutWeb, WA_WEB_AUTH_DIR } = await import("./session.js");
|
||||||
|
|
||||||
const normalizedAuthDir = path.resolve(WA_WEB_AUTH_DIR);
|
const rel = path.relative(
|
||||||
const normalizedHome = path.resolve(tmpDir);
|
path.resolve(home),
|
||||||
if (process.platform === "win32") {
|
path.resolve(WA_WEB_AUTH_DIR),
|
||||||
expect(
|
);
|
||||||
normalizedAuthDir
|
expect(rel && !rel.startsWith("..") && !path.isAbsolute(rel)).toBe(
|
||||||
.toLowerCase()
|
true,
|
||||||
.startsWith(normalizedHome.toLowerCase()),
|
);
|
||||||
).toBe(true);
|
|
||||||
} else {
|
|
||||||
expect(normalizedAuthDir.startsWith(normalizedHome)).toBe(true);
|
|
||||||
}
|
|
||||||
fs.mkdirSync(WA_WEB_AUTH_DIR, { recursive: true });
|
fs.mkdirSync(WA_WEB_AUTH_DIR, { recursive: true });
|
||||||
fs.writeFileSync(path.join(WA_WEB_AUTH_DIR, "creds.json"), "{}");
|
fs.writeFileSync(path.join(WA_WEB_AUTH_DIR, "creds.json"), "{}");
|
||||||
const result = await logoutWeb({ runtime: runtime as never });
|
const result = await logoutWeb({ runtime: runtime as never });
|
||||||
|
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
expect(fs.existsSync(WA_WEB_AUTH_DIR)).toBe(false);
|
expect(fs.existsSync(WA_WEB_AUTH_DIR)).toBe(false);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
it("no-ops when nothing to delete", { timeout: 15_000 }, async () => {
|
it("no-ops when nothing to delete", { timeout: 15_000 }, async () => {
|
||||||
|
await withTempHome(async () => {
|
||||||
|
vi.resetModules();
|
||||||
const { logoutWeb } = await import("./session.js");
|
const { logoutWeb } = await import("./session.js");
|
||||||
const result = await logoutWeb({ runtime: runtime as never });
|
const result = await logoutWeb({ runtime: runtime as never });
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
expect(runtime.log).toHaveBeenCalled();
|
expect(runtime.log).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("keeps shared oauth.json when using legacy auth dir", async () => {
|
it("keeps shared oauth.json when using legacy auth dir", async () => {
|
||||||
|
await withTempHome(async () => {
|
||||||
|
vi.resetModules();
|
||||||
const { logoutWeb } = await import("./session.js");
|
const { logoutWeb } = await import("./session.js");
|
||||||
const credsDir = path.join(tmpDir, ".clawdbot", "credentials");
|
|
||||||
|
const { resolveOAuthDir } = await import("../config/paths.js");
|
||||||
|
const credsDir = resolveOAuthDir();
|
||||||
|
|
||||||
fs.mkdirSync(credsDir, { recursive: true });
|
fs.mkdirSync(credsDir, { recursive: true });
|
||||||
fs.writeFileSync(path.join(credsDir, "creds.json"), "{}");
|
fs.writeFileSync(path.join(credsDir, "creds.json"), "{}");
|
||||||
fs.writeFileSync(path.join(credsDir, "oauth.json"), '{"token":true}');
|
fs.writeFileSync(path.join(credsDir, "oauth.json"), '{"token":true}');
|
||||||
@@ -90,6 +77,9 @@ describe("web logout", () => {
|
|||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
expect(fs.existsSync(path.join(credsDir, "oauth.json"))).toBe(true);
|
expect(fs.existsSync(path.join(credsDir, "oauth.json"))).toBe(true);
|
||||||
expect(fs.existsSync(path.join(credsDir, "creds.json"))).toBe(false);
|
expect(fs.existsSync(path.join(credsDir, "creds.json"))).toBe(false);
|
||||||
expect(fs.existsSync(path.join(credsDir, "session-abc.json"))).toBe(false);
|
expect(fs.existsSync(path.join(credsDir, "session-abc.json"))).toBe(
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user