-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathstore.test.ts
More file actions
202 lines (181 loc) · 6.24 KB
/
Copy pathstore.test.ts
File metadata and controls
202 lines (181 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
getAutoMemoryConsolidationLockPath,
getAutoMemoryExtractCursorPath,
getAutoMemoryIndexPath,
getAutoMemoryMetadataPath,
getAutoMemoryRoot,
getAutoMemoryTopicPath,
clearAutoMemoryRootCache,
} from './paths.js';
import {
createDefaultAutoMemoryIndex,
createDefaultAutoMemoryMetadata,
ensureAutoMemoryScaffold,
readAutoMemoryIndex,
} from './store.js';
import { Storage } from '../config/storage.js';
import { sanitizeCwd } from '../utils/paths.js';
const originalMemoryLocal = process.env['QWEN_CODE_MEMORY_LOCAL'];
const originalMemoryBaseDir = process.env['QWEN_CODE_MEMORY_BASE_DIR'];
const originalRuntimeDir = process.env['QWEN_RUNTIME_DIR'];
describe('auto-memory storage scaffold', () => {
let tempDir: string;
let projectRoot: string;
beforeEach(async () => {
clearAutoMemoryRootCache();
Storage.setRuntimeBaseDir(null);
if (originalMemoryLocal === undefined) {
delete process.env['QWEN_CODE_MEMORY_LOCAL'];
} else {
process.env['QWEN_CODE_MEMORY_LOCAL'] = originalMemoryLocal;
}
if (originalMemoryBaseDir === undefined) {
delete process.env['QWEN_CODE_MEMORY_BASE_DIR'];
} else {
process.env['QWEN_CODE_MEMORY_BASE_DIR'] = originalMemoryBaseDir;
}
if (originalRuntimeDir === undefined) {
delete process.env['QWEN_RUNTIME_DIR'];
} else {
process.env['QWEN_RUNTIME_DIR'] = originalRuntimeDir;
}
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'auto-memory-'));
projectRoot = path.join(tempDir, 'project');
await fs.mkdir(projectRoot, { recursive: true });
});
afterEach(async () => {
clearAutoMemoryRootCache();
Storage.setRuntimeBaseDir(null);
if (originalMemoryLocal === undefined) {
delete process.env['QWEN_CODE_MEMORY_LOCAL'];
} else {
process.env['QWEN_CODE_MEMORY_LOCAL'] = originalMemoryLocal;
}
if (originalMemoryBaseDir === undefined) {
delete process.env['QWEN_CODE_MEMORY_BASE_DIR'];
} else {
process.env['QWEN_CODE_MEMORY_BASE_DIR'] = originalMemoryBaseDir;
}
if (originalRuntimeDir === undefined) {
delete process.env['QWEN_RUNTIME_DIR'];
} else {
process.env['QWEN_RUNTIME_DIR'] = originalRuntimeDir;
}
await fs.rm(tempDir, {
recursive: true,
force: true,
maxRetries: 3,
retryDelay: 10,
});
});
it('builds stable auto-memory paths under project .qwen directory', () => {
expect(getAutoMemoryRoot(projectRoot)).toBe(
path.join(projectRoot, '.qwen', 'memory'),
);
expect(getAutoMemoryIndexPath(projectRoot)).toBe(
path.join(projectRoot, '.qwen', 'memory', 'MEMORY.md'),
);
expect(getAutoMemoryMetadataPath(projectRoot)).toBe(
path.join(projectRoot, '.qwen', 'meta.json'),
);
expect(getAutoMemoryExtractCursorPath(projectRoot)).toBe(
path.join(projectRoot, '.qwen', 'extract-cursor.json'),
);
expect(getAutoMemoryConsolidationLockPath(projectRoot)).toBe(
path.join(projectRoot, '.qwen', 'consolidation.lock'),
);
expect(getAutoMemoryTopicPath(projectRoot, 'feedback')).toBe(
path.join(projectRoot, '.qwen', 'memory', 'feedback.md'),
);
});
it('uses the runtime output directory for managed auto-memory', () => {
delete process.env['QWEN_CODE_MEMORY_LOCAL'];
const runtimeDir = path.join(tempDir, 'runtime-output');
Storage.setRuntimeBaseDir(runtimeDir);
clearAutoMemoryRootCache();
expect(getAutoMemoryRoot(projectRoot)).toBe(
path.join(
runtimeDir,
'projects',
sanitizeCwd(path.resolve(projectRoot)),
'memory',
),
);
});
it('keeps QWEN_CODE_MEMORY_BASE_DIR ahead of the runtime output directory', () => {
delete process.env['QWEN_CODE_MEMORY_LOCAL'];
const memoryBaseDir = path.join(tempDir, 'memory-base');
const runtimeDir = path.join(tempDir, 'runtime-output');
process.env['QWEN_CODE_MEMORY_BASE_DIR'] = memoryBaseDir;
Storage.setRuntimeBaseDir(runtimeDir);
clearAutoMemoryRootCache();
expect(getAutoMemoryRoot(projectRoot)).toBe(
path.join(
memoryBaseDir,
'projects',
sanitizeCwd(path.resolve(projectRoot)),
'memory',
),
);
});
it('creates a complete managed auto-memory scaffold', async () => {
const now = new Date('2026-04-01T08:00:00.000Z');
await ensureAutoMemoryScaffold(projectRoot, now);
const index = await fs.readFile(
getAutoMemoryIndexPath(projectRoot),
'utf-8',
);
expect(index).toBe(createDefaultAutoMemoryIndex());
const metadata = JSON.parse(
await fs.readFile(getAutoMemoryMetadataPath(projectRoot), 'utf-8'),
);
expect(metadata).toEqual(createDefaultAutoMemoryMetadata(now));
const cursor = JSON.parse(
await fs.readFile(getAutoMemoryExtractCursorPath(projectRoot), 'utf-8'),
);
expect(cursor).toEqual({
updatedAt: '2026-04-01T08:00:00.000Z',
});
await expect(
fs.stat(getAutoMemoryRoot(projectRoot)),
).resolves.toBeDefined();
await expect(
fs.access(getAutoMemoryTopicPath(projectRoot, 'user')),
).rejects.toThrow();
});
it('is idempotent and preserves existing index content', async () => {
await ensureAutoMemoryScaffold(
projectRoot,
new Date('2026-04-01T08:00:00.000Z'),
);
const customIndex = '# Existing Index\n\n- keep me\n';
await fs.writeFile(
getAutoMemoryIndexPath(projectRoot),
customIndex,
'utf-8',
);
await ensureAutoMemoryScaffold(
projectRoot,
new Date('2026-04-02T08:00:00.000Z'),
);
await expect(
fs.readFile(getAutoMemoryIndexPath(projectRoot), 'utf-8'),
).resolves.toBe(customIndex);
});
it('returns null when the auto-memory index does not exist yet', async () => {
await expect(readAutoMemoryIndex(projectRoot)).resolves.toBeNull();
});
it('reads the managed auto-memory index after scaffold creation', async () => {
await ensureAutoMemoryScaffold(projectRoot);
await expect(readAutoMemoryIndex(projectRoot)).resolves.toBe('');
});
});