Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/vscode-node/test/diffState.spec.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { beforeEach, describe, expect, it, vi } from 'vitest';
7
import { TestLogService } from '../../../../../platform/testing/common/testLogService';
8
import {
9
DiffStateManager,
10
type ActiveDiff,
11
} from '../diffState';
12
13
vi.mock('vscode', () => ({
14
Uri: {
15
file: (path: string) => ({ fsPath: path, scheme: 'file' }),
16
},
17
window: {
18
tabGroups: {
19
activeTabGroup: {
20
activeTab: null,
21
},
22
all: [],
23
onDidChangeTabGroups: () => ({ dispose: () => { } }),
24
onDidChangeTabs: () => ({ dispose: () => { } }),
25
},
26
},
27
commands: {
28
executeCommand: vi.fn().mockResolvedValue(undefined),
29
},
30
TabInputTextDiff: class TabInputTextDiff {
31
constructor(public original: any, public modified: any) { }
32
},
33
}));
34
35
describe('diffState', () => {
36
let diffState: DiffStateManager;
37
38
const createMockDiff = (tabName: string, diffIdSuffix?: string): ActiveDiff => ({
39
diffId: `/tmp/modified-${diffIdSuffix ?? tabName}.ts`,
40
tabName: tabName,
41
originalUri: { fsPath: `/path/to/original-${tabName}.ts`, scheme: 'file' } as any,
42
modifiedUri: { fsPath: `/tmp/modified-${diffIdSuffix ?? tabName}.ts`, scheme: 'file' } as any,
43
newContents: `// new contents for ${tabName}`,
44
cleanup: vi.fn(),
45
resolve: vi.fn(),
46
});
47
48
beforeEach(() => {
49
diffState = new DiffStateManager(new TestLogService());
50
});
51
52
describe('register and getByTabName', () => {
53
it('should register and retrieve a diff by tabName', () => {
54
const diff = createMockDiff('Test Diff 1');
55
diffState.register(diff);
56
57
const retrieved = diffState.getByTabName('Test Diff 1');
58
expect(retrieved).toBe(diff);
59
});
60
61
it('should return undefined for non-existent tabName', () => {
62
const retrieved = diffState.getByTabName('non-existent');
63
expect(retrieved).toBeUndefined();
64
});
65
});
66
67
describe('unregister', () => {
68
it('should remove a registered diff', () => {
69
const diff = createMockDiff('Test Diff 2');
70
diffState.register(diff);
71
expect(diffState.getByTabName('Test Diff 2')).toBe(diff);
72
73
diffState.unregister(diff.diffId);
74
expect(diffState.getByTabName('Test Diff 2')).toBeUndefined();
75
});
76
77
it('should not throw when unregistering non-existent diff', () => {
78
expect(() => diffState.unregister('/tmp/non-existent.ts')).not.toThrow();
79
});
80
});
81
82
describe('getByTabName', () => {
83
it('should find diff by tab name', () => {
84
const diff1 = createMockDiff('My Diff Tab');
85
const diff2 = createMockDiff('Another Tab');
86
diffState.register(diff1);
87
diffState.register(diff2);
88
89
const found = diffState.getByTabName('My Diff Tab');
90
expect(found).toBe(diff1);
91
92
const found2 = diffState.getByTabName('Another Tab');
93
expect(found2).toBe(diff2);
94
});
95
96
it('should return undefined for non-existent tab name', () => {
97
const diff = createMockDiff('Existing Tab');
98
diffState.register(diff);
99
100
const found = diffState.getByTabName('Non-existent Tab');
101
expect(found).toBeUndefined();
102
});
103
104
it('should allow multiple diffs with same tab name but different diffIds', () => {
105
const diff1 = createMockDiff('Duplicate Name', 'diff1');
106
const diff2 = createMockDiff('Duplicate Name', 'diff2');
107
diffState.register(diff1);
108
diffState.register(diff2);
109
110
const found = diffState.getByTabName('Duplicate Name');
111
expect(found).toBe(diff1);
112
113
diffState.unregister(diff1.diffId);
114
const foundAfter = diffState.getByTabName('Duplicate Name');
115
expect(foundAfter).toBe(diff2);
116
});
117
});
118
119
describe('hasActiveDiffs', () => {
120
it('should return false when no diffs are registered', () => {
121
expect(diffState.hasActiveDiffs()).toBe(false);
122
});
123
124
it('should return true when diffs are registered', () => {
125
const diff = createMockDiff('Test Diff 3');
126
diffState.register(diff);
127
expect(diffState.hasActiveDiffs()).toBe(true);
128
});
129
130
it('should return false after all diffs are unregistered', () => {
131
const diff = createMockDiff('Test Diff 3');
132
diffState.register(diff);
133
expect(diffState.hasActiveDiffs()).toBe(true);
134
135
diffState.unregister(diff.diffId);
136
expect(diffState.hasActiveDiffs()).toBe(false);
137
});
138
});
139
140
describe('edge cases', () => {
141
it('should handle multiple diffs for the same original file', () => {
142
const diff1: ActiveDiff = {
143
diffId: '/tmp/modified-v1.ts',
144
tabName: 'file.ts (version 1)',
145
originalUri: { fsPath: '/path/to/file.ts', scheme: 'file' } as any,
146
modifiedUri: { fsPath: '/tmp/modified-v1.ts', scheme: 'file' } as any,
147
newContents: '// version 1',
148
cleanup: vi.fn(),
149
resolve: vi.fn(),
150
};
151
const diff2: ActiveDiff = {
152
diffId: '/tmp/modified-v2.ts',
153
tabName: 'file.ts (version 2)',
154
originalUri: { fsPath: '/path/to/file.ts', scheme: 'file' } as any,
155
modifiedUri: { fsPath: '/tmp/modified-v2.ts', scheme: 'file' } as any,
156
newContents: '// version 2',
157
cleanup: vi.fn(),
158
resolve: vi.fn(),
159
};
160
161
diffState.register(diff1);
162
diffState.register(diff2);
163
164
expect(diffState.getByTabName('file.ts (version 1)')).toBe(diff1);
165
expect(diffState.getByTabName('file.ts (version 2)')).toBe(diff2);
166
167
diffState.unregister(diff1.diffId);
168
diffState.unregister(diff2.diffId);
169
});
170
171
it('should handle re-registering same diffId (overwrites)', () => {
172
const diff1 = createMockDiff('Original Tab');
173
const diff2: ActiveDiff = {
174
diffId: diff1.diffId,
175
tabName: 'New Tab',
176
originalUri: { fsPath: '/path/to/new.ts', scheme: 'file' } as any,
177
modifiedUri: { fsPath: '/tmp/new-modified.ts', scheme: 'file' } as any,
178
newContents: '// new content',
179
cleanup: vi.fn(),
180
resolve: vi.fn(),
181
};
182
183
diffState.register(diff1);
184
expect(diffState.getByTabName('Original Tab')).toBe(diff1);
185
186
diffState.register(diff2);
187
expect(diffState.getByTabName('Original Tab')).toBeUndefined();
188
expect(diffState.getByTabName('New Tab')).toBe(diff2);
189
});
190
191
it('should handle concurrent registrations', () => {
192
const diffs = Array.from({ length: 10 }, (_, i) =>
193
createMockDiff(`Concurrent Tab ${i}`)
194
);
195
196
diffs.forEach(diff => diffState.register(diff));
197
198
diffs.forEach((diff, i) => {
199
expect(diffState.getByTabName(`Concurrent Tab ${i}`)).toBe(diff);
200
});
201
202
diffs.forEach(diff => diffState.unregister(diff.diffId));
203
});
204
});
205
});
206
207