test: streamline memory and tts suites

This commit is contained in:
Peter Steinberger
2026-04-25 19:23:37 +01:00
parent 75fcb8c56d
commit e1495c3372
4 changed files with 189 additions and 435 deletions

View File

@@ -3,6 +3,19 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../../media/mime.js", () => ({
detectMime: async (opts: { filePath?: string }) => {
if (opts.filePath?.endsWith(".png")) {
return "image/png";
}
if (opts.filePath?.endsWith(".wav")) {
return "audio/wav";
}
return undefined;
},
}));
import {
buildMultimodalChunkForIndexing,
buildFileEntry,

View File

@@ -1,13 +1,55 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { withTempHome } from "../../test/helpers/temp-home.js";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/types.js";
import { resolveStatusTtsSnapshot } from "./status-config.js";
let fixtureRoot = "";
let fixtureId = 0;
beforeAll(() => {
fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-tts-status-"));
});
afterAll(() => {
if (fixtureRoot) {
fs.rmSync(fixtureRoot, { recursive: true, force: true });
}
});
async function withStatusTempHome(run: (home: string) => Promise<void>): Promise<void> {
const home = path.join(fixtureRoot, `case-${fixtureId++}`);
const previousHome = process.env.HOME;
const previousUserProfile = process.env.USERPROFILE;
const previousOpenClawHome = process.env.OPENCLAW_HOME;
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
fs.mkdirSync(home, { recursive: true });
process.env.HOME = home;
process.env.USERPROFILE = home;
delete process.env.OPENCLAW_HOME;
process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw");
try {
await run(home);
} finally {
restoreEnv("HOME", previousHome);
restoreEnv("USERPROFILE", previousUserProfile);
restoreEnv("OPENCLAW_HOME", previousOpenClawHome);
restoreEnv("OPENCLAW_STATE_DIR", previousStateDir);
}
}
function restoreEnv(key: string, value: string | undefined): void {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
describe("resolveStatusTtsSnapshot", () => {
it("uses prefs overrides without loading speech providers", async () => {
await withTempHome(async (home) => {
await withStatusTempHome(async (home) => {
const prefsPath = path.join(home, ".openclaw", "settings", "tts.json");
fs.mkdirSync(path.dirname(prefsPath), { recursive: true });
fs.writeFileSync(
@@ -42,7 +84,7 @@ describe("resolveStatusTtsSnapshot", () => {
});
it("reports auto provider when tts is on without an explicit provider", async () => {
await withTempHome(async () => {
await withStatusTempHome(async () => {
expect(
resolveStatusTtsSnapshot({
cfg: {
@@ -63,42 +105,40 @@ describe("resolveStatusTtsSnapshot", () => {
});
it("derives the default prefs path from OPENCLAW_CONFIG_PATH when set", async () => {
await withTempHome(
async (home) => {
const stateDir = path.join(home, ".openclaw-dev");
const prefsPath = path.join(stateDir, "settings", "tts.json");
fs.mkdirSync(path.dirname(prefsPath), { recursive: true });
fs.writeFileSync(
prefsPath,
JSON.stringify({
tts: {
auto: "always",
provider: "openai",
},
}),
);
vi.stubEnv("OPENCLAW_CONFIG_PATH", path.join(stateDir, "openclaw.json"));
try {
expect(
resolveStatusTtsSnapshot({
cfg: {
messages: {
tts: {},
},
} as OpenClawConfig,
}),
).toEqual({
autoMode: "always",
await withStatusTempHome(async (home) => {
const stateDir = path.join(home, ".openclaw-dev");
const prefsPath = path.join(stateDir, "settings", "tts.json");
fs.mkdirSync(path.dirname(prefsPath), { recursive: true });
fs.writeFileSync(
prefsPath,
JSON.stringify({
tts: {
auto: "always",
provider: "openai",
maxLength: 1500,
summarize: true,
});
} finally {
vi.unstubAllEnvs();
}
},
{ env: { OPENCLAW_STATE_DIR: undefined } },
);
},
}),
);
delete process.env.OPENCLAW_STATE_DIR;
vi.stubEnv("OPENCLAW_CONFIG_PATH", path.join(stateDir, "openclaw.json"));
try {
expect(
resolveStatusTtsSnapshot({
cfg: {
messages: {
tts: {},
},
} as OpenClawConfig,
}),
).toEqual({
autoMode: "always",
provider: "openai",
maxLength: 1500,
summarize: true,
});
} finally {
vi.unstubAllEnvs();
}
});
});
});

View File

@@ -1,18 +1,31 @@
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { afterAll, beforeAll, afterEach, beforeEach, describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { shouldAttemptTtsPayload } from "./tts-config.js";
describe("shouldAttemptTtsPayload", () => {
let originalPrefsPath: string | undefined;
let root = "";
let dir: string;
let prefsPath: string;
let caseId = 0;
beforeAll(() => {
root = mkdtempSync(path.join(tmpdir(), "openclaw-tts-config-"));
});
afterAll(() => {
if (root) {
rmSync(root, { recursive: true, force: true });
}
});
beforeEach(() => {
originalPrefsPath = process.env.OPENCLAW_TTS_PREFS;
dir = mkdtempSync(path.join(tmpdir(), "openclaw-tts-config-"));
dir = path.join(root, `case-${caseId++}`);
mkdirSync(dir, { recursive: true });
prefsPath = path.join(dir, "tts.json");
process.env.OPENCLAW_TTS_PREFS = prefsPath;
});
@@ -23,7 +36,6 @@ describe("shouldAttemptTtsPayload", () => {
} else {
process.env.OPENCLAW_TTS_PREFS = originalPrefsPath;
}
rmSync(dir, { recursive: true, force: true });
});
it("skips TTS when config, prefs, and session state leave auto mode off", () => {