Files
thedotmack--claude-mem/tests/shared/worker-utils-timeout.test.ts
wehub-resource-sync f9447f8e5f
CI / typecheck · build · test · bundle-size (push) Failing after 1s
CI / clean-room dependency closure smoke (push) Failing after 1s
CI / server-runtime e2e (docker · pg + valkey) (push) Failing after 2s
Deploy Install Scripts / deploy (push) Failing after 2s
Windows / build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:07:03 +08:00

88 lines
3.7 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { SettingsDefaultsManager } from '../../src/shared/SettingsDefaultsManager.js';
// Eagerly evaluate src/shared/paths.ts BEFORE any per-test env override:
// paths.ts freezes its DATA_DIR const at first evaluation, and without this
// import the dynamic `import('../../src/shared/worker-utils.js')` calls
// below can be the first to evaluate it — while the env var points at a
// soon-deleted per-test temp dir — poisoning every later-loaded module in the
// same bun process (e.g. ProcessManager's PID_FILE in combined runs). At this
// point the env var is the per-RUN temp dir pinned by the preload tripwire
// (tests/preload.ts), so paths.ts freezes on a stable, isolated dir that
// outlives this file. worker-utils itself is unaffected: it resolves its
// settings path at call time via SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR').
import '../../src/shared/paths.js';
describe('worker-utils API timeout resolution', () => {
let tempDir: string;
let settingsPath: string;
const originalDataDir = process.env.CLAUDE_MEM_DATA_DIR;
const originalTimeout = process.env.CLAUDE_MEM_API_TIMEOUT_MS;
beforeEach(() => {
tempDir = join(tmpdir(), `worker-timeout-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
settingsPath = join(tempDir, 'settings.json');
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
delete process.env.CLAUDE_MEM_API_TIMEOUT_MS;
mock.restore();
});
afterEach(() => {
mock.restore();
if (originalDataDir === undefined) delete process.env.CLAUDE_MEM_DATA_DIR;
else process.env.CLAUDE_MEM_DATA_DIR = originalDataDir;
if (originalTimeout === undefined) delete process.env.CLAUDE_MEM_API_TIMEOUT_MS;
else process.env.CLAUDE_MEM_API_TIMEOUT_MS = originalTimeout;
rmSync(tempDir, { recursive: true, force: true });
});
function writeSettings(timeout: string): void {
const settings = SettingsDefaultsManager.getAllDefaults();
settings.CLAUDE_MEM_DATA_DIR = tempDir;
settings.CLAUDE_MEM_API_TIMEOUT_MS = timeout;
writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf-8');
}
it('uses settings.json timeout when no env override is present', async () => {
writeSettings('45000');
const workerUtils = await import('../../src/shared/worker-utils.js');
workerUtils.clearPortCache();
expect(workerUtils.getWorkerApiRequestTimeoutMs()).toBe(45000);
});
it('prefers env timeout over settings.json', async () => {
writeSettings('45000');
process.env.CLAUDE_MEM_API_TIMEOUT_MS = '1200';
const workerUtils = await import('../../src/shared/worker-utils.js');
workerUtils.clearPortCache();
expect(workerUtils.getWorkerApiRequestTimeoutMs()).toBe(1200);
});
it('warns and falls back to default when env timeout is invalid', async () => {
writeSettings('45000');
process.env.CLAUDE_MEM_API_TIMEOUT_MS = '999999';
const workerUtils = await import('../../src/shared/worker-utils.js');
const loggerModule = await import('../../src/utils/logger.js');
const warnSpy = spyOn(loggerModule.logger, 'warn').mockImplementation(() => {});
workerUtils.clearPortCache();
expect(workerUtils.getWorkerApiRequestTimeoutMs()).toBe(
parseInt(SettingsDefaultsManager.getAllDefaults().CLAUDE_MEM_API_TIMEOUT_MS, 10)
);
expect(warnSpy).toHaveBeenCalledWith(
'SYSTEM',
'Invalid CLAUDE_MEM_API_TIMEOUT_MS, using default',
expect.objectContaining({ value: '999999', min: 500, max: 300000 })
);
});
});