|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { coachCommand } from "../coach.js"; |
| 3 | +import { |
| 4 | + readConfig, |
| 5 | + readLastNLoopLogs, |
| 6 | + readLoopLog, |
| 7 | + readBriefJson, |
| 8 | +} from "../../storage/local.js"; |
| 9 | +import { computeShippingDNA } from "../../analytics/dna.js"; |
| 10 | +import { computeBenchmarks } from "../../analytics/benchmarks.js"; |
| 11 | +import { generateStructured } from "../../ai/client.js"; |
| 12 | +import { clog, ceremonyIntro, ceremonyOutro } from "../../ui/theme.js"; |
| 13 | + |
| 14 | +// Mock storage/local.js |
| 15 | +vi.mock("../../storage/local.js", () => ({ |
| 16 | + readConfig: vi.fn(), |
| 17 | + writeConfig: vi.fn(), |
| 18 | + readLastNLoopLogs: vi.fn(), |
| 19 | + readLoopLog: vi.fn(), |
| 20 | + readBriefJson: vi.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +// Mock analytics/dna.js |
| 24 | +vi.mock("../../analytics/dna.js", () => ({ |
| 25 | + computeShippingDNA: vi.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +// Mock analytics/benchmarks.js |
| 29 | +vi.mock("../../analytics/benchmarks.js", () => ({ |
| 30 | + computeBenchmarks: vi.fn(), |
| 31 | +})); |
| 32 | + |
| 33 | +// Mock ai/client.js |
| 34 | +vi.mock("../../ai/client.js", () => ({ |
| 35 | + generateStructured: vi.fn(), |
| 36 | +})); |
| 37 | + |
| 38 | +// Mock ui/theme.js for spying on stdout and prompts |
| 39 | +vi.mock("../../ui/theme.js", async (importOriginal) => { |
| 40 | + const original = await importOriginal<any>(); |
| 41 | + return { |
| 42 | + ...original, |
| 43 | + ceremonyIntro: vi.fn(), |
| 44 | + ceremonyOutro: vi.fn(), |
| 45 | + spinner: vi.fn(() => ({ |
| 46 | + start: vi.fn(), |
| 47 | + stop: vi.fn(), |
| 48 | + })), |
| 49 | + }; |
| 50 | +}); |
| 51 | + |
| 52 | +describe("coachCommand --dna", () => { |
| 53 | + beforeEach(() => { |
| 54 | + vi.clearAllMocks(); |
| 55 | + // Default config mock |
| 56 | + vi.mocked(readConfig).mockReturnValue({ |
| 57 | + version: 1, |
| 58 | + activeProject: "test-project", |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("exits early with a friendly reminder if less than 4 weeks of data exist", async () => { |
| 63 | + vi.mocked(computeShippingDNA).mockReturnValue(null); |
| 64 | + |
| 65 | + const stepSpy = vi.spyOn(clog, "step").mockImplementation(() => {}); |
| 66 | + const messageSpy = vi.spyOn(clog, "message").mockImplementation(() => {}); |
| 67 | + const infoSpy = vi.spyOn(clog, "info").mockImplementation(() => {}); |
| 68 | + |
| 69 | + await coachCommand({ dna: true }); |
| 70 | + |
| 71 | + expect(ceremonyIntro).toHaveBeenCalledWith("Founder DNA Report"); |
| 72 | + expect(stepSpy).toHaveBeenCalledWith("Not Enough Data Yet"); |
| 73 | + expect(messageSpy).toHaveBeenCalledWith( |
| 74 | + expect.stringContaining("requires at least 4 weeks of loop data") |
| 75 | + ); |
| 76 | + expect(ceremonyOutro).toHaveBeenCalledWith( |
| 77 | + expect.stringContaining("Keep shipping") |
| 78 | + ); |
| 79 | + |
| 80 | + stepSpy.mockRestore(); |
| 81 | + messageSpy.mockRestore(); |
| 82 | + infoSpy.mockRestore(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("renders a beautiful Spotify Wrapped-style card when 4+ weeks of data exist", async () => { |
| 86 | + const mockDNA = { |
| 87 | + pattern: "All-Star" as const, |
| 88 | + patternDescription: "You maintain both high output and high consistency.", |
| 89 | + velocityTrend: "steady" as const, |
| 90 | + avgTasksCompleted: 5, |
| 91 | + avgScore: 88, |
| 92 | + peakDay: "Wednesday", |
| 93 | + completionStyle: "finisher" as const, |
| 94 | + totalWeeks: 6, |
| 95 | + streak: 4, |
| 96 | + riskWarnings: ["declining velocity"], |
| 97 | + strengths: ["highly consistent"], |
| 98 | + }; |
| 99 | + |
| 100 | + vi.mocked(computeShippingDNA).mockReturnValue(mockDNA); |
| 101 | + |
| 102 | + // Mock loop logs for Best Week calculation |
| 103 | + vi.mocked(readLastNLoopLogs).mockReturnValue([ |
| 104 | + { weekNumber: 2, overridden: false }, |
| 105 | + { weekNumber: 1, overridden: false }, |
| 106 | + ]); |
| 107 | + |
| 108 | + vi.mocked(readLoopLog).mockImplementation((weekNum) => { |
| 109 | + if (weekNum === 2) { |
| 110 | + return [ |
| 111 | + "# Week 2", |
| 112 | + "- Tasks completed: 6", |
| 113 | + "- Tasks open: 0", |
| 114 | + "- Shipping score: 95%", |
| 115 | + ].join("\n"); |
| 116 | + } |
| 117 | + if (weekNum === 1) { |
| 118 | + return [ |
| 119 | + "# Week 1", |
| 120 | + "- Tasks completed: 4", |
| 121 | + "- Tasks open: 2", |
| 122 | + "- Shipping score: 80%", |
| 123 | + ].join("\n"); |
| 124 | + } |
| 125 | + return null; |
| 126 | + }); |
| 127 | + |
| 128 | + vi.mocked(computeBenchmarks).mockReturnValue({ |
| 129 | + overallPercentile: 92, |
| 130 | + comparison: "Outperforming 92% of founders.", |
| 131 | + velocityPercentile: 85, |
| 132 | + scorePercentile: 90, |
| 133 | + }); |
| 134 | + |
| 135 | + vi.mocked(readBriefJson).mockReturnValue({ |
| 136 | + answers: { |
| 137 | + name: "TestApp", |
| 138 | + problem: "pain point", |
| 139 | + icp: "developers", |
| 140 | + whyUnsolved: "complex", |
| 141 | + mvp: "cli", |
| 142 | + }, |
| 143 | + brief: { |
| 144 | + bet: "bet on developers", |
| 145 | + riskiestAssumption: "no one uses CLI", |
| 146 | + validateAction: "launch", |
| 147 | + mvpPlainEnglish: "cli app", |
| 148 | + icpScore: 8, |
| 149 | + icpNote: "", |
| 150 | + problemScore: 8, |
| 151 | + problemNote: "", |
| 152 | + mvpScore: 8, |
| 153 | + mvpNote: "", |
| 154 | + overallScore: 8, |
| 155 | + }, |
| 156 | + }); |
| 157 | + |
| 158 | + vi.mocked(generateStructured).mockResolvedValue({ |
| 159 | + recommendation: "Keep doing what you're doing. Expand tests for reliability.", |
| 160 | + }); |
| 161 | + |
| 162 | + const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 163 | + |
| 164 | + await coachCommand({ dna: true }); |
| 165 | + |
| 166 | + expect(ceremonyIntro).toHaveBeenCalledWith("Founder DNA Report"); |
| 167 | + expect(generateStructured).toHaveBeenCalledWith( |
| 168 | + expect.objectContaining({ |
| 169 | + command: "loop", |
| 170 | + schema: expect.any(Object), |
| 171 | + }) |
| 172 | + ); |
| 173 | + expect(consoleSpy).toHaveBeenCalled(); |
| 174 | + |
| 175 | + // Verify printed card contents |
| 176 | + const output = consoleSpy.mock.calls.map((call) => call[0]).join("\n"); |
| 177 | + expect(output).toContain("FOUNDER DNA WRAPPED"); |
| 178 | + expect(output).toContain("TestApp"); |
| 179 | + expect(output).toContain("ALL-STAR"); |
| 180 | + expect(output).toContain("95%"); // Best Week score |
| 181 | + expect(output).toContain("Expand tests"); |
| 182 | + expect(output).toContain("reliability."); |
| 183 | + |
| 184 | + expect(ceremonyOutro).toHaveBeenCalledWith("Keep shipping!"); |
| 185 | + |
| 186 | + consoleSpy.mockRestore(); |
| 187 | + }); |
| 188 | +}); |
0 commit comments