src/vs/platform/agentHost/test/common/sessionTestHelpers.ts
316 LOC · 264 covered · 52 uncovered · 63 ranges · 2241 concepts · 27 introducers · 1009 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
sessionTestHelpers.ts ×33
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { IReference } from '../../../../base/common/lifecycle.js';
import { Schemas } from '../../../../base/common/network.js';
import { URI } from '../../../../base/common/uri.js';
import { Event } from '../../../../base/common/event.js';
import type { IDiffComputeService, IDiffCountResult } from '../../common/diffComputeService.js';
import type { IFileEditContent, IFileEditRecord, ILocalTurnRecord, IReviewedFileRecord, ISessionDatabase, ISessionDataService } from '../../common/sessionDataService.js';
import type { Message } from '../../common/state/sessionState.js';
export class TestSessionDatabase implements ISessionDatabase {
private readonly _metadata = new Map<string, string>();
private readonly _drafts = new Map<string, Message>();
private readonly _reviewedFiles: IReviewedFileRecord[] = [];
private readonly _localTurns = new Map<string, ILocalTurnRecord>();
getAllFileEditsCalls = 0;
getFileEditsByTurnCalls = 0;
deleteTurnsAfterCalls: string[] = [];
deleteAllTurnsCalls = 0;
setTurnEventIdCalls: Array<{ turnId: string; eventId: string }> = [];
addEdit(edit: IFileEditRecord & IFileEditContent): void {
}
async createTurn(): Promise<void> { }
async deleteTurn(turnId: string): Promise<void> {
for (let i = this._edits.length - 1; i >= 0; i--) {
if (this._edits[i].turnId === turnId) {
this._edits.splice(i, 1);
}
}
}
async storeFileEdit(edit: IFileEditRecord & IFileEditContent): Promise<void> {
const existingIndex = this._edits.findIndex(e => e.toolCallId === edit.toolCallId && e.filePath === edit.filePath);
claudeFileEditObserver.ts ×2
if (existingIndex >= 0) {
this._edits[existingIndex] = edit;
this._edits.push(edit);
}
}
async getFileEdits(toolCallIds: string[]): Promise<IFileEditRecord[]> {
const toolCallIdsSet = new Set(toolCallIds);
return this._toEditRecords(this._edits.filter(e => toolCallIdsSet.has(e.toolCallId)));
}
async getAllFileEdits(): Promise<IFileEditRecord[]> {
return this._toEditRecords(this._edits);
}
async getFileEditsByTurn(turnId: string): Promise<IFileEditRecord[]> {
return this._toEditRecords(this._edits.filter(e => e.turnId === turnId));
}
async readFileEditContent(toolCallId: string, filePath: string): Promise<IFileEditContent | undefined> {
return this._edits.find(e => e.toolCallId === toolCallId && e.filePath === filePath);
sessionTestHelpers.ts ×1
}
async getMetadata(key: string): Promise<string | undefined> {
}
async getMetadataObject<T extends Record<string, unknown>>(obj: T): Promise<{ [K in keyof T]: string | undefined }> {
return Object.fromEntries(Object.keys(obj).map(key => [key, this._metadata.get(key)])) as { [K in keyof T]: string | undefined };
sessionTestHelpers.ts ×1
}
async setMetadata(key: string, value: string): Promise<void> {
}
async setChatDraft(chat: URI, draft: Message | undefined): Promise<void> {
if (draft) {
this._drafts.set(key, draft);
} else {
this._drafts.delete(key);
}
async getChatDraft(chat: URI): Promise<Message | undefined> {
}
async close(): Promise<void> { }
async vacuumInto(_targetPath: string): Promise<void> { }
dispose(): void { }
async setTurnEventId(turnId: string, eventId: string): Promise<void> {
}
async getTurnEventId(_turnId: string): Promise<string | undefined> { return undefined; }
async getNextTurnEventId(_turnId: string): Promise<string | undefined> { return undefined; }
async getFirstTurnEventId(): Promise<string | undefined> { return undefined; }
async truncateFromTurn(_turnId: string): Promise<void> { }
async deleteTurnsAfter(turnId: string): Promise<void> {
}
async deleteAllTurns(): Promise<void> {
this._edits.length = 0;
}
async insertLocalTurn(record: ILocalTurnRecord): Promise<void> {
}
async getLocalTurns(): Promise<ILocalTurnRecord[]> {
}
async deleteLocalTurns(turnIds: readonly string[]): Promise<void> {
this._localTurns.delete(id);
}
}
async remapTurnIds(_mapping: ReadonlyMap<string, string>): Promise<void> { }
sessionTestHelpers.ts ×33
async markFileReviewed(uri: URI, nonce: string): Promise<void> {
if (!this._reviewedFiles.some(r => r.uri.toString() === uri.toString() && r.nonce === nonce)) {
this._reviewedFiles.push({ uri, nonce });
}
}
async unmarkFileReviewed(uri: URI, nonce: string): Promise<void> {
const index = this._reviewedFiles.findIndex(r => r.uri.toString() === uri.toString() && r.nonce === nonce);
if (index >= 0) {
this._reviewedFiles.splice(index, 1);
}
}
async getReviewedFiles(): Promise<IReviewedFileRecord[]> {
return [...this._reviewedFiles];
}
async getReviewedFilesForUri(uri: URI): Promise<IReviewedFileRecord[]> {
return this._reviewedFiles.filter(r => r.uri.toString() === uri.toString());
}
async isFileReviewed(uri: URI, nonce: string): Promise<boolean> {
return this._reviewedFiles.some(r => r.uri.toString() === uri.toString() && r.nonce === nonce);
}
async setTurnCheckpointRef(_turnId: string, _ref: string): Promise<void> { }
async getTurnCheckpointRef(_turnId: string): Promise<string | undefined> { return undefined; }
async getPreviousCheckpointRef(_turnId: string): Promise<string | undefined> { return undefined; }
async getAllCheckpointRefs(): Promise<string[]> { return []; }
async whenIdle(): Promise<void> { }
private _toEditRecords(edits: (IFileEditRecord & IFileEditContent)[]): IFileEditRecord[] {
return edits.map(({ beforeContent: _, afterContent: _2, ...metadata }) => metadata);
sessionTestHelpers.ts ×1
}
export class TestDiffComputeService implements IDiffComputeService {
declare readonly _serviceBrand: undefined;
callCount = 0;
constructor(private readonly _result?: IDiffCountResult) { }
async computeDiffCounts(original: string, modified: string): Promise<IDiffCountResult> {
if (this._result) {
}
const modifiedLines = modified ? modified.split('\n') : [];
return {
added: Math.max(0, modifiedLines.length - originalLines.length),
removed: Math.max(0, originalLines.length - modifiedLines.length),
};
}
export function createZeroDiffComputeService(): IDiffComputeService {
}
export function createSessionDataService(database: ISessionDatabase = new TestSessionDatabase()): ISessionDataService {
_serviceBrand: undefined,
getSessionDataDir: session => URI.from({ scheme: Schemas.inMemory, path: `/session-data${session.path}` }),
getSessionDataDirById: sessionId => URI.from({ scheme: Schemas.inMemory, path: `/session-data/${sessionId}` }),
openDatabase: () => createReference(database),
tryOpenDatabase: async () => createReference(database),
deleteSessionData: async () => { },
onWillDeleteSessionData: Event.None,
cleanupOrphanedData: async () => { },
whenIdle: async () => { },
};
}
export function createNullSessionDataService(): ISessionDataService {
_serviceBrand: undefined,
getSessionDataDir: session => URI.from({ scheme: Schemas.inMemory, path: `/session-data${session.path}` }),
getSessionDataDirById: sessionId => URI.from({ scheme: Schemas.inMemory, path: `/session-data/${sessionId}` }),
openDatabase: () => { throw new Error('not implemented'); },
tryOpenDatabase: async () => undefined,
deleteSessionData: async () => { },
onWillDeleteSessionData: Event.None,
cleanupOrphanedData: async () => { },
whenIdle: async () => { },
};
}
export function encodeString(text: string): Uint8Array {
}
/**
* Returns a no-op {@link IAgentHostGitService} suitable for tests that
* exercise the {@link AgentService} but don't care about git state.
* Tests that DO care about git state should pass their own implementation.
*/
export function createNoopGitService(): import('../../common/agentHostGitService.js').IAgentHostGitService {
_serviceBrand: undefined,
getCurrentBranch: async () => undefined,
getDefaultBranch: async () => undefined,
getBranch: async () => undefined,
getRefs: async () => [],
getBranches: async () => [],
getRepositoryRoot: async () => undefined,
getWorktreeRoots: async () => [],
addWorktree: async () => { },
copyWorktreeIncludeFiles: async () => { },
addExistingWorktree: async () => { },
removeWorktree: async () => { },
branchExists: async () => false,
hasUncommittedChanges: async () => false,
commitAll: async () => { },
restore: async () => { },
hasUpstream: async () => false,
pull: async () => { },
push: async () => { },
getSessionGitState: async () => undefined,
computeSessionFileDiffs: async () => undefined,
resolveBranchBaselineCommit: async () => undefined,
showBlob: async () => undefined,
captureWorkingTreeAsTree: async () => undefined,
commitTree: async () => undefined,
updateRef: async () => { },
deleteRefs: async () => { },
revParse: async () => undefined,
overlayPathIntoTree: async () => undefined,
diffTreePaths: async () => undefined,
computeFileDiffsBetweenRefs: async () => undefined,
getFetchRemoteUrls: async () => undefined,
getUntrackedPaths: async () => [],
getBranchDiffSafetyInfo: async () => undefined,
getDiffPatchBetweenRefs: async () => undefined,
};
}
/**
* Returns a no-op {@link IAgentHostChangesetService} for tests that need to
* inject the changeset service but don't exercise changeset computation.
* Individual methods can be reassigned by callers that want to spy on them.
*/
export function createNoopChangesetService(): import('../../common/agentHostChangesetService.js').IAgentHostChangesetService {
return {
_serviceBrand: undefined,
registerStaticChangesets: () => { },
restoreStaticChangeset: () => { },
parsePersistedStaticChangesets: () => ({}),
applyPersistedStaticChangesets: () => { },
restorePersistedStaticChangesets: () => ({}),
persistChangesSummary: () => { },
getListMetadataKeys: () => undefined,
computeListEntryChanges: () => undefined,
isStaticChangesetComputeActive: () => false,
refreshChangesetCatalog: () => { },
refreshBranchChangeset: () => { },
refreshSessionChangeset: () => { },
onWorkingDirectoryAvailable: () => { },
recomputeSubscribedChangesets: () => { },
onSessionDisposed: () => { },
computeTurnChangeset: async session => session,
computeCompareTurnsChangeset: async session => session,
computeUncommittedChangeset: async session => session,
onToolCallEditsApplied: () => { },
onTurnComplete: () => { },
onSessionTruncated: () => { },
};
}
return {
object,
dispose: () => { },
};
}