memento-mcp-server 1.16.3-a → 1.16.3-b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/domains/search/algorithms/vector-search-engine-migration.d.ts +13 -8
- package/dist/domains/search/algorithms/vector-search-engine-migration.d.ts.map +1 -1
- package/dist/domains/search/algorithms/vector-search-engine-migration.js +19 -41
- package/dist/domains/search/algorithms/vector-search-engine-migration.js.map +1 -1
- package/dist/domains/search/algorithms/vector-search-engine.d.ts +17 -36
- package/dist/domains/search/algorithms/vector-search-engine.d.ts.map +1 -1
- package/dist/domains/search/algorithms/vector-search-engine.js +94 -481
- package/dist/domains/search/algorithms/vector-search-engine.js.map +1 -1
- package/dist/domains/search/repositories/vector-search.repository.d.ts.map +1 -1
- package/dist/domains/search/repositories/vector-search.repository.js +28 -12
- package/dist/domains/search/repositories/vector-search.repository.js.map +1 -1
- package/dist/server/http-server.d.ts.map +1 -1
- package/dist/server/http-server.js +2 -7
- package/dist/server/http-server.js.map +1 -1
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +33 -7
- package/dist/server/index.js.map +1 -1
- package/dist/server/server-factory.d.ts +65 -0
- package/dist/server/server-factory.d.ts.map +1 -0
- package/dist/server/server-factory.js +40 -0
- package/dist/server/server-factory.js.map +1 -0
- package/dist/server/servers/sse-server.d.ts +33 -0
- package/dist/server/servers/sse-server.d.ts.map +1 -0
- package/dist/server/servers/sse-server.js +48 -0
- package/dist/server/servers/sse-server.js.map +1 -0
- package/dist/server/servers/stdio-server.d.ts +34 -0
- package/dist/server/servers/stdio-server.d.ts.map +1 -0
- package/dist/server/servers/stdio-server.js +58 -0
- package/dist/server/servers/stdio-server.js.map +1 -0
- package/dist/server/simple-mcp-server.d.ts +5 -0
- package/dist/server/simple-mcp-server.d.ts.map +1 -1
- package/dist/server/simple-mcp-server.js +17 -7
- package/dist/server/simple-mcp-server.js.map +1 -1
- package/dist/server/sse-server-impl.d.ts +22 -0
- package/dist/server/sse-server-impl.d.ts.map +1 -0
- package/dist/server/sse-server-impl.js +39 -0
- package/dist/server/sse-server-impl.js.map +1 -0
- package/dist/server/stdio-server-impl.d.ts +12 -0
- package/dist/server/stdio-server-impl.d.ts.map +1 -0
- package/dist/server/stdio-server-impl.js +19 -0
- package/dist/server/stdio-server-impl.js.map +1 -0
- package/dist/shared/types/vector-search.types.d.ts +1 -0
- package/dist/shared/types/vector-search.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/scripts/__tests__/check-db-integrity.integration.spec.ts +163 -0
- package/scripts/__tests__/fix-migration.integration.spec.ts +203 -0
- package/scripts/__tests__/migrate-embedding-data.integration.spec.ts +219 -0
- package/scripts/__tests__/regenerate-embeddings.integration.spec.ts +192 -0
- package/scripts/backup-embeddings.js +52 -61
- package/scripts/check-db-integrity.js +49 -25
- package/scripts/check-file-sizes.ts +4 -4
- package/scripts/check-pii-masking.ts +0 -3
- package/scripts/check-sql-injection.ts +0 -12
- package/scripts/debug-embeddings.js +74 -93
- package/scripts/fix-migration.js +115 -80
- package/scripts/fix-vector-dimensions.js +70 -89
- package/scripts/migrate-embedding-data.js +111 -25
- package/scripts/regenerate-embeddings.js +31 -15
- package/scripts/run-migration.js +144 -107
- package/scripts/safe-migration.js +192 -142
- package/scripts/save-work-memory.ts +6 -7
- package/scripts/simple-migrate.js +66 -34
- package/scripts/simple-update.js +147 -109
- package/dist/domains/search/algorithms/vector-search-engine-refactored.d.ts +0 -56
- package/dist/domains/search/algorithms/vector-search-engine-refactored.d.ts.map +0 -1
- package/dist/domains/search/algorithms/vector-search-engine-refactored.js +0 -101
- package/dist/domains/search/algorithms/vector-search-engine-refactored.js.map +0 -1
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fix-migration.js 통합 테스트
|
|
3
|
+
*
|
|
4
|
+
* Given/When/Then 구조를 따르는 통합 테스트
|
|
5
|
+
* 공통 모듈(initializeDatabase)을 사용하는 버전으로 리팩토링 후 검증
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
9
|
+
import { existsSync, unlinkSync, mkdirSync } from 'fs';
|
|
10
|
+
import { join } from 'path';
|
|
11
|
+
import { tmpdir } from 'os';
|
|
12
|
+
import { initializeDatabase, closeDatabase } from '../../src/infrastructure/database/database/init.js';
|
|
13
|
+
import Database from 'better-sqlite3';
|
|
14
|
+
|
|
15
|
+
describe('fix-migration 통합 테스트', () => {
|
|
16
|
+
let testDbPath: string;
|
|
17
|
+
let originalDbPath: string | undefined;
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
// 테스트용 임시 디렉토리 생성
|
|
21
|
+
const testDir = join(tmpdir(), `memento-test-${Date.now()}`);
|
|
22
|
+
mkdirSync(testDir, { recursive: true });
|
|
23
|
+
|
|
24
|
+
testDbPath = join(testDir, 'memory.db');
|
|
25
|
+
|
|
26
|
+
// 환경 변수 백업 및 설정
|
|
27
|
+
originalDbPath = process.env.DB_PATH;
|
|
28
|
+
process.env.DB_PATH = testDbPath;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
// 환경 변수 복원
|
|
33
|
+
if (originalDbPath !== undefined) {
|
|
34
|
+
process.env.DB_PATH = originalDbPath;
|
|
35
|
+
} else {
|
|
36
|
+
delete process.env.DB_PATH;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 테스트 파일 정리
|
|
40
|
+
try {
|
|
41
|
+
if (existsSync(testDbPath)) {
|
|
42
|
+
unlinkSync(testDbPath);
|
|
43
|
+
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
// 파일 정리 실패는 무시
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 fix-migration 함수
|
|
51
|
+
* @when memory_embedding 테이블에 필요한 컬럼이 있는 경우
|
|
52
|
+
* @then 데이터 업데이트 및 인덱스 생성 성공
|
|
53
|
+
*/
|
|
54
|
+
it('should update data and create indexes when required columns exist', async () => {
|
|
55
|
+
// Given: 정상적인 데이터베이스 초기화 및 테스트 데이터 생성
|
|
56
|
+
const db = await initializeDatabase();
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
// memory_embedding 테이블이 있는지 확인 (initializeDatabase가 생성할 수 있음)
|
|
60
|
+
const tableExists = db.prepare(`
|
|
61
|
+
SELECT name FROM sqlite_master
|
|
62
|
+
WHERE type='table' AND name='memory_embedding'
|
|
63
|
+
`).get();
|
|
64
|
+
|
|
65
|
+
if (!tableExists) {
|
|
66
|
+
// 테이블이 없으면 생성 (테스트용)
|
|
67
|
+
db.exec(`
|
|
68
|
+
CREATE TABLE IF NOT EXISTS memory_embedding (
|
|
69
|
+
id INTEGER PRIMARY KEY,
|
|
70
|
+
embedding TEXT,
|
|
71
|
+
dim INTEGER,
|
|
72
|
+
model TEXT,
|
|
73
|
+
embedding_provider TEXT,
|
|
74
|
+
dimensions INTEGER,
|
|
75
|
+
created_by TEXT
|
|
76
|
+
)
|
|
77
|
+
`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 테스트 데이터 삽입
|
|
81
|
+
db.exec(`
|
|
82
|
+
INSERT INTO memory_embedding (id, embedding, dim, model, embedding_provider, dimensions, created_by)
|
|
83
|
+
VALUES
|
|
84
|
+
(1, '[]', 384, 'lightweight-hybrid', NULL, NULL, NULL),
|
|
85
|
+
(2, '[]', 512, NULL, NULL, NULL, NULL),
|
|
86
|
+
(3, '[]', 384, '', NULL, NULL, NULL)
|
|
87
|
+
`);
|
|
88
|
+
|
|
89
|
+
// When: fix-migration 로직 실행
|
|
90
|
+
const currentSchema = db.prepare("PRAGMA table_info(memory_embedding)").all() as Array<{ name: string }>;
|
|
91
|
+
const hasProvider = currentSchema.some(col => col.name === 'embedding_provider');
|
|
92
|
+
const hasDimensions = currentSchema.some(col => col.name === 'dimensions');
|
|
93
|
+
const hasCreatedBy = currentSchema.some(col => col.name === 'created_by');
|
|
94
|
+
|
|
95
|
+
if (hasProvider && hasDimensions && hasCreatedBy) {
|
|
96
|
+
// 데이터 업데이트
|
|
97
|
+
const updateResult = db.prepare(`
|
|
98
|
+
UPDATE memory_embedding
|
|
99
|
+
SET
|
|
100
|
+
embedding_provider = CASE
|
|
101
|
+
WHEN model = 'lightweight-hybrid' THEN 'tfidf'
|
|
102
|
+
WHEN model IS NULL OR model = '' THEN 'tfidf'
|
|
103
|
+
ELSE 'unknown'
|
|
104
|
+
END,
|
|
105
|
+
dimensions = dim,
|
|
106
|
+
created_by = 'legacy'
|
|
107
|
+
WHERE embedding_provider IS NULL
|
|
108
|
+
`).run();
|
|
109
|
+
|
|
110
|
+
// 인덱스 생성
|
|
111
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_memory_embedding_provider ON memory_embedding(embedding_provider)');
|
|
112
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_memory_embedding_dimensions ON memory_embedding(dimensions)');
|
|
113
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_memory_embedding_created_by ON memory_embedding(created_by)');
|
|
114
|
+
|
|
115
|
+
// Then: 업데이트 성공 확인
|
|
116
|
+
expect(updateResult.changes).toBeGreaterThan(0);
|
|
117
|
+
|
|
118
|
+
// 검증
|
|
119
|
+
const validation = db.prepare(`
|
|
120
|
+
SELECT
|
|
121
|
+
COUNT(*) as total,
|
|
122
|
+
COUNT(CASE WHEN embedding_provider IS NOT NULL THEN 1 END) as with_provider,
|
|
123
|
+
COUNT(CASE WHEN dimensions IS NOT NULL THEN 1 END) as with_dimensions,
|
|
124
|
+
COUNT(CASE WHEN created_by IS NOT NULL THEN 1 END) as with_created_by
|
|
125
|
+
FROM memory_embedding
|
|
126
|
+
`).get() as { total: number; with_provider: number; with_dimensions: number; with_created_by: number };
|
|
127
|
+
|
|
128
|
+
expect(validation.with_provider).toBeGreaterThan(0);
|
|
129
|
+
expect(validation.with_dimensions).toBeGreaterThan(0);
|
|
130
|
+
expect(validation.with_created_by).toBeGreaterThan(0);
|
|
131
|
+
} else {
|
|
132
|
+
// 필요한 컬럼이 없는 경우 스킵
|
|
133
|
+
console.log('⚠️ 필요한 컬럼이 없습니다. 테스트 스킵');
|
|
134
|
+
}
|
|
135
|
+
} finally {
|
|
136
|
+
closeDatabase(db);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 fix-migration 함수
|
|
142
|
+
* @when 필요한 컬럼이 없는 경우
|
|
143
|
+
* @then 적절한 에러 메시지 또는 스킵 처리
|
|
144
|
+
*/
|
|
145
|
+
it('should handle missing required columns gracefully', async () => {
|
|
146
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
147
|
+
const db = await initializeDatabase();
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
// When: 컬럼 확인
|
|
151
|
+
const currentSchema = db.prepare("PRAGMA table_info(memory_embedding)").all() as Array<{ name: string }>;
|
|
152
|
+
const hasProvider = currentSchema.some(col => col.name === 'embedding_provider');
|
|
153
|
+
const hasDimensions = currentSchema.some(col => col.name === 'dimensions');
|
|
154
|
+
const hasCreatedBy = currentSchema.some(col => col.name === 'created_by');
|
|
155
|
+
|
|
156
|
+
// Then: 컬럼 존재 여부에 따라 적절히 처리
|
|
157
|
+
if (!hasProvider || !hasDimensions || !hasCreatedBy) {
|
|
158
|
+
// 필요한 컬럼이 없으면 스킵 (에러 없이)
|
|
159
|
+
expect(true).toBe(true); // 테스트 통과
|
|
160
|
+
} else {
|
|
161
|
+
// 컬럼이 있으면 정상 처리
|
|
162
|
+
expect(hasProvider).toBe(true);
|
|
163
|
+
expect(hasDimensions).toBe(true);
|
|
164
|
+
expect(hasCreatedBy).toBe(true);
|
|
165
|
+
}
|
|
166
|
+
} finally {
|
|
167
|
+
closeDatabase(db);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 fix-migration 함수
|
|
173
|
+
* @when 인덱스 생성 실행
|
|
174
|
+
* @then 인덱스가 정상적으로 생성됨
|
|
175
|
+
*/
|
|
176
|
+
it('should create indexes successfully', async () => {
|
|
177
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
178
|
+
const db = await initializeDatabase();
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
// When: 인덱스 생성
|
|
182
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_memory_embedding_provider ON memory_embedding(embedding_provider)');
|
|
183
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_memory_embedding_dimensions ON memory_embedding(dimensions)');
|
|
184
|
+
db.exec('CREATE INDEX IF NOT EXISTS idx_memory_embedding_created_by ON memory_embedding(created_by)');
|
|
185
|
+
|
|
186
|
+
// Then: 인덱스가 생성되었는지 확인
|
|
187
|
+
const indexes = db.prepare(`
|
|
188
|
+
SELECT name FROM sqlite_master
|
|
189
|
+
WHERE type='index' AND name IN (
|
|
190
|
+
'idx_memory_embedding_provider',
|
|
191
|
+
'idx_memory_embedding_dimensions',
|
|
192
|
+
'idx_memory_embedding_created_by'
|
|
193
|
+
)
|
|
194
|
+
`).all() as Array<{ name: string }>;
|
|
195
|
+
|
|
196
|
+
// 인덱스가 생성되었거나, 테이블이 없어서 생성되지 않았을 수 있음
|
|
197
|
+
expect(indexes.length).toBeGreaterThanOrEqual(0);
|
|
198
|
+
} finally {
|
|
199
|
+
closeDatabase(db);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* migrate-embedding-data.js 통합 테스트
|
|
3
|
+
*
|
|
4
|
+
* Given/When/Then 구조를 따르는 통합 테스트
|
|
5
|
+
* 공통 모듈(initializeDatabase)을 사용하는 버전으로 리팩토링 후 검증
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
9
|
+
import { existsSync, unlinkSync, mkdirSync } from 'fs';
|
|
10
|
+
import { join } from 'path';
|
|
11
|
+
import { tmpdir } from 'os';
|
|
12
|
+
import { initializeDatabase, closeDatabase } from '../../src/infrastructure/database/database/init.js';
|
|
13
|
+
import Database from 'better-sqlite3';
|
|
14
|
+
|
|
15
|
+
describe('migrate-embedding-data 통합 테스트', () => {
|
|
16
|
+
let testDbPath: string;
|
|
17
|
+
let testBackupPath: string;
|
|
18
|
+
let originalDbPath: string | undefined;
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
// 테스트용 임시 디렉토리 생성
|
|
22
|
+
const testDir = join(tmpdir(), `memento-test-${Date.now()}`);
|
|
23
|
+
mkdirSync(testDir, { recursive: true });
|
|
24
|
+
|
|
25
|
+
testDbPath = join(testDir, 'memory.db');
|
|
26
|
+
testBackupPath = join(testDir, `memory-backup-${Date.now()}.db`);
|
|
27
|
+
|
|
28
|
+
// 환경 변수 백업 및 설정
|
|
29
|
+
originalDbPath = process.env.DB_PATH;
|
|
30
|
+
process.env.DB_PATH = testDbPath;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
// 환경 변수 복원
|
|
35
|
+
if (originalDbPath !== undefined) {
|
|
36
|
+
process.env.DB_PATH = originalDbPath;
|
|
37
|
+
} else {
|
|
38
|
+
delete process.env.DB_PATH;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 테스트 파일 정리
|
|
42
|
+
try {
|
|
43
|
+
if (existsSync(testDbPath)) {
|
|
44
|
+
unlinkSync(testDbPath);
|
|
45
|
+
}
|
|
46
|
+
if (existsSync(testBackupPath)) {
|
|
47
|
+
unlinkSync(testBackupPath);
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// 파일 정리 실패는 무시
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 migrate-embedding-data 함수
|
|
56
|
+
* @when 데이터베이스 연결 및 백업 생성
|
|
57
|
+
* @then 백업 파일이 정상적으로 생성됨
|
|
58
|
+
*/
|
|
59
|
+
it('should create backup successfully', async () => {
|
|
60
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
61
|
+
const db = await initializeDatabase();
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
// 테스트 데이터 삽입
|
|
65
|
+
db.exec(`
|
|
66
|
+
INSERT INTO memory_embedding (id, embedding, dim, model)
|
|
67
|
+
VALUES (1, '[]', 384, 'lightweight-hybrid')
|
|
68
|
+
`);
|
|
69
|
+
|
|
70
|
+
// When: 백업 생성
|
|
71
|
+
const backupDb = new Database(testBackupPath);
|
|
72
|
+
db.backup(testBackupPath);
|
|
73
|
+
backupDb.close();
|
|
74
|
+
|
|
75
|
+
// Then: 백업 파일이 생성되었는지 확인
|
|
76
|
+
expect(existsSync(testBackupPath)).toBe(true);
|
|
77
|
+
} finally {
|
|
78
|
+
closeDatabase(db);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 migrate-embedding-data 함수
|
|
84
|
+
* @when 기존 데이터 분석 실행
|
|
85
|
+
* @then 분석 결과가 정상적으로 반환됨
|
|
86
|
+
*/
|
|
87
|
+
it('should analyze existing data correctly', async () => {
|
|
88
|
+
// Given: 정상적인 데이터베이스 초기화 및 테스트 데이터
|
|
89
|
+
const db = await initializeDatabase();
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
// memory_embedding 테이블이 있는지 확인
|
|
93
|
+
const tableExists = db.prepare(`
|
|
94
|
+
SELECT name FROM sqlite_master
|
|
95
|
+
WHERE type='table' AND name='memory_embedding'
|
|
96
|
+
`).get();
|
|
97
|
+
|
|
98
|
+
if (tableExists) {
|
|
99
|
+
// When: 데이터 분석
|
|
100
|
+
const dimensionStats = db.prepare(`
|
|
101
|
+
SELECT dim, COUNT(*) as count,
|
|
102
|
+
COUNT(CASE WHEN model IS NOT NULL AND model != '' THEN 1 END) as with_model
|
|
103
|
+
FROM memory_embedding
|
|
104
|
+
GROUP BY dim
|
|
105
|
+
`).all();
|
|
106
|
+
|
|
107
|
+
const totalStats = db.prepare(`
|
|
108
|
+
SELECT COUNT(*) as total,
|
|
109
|
+
COUNT(CASE WHEN embedding_provider IS NOT NULL THEN 1 END) as migrated
|
|
110
|
+
FROM memory_embedding
|
|
111
|
+
`).get();
|
|
112
|
+
|
|
113
|
+
// Then: 분석 결과가 정상적으로 반환됨
|
|
114
|
+
expect(dimensionStats).toBeDefined();
|
|
115
|
+
expect(totalStats).toBeDefined();
|
|
116
|
+
expect(totalStats.total).toBeGreaterThanOrEqual(0);
|
|
117
|
+
} else {
|
|
118
|
+
// 테이블이 없으면 스킵
|
|
119
|
+
console.log('⚠️ memory_embedding 테이블이 없습니다. 테스트 스킵');
|
|
120
|
+
}
|
|
121
|
+
} finally {
|
|
122
|
+
closeDatabase(db);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 migrate-embedding-data 함수
|
|
128
|
+
* @when 메타데이터 업데이트 실행
|
|
129
|
+
* @then 메타데이터가 정상적으로 업데이트됨
|
|
130
|
+
*/
|
|
131
|
+
it('should update metadata successfully', async () => {
|
|
132
|
+
// Given: 정상적인 데이터베이스 초기화 및 테스트 데이터
|
|
133
|
+
const db = await initializeDatabase();
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
// memory_embedding 테이블 확인
|
|
137
|
+
const tableExists = db.prepare(`
|
|
138
|
+
SELECT name FROM sqlite_master
|
|
139
|
+
WHERE type='table' AND name='memory_embedding'
|
|
140
|
+
`).get();
|
|
141
|
+
|
|
142
|
+
if (tableExists) {
|
|
143
|
+
// 컬럼 존재 확인
|
|
144
|
+
const schema = db.prepare("PRAGMA table_info(memory_embedding)").all() as Array<{ name: string }>;
|
|
145
|
+
const hasProvider = schema.some(col => col.name === 'embedding_provider');
|
|
146
|
+
const hasDimensions = schema.some(col => col.name === 'dimensions');
|
|
147
|
+
const hasCreatedBy = schema.some(col => col.name === 'created_by');
|
|
148
|
+
|
|
149
|
+
if (hasProvider && hasDimensions && hasCreatedBy) {
|
|
150
|
+
// When: 메타데이터 업데이트
|
|
151
|
+
const updateStmt = db.prepare(`
|
|
152
|
+
UPDATE memory_embedding
|
|
153
|
+
SET
|
|
154
|
+
embedding_provider = CASE
|
|
155
|
+
WHEN model = 'lightweight-hybrid' THEN 'tfidf'
|
|
156
|
+
WHEN model IS NULL OR model = '' THEN 'tfidf'
|
|
157
|
+
ELSE 'unknown'
|
|
158
|
+
END,
|
|
159
|
+
dimensions = dim,
|
|
160
|
+
created_by = 'legacy'
|
|
161
|
+
WHERE embedding_provider IS NULL
|
|
162
|
+
`);
|
|
163
|
+
|
|
164
|
+
const result = updateStmt.run();
|
|
165
|
+
|
|
166
|
+
// Then: 업데이트 성공 확인
|
|
167
|
+
expect(result.changes).toBeGreaterThanOrEqual(0);
|
|
168
|
+
} else {
|
|
169
|
+
console.log('⚠️ 필요한 컬럼이 없습니다. 테스트 스킵');
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
console.log('⚠️ memory_embedding 테이블이 없습니다. 테스트 스킵');
|
|
173
|
+
}
|
|
174
|
+
} finally {
|
|
175
|
+
closeDatabase(db);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 migrate-embedding-data 함수
|
|
181
|
+
* @when 마이그레이션 검증 실행
|
|
182
|
+
* @then 검증 결과가 정상적으로 반환됨
|
|
183
|
+
*/
|
|
184
|
+
it('should validate migration successfully', async () => {
|
|
185
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
186
|
+
const db = await initializeDatabase();
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
// memory_embedding 테이블 확인
|
|
190
|
+
const tableExists = db.prepare(`
|
|
191
|
+
SELECT name FROM sqlite_master
|
|
192
|
+
WHERE type='table' AND name='memory_embedding'
|
|
193
|
+
`).get();
|
|
194
|
+
|
|
195
|
+
if (tableExists) {
|
|
196
|
+
// When: 마이그레이션 검증
|
|
197
|
+
const validation = db.prepare(`
|
|
198
|
+
SELECT
|
|
199
|
+
COUNT(*) as total,
|
|
200
|
+
COUNT(CASE WHEN embedding_provider IS NOT NULL THEN 1 END) as with_provider,
|
|
201
|
+
COUNT(CASE WHEN dimensions IS NOT NULL THEN 1 END) as with_dimensions,
|
|
202
|
+
COUNT(CASE WHEN created_by IS NOT NULL THEN 1 END) as with_created_by
|
|
203
|
+
FROM memory_embedding
|
|
204
|
+
`).get() as { total: number; with_provider: number; with_dimensions: number; with_created_by: number };
|
|
205
|
+
|
|
206
|
+
// Then: 검증 결과가 정상적으로 반환됨
|
|
207
|
+
expect(validation.total).toBeGreaterThanOrEqual(0);
|
|
208
|
+
expect(validation.with_provider).toBeGreaterThanOrEqual(0);
|
|
209
|
+
expect(validation.with_dimensions).toBeGreaterThanOrEqual(0);
|
|
210
|
+
expect(validation.with_created_by).toBeGreaterThanOrEqual(0);
|
|
211
|
+
} else {
|
|
212
|
+
console.log('⚠️ memory_embedding 테이블이 없습니다. 테스트 스킵');
|
|
213
|
+
}
|
|
214
|
+
} finally {
|
|
215
|
+
closeDatabase(db);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* regenerate-embeddings.js 통합 테스트
|
|
3
|
+
*
|
|
4
|
+
* Given/When/Then 구조를 따르는 통합 테스트
|
|
5
|
+
* 공통 모듈(initializeDatabase)을 사용하는 버전으로 리팩토링 후 검증
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
9
|
+
import { existsSync, unlinkSync, mkdirSync } from 'fs';
|
|
10
|
+
import { join } from 'path';
|
|
11
|
+
import { tmpdir } from 'os';
|
|
12
|
+
import { initializeDatabase, closeDatabase } from '../../src/infrastructure/database/database/init.js';
|
|
13
|
+
import Database from 'better-sqlite3';
|
|
14
|
+
|
|
15
|
+
describe('regenerate-embeddings 통합 테스트', () => {
|
|
16
|
+
let testDbPath: string;
|
|
17
|
+
let originalDbPath: string | undefined;
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
// 테스트용 임시 디렉토리 생성
|
|
21
|
+
const testDir = join(tmpdir(), `memento-test-${Date.now()}`);
|
|
22
|
+
mkdirSync(testDir, { recursive: true });
|
|
23
|
+
|
|
24
|
+
testDbPath = join(testDir, 'memory.db');
|
|
25
|
+
|
|
26
|
+
// 환경 변수 백업 및 설정
|
|
27
|
+
originalDbPath = process.env.DB_PATH;
|
|
28
|
+
process.env.DB_PATH = testDbPath;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
// 환경 변수 복원
|
|
33
|
+
if (originalDbPath !== undefined) {
|
|
34
|
+
process.env.DB_PATH = originalDbPath;
|
|
35
|
+
} else {
|
|
36
|
+
delete process.env.DB_PATH;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 테스트 파일 정리
|
|
40
|
+
try {
|
|
41
|
+
if (existsSync(testDbPath)) {
|
|
42
|
+
unlinkSync(testDbPath);
|
|
43
|
+
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
// 파일 정리 실패는 무시
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 regenerate-embeddings 함수
|
|
51
|
+
* @when 데이터베이스 연결 및 기억 조회
|
|
52
|
+
* @then 기억 목록이 정상적으로 조회됨
|
|
53
|
+
*/
|
|
54
|
+
it('should query memories successfully', async () => {
|
|
55
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
56
|
+
const db = await initializeDatabase();
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
// memory_item 테이블 확인
|
|
60
|
+
const tableExists = db.prepare(`
|
|
61
|
+
SELECT name FROM sqlite_master
|
|
62
|
+
WHERE type='table' AND name='memory_item'
|
|
63
|
+
`).get();
|
|
64
|
+
|
|
65
|
+
if (tableExists) {
|
|
66
|
+
// When: 기억 조회
|
|
67
|
+
const memories = db.prepare(`
|
|
68
|
+
SELECT id, content, type, importance, created_at
|
|
69
|
+
FROM memory_item
|
|
70
|
+
ORDER BY created_at
|
|
71
|
+
`).all();
|
|
72
|
+
|
|
73
|
+
// Then: 기억 목록이 정상적으로 조회됨
|
|
74
|
+
expect(memories).toBeDefined();
|
|
75
|
+
expect(Array.isArray(memories)).toBe(true);
|
|
76
|
+
} else {
|
|
77
|
+
console.log('⚠️ memory_item 테이블이 없습니다. 테스트 스킵');
|
|
78
|
+
}
|
|
79
|
+
} finally {
|
|
80
|
+
closeDatabase(db);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 regenerate-embeddings 함수
|
|
86
|
+
* @when 임베딩 통계 조회
|
|
87
|
+
* @then 통계가 정상적으로 반환됨
|
|
88
|
+
*/
|
|
89
|
+
it('should query embedding statistics successfully', async () => {
|
|
90
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
91
|
+
const db = await initializeDatabase();
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
// memory_embedding 테이블 확인
|
|
95
|
+
const tableExists = db.prepare(`
|
|
96
|
+
SELECT name FROM sqlite_master
|
|
97
|
+
WHERE type='table' AND name='memory_embedding'
|
|
98
|
+
`).get();
|
|
99
|
+
|
|
100
|
+
if (tableExists) {
|
|
101
|
+
// When: 임베딩 통계 조회
|
|
102
|
+
const finalStats = db.prepare(`
|
|
103
|
+
SELECT
|
|
104
|
+
COUNT(*) as total,
|
|
105
|
+
AVG(dim) as avg_dim,
|
|
106
|
+
MIN(dim) as min_dim,
|
|
107
|
+
MAX(dim) as max_dim
|
|
108
|
+
FROM memory_embedding
|
|
109
|
+
`).get();
|
|
110
|
+
|
|
111
|
+
// Then: 통계가 정상적으로 반환됨
|
|
112
|
+
expect(finalStats).toBeDefined();
|
|
113
|
+
expect(finalStats.total).toBeGreaterThanOrEqual(0);
|
|
114
|
+
} else {
|
|
115
|
+
console.log('⚠️ memory_embedding 테이블이 없습니다. 테스트 스킵');
|
|
116
|
+
}
|
|
117
|
+
} finally {
|
|
118
|
+
closeDatabase(db);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 regenerate-embeddings 함수
|
|
124
|
+
* @when 차원 일치성 확인
|
|
125
|
+
* @then 차원 불일치가 정상적으로 감지됨
|
|
126
|
+
*/
|
|
127
|
+
it('should detect dimension mismatches correctly', async () => {
|
|
128
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
129
|
+
const db = await initializeDatabase();
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
// memory_embedding 테이블 확인
|
|
133
|
+
const tableExists = db.prepare(`
|
|
134
|
+
SELECT name FROM sqlite_master
|
|
135
|
+
WHERE type='table' AND name='memory_embedding'
|
|
136
|
+
`).get();
|
|
137
|
+
|
|
138
|
+
if (tableExists) {
|
|
139
|
+
// When: 차원 일치성 확인
|
|
140
|
+
const expectedDim = 384; // 예상 차원
|
|
141
|
+
const mismatchedDims = db.prepare(`
|
|
142
|
+
SELECT COUNT(*) as count FROM memory_embedding WHERE dim != ?
|
|
143
|
+
`).get(expectedDim) as { count: number };
|
|
144
|
+
|
|
145
|
+
// Then: 차원 불일치가 정상적으로 감지됨
|
|
146
|
+
expect(mismatchedDims).toBeDefined();
|
|
147
|
+
expect(mismatchedDims.count).toBeGreaterThanOrEqual(0);
|
|
148
|
+
} else {
|
|
149
|
+
console.log('⚠️ memory_embedding 테이블이 없습니다. 테스트 스킵');
|
|
150
|
+
}
|
|
151
|
+
} finally {
|
|
152
|
+
closeDatabase(db);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @given 공통 모듈(initializeDatabase)을 사용하는 regenerate-embeddings 함수
|
|
158
|
+
* @when 빈 기억 목록 처리
|
|
159
|
+
* @then 적절한 메시지 출력 및 종료
|
|
160
|
+
*/
|
|
161
|
+
it('should handle empty memory list gracefully', async () => {
|
|
162
|
+
// Given: 정상적인 데이터베이스 초기화
|
|
163
|
+
const db = await initializeDatabase();
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
// memory_item 테이블 확인
|
|
167
|
+
const tableExists = db.prepare(`
|
|
168
|
+
SELECT name FROM sqlite_master
|
|
169
|
+
WHERE type='table' AND name='memory_item'
|
|
170
|
+
`).get();
|
|
171
|
+
|
|
172
|
+
if (tableExists) {
|
|
173
|
+
// When: 기억 조회
|
|
174
|
+
const memories = db.prepare(`
|
|
175
|
+
SELECT id, content, type, importance, created_at
|
|
176
|
+
FROM memory_item
|
|
177
|
+
ORDER BY created_at
|
|
178
|
+
`).all();
|
|
179
|
+
|
|
180
|
+
// Then: 빈 목록도 정상적으로 처리됨
|
|
181
|
+
expect(memories).toBeDefined();
|
|
182
|
+
expect(Array.isArray(memories)).toBe(true);
|
|
183
|
+
// 빈 목록인 경우에도 에러 없이 처리되어야 함
|
|
184
|
+
} else {
|
|
185
|
+
console.log('⚠️ memory_item 테이블이 없습니다. 테스트 스킵');
|
|
186
|
+
}
|
|
187
|
+
} finally {
|
|
188
|
+
closeDatabase(db);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|