| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 4 | |
| 5 | import { |
| 6 | TipsDisplay, |
| 7 | shouldShowTip, |
| 8 | CONTINUE_CLI_TIPS, |
| 9 | } from "./TipsDisplay.js"; |
| 10 | |
| 11 | describe("TipsDisplay", () => { |
| 12 | beforeEach(() => { |
| 13 | // Mock Math.random to make tests deterministic |
| 14 | vi.spyOn(Math, "random"); |
| 15 | }); |
| 16 | |
| 17 | afterEach(() => { |
| 18 | vi.restoreAllMocks(); |
| 19 | }); |
| 20 | |
| 21 | it("renders a random tip from the tips array", () => { |
| 22 | // Mock Math.random to return 0.5, which should select the middle tip |
| 23 | vi.mocked(Math.random).mockReturnValue(0.5); |
| 24 | |
| 25 | const expectedTipIndex = Math.floor(0.5 * CONTINUE_CLI_TIPS.length); |
| 26 | const expectedTip = CONTINUE_CLI_TIPS[expectedTipIndex]; |
| 27 | |
| 28 | const { lastFrame } = render(<TipsDisplay />); |
| 29 | |
| 30 | expect(lastFrame()).toContain("* Tip:"); |
| 31 | expect(lastFrame()).toContain(expectedTip); |
| 32 | }); |
| 33 | |
| 34 | it("renders different tips based on random selection", () => { |
| 35 | // Test with first tip |
| 36 | vi.mocked(Math.random).mockReturnValue(0.0); |
| 37 | const { lastFrame: firstFrame } = render(<TipsDisplay />); |
| 38 | expect(firstFrame()).toContain(CONTINUE_CLI_TIPS[0]); |
| 39 | |
| 40 | // Test with last tip - use partial match due to line wrapping |
| 41 | vi.mocked(Math.random).mockReturnValue(0.99); |
| 42 | const { lastFrame: lastFrame } = render(<TipsDisplay />); |
| 43 | const lastTip = CONTINUE_CLI_TIPS[CONTINUE_CLI_TIPS.length - 1]; |
| 44 | const firstPartOfLastTip = lastTip.substring(0, 30); // Check first 30 chars |
| 45 | expect(lastFrame()).toContain(firstPartOfLastTip); |
| 46 | }); |
| 47 | |
| 48 | it("always renders the tip structure correctly", () => { |
| 49 | vi.mocked(Math.random).mockReturnValue(0.5); |
| 50 | |
| 51 | const { lastFrame } = render(<TipsDisplay />); |
| 52 | const output = lastFrame(); |
| 53 | |
| 54 | // Should contain the tip icon and label |
| 55 | expect(output).toContain("* Tip:"); |
| 56 | |
| 57 | // Should contain one of the tips |
| 58 | const containsAtLeastOneTip = CONTINUE_CLI_TIPS.some( |
| 59 | (tip) => output?.includes(tip) ?? false, |
| 60 | ); |
| 61 | expect(containsAtLeastOneTip).toBe(true); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | describe("shouldShowTip", () => { |
| 66 | beforeEach(() => { |
| 67 | vi.spyOn(Math, "random"); |
| 68 | }); |
| 69 | |
| 70 | afterEach(() => { |
| 71 | vi.restoreAllMocks(); |
| 72 | }); |
| 73 | |
| 74 | it("returns true when random value is less than 0.2 (20%)", () => { |
| 75 | vi.mocked(Math.random).mockReturnValue(0.1); |
| 76 | expect(shouldShowTip()).toBe(true); |
| 77 | |
| 78 | vi.mocked(Math.random).mockReturnValue(0.19); |
| 79 | expect(shouldShowTip()).toBe(true); |
| 80 | }); |
| 81 | |
| 82 | it("returns false when random value is 0.2 or greater", () => { |
| 83 | vi.mocked(Math.random).mockReturnValue(0.2); |
| 84 | expect(shouldShowTip()).toBe(false); |
| 85 | |
| 86 | vi.mocked(Math.random).mockReturnValue(0.5); |
| 87 | expect(shouldShowTip()).toBe(false); |
| 88 | |
| 89 | vi.mocked(Math.random).mockReturnValue(0.99); |
| 90 | expect(shouldShowTip()).toBe(false); |
| 91 | }); |
| 92 | |
| 93 | it("has approximately 20% probability over many calls", () => { |
| 94 | // Restore original Math.random for this statistical test |
| 95 | vi.restoreAllMocks(); |
| 96 | |
| 97 | // Test the probability distribution over many calls |
| 98 | let trueCount = 0; |
| 99 | const iterations = 1000; |
| 100 | |
| 101 | for (let i = 0; i < iterations; i++) { |
| 102 | if (shouldShowTip()) { |
| 103 | trueCount++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | const probability = trueCount / iterations; |
| 108 | // Allow for some variance (15% to 25% range) |
| 109 | expect(probability).toBeGreaterThan(0.15); |
| 110 | expect(probability).toBeLessThan(0.25); |
| 111 | |
| 112 | // Re-mock for proper cleanup |
| 113 | vi.spyOn(Math, "random"); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | describe("CONTINUE_CLI_TIPS", () => { |
| 118 | it("contains at least 6 tips", () => { |
| 119 | expect(CONTINUE_CLI_TIPS.length).toBeGreaterThanOrEqual(6); |
| 120 | }); |
| 121 | |
| 122 | it("all tips are non-empty strings", () => { |
| 123 | CONTINUE_CLI_TIPS.forEach((tip, index) => { |
| 124 | expect(typeof tip).toBe("string"); |
| 125 | expect(tip.trim().length).toBeGreaterThan(0); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | it("tips are helpful and informative", () => { |
| 130 | // Check that tips contain useful keywords |
| 131 | const usefulKeywords = [ |
| 132 | "/help", |
| 133 | "escape", |
| 134 | "pause", |
| 135 | "arrow keys", |
| 136 | "multi-line", |
| 137 | "history", |
| 138 | "/resume", |
| 139 | "headless", |
| 140 | "flag", |
| 141 | "keyboard shortcuts", |
| 142 | ]; |
| 143 | |
| 144 | const allTipsText = CONTINUE_CLI_TIPS.join(" ").toLowerCase(); |
| 145 | |
| 146 | // At least half of the keywords should appear in the tips |
| 147 | const foundKeywords = usefulKeywords.filter((keyword) => |
| 148 | allTipsText.includes(keyword.toLowerCase()), |
| 149 | ); |
| 150 | |
| 151 | expect(foundKeywords.length).toBeGreaterThanOrEqual( |
| 152 | usefulKeywords.length / 2, |
| 153 | ); |
| 154 | }); |
| 155 | }); |