File size: 10,852 Bytes
d522ffa | 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | import { describe, expect, it, beforeEach, afterEach } from 'vitest';
import { mkdir, mkdtemp, writeFile, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { detectMigration, runMigration } from '../src/index.js';
import { computeWorkdirBucket, oldMd5BucketName } from '../src/sessions/workdir-bucket.js';
const FIXTURES = fileURLToPath(new URL('./fixtures', import.meta.url));
const SOURCE_HOME = join(FIXTURES, 'multi-workdir', '.kimi');
const MARKER_PATH = join(SOURCE_HOME, '.migrated-to-kimi-code');
const FIXTURE_CONFIG = join(SOURCE_HOME, 'config.toml');
let tgt: string;
beforeEach(async () => {
tgt = await mkdtemp(join(tmpdir(), 'integration-'));
// Clean any leftover artifacts that previous failed runs might have left in
// the committed fixture directory.
await rm(MARKER_PATH, { recursive: true, force: true });
await rm(FIXTURE_CONFIG, { force: true });
});
afterEach(async () => {
await rm(tgt, { recursive: true, force: true });
await rm(MARKER_PATH, { recursive: true, force: true });
await rm(FIXTURE_CONFIG, { force: true });
});
describe('runMigration (end-to-end on multi-workdir fixture)', () => {
it('migrates everything when scope is full and limit is null', async () => {
const plan = await detectMigration({ sourcePath: SOURCE_HOME });
const report = await runMigration({
plan,
scope: {
config: true,
mcp: true,
userHistory: true,
skills: true,
sessions: true,
},
source: SOURCE_HOME,
target: tgt,
});
expect(report.summary.sessions.sessionsMigrated).toBeGreaterThan(0);
const indexText = await readFile(join(tgt, 'session_index.jsonl'), 'utf-8');
const indexLines = indexText.split('\n').filter((l) => l.length > 0);
expect(indexLines.length).toBeGreaterThan(0);
const markerText = await readFile(MARKER_PATH, 'utf-8');
const marker: unknown = JSON.parse(markerText);
expect((marker as { version: number }).version).toBe(1);
});
it('completes the migration even when the source marker cannot be written', async () => {
// Block the marker path with a directory so writeMarker()'s writeFile fails.
await mkdir(MARKER_PATH, { recursive: true });
const plan = await detectMigration({ sourcePath: SOURCE_HOME });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: true },
source: SOURCE_HOME,
target: tgt,
});
// All data migrated — a completed run must return a report, not reject.
expect(report.summary.sessions.sessionsMigrated).toBeGreaterThan(0);
});
it('does not write a completed marker when legacy session data remains unreadable', async () => {
const src = await mkdtemp(join(tmpdir(), 'failed-session-marker-src-'));
try {
const bucket = join(src, 'sessions', '11111111111111111111111111111111');
await mkdir(join(bucket, 'legacy-session'), { recursive: true });
const plan = await detectMigration({ sourcePath: src });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: true },
source: src,
target: tgt,
});
expect(report.summary.sessions.sessionsFailed).toHaveLength(1);
await expect(readFile(join(src, '.migrated-to-kimi-code'), 'utf-8')).rejects.toThrow();
} finally {
await rm(src, { recursive: true, force: true });
}
});
it('config-only scope writes config but skips sessions', async () => {
// Materialize a config.toml in the fixture; afterEach cleans it up.
await writeFile(FIXTURE_CONFIG, 'default_thinking = true\n');
const plan = await detectMigration({ sourcePath: SOURCE_HOME });
const report = await runMigration({
plan,
scope: {
config: true,
mcp: true,
userHistory: true,
skills: true,
sessions: false,
},
source: SOURCE_HOME,
target: tgt,
});
expect(report.summary.sessions.scope).toBe('config-only');
expect(report.summary.sessions.sessionsMigrated).toBe(0);
});
it('migrates user skills bundles end-to-end and surfaces them in the report', async () => {
// Drive the full pipeline against a synthetic source so the assertion
// tests integration (run-migration wiring + paths + summary plumbing),
// not just the step in isolation.
const src = await mkdtemp(join(tmpdir(), 'skills-e2e-src-'));
try {
await mkdir(join(src, 'skills', 'my-bundle'), { recursive: true });
await writeFile(
join(src, 'skills', 'my-bundle', 'SKILL.md'),
'---\nname: my-bundle\ndescription: e2e\n---\n',
);
await writeFile(join(src, 'skills', 'flat.md'), '---\nname: flat\ndescription: e2e\n---\n');
const plan = await detectMigration({ sourcePath: src });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: false },
source: src,
target: tgt,
});
expect(report.summary.skills.copied).toBe(2);
expect(report.summary.skills.skippedExisting).toBe(0);
await expect(
readFile(join(tgt, 'skills', 'my-bundle', 'SKILL.md'), 'utf-8'),
).resolves.toContain('my-bundle');
await expect(readFile(join(tgt, 'skills', 'flat.md'), 'utf-8')).resolves.toContain('flat');
} finally {
await rm(src, { recursive: true, force: true });
}
});
it('skips skills migration when scope.skills is false', async () => {
const src = await mkdtemp(join(tmpdir(), 'skills-off-src-'));
try {
await mkdir(join(src, 'skills', 'mine'), { recursive: true });
await writeFile(join(src, 'skills', 'mine', 'SKILL.md'), 'x');
const plan = await detectMigration({ sourcePath: src });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: false, sessions: false },
source: src,
target: tgt,
});
expect(report.summary.skills).toEqual({ copied: 0, skippedExisting: 0 });
await expect(readFile(join(tgt, 'skills', 'mine', 'SKILL.md'))).rejects.toThrow();
} finally {
await rm(src, { recursive: true, force: true });
}
});
it('does not write a completed marker when the legacy config cannot be parsed', async () => {
const src = await mkdtemp(join(tmpdir(), 'bad-config-marker-src-'));
try {
await writeFile(join(src, 'config.toml'), 'this is = = not valid toml [[[');
const plan = await detectMigration({ sourcePath: src });
expect(plan.hasConfig).toBe(true);
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: true },
source: src,
target: tgt,
});
expect(report.summary.config.sourceUnreadable).toBe(true);
await expect(readFile(join(src, '.migrated-to-kimi-code'), 'utf-8')).rejects.toThrow();
} finally {
await rm(src, { recursive: true, force: true });
}
});
it('does not write a completed marker when the legacy mcp.json cannot be parsed', async () => {
const src = await mkdtemp(join(tmpdir(), 'bad-mcp-marker-src-'));
try {
await writeFile(join(src, 'mcp.json'), 'not json {{{');
const plan = await detectMigration({ sourcePath: src });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: true },
source: src,
target: tgt,
});
expect(report.summary.mcp.sourceUnreadable).toBe(true);
await expect(readFile(join(src, '.migrated-to-kimi-code'), 'utf-8')).rejects.toThrow();
} finally {
await rm(src, { recursive: true, force: true });
}
});
it('does not write a completed marker when a session target is occupied by a foreign session', async () => {
const src = await mkdtemp(join(tmpdir(), 'conflict-marker-src-'));
try {
const workdir = '/workspace/conflict-proj';
const uuid = 'conflict-session';
const bucket = join(src, 'sessions', oldMd5BucketName(workdir));
await mkdir(join(bucket, uuid), { recursive: true });
await writeFile(join(src, 'kimi.json'), JSON.stringify({ work_dirs: [{ path: workdir }] }));
await writeFile(join(bucket, uuid, 'context.jsonl'), '{"role":"user","content":"hi"}\n');
const foreignDir = join(
tgt,
'sessions',
computeWorkdirBucket(workdir),
`ses_${uuid}`,
);
await mkdir(foreignDir, { recursive: true });
await writeFile(join(foreignDir, 'state.json'), '{}');
const plan = await detectMigration({ sourcePath: src });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: true },
source: src,
target: tgt,
});
expect(report.summary.sessions.sessionsConflicts).toHaveLength(1);
await expect(readFile(join(src, '.migrated-to-kimi-code'), 'utf-8')).rejects.toThrow();
} finally {
await rm(src, { recursive: true, force: true });
}
});
it('does not copy OAuth credentials into the target', async () => {
// OAuth refresh tokens rotate server-side: they are single-use and
// single-owner. Copying a credential to a second install breaks login
// for whichever side refreshes second. The migration must NOT copy
// credentials — it leaves the legacy login alone and asks the user to
// run /login in kimi-code instead.
const src = await mkdtemp(join(tmpdir(), 'oauth-src-'));
try {
await mkdir(join(src, 'credentials'), { recursive: true });
await writeFile(
join(src, 'credentials', 'kimi-code.json'),
JSON.stringify({
access_token: 'a',
refresh_token: 'r',
expires_at: 1,
scope: 's',
token_type: 'Bearer',
}),
);
const plan = await detectMigration({ sourcePath: src });
const report = await runMigration({
plan,
scope: { config: true, mcp: true, userHistory: true, skills: true, sessions: false },
source: src,
target: tgt,
});
// The credential must not be copied into the target.
await expect(
readFile(join(tgt, 'credentials', 'kimi-code.json'), 'utf-8'),
).rejects.toThrow();
// The report tells the user to sign in again in kimi-code.
expect(report.notices.oauthLoginsRequiringRelogin).toContain('kimi-code');
} finally {
await rm(src, { recursive: true, force: true });
}
});
});
|