mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-15 08:31:14 -07:00
feat: add Rewind Confirmation dialog and Rewind Viewer component (#15717)
This commit is contained in:
@@ -4,8 +4,13 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { formatDuration, formatMemoryUsage } from './formatters.js';
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import {
|
||||
formatDuration,
|
||||
formatMemoryUsage,
|
||||
formatTimeAgo,
|
||||
stripReferenceContent,
|
||||
} from './formatters.js';
|
||||
|
||||
describe('formatters', () => {
|
||||
describe('formatMemoryUsage', () => {
|
||||
@@ -69,4 +74,93 @@ describe('formatters', () => {
|
||||
expect(formatDuration(-100)).toBe('0s');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatTimeAgo', () => {
|
||||
const NOW = new Date('2025-01-01T12:00:00Z');
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(NOW);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should return "just now" for dates less than a minute ago', () => {
|
||||
const past = new Date(NOW.getTime() - 30 * 1000);
|
||||
expect(formatTimeAgo(past)).toBe('just now');
|
||||
});
|
||||
|
||||
it('should return minutes ago', () => {
|
||||
const past = new Date(NOW.getTime() - 5 * 60 * 1000);
|
||||
expect(formatTimeAgo(past)).toBe('5m ago');
|
||||
});
|
||||
|
||||
it('should return hours ago', () => {
|
||||
const past = new Date(NOW.getTime() - 3 * 60 * 60 * 1000);
|
||||
expect(formatTimeAgo(past)).toBe('3h ago');
|
||||
});
|
||||
|
||||
it('should return days ago', () => {
|
||||
const past = new Date(NOW.getTime() - 2 * 24 * 60 * 60 * 1000);
|
||||
expect(formatTimeAgo(past)).toBe('48h ago');
|
||||
});
|
||||
|
||||
it('should handle string dates', () => {
|
||||
const past = '2025-01-01T11:00:00Z'; // 1 hour ago
|
||||
expect(formatTimeAgo(past)).toBe('1h ago');
|
||||
});
|
||||
|
||||
it('should handle number timestamps', () => {
|
||||
const past = NOW.getTime() - 10 * 60 * 1000; // 10 minutes ago
|
||||
expect(formatTimeAgo(past)).toBe('10m ago');
|
||||
});
|
||||
it('should handle invalid timestamps', () => {
|
||||
const past = 'hello';
|
||||
expect(formatTimeAgo(past)).toBe('invalid date');
|
||||
});
|
||||
});
|
||||
|
||||
describe('stripReferenceContent', () => {
|
||||
it('should return the original text if no markers are present', () => {
|
||||
const text = 'Hello world';
|
||||
expect(stripReferenceContent(text)).toBe(text);
|
||||
});
|
||||
|
||||
it('should strip content between markers', () => {
|
||||
const text =
|
||||
'Prompt @file.txt\n--- Content from referenced files ---\nFile content here\n--- End of content ---';
|
||||
expect(stripReferenceContent(text)).toBe('Prompt @file.txt');
|
||||
});
|
||||
|
||||
it('should strip content and keep text after the markers', () => {
|
||||
const text =
|
||||
'Before\n--- Content from referenced files ---\nMiddle\n--- End of content ---\nAfter';
|
||||
expect(stripReferenceContent(text)).toBe('Before\nAfter');
|
||||
});
|
||||
|
||||
it('should handle missing end marker gracefully', () => {
|
||||
const text = 'Before\n--- Content from referenced files ---\nMiddle';
|
||||
expect(stripReferenceContent(text)).toBe(text);
|
||||
});
|
||||
|
||||
it('should handle end marker before start marker gracefully', () => {
|
||||
const text =
|
||||
'--- End of content ---\n--- Content from referenced files ---';
|
||||
expect(stripReferenceContent(text)).toBe(text);
|
||||
});
|
||||
|
||||
it('should strip even if markers are on the same line (though unlikely)', () => {
|
||||
const text =
|
||||
'A--- Content from referenced files ---B--- End of content ---C';
|
||||
expect(stripReferenceContent(text)).toBe('AC');
|
||||
});
|
||||
|
||||
it('should strip multiple blocks correctly and preserve text in between', () => {
|
||||
const text =
|
||||
'Start\n--- Content from referenced files ---\nBlock1\n--- End of content ---\nMiddle\n--- Content from referenced files ---\nBlock2\n--- End of content ---\nEnd';
|
||||
expect(stripReferenceContent(text)).toBe('Start\nMiddle\nEnd');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -61,3 +61,37 @@ export const formatDuration = (milliseconds: number): string => {
|
||||
|
||||
return parts.join(' ');
|
||||
};
|
||||
|
||||
export const formatTimeAgo = (date: string | number | Date): string => {
|
||||
const past = new Date(date);
|
||||
if (isNaN(past.getTime())) {
|
||||
return 'invalid date';
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - past.getTime();
|
||||
if (diffMs < 60000) {
|
||||
return 'just now';
|
||||
}
|
||||
return `${formatDuration(diffMs)} ago`;
|
||||
};
|
||||
|
||||
const REFERENCE_CONTENT_START = '--- Content from referenced files ---';
|
||||
const REFERENCE_CONTENT_END = '--- End of content ---';
|
||||
|
||||
/**
|
||||
* Removes content bounded by reference content markers from the given text.
|
||||
* The markers are "--- Content from referenced files ---" and "--- End of content ---".
|
||||
*
|
||||
* @param text The input text containing potential reference blocks.
|
||||
* @returns The text with reference blocks removed and trimmed.
|
||||
*/
|
||||
export function stripReferenceContent(text: string): string {
|
||||
// Match optional newline, the start marker, content (non-greedy), and the end marker
|
||||
const pattern = new RegExp(
|
||||
`\\n?${REFERENCE_CONTENT_START}[\\s\\S]*?${REFERENCE_CONTENT_END}`,
|
||||
'g',
|
||||
);
|
||||
|
||||
return text.replace(pattern, '').trim();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user