fix: harden startup and schema repair contracts
Reliability patch covering startup path resolution, install marker compatibility, export CLI request contracts, schema repair safety, hard-stop retry-loop handling, and the PR babysit status helper.
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
import { afterEach, describe, expect, it, mock, spyOn } from 'bun:test';
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
const originalDataDir = process.env.CLAUDE_MEM_DATA_DIR;
|
||||
const originalNoMain = process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN;
|
||||
|
||||
describe('export-memories script', () => {
|
||||
let tempDir: string | undefined;
|
||||
const consoleSpies: ReturnType<typeof spyOn>[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
|
||||
if (originalDataDir === undefined) {
|
||||
delete process.env.CLAUDE_MEM_DATA_DIR;
|
||||
} else {
|
||||
process.env.CLAUDE_MEM_DATA_DIR = originalDataDir;
|
||||
}
|
||||
|
||||
if (originalNoMain === undefined) {
|
||||
delete process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN;
|
||||
} else {
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = originalNoMain;
|
||||
}
|
||||
|
||||
consoleSpies.splice(0).forEach(spy => spy.mockRestore());
|
||||
|
||||
if (tempDir && existsSync(tempDir)) {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
tempDir = undefined;
|
||||
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
it('loads settings from CLAUDE_MEM_DATA_DIR and sends canonical memorySessionIds', async () => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-export-'));
|
||||
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = '1';
|
||||
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify({
|
||||
CLAUDE_MEM_WORKER_PORT: '45678',
|
||||
}));
|
||||
|
||||
consoleSpies.push(
|
||||
spyOn(console, 'log').mockImplementation(() => {}),
|
||||
spyOn(console, 'warn').mockImplementation(() => {}),
|
||||
spyOn(console, 'error').mockImplementation(() => {}),
|
||||
);
|
||||
|
||||
let batchBody: unknown;
|
||||
let searchSignal: unknown;
|
||||
let batchSignal: unknown;
|
||||
const fetchMock = mock(async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.startsWith('http://localhost:45678/api/search?')) {
|
||||
searchSignal = init?.signal;
|
||||
return new Response(JSON.stringify({
|
||||
observations: [
|
||||
{ memory_session_id: 'memory-a' },
|
||||
{ memory_session_id: 'memory-b' },
|
||||
],
|
||||
sessions: [
|
||||
{ memory_session_id: 'memory-a' },
|
||||
],
|
||||
prompts: [],
|
||||
}), { status: 200 });
|
||||
}
|
||||
|
||||
if (url === 'http://localhost:45678/api/sdk-sessions/batch') {
|
||||
batchSignal = init?.signal;
|
||||
batchBody = JSON.parse(String(init?.body));
|
||||
return new Response(JSON.stringify([
|
||||
{ memory_session_id: 'memory-a' },
|
||||
{ memory_session_id: 'memory-b' },
|
||||
]), { status: 200 });
|
||||
}
|
||||
|
||||
return new Response('unexpected url', { status: 500 });
|
||||
});
|
||||
globalThis.fetch = fetchMock as typeof fetch;
|
||||
|
||||
const { exportMemories } = await import('../../scripts/export-memories.ts');
|
||||
const outputFile = join(tempDir, 'export.json');
|
||||
|
||||
await exportMemories('needle', outputFile, 'project-a');
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2);
|
||||
expect(searchSignal).toBeInstanceOf(AbortSignal);
|
||||
expect(batchSignal).toBeInstanceOf(AbortSignal);
|
||||
expect(batchBody).toEqual({ memorySessionIds: ['memory-a', 'memory-b'] });
|
||||
expect(batchBody).not.toHaveProperty('sdkSessionIds');
|
||||
|
||||
const exported = JSON.parse(readFileSync(outputFile, 'utf-8'));
|
||||
expect(exported.query).toBe('needle');
|
||||
expect(exported.project).toBe('project-a');
|
||||
expect(exported.totalSessions).toBe(2);
|
||||
});
|
||||
|
||||
it('rejects an invalid worker port before fetching', async () => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-export-'));
|
||||
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = '1';
|
||||
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify({
|
||||
CLAUDE_MEM_WORKER_PORT: '45678abc',
|
||||
}));
|
||||
|
||||
const fetchMock = mock(async () => new Response('{}', { status: 200 }));
|
||||
globalThis.fetch = fetchMock as typeof fetch;
|
||||
|
||||
const { exportMemories } = await import('../../scripts/export-memories.ts');
|
||||
|
||||
await expect(exportMemories('needle', join(tempDir, 'export.json'))).rejects.toThrow(
|
||||
'Invalid CLAUDE_MEM_WORKER_PORT',
|
||||
);
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects an empty worker port with a clear configuration error', async () => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-export-'));
|
||||
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = '1';
|
||||
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify({
|
||||
CLAUDE_MEM_WORKER_PORT: '',
|
||||
}));
|
||||
|
||||
const fetchMock = mock(async () => new Response('{}', { status: 200 }));
|
||||
globalThis.fetch = fetchMock as typeof fetch;
|
||||
|
||||
const { exportMemories } = await import('../../scripts/export-memories.ts');
|
||||
|
||||
await expect(exportMemories('needle', join(tempDir, 'export.json'))).rejects.toThrow(
|
||||
'Invalid CLAUDE_MEM_WORKER_PORT in settings.json: missing',
|
||||
);
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects a non-string worker port with a clear configuration error', async () => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-export-'));
|
||||
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = '1';
|
||||
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify({
|
||||
CLAUDE_MEM_WORKER_PORT: 45678,
|
||||
}));
|
||||
|
||||
const fetchMock = mock(async () => new Response('{}', { status: 200 }));
|
||||
globalThis.fetch = fetchMock as typeof fetch;
|
||||
|
||||
const { exportMemories } = await import('../../scripts/export-memories.ts');
|
||||
|
||||
await expect(exportMemories('needle', join(tempDir, 'export.json'))).rejects.toThrow(
|
||||
'Invalid CLAUDE_MEM_WORKER_PORT in settings.json: missing',
|
||||
);
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('fails the export when SDK session metadata cannot be fetched', async () => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-export-'));
|
||||
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = '1';
|
||||
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify({
|
||||
CLAUDE_MEM_WORKER_PORT: '45678',
|
||||
}));
|
||||
|
||||
consoleSpies.push(
|
||||
spyOn(console, 'log').mockImplementation(() => {}),
|
||||
spyOn(console, 'warn').mockImplementation(() => {}),
|
||||
spyOn(console, 'error').mockImplementation(() => {}),
|
||||
);
|
||||
|
||||
const fetchMock = mock(async (input: RequestInfo | URL) => {
|
||||
const url = String(input);
|
||||
if (url.startsWith('http://localhost:45678/api/search?')) {
|
||||
return new Response(JSON.stringify({
|
||||
observations: [{ memory_session_id: 'memory-a' }],
|
||||
sessions: [],
|
||||
prompts: [],
|
||||
}), { status: 200 });
|
||||
}
|
||||
|
||||
if (url === 'http://localhost:45678/api/sdk-sessions/batch') {
|
||||
return new Response('worker unavailable', {
|
||||
status: 503,
|
||||
statusText: 'Service Unavailable',
|
||||
});
|
||||
}
|
||||
|
||||
return new Response('unexpected url', { status: 500 });
|
||||
});
|
||||
globalThis.fetch = fetchMock as typeof fetch;
|
||||
|
||||
const { exportMemories } = await import('../../scripts/export-memories.ts');
|
||||
const outputFile = join(tempDir, 'export.json');
|
||||
|
||||
await expect(exportMemories('needle', outputFile)).rejects.toThrow(
|
||||
'Failed to fetch SDK sessions: 503 Service Unavailable worker unavailable',
|
||||
);
|
||||
expect(existsSync(outputFile)).toBe(false);
|
||||
});
|
||||
|
||||
it('fails deterministically when a worker request times out', async () => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-export-'));
|
||||
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
|
||||
process.env.CLAUDE_MEM_EXPORT_MEMORIES_NO_MAIN = '1';
|
||||
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify({
|
||||
CLAUDE_MEM_WORKER_PORT: '45678',
|
||||
}));
|
||||
|
||||
consoleSpies.push(
|
||||
spyOn(console, 'log').mockImplementation(() => {}),
|
||||
spyOn(console, 'warn').mockImplementation(() => {}),
|
||||
spyOn(console, 'error').mockImplementation(() => {}),
|
||||
);
|
||||
|
||||
const fetchMock = mock(async () => {
|
||||
throw new DOMException('The operation was aborted.', 'AbortError');
|
||||
});
|
||||
globalThis.fetch = fetchMock as typeof fetch;
|
||||
|
||||
const { exportMemories } = await import('../../scripts/export-memories.ts');
|
||||
|
||||
await expect(exportMemories('needle', join(tempDir, 'export.json'))).rejects.toThrow(
|
||||
'Worker request timed out after 30000ms',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
process.env.PR_BABYSIT_STATUS_NO_MAIN = '1';
|
||||
|
||||
describe('pr-babysit-status helpers', () => {
|
||||
it('extracts concise actionable hints from bot review bodies', async () => {
|
||||
const { extractActionableHints } = await import('../../scripts/pr-babysit-status.ts');
|
||||
|
||||
const hints = extractActionableHints(`
|
||||
**Actionable comments posted: 2**
|
||||
|
||||
<details>
|
||||
<summary>Prompt for all review comments with AI agents</summary>
|
||||
|
||||
Inline comments:
|
||||
In \`@src/file.ts\`:
|
||||
- Line 10: Replace the unsafe fallback with a checked path.
|
||||
- Around line 22: Treat a missing binary as stale.
|
||||
</details>
|
||||
`);
|
||||
|
||||
expect(hints).toContain('Actionable comments posted: 2');
|
||||
expect(hints).toContain('10: Replace the unsafe fallback with a checked path.');
|
||||
expect(hints).toContain('22: Treat a missing binary as stale.');
|
||||
expect(hints.join('\n')).not.toContain('Prompt for all review comments');
|
||||
});
|
||||
|
||||
it('extracts review comment headings without dumping full markdown', async () => {
|
||||
const { extractActionableHints } = await import('../../scripts/pr-babysit-status.ts');
|
||||
|
||||
const hints = extractActionableHints(`
|
||||
_Potential issue_ | _Major_ | _Quick win_
|
||||
|
||||
**Treat a missing current Bun binary as stale too.**
|
||||
|
||||
If the marker says this install was created with Bun but getBunVersion now
|
||||
returns null, this still reports the install as current and skips repair.
|
||||
`);
|
||||
|
||||
expect(hints).toContain('Treat a missing current Bun binary as stale too.');
|
||||
expect(hints.some(hint => hint.includes('skips repair'))).toBe(false);
|
||||
});
|
||||
|
||||
it('summarizes branch protection without requiring unavailable fields', async () => {
|
||||
const { summarizeProtection } = await import('../../scripts/pr-babysit-status.ts');
|
||||
|
||||
expect(summarizeProtection({
|
||||
required_pull_request_reviews: {
|
||||
dismiss_stale_reviews: true,
|
||||
require_code_owner_reviews: false,
|
||||
require_last_push_approval: false,
|
||||
required_approving_review_count: 1,
|
||||
},
|
||||
enforce_admins: { enabled: false },
|
||||
allow_force_pushes: { enabled: true },
|
||||
})).toEqual([
|
||||
'Required checks: none',
|
||||
'Required reviews: 1 approval',
|
||||
'Dismiss stale reviews: yes',
|
||||
'Code owner reviews: no',
|
||||
'Last-push approval: no',
|
||||
'Conversation resolution: no',
|
||||
'Signed commits: no',
|
||||
'Enforce admins: no',
|
||||
'Allow force pushes: yes',
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user