mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 08:12:43 +00:00
perf: skip cache-busting for bundled hooks, use mtime for workspace hooks (openclaw#16960) thanks @mudrii
Verified: - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: mudrii <220262+mudrii@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
62
src/hooks/import-url.test.ts
Normal file
62
src/hooks/import-url.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { buildImportUrl } from "./import-url.js";
|
||||
|
||||
describe("buildImportUrl", () => {
|
||||
let tmpDir: string;
|
||||
let tmpFile: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "import-url-test-"));
|
||||
tmpFile = path.join(tmpDir, "handler.js");
|
||||
fs.writeFileSync(tmpFile, "export default () => {};");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("returns bare URL for bundled hooks (no query string)", () => {
|
||||
const url = buildImportUrl(tmpFile, "openclaw-bundled");
|
||||
expect(url).not.toContain("?t=");
|
||||
expect(url).toMatch(/^file:\/\//);
|
||||
});
|
||||
|
||||
it("appends mtime-based cache buster for workspace hooks", () => {
|
||||
const url = buildImportUrl(tmpFile, "openclaw-workspace");
|
||||
expect(url).toMatch(/\?t=[\d.]+&s=\d+/);
|
||||
|
||||
const { mtimeMs, size } = fs.statSync(tmpFile);
|
||||
expect(url).toContain(`?t=${mtimeMs}`);
|
||||
expect(url).toContain(`&s=${size}`);
|
||||
});
|
||||
|
||||
it("appends mtime-based cache buster for managed hooks", () => {
|
||||
const url = buildImportUrl(tmpFile, "openclaw-managed");
|
||||
expect(url).toMatch(/\?t=[\d.]+&s=\d+/);
|
||||
});
|
||||
|
||||
it("appends mtime-based cache buster for plugin hooks", () => {
|
||||
const url = buildImportUrl(tmpFile, "openclaw-plugin");
|
||||
expect(url).toMatch(/\?t=[\d.]+&s=\d+/);
|
||||
});
|
||||
|
||||
it("returns same URL for bundled hooks across calls (cacheable)", () => {
|
||||
const url1 = buildImportUrl(tmpFile, "openclaw-bundled");
|
||||
const url2 = buildImportUrl(tmpFile, "openclaw-bundled");
|
||||
expect(url1).toBe(url2);
|
||||
});
|
||||
|
||||
it("returns same URL for workspace hooks when file is unchanged", () => {
|
||||
const url1 = buildImportUrl(tmpFile, "openclaw-workspace");
|
||||
const url2 = buildImportUrl(tmpFile, "openclaw-workspace");
|
||||
expect(url1).toBe(url2);
|
||||
});
|
||||
|
||||
it("falls back to Date.now() when file does not exist", () => {
|
||||
const url = buildImportUrl("/nonexistent/handler.js", "openclaw-workspace");
|
||||
expect(url).toMatch(/\?t=\d+/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user