Skip to content

Commit f7facea

Browse files
sergeibbbd13
authored andcommitted
Add tests for findOldestStashTimestamp to cover edge cases and performance
(#4401, #4468)
1 parent 24ea659 commit f7facea

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import * as assert from 'assert';
2+
import type { GitStashCommit, GitStashParentInfo } from '../../../../../git/models/commit';
3+
import { findOldestStashTimestamp } from '../stash';
4+
5+
suite('findOldestStashTimestamp Test Suite', () => {
6+
function createMockStashCommit(date: Date, parentTimestamps?: GitStashParentInfo[]): Partial<GitStashCommit> {
7+
return {
8+
date: date,
9+
parentTimestamps: parentTimestamps,
10+
};
11+
}
12+
13+
test('should return Infinity for empty stashes collection', () => {
14+
const result = findOldestStashTimestamp([]);
15+
assert.strictEqual(result, Infinity);
16+
});
17+
18+
test('should return stash date when no parent timestamps exist', () => {
19+
const stashDate = new Date('2022-01-02T12:00:00Z');
20+
const stashes = [createMockStashCommit(stashDate)] as GitStashCommit[];
21+
22+
const result = findOldestStashTimestamp(stashes);
23+
assert.strictEqual(result, stashDate.getTime());
24+
});
25+
26+
test('should return stash date when parent timestamps are empty', () => {
27+
const stashDate = new Date('2022-01-02T12:00:00Z');
28+
const stashes = [createMockStashCommit(stashDate, [])] as GitStashCommit[];
29+
30+
const result = findOldestStashTimestamp(stashes);
31+
assert.strictEqual(result, stashDate.getTime());
32+
});
33+
34+
test('should return oldest parent timestamp when parent is older than stash', () => {
35+
const stashDate = new Date('2022-01-02T12:00:00Z');
36+
const oldest = 1640995200; // 2022-01-01 00:00:00 UTC
37+
const parentTimestamps: GitStashParentInfo[] = [
38+
{
39+
sha: 'parent1',
40+
authorDate: oldest,
41+
committerDate: 1640995260,
42+
},
43+
];
44+
const stashes = [createMockStashCommit(stashDate, parentTimestamps)] as GitStashCommit[];
45+
46+
const result = findOldestStashTimestamp(stashes);
47+
const expectedOldest = oldest * 1000; // Convert to milliseconds
48+
assert.strictEqual(result, expectedOldest);
49+
});
50+
51+
test('should return stash date when stash is older than parents', () => {
52+
const stashDate = new Date('2022-01-01T00:00:00Z'); // Older
53+
const parentTimestamps: GitStashParentInfo[] = [
54+
{
55+
sha: 'parent1',
56+
authorDate: 1641081600, // 2022-01-02 00:00:00 UTC (newer)
57+
committerDate: 1641081660,
58+
},
59+
];
60+
const stashes = [createMockStashCommit(stashDate, parentTimestamps)] as GitStashCommit[];
61+
62+
const result = findOldestStashTimestamp(stashes);
63+
assert.strictEqual(result, stashDate.getTime());
64+
});
65+
66+
test('should handle multiple stashes and find the globally oldest timestamp', () => {
67+
const stash1Date = new Date('2022-01-03T00:00:00Z');
68+
const oldest = 1640995200; // 2022-01-01 00:00:00 UTC (oldest overall)
69+
const stash1Parents: GitStashParentInfo[] = [
70+
{
71+
sha: 'parent1',
72+
authorDate: oldest,
73+
committerDate: 1640995260,
74+
},
75+
];
76+
77+
const stash2Date = new Date('2022-01-02T00:00:00Z');
78+
const stash2Parents: GitStashParentInfo[] = [
79+
{
80+
sha: 'parent2',
81+
authorDate: 1641081600, // 2022-01-02 00:00:00 UTC
82+
committerDate: 1641081660,
83+
},
84+
];
85+
86+
const stashes = [
87+
createMockStashCommit(stash1Date, stash1Parents),
88+
createMockStashCommit(stash2Date, stash2Parents),
89+
] as GitStashCommit[];
90+
91+
const result = findOldestStashTimestamp(stashes);
92+
const expectedOldest = oldest * 1000; // parent1's authorDate
93+
assert.strictEqual(result, expectedOldest);
94+
});
95+
96+
test('should consider both authorDate and committerDate of parents', () => {
97+
const stashDate = new Date('2022-01-02T00:00:00Z');
98+
const oldest = 1640995200; // 2022-01-01 00:00:00 UTC (older)
99+
const parentTimestamps: GitStashParentInfo[] = [
100+
{
101+
sha: 'parent1',
102+
authorDate: 1641081600, // 2022-01-02 00:00:00 UTC
103+
committerDate: oldest,
104+
},
105+
];
106+
const stashes = [createMockStashCommit(stashDate, parentTimestamps)] as GitStashCommit[];
107+
108+
const result = findOldestStashTimestamp(stashes);
109+
const expectedOldest = oldest * 1000; // committerDate is older
110+
assert.strictEqual(result, expectedOldest);
111+
});
112+
113+
test('should handle null/undefined parent timestamps gracefully', () => {
114+
const stashDate = new Date('2022-01-02T00:00:00Z');
115+
const oldest = 1640995200; // 2022-01-01 00:00:00 UTC
116+
const parentTimestamps: GitStashParentInfo[] = [
117+
{
118+
sha: 'parent1',
119+
authorDate: undefined,
120+
committerDate: null as any,
121+
},
122+
{
123+
sha: 'parent2',
124+
authorDate: oldest,
125+
committerDate: undefined,
126+
},
127+
];
128+
const stashes = [createMockStashCommit(stashDate, parentTimestamps)] as GitStashCommit[];
129+
130+
const result = findOldestStashTimestamp(stashes);
131+
const expectedOldest = oldest * 1000; // Only valid timestamp
132+
assert.strictEqual(result, expectedOldest);
133+
});
134+
135+
test('should handle multiple parents per stash', () => {
136+
const stashDate = new Date('2022-01-03T00:00:00Z');
137+
const oldest = 1640995200; // 2022-01-01 00:00:00 UTC (oldest)
138+
const parentTimestamps: GitStashParentInfo[] = [
139+
{
140+
sha: 'parent1',
141+
authorDate: 1641081600, // 2022-01-02 00:00:00 UTC
142+
committerDate: 1641081660,
143+
},
144+
{
145+
sha: 'parent2',
146+
authorDate: oldest,
147+
committerDate: 1640995260,
148+
},
149+
{
150+
sha: 'parent3',
151+
authorDate: 1641168000, // 2022-01-03 00:00:00 UTC
152+
committerDate: 1641168060,
153+
},
154+
];
155+
const stashes = [createMockStashCommit(stashDate, parentTimestamps)] as GitStashCommit[];
156+
157+
const result = findOldestStashTimestamp(stashes);
158+
const expectedOldest = oldest * 1000; // parent2's authorDate
159+
assert.strictEqual(result, expectedOldest);
160+
});
161+
162+
test('should work with Map.values() as used in production code', () => {
163+
const stashMap = new Map<string, GitStashCommit>();
164+
const oldest = 1640995200; // 2022-01-01 00:00:00 UTC (oldest)
165+
166+
const stash1Date = new Date('2022-01-02T00:00:00Z');
167+
const stash1Parents: GitStashParentInfo[] = [
168+
{
169+
sha: 'parent1',
170+
authorDate: oldest,
171+
committerDate: 1640995260,
172+
},
173+
];
174+
175+
stashMap.set('stash1', createMockStashCommit(stash1Date, stash1Parents) as GitStashCommit);
176+
177+
const result = findOldestStashTimestamp(stashMap.values());
178+
const expectedOldest = oldest * 1000;
179+
assert.strictEqual(result, expectedOldest);
180+
});
181+
182+
test('should handle edge case with only null parent timestamps', () => {
183+
const stashDate = new Date('2022-01-02T00:00:00Z');
184+
const parentTimestamps: GitStashParentInfo[] = [
185+
{
186+
sha: 'parent1',
187+
authorDate: null as any,
188+
committerDate: undefined,
189+
},
190+
];
191+
const stashes = [createMockStashCommit(stashDate, parentTimestamps)] as GitStashCommit[];
192+
193+
const result = findOldestStashTimestamp(stashes);
194+
assert.strictEqual(result, stashDate.getTime()); // Falls back to stash date
195+
});
196+
197+
test('should handle very large collections efficiently', () => {
198+
const stashes: GitStashCommit[] = [];
199+
const baseTime = new Date('2022-01-01T00:00:00Z').getTime();
200+
201+
// Create 1000 stashes with various timestamps
202+
for (let i = 0; i < 1000; i++) {
203+
const stashDate = new Date(baseTime + i * 60000); // Each stash 1 minute apart
204+
const parentTimestamps: GitStashParentInfo[] = [
205+
{
206+
sha: `parent${i}`,
207+
authorDate: Math.floor((baseTime + i * 30000) / 1000), // 30 seconds apart
208+
committerDate: Math.floor((baseTime + i * 45000) / 1000), // 45 seconds apart
209+
},
210+
];
211+
stashes.push(createMockStashCommit(stashDate, parentTimestamps) as GitStashCommit);
212+
}
213+
214+
const result = findOldestStashTimestamp(stashes);
215+
assert.strictEqual(result, baseTime); // Should be the first parent's authorDate
216+
});
217+
});

0 commit comments

Comments
 (0)