mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 04:41:25 +00:00
test(infra): dedupe store temp fixtures and cover json5 voicewake sanitization
This commit is contained in:
@@ -20,10 +20,19 @@ import {
|
|||||||
setVoiceWakeTriggers,
|
setVoiceWakeTriggers,
|
||||||
} from "./voicewake.js";
|
} from "./voicewake.js";
|
||||||
|
|
||||||
|
async function withTempDir(prefix: string, run: (dir: string) => Promise<void>) {
|
||||||
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||||
|
try {
|
||||||
|
await run(dir);
|
||||||
|
} finally {
|
||||||
|
await fs.rm(dir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("infra store", () => {
|
describe("infra store", () => {
|
||||||
describe("state migrations fs", () => {
|
describe("state migrations fs", () => {
|
||||||
it("treats array session stores as invalid", async () => {
|
it("treats array session stores as invalid", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-store-"));
|
await withTempDir("openclaw-session-store-", async (dir) => {
|
||||||
const storePath = path.join(dir, "sessions.json");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
await fs.writeFile(storePath, "[]", "utf-8");
|
await fs.writeFile(storePath, "[]", "utf-8");
|
||||||
|
|
||||||
@@ -33,16 +42,34 @@ describe("infra store", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("parses JSON5 object session stores", async () => {
|
||||||
|
await withTempDir("openclaw-session-store-", async (dir) => {
|
||||||
|
const storePath = path.join(dir, "sessions.json");
|
||||||
|
await fs.writeFile(
|
||||||
|
storePath,
|
||||||
|
"{\n // comment allowed in JSON5\n main: { sessionId: 's1', updatedAt: 123 },\n}\n",
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = readSessionStoreJson5(storePath);
|
||||||
|
expect(result.ok).toBe(true);
|
||||||
|
expect(result.store.main?.sessionId).toBe("s1");
|
||||||
|
expect(result.store.main?.updatedAt).toBe(123);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("voicewake store", () => {
|
describe("voicewake store", () => {
|
||||||
it("returns defaults when missing", async () => {
|
it("returns defaults when missing", async () => {
|
||||||
const baseDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-voicewake-"));
|
await withTempDir("openclaw-voicewake-", async (baseDir) => {
|
||||||
const cfg = await loadVoiceWakeConfig(baseDir);
|
const cfg = await loadVoiceWakeConfig(baseDir);
|
||||||
expect(cfg.triggers).toEqual(defaultVoiceWakeTriggers());
|
expect(cfg.triggers).toEqual(defaultVoiceWakeTriggers());
|
||||||
expect(cfg.updatedAtMs).toBe(0);
|
expect(cfg.updatedAtMs).toBe(0);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("sanitizes and persists triggers", async () => {
|
it("sanitizes and persists triggers", async () => {
|
||||||
const baseDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-voicewake-"));
|
await withTempDir("openclaw-voicewake-", async (baseDir) => {
|
||||||
const saved = await setVoiceWakeTriggers([" hi ", "", " there "], baseDir);
|
const saved = await setVoiceWakeTriggers([" hi ", "", " there "], baseDir);
|
||||||
expect(saved.triggers).toEqual(["hi", "there"]);
|
expect(saved.triggers).toEqual(["hi", "there"]);
|
||||||
expect(saved.updatedAtMs).toBeGreaterThan(0);
|
expect(saved.updatedAtMs).toBeGreaterThan(0);
|
||||||
@@ -51,14 +78,34 @@ describe("infra store", () => {
|
|||||||
expect(loaded.triggers).toEqual(["hi", "there"]);
|
expect(loaded.triggers).toEqual(["hi", "there"]);
|
||||||
expect(loaded.updatedAtMs).toBeGreaterThan(0);
|
expect(loaded.updatedAtMs).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("falls back to defaults when triggers empty", async () => {
|
it("falls back to defaults when triggers empty", async () => {
|
||||||
const baseDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-voicewake-"));
|
await withTempDir("openclaw-voicewake-", async (baseDir) => {
|
||||||
const saved = await setVoiceWakeTriggers(["", " "], baseDir);
|
const saved = await setVoiceWakeTriggers(["", " "], baseDir);
|
||||||
expect(saved.triggers).toEqual(defaultVoiceWakeTriggers());
|
expect(saved.triggers).toEqual(defaultVoiceWakeTriggers());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("sanitizes malformed persisted config values", async () => {
|
||||||
|
await withTempDir("openclaw-voicewake-", async (baseDir) => {
|
||||||
|
await fs.mkdir(path.join(baseDir, "settings"), { recursive: true });
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(baseDir, "settings", "voicewake.json"),
|
||||||
|
JSON.stringify({
|
||||||
|
triggers: [" wake ", "", 42, null],
|
||||||
|
updatedAtMs: -1,
|
||||||
|
}),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const loaded = await loadVoiceWakeConfig(baseDir);
|
||||||
|
expect(loaded.triggers).toEqual(["wake"]);
|
||||||
|
expect(loaded.updatedAtMs).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("diagnostic-events", () => {
|
describe("diagnostic-events", () => {
|
||||||
it("emits monotonic seq", async () => {
|
it("emits monotonic seq", async () => {
|
||||||
resetDiagnosticEventsForTest();
|
resetDiagnosticEventsForTest();
|
||||||
|
|||||||
Reference in New Issue
Block a user