|
| 1 | +import { EventEmitter } from "node:events"; |
1 | 2 | import fs from "node:fs"; |
2 | 3 | import net from "node:net"; |
3 | 4 | import os from "node:os"; |
4 | 5 | import path from "node:path"; |
5 | | -import { afterEach, describe, expect, it } from "vitest"; |
6 | | -import { linuxCanvasSocketExists } from "./socket-path.js"; |
| 6 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { linuxCanvasSocketExists, watchLinuxCanvasSocket } from "./socket-path.js"; |
| 8 | + |
| 9 | +const loggerMocks = vi.hoisted(() => ({ warn: vi.fn() })); |
| 10 | + |
| 11 | +vi.mock("openclaw/plugin-sdk/runtime-env", () => ({ |
| 12 | + createSubsystemLogger: () => loggerMocks, |
| 13 | +})); |
7 | 14 |
|
8 | 15 | const tempDirs: string[] = []; |
9 | 16 |
|
10 | 17 | afterEach(() => { |
| 18 | + vi.restoreAllMocks(); |
| 19 | + loggerMocks.warn.mockReset(); |
11 | 20 | for (const dir of tempDirs.splice(0)) { |
12 | 21 | fs.rmSync(dir, { recursive: true, force: true }); |
13 | 22 | } |
@@ -41,4 +50,31 @@ describe("Linux Canvas socket availability", () => { |
41 | 50 | expect(linuxCanvasSocketExists(socketPath)).toBe(false); |
42 | 51 | }, |
43 | 52 | ); |
| 53 | + |
| 54 | + it("reports synchronous and asynchronous watcher failures before falling back to polling", () => { |
| 55 | + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-linux-canvas-watch-")); |
| 56 | + tempDirs.push(directory); |
| 57 | + const socketPath = path.join(directory, "canvas.sock"); |
| 58 | + const watcher = Object.assign(new EventEmitter(), { close: vi.fn() }); |
| 59 | + const watch = vi |
| 60 | + .spyOn(fs, "watch") |
| 61 | + .mockImplementationOnce(() => watcher as unknown as fs.FSWatcher) |
| 62 | + .mockImplementationOnce(() => { |
| 63 | + throw new Error("EMFILE"); |
| 64 | + }); |
| 65 | + |
| 66 | + const stop = watchLinuxCanvasSocket(socketPath, vi.fn()); |
| 67 | + watcher.emit("error", new Error("ENOSPC")); |
| 68 | + expect(loggerMocks.warn).toHaveBeenCalledWith( |
| 69 | + expect.stringContaining("continuing with availability polling: Error: ENOSPC"), |
| 70 | + ); |
| 71 | + stop(); |
| 72 | + expect(watcher.close).toHaveBeenCalledOnce(); |
| 73 | + |
| 74 | + expect(() => watchLinuxCanvasSocket(socketPath, vi.fn())).not.toThrow(); |
| 75 | + expect(loggerMocks.warn).toHaveBeenCalledWith( |
| 76 | + expect.stringContaining("continuing with availability polling: Error: EMFILE"), |
| 77 | + ); |
| 78 | + expect(watch).toHaveBeenCalledTimes(2); |
| 79 | + }); |
44 | 80 | }); |
0 commit comments