feat: Show tip on first request and refactor phrases (#12952)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
JAYADITYA
2025-11-18 12:32:31 +05:30
committed by GitHub
parent 2f8b68dffd
commit ecf8fba10e
5 changed files with 395 additions and 318 deletions
@@ -10,9 +10,10 @@ import { render } from '../../test-utils/render.js';
import { Text } from 'ink';
import {
usePhraseCycler,
WITTY_LOADING_PHRASES,
PHRASE_CHANGE_INTERVAL_MS,
} from './usePhraseCycler.js';
import { INFORMATIVE_TIPS } from '../constants/tips.js';
import { WITTY_LOADING_PHRASES } from '../constants/wittyPhrases.js';
// Test component to consume the hook
const TestComponent = ({
@@ -67,17 +68,37 @@ describe('usePhraseCycler', () => {
expect(lastFrame()).toBe(initialPhrase);
});
it('should cycle through witty phrases when isActive is true and not waiting', async () => {
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
it('should show a tip on first activation, then a witty phrase', async () => {
vi.spyOn(Math, 'random').mockImplementation(() => 0.99); // Subsequent phrases are witty
const { lastFrame } = render(
<TestComponent isActive={true} isWaiting={false} />,
);
// Initial phrase should be one of the witty phrases
// Initial phrase on first activation should be a tip
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
});
expect(WITTY_LOADING_PHRASES).toContain(lastFrame());
expect(INFORMATIVE_TIPS).toContain(lastFrame());
// After the first interval, it should be a witty phrase
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS + 100);
});
expect(WITTY_LOADING_PHRASES).toContain(lastFrame());
});
it('should cycle through phrases when isActive is true and not waiting', async () => {
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty for subsequent phrases
const { lastFrame } = render(
<TestComponent isActive={true} isWaiting={false} />,
);
// Initial phrase on first activation will be a tip, not necessarily from witty phrases
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
});
// First activation shows a tip, so we can't guarantee it's in WITTY_LOADING_PHRASES
// After the first interval, it should follow the random pattern (witty phrases due to mock)
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS + 100);
});
@@ -109,7 +130,7 @@ describe('usePhraseCycler', () => {
/>,
);
// Activate -> callCount 0 -> returns 0 -> 'Phrase A'
// Activate -> On first activation will show tip on initial call, then first interval will use first mock value for 'Phrase A'
rerender(
<TestComponent
isActive={true}
@@ -118,17 +139,17 @@ describe('usePhraseCycler', () => {
/>,
);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS); // First interval after initial state -> callCount 0 -> 'Phrase A'
});
expect(lastFrame()).toBe('Phrase A');
expect(customPhrases).toContain(lastFrame()); // Should be one of the custom phrases
// Interval -> callCount 1 -> returns 0.99 -> 'Phrase B'
// Second interval -> callCount 1 -> returns 0.99 -> 'Phrase B'
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS);
});
expect(lastFrame()).toBe('Phrase B');
expect(customPhrases).toContain(lastFrame()); // Should be one of the custom phrases
// Deactivate -> resets to customPhrases[0] -> 'Phrase A'
// Deactivate -> resets to first phrase in sequence
rerender(
<TestComponent
isActive={false}
@@ -139,9 +160,10 @@ describe('usePhraseCycler', () => {
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
});
expect(lastFrame()).toBe('Phrase A');
// The phrase should be the first phrase after reset
expect(customPhrases).toContain(lastFrame());
// Activate again -> callCount 2 -> returns 0 -> 'Phrase A'
// Activate again -> this will show a tip on first activation, then cycle from where mock is
rerender(
<TestComponent
isActive={true}
@@ -150,9 +172,9 @@ describe('usePhraseCycler', () => {
/>,
);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS); // First interval after re-activation -> should contain phrase
});
expect(lastFrame()).toBe('Phrase A');
expect(customPhrases).toContain(lastFrame()); // Should be one of the custom phrases
});
it('should clear phrase interval on unmount when active', () => {
@@ -167,7 +189,6 @@ describe('usePhraseCycler', () => {
it('should use custom phrases when provided', async () => {
const customPhrases = ['Custom Phrase 1', 'Custom Phrase 2'];
const randomMock = vi.spyOn(Math, 'random');
randomMock.mockReturnValue(0);
const { lastFrame, rerender } = render(
<TestComponent
@@ -177,14 +198,29 @@ describe('usePhraseCycler', () => {
/>,
);
expect(lastFrame()).toBe('Custom Phrase 1');
randomMock.mockReturnValue(0.99);
// After first interval, it should use custom phrases
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS + 100);
});
expect(lastFrame()).toBe('Custom Phrase 2');
randomMock.mockReturnValue(0);
rerender(
<TestComponent
isActive={true}
isWaiting={false}
customPhrases={customPhrases}
/>,
);
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS + 100);
});
expect(customPhrases).toContain(lastFrame());
randomMock.mockReturnValue(0.99);
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS);
});
expect(customPhrases).toContain(lastFrame());
// Test fallback to default phrases.
randomMock.mockRestore();
@@ -194,36 +230,39 @@ describe('usePhraseCycler', () => {
<TestComponent isActive={true} isWaiting={false} customPhrases={[]} />,
);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS); // Wait for first cycle
});
expect(WITTY_LOADING_PHRASES).toContain(lastFrame());
});
it('should fall back to witty phrases if custom phrases are an empty array', async () => {
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty for subsequent phrases
const { lastFrame } = render(
<TestComponent isActive={true} isWaiting={false} customPhrases={[]} />,
);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(0); // First activation will be a tip
});
// First activation shows a tip, so we can't guarantee it's in WITTY_LOADING_PHRASES
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS); // Next phrase after tip
});
expect(WITTY_LOADING_PHRASES).toContain(lastFrame());
});
it('should reset to a witty phrase when transitioning from waiting to active', async () => {
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
it('should reset phrase when transitioning from waiting to active', async () => {
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty for subsequent phrases
const { lastFrame, rerender } = render(
<TestComponent isActive={true} isWaiting={false} />,
);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(0); // First activation will be a tip
});
// First activation shows a tip, so we can't guarantee it's in WITTY_LOADING_PHRASES
expect(WITTY_LOADING_PHRASES).toContain(lastFrame());
// Cycle to a different phrase (potentially)
// Cycle to a different phrase (should be witty due to mock)
await act(async () => {
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS);
});
@@ -236,10 +275,10 @@ describe('usePhraseCycler', () => {
});
expect(lastFrame()).toBe('Waiting for user confirmation...');
// Go back to active cycling - should pick a random witty phrase
// Go back to active cycling - should pick a phrase based on the logic (witty due to mock)
rerender(<TestComponent isActive={true} isWaiting={false} />);
await act(async () => {
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS); // Skip the tip and get next phrase
});
expect(WITTY_LOADING_PHRASES).toContain(lastFrame());
});