



















@@ -0,0 +1,98 @@
1+import { appendFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+import { tmpdir } from "node:os";
3+import path from "node:path";
4+import { afterEach, describe, expect, it } from "vitest";
5+import { createConfigReloadLogScanner } from "../../scripts/e2e/lib/config-reload/log-scanner.mjs";
6+7+const tempRoots: string[] = [];
8+9+function makeTempRoot(): string {
10+const root = mkdtempSync(path.join(tmpdir(), "openclaw-config-reload-log-"));
11+tempRoots.push(root);
12+return root;
13+}
14+15+afterEach(() => {
16+for (const root of tempRoots.splice(0)) {
17+rmSync(root, { recursive: true, force: true });
18+}
19+});
20+21+describe("config reload log scanner", () => {
22+it("keeps previous matches while reading only appended log lines", () => {
23+const logPath = path.join(makeTempRoot(), "gateway.log");
24+const scanner = createConfigReloadLogScanner(logPath, {
25+maxReadBytes: 1024,
26+tailLineLimit: 4,
27+});
28+29+expect(scanner.scan()).toEqual({ reloadLines: [], restartLines: [], tailLines: [] });
30+31+writeFileSync(logPath, "gateway boot\n");
32+expect(scanner.scan()).toEqual({
33+reloadLines: [],
34+restartLines: [],
35+tailLines: ["gateway boot"],
36+});
37+38+appendFileSync(logPath, "config change detected; evaluating reload: plugins.entries.demo\n");
39+expect(scanner.scan().reloadLines).toEqual([
40+"config change detected; evaluating reload: plugins.entries.demo",
41+]);
42+43+appendFileSync(logPath, "later noise\n");
44+expect(scanner.scan().reloadLines).toEqual([
45+"config change detected; evaluating reload: plugins.entries.demo",
46+]);
47+});
48+49+it("preserves partial lines between polls", () => {
50+const logPath = path.join(makeTempRoot(), "gateway.log");
51+const scanner = createConfigReloadLogScanner(logPath, {
52+maxReadBytes: 1024,
53+tailLineLimit: 4,
54+});
55+56+writeFileSync(logPath, "config change detected");
57+expect(scanner.scan().reloadLines).toEqual([]);
58+59+appendFileSync(logPath, "; evaluating reload: gateway.channelHealthCheckMinutes\n");
60+expect(scanner.scan().reloadLines).toEqual([
61+"config change detected; evaluating reload: gateway.channelHealthCheckMinutes",
62+]);
63+});
64+65+it("starts from a bounded tail of oversized logs", () => {
66+const logPath = path.join(makeTempRoot(), "gateway.log");
67+const reloadLine =
68+"config change detected; evaluating reload: gateway.channelHealthCheckMinutes\n";
69+writeFileSync(logPath, `${"x".repeat(4096)}\n${reloadLine}`);
70+71+const scanner = createConfigReloadLogScanner(logPath, {
72+maxReadBytes: reloadLine.length,
73+tailLineLimit: 4,
74+});
75+76+expect(scanner.scan().reloadLines).toEqual([
77+"config change detected; evaluating reload: gateway.channelHealthCheckMinutes",
78+]);
79+});
80+81+it("resets accumulated matches when the log rotates", () => {
82+const logPath = path.join(makeTempRoot(), "gateway.log");
83+const scanner = createConfigReloadLogScanner(logPath, {
84+maxReadBytes: 1024,
85+tailLineLimit: 4,
86+});
87+88+writeFileSync(logPath, "config change detected; evaluating reload: old.path\n");
89+expect(scanner.scan().reloadLines).toEqual([
90+"config change detected; evaluating reload: old.path",
91+]);
92+93+writeFileSync(logPath, "config change requires gateway restart: new.path\n");
94+const result = scanner.scan();
95+expect(result.reloadLines).toEqual([]);
96+expect(result.restartLines).toEqual(["config change requires gateway restart: new.path"]);
97+});
98+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。