Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/__tests__/migrate-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, expect, it, beforeAll, afterAll } from 'vitest';
import os from 'node:os';
import type { AgentAdapter, SessionMeta } from '../session-flow/adapters/base.js';
import { ADAPTER_REGISTRY, type AdapterFactory } from '../session-flow/adapters/index.js';
import { fidelityFromSession } from '../session-flow/migrate.js';
import { fidelityFromSession, previewMigration } from '../session-flow/migrate.js';
import { AgentSession } from '../session-flow/agent-session.js';
import type { Session } from '../session-flow/ir.js';

Expand Down Expand Up @@ -217,3 +217,21 @@ describe('fidelityFromSession — 真实统计', () => {
});
});
});

describe('previewMigration', () => {
it('returns the source, target, session metadata, and computed fidelity', () => {
const session = makeSession();
const sourcePlatform = session.platform;
const targetPlatform = 'cursor';

expect(previewMigration(session, sourcePlatform, targetPlatform)).toEqual({
sourcePlatform,
targetPlatform,
sessionId: session.sessionId,
sessionTitle: session.title,
cwd: session.cwd,
messageCount: session.messages.length,
fidelity: fidelityFromSession(session, targetPlatform),
});
});
});
18 changes: 6 additions & 12 deletions src/__tests__/session-cmd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const mocks = vi.hoisted(() => ({
adaptersByPlatform: {} as Record<string, unknown>,
/** migrate.js mock 的 preview 返回值(Error 时抛出);agent-session.js mock 的 copy 返回值 */
previewResult: null as unknown,
previewCalls: [] as Array<{ session: unknown; source: string; target: string }>,
copyResult: null as unknown,
/** The OS the commands' `noAdapter` checks judge by; this machine's when unset. */
os: undefined as NodeJS.Platform | undefined,
Expand Down Expand Up @@ -119,14 +120,9 @@ vi.mock('../session-flow/adapters/index.js', () => ({
}));

vi.mock('../session-flow/migrate.js', () => ({
MigrationEngine: class {
constructor(
public readonly source: string,
public readonly target: string,
) {}
preview(): unknown {
return mocks.previewResult;
}
previewMigration: (session: unknown, source: string, target: string): unknown => {
mocks.previewCalls.push({ session, source, target });
return mocks.previewResult;
},
resolveTargetCwd: (explicit: string | undefined, sourceCwd: string | undefined) => explicit ?? sourceCwd,
}));
Expand Down Expand Up @@ -186,6 +182,7 @@ beforeEach(() => {
mocks.gitCalls.length = 0;
mocks.adaptersByPlatform = {};
mocks.previewResult = null;
mocks.previewCalls.length = 0;
mocks.copyResult = null;
mocks.os = undefined;
mocks.heldArchiveLock = '';
Expand Down Expand Up @@ -714,16 +711,13 @@ describe('session migrate interactive picker', () => {
filePath: '/tmp/tgt.jsonl',
fidelity: { score: 1 },
};
const { MigrationEngine } = await import('../session-flow/migrate.js');
const previewSpy = vi.spyOn(MigrationEngine.prototype, 'preview');

feedAnswers(['n', '11']);
await runSession('migrate', '-s', 'fakeplat', '-t', 'tgtplat', '--cwd', '/run/dir');

// The picker is on page 2 by the time the number is entered, and the
// selected id is the 11th newest -- unreachable before paging existed.
expect(out.join('\n')).toContain('page 2/2');
expect(previewSpy).toHaveBeenCalledWith(sessions[1]);
expect(mocks.previewCalls).toContainEqual({ session: sessions[1], source: 'fakeplat', target: 'tgtplat' });
});

it('cancels on an empty answer instead of paging forever', async () => {
Expand Down
39 changes: 15 additions & 24 deletions src/session-flow/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,21 @@ export function resolveTargetCwd(
return fallback;
}

// ---------------------------------------------------------------------------
// MigrationEngine
// ---------------------------------------------------------------------------

export class MigrationEngine {
constructor(
private sourcePlatform: string,
private targetPlatform: string,
) {}

/** What migrating `session`, read from the source platform, would carry over. */
preview(session: Session): MigrationPreview {
const fidelity = fidelityFromSession(session, this.targetPlatform);

return {
sourcePlatform: this.sourcePlatform,
targetPlatform: this.targetPlatform,
sessionId: session.sessionId,
sessionTitle: session.title,
cwd: session.cwd,
messageCount: session.messages.length,
fidelity,
};
}
/** What migrating `session`, read from `sourcePlatform`, would carry over. */
export function previewMigration(
session: Session,
sourcePlatform: string,
targetPlatform: string,
): MigrationPreview {
return {
sourcePlatform,
targetPlatform,
sessionId: session.sessionId,
sessionTitle: session.title,
cwd: session.cwd,
messageCount: session.messages.length,
fidelity: fidelityFromSession(session, targetPlatform),
};
}

export { getAdapter, listAvailablePlatforms, listInstalledPlatforms };
Loading
Loading