Files
homeclaw/tests/claude-memory.test.ts
T

94 lines
5.0 KiB
TypeScript

/**
* ensureSharedClaudeMemory — 프로젝트 폴더의 Claude Code 메모리를 기준(홈클로) 메모리로 링크
* 핵심: 이미 내용이 있는 프로젝트 고유 메모리는 절대 지우거나 덮지 않는다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { claudeProjectKey, ensureSharedClaudeMemory, ensureProjectClaudeMd } from '../src/gateway/claude-memory';
const mk = () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cm-'));
const masterRoot = '/srv/homeclaw';
const master = path.join(home, '.claude', 'projects', claudeProjectKey(masterRoot), 'memory');
fs.mkdirSync(master, { recursive: true });
fs.writeFileSync(path.join(master, 'MEMORY.md'), '# index');
return { home, masterRoot, master };
};
const CWD = '/srv/homeclaw/.smallclaw/users/papa/workspace/workshop/proj_m5j2c2bp';
describe('claudeProjectKey', () => {
test('실측 규칙: 영숫자 외 모든 문자가 - (점·밑줄 포함)', () => {
assert.equal(claudeProjectKey('/srv/homeclaw'), '-srv-homeclaw');
assert.equal(claudeProjectKey(CWD), '-srv-homeclaw--smallclaw-users-papa-workspace-workshop-proj-m5j2c2bp');
});
});
describe('ensureSharedClaudeMemory', () => {
test('메모리가 없으면 기준 메모리로 링크한다(MEMORY.md가 보임)', () => {
const { home, masterRoot, master } = mk();
assert.deepEqual(ensureSharedClaudeMemory(CWD, { home, masterRoot }), { action: 'linked' });
const link = path.join(home, '.claude/projects', claudeProjectKey(CWD), 'memory');
assert.equal(fs.realpathSync(link), fs.realpathSync(master));
assert.equal(fs.readFileSync(path.join(link, 'MEMORY.md'), 'utf8'), '# index');
});
test('두 번째 호출은 아무것도 바꾸지 않는다', () => {
const { home, masterRoot } = mk();
ensureSharedClaudeMemory(CWD, { home, masterRoot });
assert.deepEqual(ensureSharedClaudeMemory(CWD, { home, masterRoot }), { action: 'exists' });
});
test('빈 memory 폴더는 링크로 바꾼다', () => {
const { home, masterRoot } = mk();
const link = path.join(home, '.claude/projects', claudeProjectKey(CWD), 'memory');
fs.mkdirSync(link, { recursive: true });
assert.deepEqual(ensureSharedClaudeMemory(CWD, { home, masterRoot }), { action: 'linked' });
assert.ok(fs.lstatSync(link).isSymbolicLink());
});
test('내용이 있는 프로젝트 고유 메모리는 건드리지 않는다 — 데이터 보존', () => {
const { home, masterRoot } = mk();
const link = path.join(home, '.claude/projects', claudeProjectKey(CWD), 'memory');
fs.mkdirSync(link, { recursive: true });
fs.writeFileSync(path.join(link, 'mine.md'), '내 메모');
const r = ensureSharedClaudeMemory(CWD, { home, masterRoot });
assert.equal(r.action, 'skipped');
assert.ok(!fs.lstatSync(link).isSymbolicLink());
assert.equal(fs.readFileSync(path.join(link, 'mine.md'), 'utf8'), '내 메모');
});
test('기준 메모리가 없거나 기준 폴더 자신이면 아무것도 하지 않는다', () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cm-'));
assert.equal(ensureSharedClaudeMemory(CWD, { home, masterRoot: '/srv/homeclaw' }).action, 'skipped');
const { home: h2, masterRoot } = mk();
assert.equal(ensureSharedClaudeMemory(masterRoot, { home: h2, masterRoot }).action, 'skipped');
});
});
describe('ensureProjectClaudeMd — 프로젝트 전용 메모리(CLAUDE.md)', () => {
test('없으면 이름·id·개요가 들어간 CLAUDE.md를 만든다', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cmd-'));
assert.equal(ensureProjectClaudeMd(dir, { id: 'proj_x', name: 'WALL-E AI', description: '카메라가 있는 소형 로봇' }), 'created');
const body = fs.readFileSync(path.join(dir, 'CLAUDE.md'), 'utf8');
assert.match(body, /^# WALL-E AI — 작업실 프로젝트 \(id: proj_x\)/);
assert.match(body, /카메라가 있는 소형 로봇/);
assert.match(body, /## 프로젝트 메모리/);
});
test('이미 있으면 절대 덮어쓰지 않는다(사용자가 쌓은 메모리 보존)', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cmd-'));
fs.writeFileSync(path.join(dir, 'CLAUDE.md'), '내가 쓴 메모');
assert.equal(ensureProjectClaudeMd(dir, { id: 'p', name: 'n' }), 'exists');
assert.equal(fs.readFileSync(path.join(dir, 'CLAUDE.md'), 'utf8'), '내가 쓴 메모');
});
test('개요가 길면 600자로 자르고, 이름의 줄바꿈은 한 줄로 만든다', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cmd-'));
ensureProjectClaudeMd(dir, { id: 'p', name: '이름\n두줄', description: 'x'.repeat(2000) });
const body = fs.readFileSync(path.join(dir, 'CLAUDE.md'), 'utf8');
assert.match(body, /^# 이름 두줄 —/);
assert.ok(!body.includes('x'.repeat(601)));
});
test('폴더가 없으면 조용히 건너뛴다', () => {
assert.equal(ensureProjectClaudeMd('/nonexistent/dir/zzz', { id: 'p' }), 'skipped');
});
});